Skip to content

Commit

Permalink
Uplift to use new events (#42)
Browse files Browse the repository at this point in the history
* new events integration

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

* update chart config for events

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 Oct 20, 2023
1 parent eafbd8c commit bfc3a02
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 209 deletions.
13 changes: 10 additions & 3 deletions chart/resource-provider-api/templates/api-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ metadata:
labels:
{{- include "common.labels.standard" . | nindent 4 }}
data:
RESOURCEPROVIDERAPI_EVENTS_PUBLISHER_PREFIX: "{{ .Values.api.events.topicPrefix }}"
RESOURCEPROVIDERAPI_EVENTS_PUBLISHER_URL: "{{ .Values.api.events.connectionURL }}"
RESOURCEPROVIDERAPI_EVENTS_NATS_URL: "{{ .Values.api.events.nats.url }}"
RESOURCEPROVIDERAPI_EVENTS_NATS_PUBLISHPREFIX: "{{ .Values.api.events.nats.publishPrefix }}"
RESOURCEPROVIDERAPI_EVENTS_NATS_QUEUEGROUP: "{{ .Values.api.events.nats.queueGroup }}"
RESOURCEPROVIDERAPI_EVENTS_NATS_SOURCE: "{{ .Values.api.events.nats.source }}"
RESOURCEPROVIDERAPI_EVENTS_NATS_CONNECTTIMEOUT: "{{ .Values.api.events.nats.connectTimeout }}"
RESOURCEPROVIDERAPI_EVENTS_NATS_SHUTDOWNTIMEOUT: "{{ .Values.api.events.nats.shutdownTimeout }}"
{{- if .Values.api.events.nats.credsSecretName }}
RESOURCEPROVIDERAPI_EVENTS_NATS_CREDSFILE: "{{ .Values.api.events.nats.credsFile }}"
{{- end }}
RESOURCEPROVIDERAPI_OIDC_ENABLED: "{{ .Values.api.oidc.enabled }}"
RESOURCEPROVIDERAPI_OIDC_AUDIENCE: "{{ .Values.api.oidc.audience }}"
RESOURCEPROVIDERAPI_OIDC_ISSUER: "{{ .Values.api.oidc.issuer }}"
Expand All @@ -31,4 +38,4 @@ data:
{{- end }}
{{- with .Values.api.trustedProxies }}
RESOURCEPROVIDERAPI_SERVER_TRUSTED_PROXIES: "{{ join " " . }}"
{{- end }}
{{- end }}
10 changes: 5 additions & 5 deletions chart/resource-provider-api/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ spec:
- name: {{ .name }}
value: {{ .value }}
{{- end }}
{{- end }}
{{- end }}
envFrom:
- configMapRef:
name: {{ include "common.names.fullname" . }}-config
Expand Down Expand Up @@ -103,7 +103,7 @@ spec:
path: /readyz
port: http
volumeMounts:
{{- if .Values.api.events.auth.secretName }}
{{- if .Values.api.events.nats.credsSecretName }}
- name: events-creds
mountPath: /nats
{{- end }}
Expand All @@ -127,13 +127,13 @@ spec:
{{- toYaml . | nindent 8 }}
{{- end }}
volumes:
{{- if .Values.api.events.auth.secretName }}
{{- if .Values.api.events.nats.credsSecretName }}
- name: events-creds
secret:
secretName: "{{ .Values.api.events.auth.secretName }}"
secretName: "{{ .Values.api.events.nats.credsSecretName }}"
{{- end }}
{{- if .Values.api.db.certSecret }}
- name: dbcerts
secret:
secretName: "{{ .Values.api.db.certSecret }}"
{{- end }}
{{- end }}
30 changes: 22 additions & 8 deletions chart/resource-provider-api/values.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
image:
repository: ghcr.io/infratographer/resource-provider-api
pullPolicy: IfNotPresent
tag: "v0.0.2"
tag: "main-latest"

imagePullSecrets: []
nameOverride: ""
Expand Down Expand Up @@ -31,7 +31,7 @@ ingress:

api:
replicas: 2
listenPort: 7610
listenPort: 17608
extraLabels: {}
extraAnnotations: {}
extraEnvVars: []
Expand All @@ -41,15 +41,29 @@ api:
podSecurityContext: {}
securityContext: {}
events:
connectionURL: "nats://nats.nats:4222"
auth:
secretName: ""
credsPath: "/nats/creds"
topicPrefix: "com.infratographer"
nats:
# url is the event server connection url
url: "nats://my-events-cluster.example.com:4222"
# publishPrefix is the subscribe event prefix
publishPrefix: "com.infratographer"
# queueGroup defines the events queue group
queueGroup: ""
# source defines the source of the events (defaults to application name)
source: ""
# connectTimeout is event connection timeout
connectTimeout: "10s"
# shutdownTimeout is the shutdown grace period
shutdownTimeout: "5s"
# tokenSecretName is the secret to load the auth token
tokenSecretName: ""
# credsSecretName is the secret to load the creds auth file from
credsSecretName: ""
# credsFile is the location to read the creds file from
credsFile: "/nats/creds"
db:
uriSecret: ""
certSecret: ""
migrateOnInit: true
migrateOnInit: true

oidc:
enabled: false
Expand Down
27 changes: 18 additions & 9 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strconv"
"syscall"
"time"

"entgo.io/ent/dialect"
entsql "entgo.io/ent/dialect/sql"
Expand All @@ -31,6 +32,7 @@ import (

const (
defaultListenAddr = ":17608"
shutdownTimeout = 10 * time.Second
)

var (
Expand Down Expand Up @@ -61,7 +63,7 @@ func init() {

echox.MustViperFlags(viper.GetViper(), serveCmd.Flags(), defaultListenAddr)
echojwtx.MustViperFlags(viper.GetViper(), serveCmd.Flags())
events.MustViperFlagsForPublisher(viper.GetViper(), serveCmd.Flags(), appName)
events.MustViperFlags(viper.GetViper(), serveCmd.Flags(), appName)
permissions.MustViperFlags(viper.GetViper(), serveCmd.Flags())

// only available as a CLI arg because it shouldn't be something that could accidentially end up in a config file or env var
Expand All @@ -82,9 +84,9 @@ func serve(ctx context.Context) error {
viper.Set("oidc.enabled", false)
}

pub, err := events.NewPublisher(config.AppConfig.Events.Publisher)
events, err := events.NewConnection(config.AppConfig.Events, events.WithLogger(logger))
if err != nil {
logger.Fatalw("failed to create publisher", "error", err)
logger.Fatalw("failed to initialize events", "error", err)
}

err = otelx.InitTracer(config.AppConfig.Tracing, appName, logger)
Expand All @@ -101,7 +103,7 @@ func serve(ctx context.Context) error {

entDB := entsql.OpenDB(dialect.Postgres, db)

cOpts := []ent.Option{ent.Driver(entDB), ent.EventsPublisher(pub)}
cOpts := []ent.Option{ent.Driver(entDB), ent.EventsPublisher(events)}

if config.AppConfig.Logging.Debug {
cOpts = append(cOpts,
Expand Down Expand Up @@ -133,32 +135,39 @@ func serve(ctx context.Context) error {
middleware = append(middleware, auth.Middleware())
}

srv, err := echox.NewServer(logger.Desugar(), config.AppConfig.Server, versionx.BuildDetails())
srv, err := echox.NewServer(logger.Desugar(), config.AppConfig.Server, versionx.BuildDetails(), echox.WithLoggingSkipper(echox.SkipDefaultEndpoints))
if err != nil {
logger.Error("failed to create server", zap.Error(err))
}

perms, err := permissions.New(config.AppConfig.Permissions,
permissions.WithLogger(logger),
permissions.WithDefaultChecker(permissions.DefaultAllowChecker),
permissions.WithEventsPublisher(events),
)

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

if err != nil {
logger.Fatal("failed to initialize permissions", zap.Error(err))
}

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

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

srv.AddHandler(handler)

defer func() {
ctx, cancel := context.WithTimeout(ctx, shutdownTimeout)
defer cancel()

_ = events.Shutdown(ctx)
}()

if err := srv.RunWithContext(ctx); err != nil {
logger.Error("failed to run server", zap.Error(err))
}

return err
return nil
}

// Write a pid file, but first make sure it doesn't exist with a running pid.
Expand Down
96 changes: 47 additions & 49 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ require (
github.com/mattn/go-sqlite3 v1.14.17
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go/modules/postgres v0.25.0
github.com/vektah/gqlparser/v2 v2.5.10
github.com/wundergraph/graphql-go-tools v1.66.3
go.infratographer.com/permissions-api v0.1.19
go.infratographer.com/x v0.3.4
go.infratographer.com/permissions-api v0.2.6
go.infratographer.com/x v0.3.9
go.uber.org/zap v1.26.0
)

Expand All @@ -32,8 +32,6 @@ require (
github.com/MicahParks/keyfunc/v2 v2.1.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.0 // indirect
github.com/ThreeDotsLabs/watermill v1.2.0 // indirect
github.com/ThreeDotsLabs/watermill-nats/v2 v2.0.0 // indirect
github.com/XSAM/otelsql v0.23.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
Expand All @@ -44,14 +42,13 @@ require (
github.com/cockroachdb/cockroach-go/v2 v2.3.5 // indirect
github.com/containerd/containerd v1.7.6 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/garsue/watermillzap v1.2.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -64,7 +61,7 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -79,50 +76,52 @@ require (
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.1 // indirect
github.com/jaevor/go-nanoid v1.3.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/compress v1.17.1 // indirect
github.com/labstack/echo v3.3.10+incompatible // indirect
github.com/labstack/echo-contrib v0.15.0 // indirect
github.com/labstack/echo-jwt/v4 v4.2.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/lithammer/shortuuid/v3 v3.0.7 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/nats-io/jwt/v2 v2.4.1 // indirect
github.com/nats-io/nats-server/v2 v2.9.17 // indirect
github.com/nats-io/nats.go v1.27.1 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/jwt/v2 v2.5.2 // indirect
github.com/nats-io/nats-server/v2 v2.10.1 // indirect
github.com/nats-io/nats.go v1.31.0 // indirect
github.com/nats-io/nkeys v0.4.5 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.7 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/pressly/goose/v3 v3.13.4 // indirect
github.com/prometheus/client_golang v1.15.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/pressly/goose/v3 v3.15.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.8 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.9.5 // 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
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/testcontainers/testcontainers-go v0.25.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand All @@ -132,35 +131,34 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
github.com/zclconf/go-cty v1.13.2 // indirect
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.42.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.20.0 // indirect
go.step.sm/crypto v0.31.2 // indirect
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.45.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.step.sm/crypto v0.35.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
Expand Down
Loading

0 comments on commit bfc3a02

Please sign in to comment.