Skip to content

Commit

Permalink
feat: context leaking in kubernets & kubernetes resource check
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed May 3, 2024
1 parent ef0effe commit 6da9f79
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
20 changes: 10 additions & 10 deletions checks/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/is-healthy/pkg/health"
"github.com/gobwas/glob"
"github.com/samber/lo"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
Expand All @@ -26,14 +27,13 @@ func (c *KubernetesChecker) Type() string {
func (c *KubernetesChecker) Run(ctx *context.Context) pkg.Results {
var results pkg.Results
for _, conf := range ctx.Canary.Spec.Kubernetes {
results = append(results, c.Check(ctx, conf)...)
results = append(results, c.Check(*ctx, conf)...)
}

return results
}

// CheckConfig : Check every ldap entry for lookup and auth
// Returns check result and metrics
func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check) pkg.Results {
func (c *KubernetesChecker) Check(ctx context.Context, extConfig external.Check) pkg.Results {
check := extConfig.(v1.KubernetesCheck)
result := pkg.Success(check, ctx.Canary)
var results pkg.Results
Expand All @@ -51,16 +51,16 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check
return results.Failf("failed to initialize kubernetes client from the provided kubeconfig: %v", err)
}

ctx = ctx.WithDutyContext(ctx.WithKommons(kClient))
ctx = ctx.WithDutyContext(ctx.WithKubernetes(kube))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKommons(kClient)))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKubernetes(kube)))
} else {
kClient, kube, err := pkg.NewKommonsClientWithConfig(val)
if err != nil {
return results.Failf("failed to initialize kubernetes client from the provided kubeconfig: %v", err)
}

ctx = ctx.WithDutyContext(ctx.WithKommons(kClient))
ctx = ctx.WithDutyContext(ctx.WithKubernetes(kube))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKommons(kClient)))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKubernetes(kube)))
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check
return results
}

