Skip to content

Commit

Permalink
chore: add kommons to context
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Oct 23, 2023
1 parent 8cce7a5 commit 0b13bf5
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 19 deletions.
23 changes: 15 additions & 8 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
gocontext "context"

commons "github.com/flanksource/commons/context"
"github.com/flanksource/kommons"
"github.com/jackc/pgx/v5/pgxpool"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -57,6 +58,7 @@ func (k Context) WithObject(object metav1.ObjectMeta) Context {
}

func (k Context) WithUser(user *models.Person) Context {
k.GetSpan().SetAttributes(attribute.String("user-id", user.ID.String()))
return Context{
Context: k.WithValue("user", user),
}
Expand All @@ -72,6 +74,12 @@ func (k Context) WithKubernetes(client kubernetes.Interface) Context {
}
}

func (k Context) WithKommons(client *kommons.Client) Context {
return Context{
Context: k.WithValue("kommons", client),
}
}

func (k Context) WithNamespace(namespace string) Context {
return Context{
Context: k.WithValue("namespace", namespace),
Expand All @@ -92,10 +100,16 @@ func (k Context) Pool() *pgxpool.Pool {
return k.Value("pgxpool").(*pgxpool.Pool)
}

// TODO: Handle it being nil/empty
func (k *Context) Kubernetes() kubernetes.Interface {
return k.Value("kubernetes").(kubernetes.Interface)
}

// TODO: Handle it being nil/empty
func (k *Context) Kommons() *kommons.Client {
return k.Value("kommons").(*kommons.Client)
}

func (k Context) StartSpan(name string) (Context, trace.Span) {
ctx, span := k.Context.StartSpan(name)
span.SetAttributes(
Expand Down Expand Up @@ -165,17 +179,10 @@ func (k *Context) HydrateConnection(connection *models.Connection, namespace str
return duty.HydrateConnection(k, k.Kubernetes(), k.DB(), connection, namespace)
}

func WrapContext(gormDB *gorm.DB, pool *pgxpool.Pool, k8s kubernetes.Interface, tracer trace.Tracer) func(gocontext.Context) Context {
return func(ctx gocontext.Context) Context {
return NewContext(ctx, commons.WithTracer(tracer)).
WithDB(gormDB, pool).
WithKubernetes(k8s)
}
}

func (k Context) Wrap(ctx gocontext.Context) Context {
return NewContext(ctx, commons.WithTracer(k.GetTracer())).
WithDB(k.DB(), k.Pool()).
WithKubernetes(k.Kubernetes()).
WithKommons(k.Kommons()).
WithNamespace(k.GetNamespace())
}
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
ariga.io/atlas v0.14.2
github.com/fergusstrange/embedded-postgres v1.21.0
github.com/flanksource/commons v1.15.1
github.com/flanksource/kommons v0.31.4
github.com/flanksource/postq v0.1.1
github.com/google/uuid v1.3.1
github.com/hashicorp/hcl/v2 v2.18.1
Expand Down Expand Up @@ -38,8 +39,10 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.2 // indirect
cloud.google.com/go/storage v1.33.0 // indirect
github.com/AlekSi/pointer v1.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/TomOnTime/utfutil v0.0.0-20210710122150-437f72b26edf // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect
github.com/antonmedv/expr v1.15.3 // indirect
Expand All @@ -54,6 +57,7 @@ require (
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 // indirect
github.com/flanksource/mapstructure v1.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
Expand All @@ -65,22 +69,26 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/cel-go v0.18.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230323073829-e72429f035bd // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gosimple/slug v1.13.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hairyhenderson/toml v0.4.2-0.20210923231440-40456b8e66cf // indirect
github.com/hairyhenderson/yaml v0.0.0-20220618171115-2d35fca545ce // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.3 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -97,10 +105,14 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/robertkrimen/otto v0.2.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
Expand All @@ -116,9 +128,11 @@ require (
github.com/vadimi/go-http-ntlm/v2 v2.4.1 // indirect
github.com/vadimi/go-ntlm v1.2.1 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/yuin/gopher-lua v1.1.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
Expand All @@ -144,11 +158,16 @@ require (
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.24.4 // indirect
k8s.io/cli-runtime v0.28.0 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize v2.0.3+incompatible // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 0b13bf5

Please sign in to comment.