Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cmd/liquidapi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company
// SPDX-License-Identifier: Apache-2.0

package liquidapicmd

import (
"net/http"
"time"

"github.com/dlmiddlecote/sqlstats"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sapcc/go-bits/easypg"
"github.com/sapcc/go-bits/httpapi"
"github.com/sapcc/go-bits/httpapi/pprofapi"
"github.com/sapcc/go-bits/httpext"
"github.com/sapcc/go-bits/must"
"github.com/sapcc/go-bits/osext"
"github.com/spf13/cobra"

"github.com/sapcc/keppel/internal/api/liquid"
"github.com/sapcc/keppel/internal/keppel"
)

// AddCommandTo mounts this command into the command hierarchy.
func AddCommandTo(parent *cobra.Command) {
cmd := &cobra.Command{
Use: "liquidapi",
Short: "Run the keppel-liquidapi server component.",
Long: "Run the keppel-liquidapi server component. Configuration is read from environment variables as described in README.md.",
Args: cobra.NoArgs,
Run: run,
}
parent.AddCommand(cmd)
}
Comment thread
SuperSandro2000 marked this conversation as resolved.

func run(cmd *cobra.Command, args []string) {
_, _ = cmd, args

keppel.SetTaskName("liquidapi")

cfg := keppel.ParseConfiguration()
ctx := httpext.ContextWithSIGINT(cmd.Context(), 10*time.Second)
auditor := must.Return(keppel.InitAuditTrail(ctx))

dbURL, dbName := keppel.GetDatabaseURLFromEnvironment()
dbConn := must.Return(easypg.Connect(dbURL, keppel.DBConfiguration()))
prometheus.MustRegister(sqlstats.NewStatsCollector(dbName, dbConn))
db := keppel.InitORM(dbConn)

ad := must.Return(keppel.NewAuthDriver(ctx, osext.MustGetenv("KEPPEL_DRIVER_AUTH"), nil))
sd := must.Return(keppel.NewStorageDriver(osext.MustGetenv("KEPPEL_DRIVER_STORAGE"), ad, cfg))

// wire up HTTP handlers
handler := httpapi.Compose(
liquid.NewLiquidAPI(cfg, ad, sd, db, auditor),
httpapi.HealthCheckAPI{
SkipRequestLog: true,
Check: func() error {
return db.Db.PingContext(ctx)
},
},
pprofapi.API{IsAuthorized: pprofapi.IsRequestFromLocalhost},
)
mux := http.NewServeMux()
mux.Handle("/", handler)
mux.Handle("/metrics", promhttp.Handler())

// start HTTP server
apiListenAddress := osext.GetenvOrDefault("KEPPEL_LIQUIDAPI_LISTEN_ADDRESS", ":8080")
must.Succeed(httpext.ListenAndServeContext(ctx, apiListenAddress, mux))
}
2 changes: 1 addition & 1 deletion cmd/trivyproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func run(cmd *cobra.Command, args []string) {
smux.Handle("/", handler)
smux.Handle("/metrics", promhttp.Handler())

apiListenAddress := osext.GetenvOrDefault("KEPPEL_API_LISTEN_ADDRESS", ":8080")
apiListenAddress := osext.GetenvOrDefault("KEPPEL_TRIVY_LISTEN_ADDRESS", ":8080")
must.Succeed(httpext.ListenAndServeContext(ctx, apiListenAddress, smux))
}

Expand Down
136 changes: 61 additions & 75 deletions docs/operator-guide.md

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions internal/api/keppel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ func (a *API) AddTo(r *mux.Router) {

r.Methods("GET").Path("/keppel/v1/quotas/{auth_tenant_id}").HandlerFunc(a.handleGetQuotas)
r.Methods("PUT").Path("/keppel/v1/quotas/{auth_tenant_id}").HandlerFunc(a.handlePutQuotas)

// Besides the native Keppel API, this handler also implements LIQUID.
// Ref: <https://pkg.go.dev/github.com/sapcc/go-api-declarations/liquid>
r.Methods("GET").Path("/liquid/v1/info").HandlerFunc(a.handleLiquidGetInfo)
r.Methods("POST").Path("/liquid/v1/report-capacity").HandlerFunc(a.handleLiquidReportCapacity)
r.Methods("POST").Path("/liquid/v1/projects/{auth_tenant_id}/report-usage").HandlerFunc(a.handleLiquidReportUsage)
r.Methods("PUT").Path("/liquid/v1/projects/{auth_tenant_id}/quota").HandlerFunc(a.handleLiquidSetQuota)
}

func (a *API) processor() *processor.Processor {
Expand Down
125 changes: 0 additions & 125 deletions internal/api/keppel/liquid.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/api/keppel/quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (a *API) handleGetQuotas(w http.ResponseWriter, r *http.Request) {
return
}

resp, err := a.processor().GetQuotas(authTenantID)
resp, err := a.processor().GetQuotas(r.Context(), authTenantID)
if respondwith.ObfuscatedErrorText(w, err) {
return
}
Expand All @@ -44,7 +44,7 @@ func (a *API) handlePutQuotas(w http.ResponseWriter, r *http.Request) {
return
}

resp, err := a.processor().SetQuotas(authTenantID, req, authz.UserIdentity.UserInfo(), r)
resp, err := a.processor().SetQuotas(r.Context(), authTenantID, req, authz.UserIdentity.UserInfo(), r)
if iqerr, ok := errext.As[processor.ImpossibleQuotaError](err); ok {
http.Error(w, iqerr.Message, http.StatusUnprocessableEntity)
return
Expand Down
Loading
Loading