From 30d44b64372bc37133007e68d8a472e84041fa86 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Tue, 17 Oct 2023 18:58:21 +0530 Subject: [PATCH] chore: add context.wrap func --- context/context.go | 34 +++++++++++++++++++++++++--------- go.mod | 2 +- go.sum | 4 ++-- models/people.go | 1 + 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/context/context.go b/context/context.go index 7f8e8eef..8afff342 100644 --- a/context/context.go +++ b/context/context.go @@ -2,6 +2,7 @@ package context import ( gocontext "context" + commons "github.com/flanksource/commons/context" "github.com/jackc/pgx/v5/pgxpool" "go.opentelemetry.io/otel/attribute" @@ -13,8 +14,9 @@ import ( "github.com/flanksource/duty/models" "github.com/flanksource/duty/types" - "k8s.io/client-go/kubernetes" "time" + + "k8s.io/client-go/kubernetes" ) type Context struct { @@ -70,6 +72,12 @@ func (k Context) WithKubernetes(client kubernetes.Interface) Context { } } +func (k Context) WithNamespace(namespace string) Context { + return Context{ + Context: k.WithValue("namespace", namespace), + } +} + func (k Context) WithDB(db *gorm.DB, pool *pgxpool.Pool) Context { return Context{ Context: k.WithValue("db", db).WithValue("pgxpool", pool), @@ -80,11 +88,6 @@ func (k Context) DB() *gorm.DB { return k.Value("db").(*gorm.DB) } -func (k Context) PgxPool() *pgxpool.Pool { - return k.Value("pgxpool").(*pgxpool.Pool) -} - -// For compatibility with duty.DBContext func (k Context) Pool() *pgxpool.Pool { return k.Value("pgxpool").(*pgxpool.Pool) } @@ -118,7 +121,13 @@ func (k Context) GetObjectMeta() metav1.ObjectMeta { } func (k Context) GetNamespace() string { - return k.GetObjectMeta().Namespace + if k.Value("object") != nil { + return k.GetObjectMeta().Namespace + } + if k.Value("namespace") != nil { + return k.Value("namespace").(string) + } + return "" } func (k Context) GetName() string { return k.GetObjectMeta().Name @@ -132,7 +141,7 @@ func (k Context) GetAnnotations() map[string]string { return k.GetObjectMeta().Annotations } -func (k *Context) GetEnvValueFromCache(input types.EnvVar, namespace string) (string, error) { +func (k Context) GetEnvValueFromCache(input types.EnvVar, namespace string) (string, error) { return duty.GetEnvValueFromCache(k.Kubernetes(), input, namespace) } @@ -148,7 +157,7 @@ func (k *Context) GetConfigMapFromCache(namespace, name, key string) (string, er return duty.GetConfigMapFromCache(k.Kubernetes(), namespace, name, key) } -func (k *Context) HydratedConnectionByURL(namespace, connectionString string) (*models.Connection, error) { +func (k Context) HydratedConnectionByURL(namespace, connectionString string) (*models.Connection, error) { return duty.HydratedConnectionByURL(k, k.DB(), k.Kubernetes(), namespace, connectionString) } @@ -163,3 +172,10 @@ func WrapContext(gormDB *gorm.DB, pool *pgxpool.Pool, k8s kubernetes.Interface, 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()). + WithNamespace(k.GetNamespace()) +} diff --git a/go.mod b/go.mod index 47f76e9a..5f945839 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( ariga.io/atlas v0.14.2 github.com/fergusstrange/embedded-postgres v1.21.0 - github.com/flanksource/commons v1.15.0 + github.com/flanksource/commons v1.15.1 github.com/flanksource/postq v0.1.1 github.com/google/uuid v1.3.1 github.com/hashicorp/hcl/v2 v2.18.1 diff --git a/go.sum b/go.sum index 26268861..a7812758 100644 --- a/go.sum +++ b/go.sum @@ -700,8 +700,8 @@ github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLi github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fergusstrange/embedded-postgres v1.21.0 h1:Sups0nR31+OB4iOZ0ZU4IwUDsB+dVGmcqj4S2ko0qTI= github.com/fergusstrange/embedded-postgres v1.21.0/go.mod h1:wL562t1V+iuFwq0UcgMi2e9rp8CROY9wxWZEfP8Y874= -github.com/flanksource/commons v1.15.0 h1:p74hrKzIz0r3H8YN3CuB8ePJOjzPFO0BRLVmpXmeqvY= -github.com/flanksource/commons v1.15.0/go.mod h1:FMZFLcQr98JwBKuKLs44DvCQ2JNoHz5maRIzVufQ9Cs= +github.com/flanksource/commons v1.15.1 h1:cFvxQd5SBFe+q16ciz8Q2IeBMeQ7+atdACGanbW27hg= +github.com/flanksource/commons v1.15.1/go.mod h1:FMZFLcQr98JwBKuKLs44DvCQ2JNoHz5maRIzVufQ9Cs= github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc= github.com/flanksource/gomplate/v3 v3.20.16 h1:Bfn+nbD0iK0iGQcu6alV8Nr7O5+KpeDo8OD9WOu831Q= github.com/flanksource/gomplate/v3 v3.20.16/go.mod h1:2GgHZ2vWmtDspJMBfUIryOuzJSwc8jU7Kw9fDLr0TMA= diff --git a/models/people.go b/models/people.go index ffa77811..f77cd822 100644 --- a/models/people.go +++ b/models/people.go @@ -14,6 +14,7 @@ type Person struct { Email string `json:"email,omitempty" gorm:"default:null"` Type string `json:"type,omitempty" gorm:"default:null"` Avatar string `json:"avatar,omitempty" gorm:"default:null"` + ExternalID string `json:"external_id,omitempty" gorm:"default:null"` Properties PersonProperties `json:"properties,omitempty" gorm:"default:null"` }