func getResourcesFromNamespace(ctx *context.Context, client dynamic.NamespaceableResourceInterface, check v1.KubernetesCheck, namespace string) ([]unstructured.Unstructured, error) {
func getResourcesFromNamespace(ctx context.Context, client dynamic.NamespaceableResourceInterface, check v1.KubernetesCheck, namespace string) ([]unstructured.Unstructured, error) {
var resources []unstructured.Unstructured
if check.Resource.Name != "" {
resource, err := client.Namespace(namespace).Get(ctx, check.Resource.Name, metav1.GetOptions{})
Expand All @@ -145,7 +145,7 @@ func getResourcesFromNamespace(ctx *context.Context, client dynamic.Namespaceabl
return resources, nil
}

func getNamespaces(ctx *context.Context, check v1.KubernetesCheck) ([]string, error) {
func getNamespaces(ctx context.Context, check v1.KubernetesCheck) ([]string, error) {
var namespaces []string
if check.Namespace.Name != "" {
return []string{check.Namespace.Name}, nil
Expand Down
30 changes: 15 additions & 15 deletions checks/kubernetes_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func (c *KubernetesResourceChecker) Type() string {
func (c *KubernetesResourceChecker) Run(ctx *context.Context) pkg.Results {
var results pkg.Results
for _, conf := range ctx.Canary.Spec.KubernetesResource {
results = append(results, c.Check(ctx, conf)...)
results = append(results, c.Check(*ctx, conf)...)
}
return results
}

func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.KubernetesResourceCheck) pkg.Results {
func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.KubernetesResourceCheck) pkg.Results {
result := pkg.Success(check, ctx.Canary)
var err error
var results pkg.Results
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne
retryErr := retry.Do(ctx, backoff, func(_ctx gocontext.Context) error {
ctx.Logger.V(4).Infof("running check: %s", virtualCanary.Name)

ctx = _ctx.(*context.Context)
ctx = _ctx.(context.Context)
checkCtx := context.New(ctx.Context, virtualCanary)
res, err := Exec(checkCtx)
if err != nil {
Expand All @@ -178,7 +178,7 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne
return results
}

func (c *KubernetesResourceChecker) evalWaitFor(ctx *context.Context, check v1.KubernetesResourceCheck) error {
func (c *KubernetesResourceChecker) evalWaitFor(ctx context.Context, check v1.KubernetesResourceCheck) error {
waitTimeout := resourceWaitTimeoutDefault
if wt, _ := check.WaitFor.GetTimeout(); wt > 0 {
waitTimeout = wt
Expand All @@ -194,7 +194,7 @@ func (c *KubernetesResourceChecker) evalWaitFor(ctx *context.Context, check v1.K
var attempts int
backoff := retry.WithMaxDuration(waitTimeout, retry.NewConstant(waitInterval))
retryErr := retry.Do(ctx, backoff, func(_ctx gocontext.Context) error {
ctx = _ctx.(*context.Context)
ctx = _ctx.(context.Context)
attempts++

ctx.Logger.V(4).Infof("waiting for %d resources to be in the desired state. (attempts: %d)", check.TotalResources(), attempts)
Expand Down Expand Up @@ -238,34 +238,34 @@ func (c *KubernetesResourceChecker) evalWaitFor(ctx *context.Context, check v1.K
return retryErr
}

func (c *KubernetesResourceChecker) applyKubeconfig(ctx *context.Context, kubeConfig types.EnvVar) (*context.Context, error) {
func (c *KubernetesResourceChecker) applyKubeconfig(ctx context.Context, kubeConfig types.EnvVar) (context.Context, error) {
val, err := ctx.GetEnvValueFromCache(kubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to get kubeconfig from env: %w", err)
return ctx, fmt.Errorf("failed to get kubeconfig from env: %w", err)
}

if strings.HasPrefix(val, "/") {
kClient, kube, err := pkg.NewKommonsClientWithConfigPath(val)
if err != nil {
return nil, fmt.Errorf("failed to initialize kubernetes client from the provided kubeconfig: %w", err)
return ctx, fmt.Errorf("failed to initialize kubernetes client from the provided kubeconfig: %w", err)
}

ctx = ctx.WithDutyContext(ctx.WithKommons(kClient))
ctx = ctx.WithDutyContext(ctx.WithKubernetes(kube))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKommons(kClient)))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKubernetes(kube)))
} else {
kClient, kube, err := pkg.NewKommonsClientWithConfig(val)
if err != nil {
return nil, fmt.Errorf("failed to initialize kubernetes client from the provided kubeconfig: %w", err)
return ctx, fmt.Errorf("failed to initialize kubernetes client from the provided kubeconfig: %w", err)
}

ctx = ctx.WithDutyContext(ctx.WithKommons(kClient))
ctx = ctx.WithDutyContext(ctx.WithKubernetes(kube))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKommons(kClient)))
ctx = lo.FromPtr(ctx.WithDutyContext(ctx.WithKubernetes(kube)))
}

return ctx, nil
}

func (c *KubernetesResourceChecker) validate(ctx *context.Context, check v1.KubernetesResourceCheck) error {
func (c *KubernetesResourceChecker) validate(ctx context.Context, check v1.KubernetesResourceCheck) error {
if _, err := check.WaitFor.GetTimeout(); err != nil {
return fmt.Errorf("invalid wait timeout (%s): %w", check.WaitFor.Timeout, err)
}
Expand Down Expand Up @@ -294,7 +294,7 @@ func (c *KubernetesResourceChecker) validate(ctx *context.Context, check v1.Kube
return nil
}

func DeleteResources(ctx *context.Context, check v1.KubernetesResourceCheck, deleteStatic bool) error {
func DeleteResources(ctx context.Context, check v1.KubernetesResourceCheck, deleteStatic bool) error {
ctx.Logger.V(4).Infof("deleting resources")

resources := check.Resources
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func deleteAllKubernetesResourcesOfCanary(ctx context.Context, id string) error

for _, kr := range spec.KubernetesResource {
scrapeCtx := apiContext.New(ctx, *canaryV1)
if err := checks.DeleteResources(scrapeCtx, kr, true); err != nil {
if err := checks.DeleteResources(*scrapeCtx, kr, true); err != nil {
logger.Errorf("error clearing resource: %v", err)
}
}
Expand Down

0 comments on commit 6da9f79

Please sign in to comment.