Skip to content

Commit

Permalink
chore: add context.wrap func
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Oct 17, 2023
1 parent 4a5cfd3 commit 30d44b6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
34 changes: 25 additions & 9 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions models/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit 30d44b6

Please sign in to comment.