Skip to content

Commit

Permalink
refactor: cleanup context functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Feb 24, 2025
1 parent e871a05 commit 1ec3cce
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (k Context) Pool() *pgxpool.Pool {

// KubeAuthFingerprint generates a unique SHA-256 hash to identify the Kubernetes API server
// and client authentication details from the REST configuration.
func (k *Context) KubeAuthFingerprint() string {
func (k Context) KubeAuthFingerprint() string {
kc, _ := k.Kubernetes()
if kc == nil {
return ""
Expand All @@ -367,8 +367,12 @@ func (k *Context) KubeAuthFingerprint() string {
return dutyKubernetes.RestConfigFingerprint(rc)
}

func (k *Context) KubernetesConnection() KubernetesConnection {
if v, ok := k.Value("kubernetes-connection").(KubernetesConnection); ok {
func (k Context) KubernetesConnection() KubernetesConnection {
val := k.Value("kubernetes-connection")
if val == nil {
return nil
}
if v, ok := val.(KubernetesConnection); ok {
return v
}
return nil
Expand All @@ -377,9 +381,9 @@ func (k *Context) KubernetesConnection() KubernetesConnection {
var k8sclientcache = cache.NewCache[*KubernetesClient]("k8s-client-cache", 24*time.Hour)

func (k Context) Kubernetes() (*dutyKubernetes.Client, error) {
conn, ok := k.Value("kubernetes-connection").(KubernetesConnection)
if !ok {
return nil, fmt.Errorf("invalid type for KubernetesConnection")
conn := k.KubernetesConnection()
if conn == nil {
return nil, fmt.Errorf("kubernetes connection not set")
}
connHash := conn.Hash()
if client, err := k8sclientcache.Get(k, connHash); err == nil {
Expand All @@ -403,7 +407,7 @@ func (k Context) WithLocalKubernetes(client *dutyKubernetes.Client) Context {
return k
}

func (k *Context) LocalKubernetes() (*dutyKubernetes.Client, error) {
func (k Context) LocalKubernetes() (*dutyKubernetes.Client, error) {
if localKubernetes != nil {
return localKubernetes, nil
}
Expand Down

0 comments on commit 1ec3cce

Please sign in to comment.