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 aa2107f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
29 changes: 25 additions & 4 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 Down Expand Up @@ -118,7 +126,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 +146,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 +162,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 +177,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())).

Check failure on line 182 in context/context.go

View workflow job for this annotation

GitHub Actions / lint

k.GetTracer undefined (type Context has no field or method GetTracer)) (typecheck)

Check failure on line 182 in context/context.go

View workflow job for this annotation

GitHub Actions / lint

k.GetTracer undefined (type Context has no field or method GetTracer)) (typecheck)

Check failure on line 182 in context/context.go

View workflow job for this annotation

GitHub Actions / lint

k.GetTracer undefined (type Context has no field or method GetTracer) (typecheck)

Check failure on line 182 in context/context.go

View workflow job for this annotation

GitHub Actions / test

k.GetTracer undefined (type Context has no field or method GetTracer)
WithDB(k.DB(), k.Pool()).
WithKubernetes(k.Kubernetes()).
WithNamespace(k.GetNamespace())
}
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 aa2107f

Please sign in to comment.