Skip to content

Commit

Permalink
Initial: update loadbalancer metadata status in resolvers (#273)
Browse files Browse the repository at this point in the history
* bump testcontainers to v0.26.0

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* bump chart dependencies - bitnami common (2.13.3), reloader (1.0.50)

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* add supergraph url, metadata status namespace id, timeout and oidc client to config

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* update metadata status for loadbalancer cud

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* move to supergraph config

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* port cud - update loadbalancer status

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* permissions ignoreNoResponders

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* update loadbalancer status - pools/origins cud

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* add oidc client issuer to chart

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* use helper resolver method

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* conditional metadata usage

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

* loadbalancer metadata pkg

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>

---------

Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>
  • Loading branch information
rizzza authored Nov 15, 2023
1 parent 92a7fc6 commit 64a3c37
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 56 deletions.
8 changes: 4 additions & 4 deletions chart/load-balancer-api/Chart.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
dependencies:
- name: common
repository: https://charts.bitnami.com/bitnami
version: 2.9.2
version: 2.13.3
- name: reloader
repository: https://stakater.github.io/stakater-charts
version: 1.0.41
digest: sha256:fdc264d2df58440f4ffbe739352a634726fb29cacd9beec3b4f20eab34b890e6
generated: "2023-10-02T18:43:45.099356257Z"
version: 1.0.50
digest: sha256:f1aa54b58b177001f10e6c2d72d39de7bba5e3ca5bf84dffb2256e40b31d4ce6
generated: "2023-11-07T16:03:46.737333402Z"
4 changes: 2 additions & 2 deletions chart/load-balancer-api/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ dependencies:
repository: https://charts.bitnami.com/bitnami
tags:
- bitnami-common
version: 2.9.2
version: 2.13.3
- name: reloader
condition: reloader.enabled
version: "1.0.41"
version: "1.0.50"
repository: "https://stakater.github.io/stakater-charts"
Binary file removed chart/load-balancer-api/charts/common-2.1.1.tgz
Binary file not shown.
Binary file added chart/load-balancer-api/charts/common-2.13.3.tgz
Binary file not shown.
Binary file not shown.
Binary file removed chart/load-balancer-api/charts/reloader-v0.0.124.tgz
Binary file not shown.
7 changes: 6 additions & 1 deletion chart/load-balancer-api/templates/api-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ data:
{{- if .Values.api.events.nats.credsSecretName }}
LOADBALANCERAPI_EVENTS_NATS_CREDSFILE: "{{ .Values.api.events.nats.credsFile }}"
{{- end }}
LOADBALANCERAPI_METADATA_STATUS_NAMESPACE_ID: "{{ .Values.api.metadata.statusNamespaceID }}"
LOADBALANCERAPI_OIDC_ENABLED: "{{ .Values.api.oidc.enabled }}"
LOADBALANCERAPI_OIDC_AUDIENCE: "{{ .Values.api.oidc.audience }}"
LOADBALANCERAPI_OIDC_ISSUER: "{{ .Values.api.oidc.issuer }}"
LOADBALANCERAPI_OIDC_JWKS_REMOTE_TIMEOUT: "{{ .Values.api.oidc.jwksRemoteTimeout }}"
LOADBALANCERAPI_OIDC_CLIENT_ISSUER: "{{ .Values.api.oidc.client.issuer }}"
LOADBALANCERAPI_PERMISSIONS_IGNORENORESPONDERS: "{{ .Values.api.permissions.ignoreNoResponders }}"
LOADBALANCERAPI_PERMISSIONS_URL: "{{ .Values.api.permissions.url }}"
LOADBALANCERAPI_SERVER_LISTEN: ":{{ .Values.api.listenPort }}"
LOADBALANCERAPI_SERVER_SHUTDOWN_GRACE_PERIOD: "{{ .Values.api.shutdownGracePeriod }}"
LOADBALANCERAPI_PERMISSIONS_URL: "{{ .Values.api.permissions.url }}"
LOADBALANCERAPI_SUPERGRAPH_URL: "{{ .Values.api.supergraph.url }}"
LOADBALANCERAPI_SUPERGRAPH_TIMEOUT: "{{ .Values.api.supergraph.timeout }}"
{{- if .Values.api.tracing.enabled }}
LOADBALANCERAPI_TRACING_ENABLED: "{{ .Values.api.tracing.enabled }}"
LOADBALANCERAPI_TRACING_PROVIDER: "{{ .Values.api.tracing.provider }}"
Expand Down
13 changes: 13 additions & 0 deletions chart/load-balancer-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,30 @@ api:
certSecret: ""
migrateOnInit: false

metadata:
# statusNamespaceID is the namespace ID to use for status updates
statusNamespaceID: ""

oidc:
enabled: false
audience: ""
issuer: ""
jwks:
remoteTimeout: 1m
client:
issuer: ""

permissions:
url: ""
# ignoreNoResponders whether or not to ignore errors when no AuthRelationship request-reply responders are available
ignoreNoResponders: false

shutdownGracePeriod: 5s

supergraph:
url: ""
timeout: 5s

trustedProxies: []
# - "1.2.3.4"
# - "1.2.3.4/32"
Expand Down
42 changes: 38 additions & 4 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ import (
"go.infratographer.com/x/crdbx"
"go.infratographer.com/x/echojwtx"
"go.infratographer.com/x/echox"
"go.infratographer.com/x/oauth2x"
"go.infratographer.com/x/otelx"
"go.infratographer.com/x/versionx"
"go.infratographer.com/x/viperx"
"go.uber.org/zap"

metadata "go.infratographer.com/metadata-api/pkg/client"

"go.infratographer.com/load-balancer-api/internal/config"
ent "go.infratographer.com/load-balancer-api/internal/ent/generated"
"go.infratographer.com/load-balancer-api/internal/graphapi"
Expand All @@ -34,6 +38,7 @@ import (
const (
defaultLBAPIListenAddr = ":7608"
shutdownTimeout = 10 * time.Second
defaultTimeout = 5 * time.Second
)

var (
Expand Down Expand Up @@ -64,15 +69,24 @@ func init() {

echox.MustViperFlags(viper.GetViper(), serveCmd.Flags(), defaultLBAPIListenAddr)
echojwtx.MustViperFlags(viper.GetViper(), serveCmd.Flags())
events.MustViperFlags(viper.GetViper(), serveCmd.Flags(), appName)
oauth2x.MustViperFlags(viper.GetViper(), serveCmd.Flags())
permissions.MustViperFlags(viper.GetViper(), serveCmd.Flags())

serveCmd.Flags().String("metadata-status-namespace-id", "", "status namespace id to update loadbalancer metadata status")
viperx.MustBindFlag(viper.GetViper(), "metadata.status-namespace-id", serveCmd.Flags().Lookup("metadata-status-namespace-id"))

serveCmd.Flags().Duration("supergraph-timeout", defaultTimeout, "client timeout")
viperx.MustBindFlag(viper.GetViper(), "supergraph.timeout", serveCmd.Flags().Lookup("supergraph-timeout"))

serveCmd.Flags().String("supergraph-url", "", "endpoint for supergraph gateway")
viperx.MustBindFlag(viper.GetViper(), "supergraph.url", serveCmd.Flags().Lookup("supergraph-url"))

// only available as a CLI arg because it shouldn't be something that could accidentially end up in a config file or env var
serveCmd.Flags().BoolVar(&serveDevMode, "dev", false, "dev mode: enables playground, disables all auth checks, sets CORS to allow all, pretty logging, etc.")
serveCmd.Flags().BoolVar(&enablePlayground, "playground", false, "enable the graph playground")
serveCmd.Flags().StringVar(&pidFileName, "pid-file", "", "path to the pid file")
serveCmd.Flags().IntSlice("restricted-ports", []int{}, "ports that are restricted from being used by the load balancer (e.g. 22, 8086, etc.)")

events.MustViperFlags(viper.GetViper(), serveCmd.Flags(), appName)
permissions.MustViperFlags(viper.GetViper(), serveCmd.Flags())
}

// Write a pid file, but first make sure it doesn't exist with a running pid.
Expand Down Expand Up @@ -138,6 +152,26 @@ func serve(ctx context.Context) error {
client := ent.NewClient(cOpts...)
defer client.Close()

// TODO - @rizzza - supergraph client
var metadataClient *metadata.Client

if config.AppConfig.Supergraph.URL != "" {
if config.AppConfig.OIDCClient.Config.Issuer != "" {
oidcTS, err := oauth2x.NewClientCredentialsTokenSrc(ctx, config.AppConfig.OIDCClient.Config)
if err != nil {
logger.Fatalw("failed to create oauth2 token source", "error", err)
}

oauthHTTPClient := oauth2x.NewClient(ctx, oidcTS)
oauthHTTPClient.Timeout = config.AppConfig.Supergraph.Timeout

metadataClient = metadata.New(config.AppConfig.Supergraph.URL,
metadata.WithHTTPClient(oauthHTTPClient),
)
} else {
metadataClient = metadata.New(config.AppConfig.Supergraph.URL)
}
}
// TODO: fix generated pubsubhooks
// eventhooks.PubsubHooks(client)

Expand Down Expand Up @@ -179,7 +213,7 @@ func serve(ctx context.Context) error {

middleware = append(middleware, perms.Middleware())

r := graphapi.NewResolver(client, logger.Named("resolvers"))
r := graphapi.NewResolver(client, logger.Named("resolvers"), graphapi.WithMetadataClient(metadataClient))
handler := r.Handler(enablePlayground, middleware...)

srv.AddHandler(handler)
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
entgo.io/contrib v0.4.5
entgo.io/ent v0.12.4
github.com/99designs/gqlgen v0.17.38
github.com/99designs/gqlgen v0.17.39
github.com/Yamashou/gqlgenc v0.15.1
github.com/brianvoe/gofakeit/v6 v6.24.0
github.com/docker/go-connections v0.4.0
Expand All @@ -21,8 +21,9 @@ require (
github.com/testcontainers/testcontainers-go v0.26.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.26.0
github.com/vektah/gqlparser/v2 v2.5.10
github.com/wundergraph/graphql-go-tools v1.66.4
go.infratographer.com/permissions-api v0.2.6
github.com/wundergraph/graphql-go-tools v1.67.0
go.infratographer.com/metadata-api v0.0.4-0.20231113202341-c8f116fbcde2
go.infratographer.com/permissions-api v0.2.7-0.20231103213152-84681822dedd
go.infratographer.com/x v0.3.9
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
Expand Down Expand Up @@ -58,11 +59,9 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/gofrs/uuid v4.3.1+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/golang/glog v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.1 // indirect
Expand Down Expand Up @@ -121,6 +120,7 @@ require (
github.com/shirou/gopsutil/v3 v3.23.9 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sosodev/duration v1.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ entgo.io/ent v0.12.4 h1:LddPnAyxls/O7DTXZvUGDj0NZIdGSu317+aoNLJWbD8=
entgo.io/ent v0.12.4/go.mod h1:Y3JVAjtlIk8xVZYSn3t3mf8xlZIn5SAOXZQxD6kKI+Q=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
github.com/99designs/gqlgen v0.17.38 h1:3r7G7i8UAdY0iYreNiBAA55auVsrowO0+ZhMl5g4GYU=
github.com/99designs/gqlgen v0.17.38/go.mod h1:2v+dKtpI8mIzYeW9dYN8mO69tMmjszW2xKLNcWR/5wQ=
github.com/99designs/gqlgen v0.17.39 h1:wPTAyc2fqVjAWT5DsJ21k/lLudgnXzURwbsjVNegFpU=
github.com/99designs/gqlgen v0.17.39/go.mod h1:b62q1USk82GYIVjC60h02YguAZLqYZtvWml8KkhJps4=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
Expand Down Expand Up @@ -504,6 +504,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smallstep/assert v0.0.0-20200723003110-82e2b9b3b262 h1:unQFBIznI+VYD1/1fApl1A+9VcBk+9dcqGfnePY87LY=
github.com/smallstep/assert v0.0.0-20200723003110-82e2b9b3b262/go.mod h1:MyOHs9Po2fbM1LHej6sBUT8ozbxmMOFG+E+rx/GSGuc=
github.com/sosodev/duration v1.1.0 h1:kQcaiGbJaIsRqgQy7VGlZrVw1giWO+lDoX3MCPnpVO4=
github.com/sosodev/duration v1.1.0/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
Expand Down Expand Up @@ -563,8 +565,8 @@ github.com/vmihailenco/msgpack/v5 v5.0.0-beta.9/go.mod h1:HVxBVPUK/+fZMonk4bi1is
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/wundergraph/graphql-go-tools v1.66.4 h1:yRvXYi0jjTghi5zimTluqHXAmyS7JVlGzTlxY6aL0sI=
github.com/wundergraph/graphql-go-tools v1.66.4/go.mod h1:obaEJWub7088qodhKbSGHyhRVnHlBP5M9HigN/oalLE=
github.com/wundergraph/graphql-go-tools v1.67.0 h1:HcZowgnfDJVyew9egwey38C0Ew3gjmF+fHGia6s2SSA=
github.com/wundergraph/graphql-go-tools v1.67.0/go.mod h1:0IQz0Tn4g3iJOs8HlxTzrOjgOXlRN805cuorvkZ2NX8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -575,8 +577,10 @@ github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.infratographer.com/permissions-api v0.2.6 h1:Xet3HF18SJHCoP5S3KTF7JAhD9RA1jGTa80QSII7ieY=
go.infratographer.com/permissions-api v0.2.6/go.mod h1:2uNlcC/gsWXhxPaQ+NP/Kwf02Wu3bCPQ3FF7vGgbnwU=
go.infratographer.com/metadata-api v0.0.4-0.20231113202341-c8f116fbcde2 h1:VJPa1K6AzNKAwqG1GAjtBhqh+g2/nZxyKFRoeO85heE=
go.infratographer.com/metadata-api v0.0.4-0.20231113202341-c8f116fbcde2/go.mod h1:p8tjpW1L/Qt9PIR1LHVWsBOwE3VW5+O5nIbPS09cpTA=
go.infratographer.com/permissions-api v0.2.7-0.20231103213152-84681822dedd h1:1PgyFVRttmnz5l1aIjZKft9Bd18DUZz6QBFSXZkhZc4=
go.infratographer.com/permissions-api v0.2.7-0.20231103213152-84681822dedd/go.mod h1:2uNlcC/gsWXhxPaQ+NP/Kwf02Wu3bCPQ3FF7vGgbnwU=
go.infratographer.com/x v0.3.9 h1:fsfF/w5zHgiNAHvYmvsWlICNha2X53WNLVSKOkyPnWo=
go.infratographer.com/x v0.3.9/go.mod h1:n/61MZRKFbGlS8xUwAhTyDhqcL2Wk6uPsXADC2n5t1I=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
25 changes: 24 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,46 @@
package config

import (
"time"

"go.infratographer.com/permissions-api/pkg/permissions"
"go.infratographer.com/x/crdbx"
"go.infratographer.com/x/echojwtx"
"go.infratographer.com/x/echox"
"go.infratographer.com/x/events"
"go.infratographer.com/x/gidx"
"go.infratographer.com/x/loggingx"
"go.infratographer.com/x/oauth2x"
"go.infratographer.com/x/otelx"
)

// AppConfig stores all the config values for our application
var AppConfig struct {
OIDC echojwtx.AuthConfig
OIDC echojwtx.AuthConfig `mapstructure:"oidc"`
OIDCClient OIDCClientConfig `mapstructure:"oidc"`
CRDB crdbx.Config
Logging loggingx.Config
Server echox.Config
Tracing otelx.Config
Events events.Config
Permissions permissions.Config
Metadata MetadataConfig
RestrictedPorts []int
Supergraph SupergraphConfig
}

// MetadataConfig stores the configuration for metadata
type MetadataConfig struct {
StatusNamespaceID gidx.PrefixedID `mapstructure:"status-namespace-id"`
}

// SupergraphConfig stores the configuration for the supergraph
type SupergraphConfig struct {
URL string
Timeout time.Duration
}

// OIDCClientConfig stores the configuration for an OIDC client
type OIDCClientConfig struct {
Config oauth2x.Config `mapstructure:"client"`
}
24 changes: 21 additions & 3 deletions internal/graphapi/loadbalancer.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions internal/graphapi/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package graphapi

import (
"context"
"encoding/json"
"fmt"

metadata "go.infratographer.com/metadata-api/pkg/client"
"go.infratographer.com/x/gidx"

lbstate "go.infratographer.com/load-balancer-api/pkg/metadata"

"go.infratographer.com/load-balancer-api/internal/config"
)

const metadataStatusSource = "load-balancer-api"

// Metadata interface for the metadata client
type Metadata interface {
StatusUpdate(ctx context.Context, input *metadata.StatusUpdateInput) (*metadata.StatusUpdate, error)
}

// LoadBalancerStatusUpdate updates the state of a load balancer in the metadata service
func (r Resolver) LoadBalancerStatusUpdate(ctx context.Context, loadBalancerID gidx.PrefixedID, state lbstate.LoadBalancerState) error {
if r.metadata == nil {
r.logger.Warnln("metadata client not configured")
return nil
}

if _, err := r.metadata.StatusUpdate(ctx, &metadata.StatusUpdateInput{
NodeID: loadBalancerID.String(),
NamespaceID: config.AppConfig.Metadata.StatusNamespaceID.String(),
Source: metadataStatusSource,
Data: json.RawMessage(fmt.Sprintf(`{"state": "%s"}`, state)),
}); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 64a3c37

Please sign in to comment.