Skip to content

Commit

Permalink
fix: duty bump
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Dec 12, 2024
1 parent 7347a2e commit 8e1f7f0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 43 deletions.
33 changes: 22 additions & 11 deletions checks/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package checks
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -122,30 +123,40 @@ func deletePod(ctx *context.Context, pod *corev1.Pod) {
if ctx.Canary.Annotations["skipDelete"] == "true" { // nolint: goconst
return
}
if err := ctx.Kommons().DeleteByKind(podKind, pod.Namespace, pod.Name); err != nil {

if _, err := ctx.KubernetesClient().DeleteByGVK(ctx, pod.Namespace, pod.Name, pod.GroupVersionKind()); err != nil {
ctx.Warnf("failed to delete pod %s/%s: %v", pod.Namespace, pod.Name, err)
}
}

func getLogs(ctx *context.Context, pod corev1.Pod) string {
message, _ := ctx.Kommons().GetPodLogs(pod.Namespace, pod.Name, pod.Spec.InitContainers[0].Name)
reader, err := ctx.KubernetesClient().GetPodLogs(ctx, pod.Namespace, pod.Name, pod.Spec.InitContainers[0].Name)
if err != nil {
ctx.Errorf("failed to get pod logs %s/%s: %v", pod.Namespace, pod.Name, err)
return ""
}
defer reader.Close()

message, err := io.ReadAll(reader)
if err != nil {
ctx.Errorf("failed to read pod logs %s/%s: %v", pod.Namespace, pod.Name, err)
return ""
}

if !ctx.IsTrace() && !ctx.IsDebug() && len(message) > 3000 {
message = message[len(message)-3000:]
}
return message
return string(message)
}

func podExecf(ctx *context.Context, pod corev1.Pod, results pkg.Results, cmd string, args ...interface{}) (string, bool) {
if !ctx.IsTrace() {
ctx.Kommons().Logger.SetLogLevel(0)
}

_cmd := fmt.Sprintf(cmd, args...)
stdout, stderr, err := ctx.Kommons().ExecutePodf(pod.Namespace, pod.Name, containerName, "bash", "-c", _cmd)
stdout, stderr, err := ctx.KubernetesClient().ExecutePodf(ctx, pod.Namespace, pod.Name, containerName, "bash", "-c", _cmd)
if stderr != "" || err != nil {
podFail(ctx, pod, results.Failf("error running %s: %v %v %v", _cmd, stdout, stderr, err))
return "", false
}

return strings.TrimSpace(stdout), true
}

Expand Down Expand Up @@ -189,7 +200,7 @@ func (c *JunitChecker) Check(ctx *context.Context, extConfig external.Check) pkg
var results pkg.Results
results = append(results, result)

if ctx.Kommons() == nil {
if ctx.KubernetesClient() == nil {
return results.Failf("Kubernetes is not initialized")
}

Expand All @@ -216,13 +227,13 @@ func (c *JunitChecker) Check(ctx *context.Context, extConfig external.Check) pkg
ctx.Tracef("[%s/%s] waiting for tests to complete", ctx.Namespace, ctx.Canary.Name)
if ctx.IsTrace() {
go func() {
if err := ctx.Kommons().StreamLogsV2(ctx.Namespace, pod.Name, timeout, pod.Spec.InitContainers[0].Name); err != nil {
if err := ctx.KubernetesClient().StreamLogsV2(ctx, ctx.Namespace, pod.Name, timeout, pod.Spec.InitContainers[0].Name); err != nil {
ctx.Error(err, "error streaming")
}
}()
}

if err := ctx.Kommons().WaitForPod(ctx.Namespace, pod.Name, timeout, corev1.PodRunning, corev1.PodSucceeded, corev1.PodFailed); err != nil {
if err := ctx.KubernetesClient().WaitForPod(ctx, ctx.Namespace, pod.Name, timeout, corev1.PodRunning, corev1.PodSucceeded, corev1.PodFailed); err != nil {
result.ErrorMessage(err)
}

Expand Down
2 changes: 1 addition & 1 deletion checks/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *KubernetesChecker) Check(ctx context.Context, extConfig external.Check)
return results.Failf("Kubernetes is not initialized")
}

client, err := ctx.KubernetesDynamicClient().GetClientByKind(check.Kind)
client, err := ctx.KubernetesClient().GetClientByKind(check.Kind)
if err != nil {
return results.Failf("Failed to get client for kind %s: %v", check.Kind, err)
}
Expand Down
18 changes: 10 additions & 8 deletions checks/kubernetes_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/flanksource/gomplate/v3"
"github.com/flanksource/is-healthy/pkg/health"
"github.com/flanksource/is-healthy/pkg/lua"
"github.com/flanksource/kommons"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
"github.com/sethvargo/go-retry"
Expand All @@ -26,7 +27,6 @@ import (
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/utils"
)

const (
Expand Down Expand Up @@ -94,9 +94,11 @@ func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.Kubernet
return results.Failf("templating error: %v", err)
}

kClient := kommons.NewClient(ctx.KubernetesRestConfig(), ctx.Logger)

for i := range check.StaticResources {
resource := check.StaticResources[i]
if err := ctx.Kommons().ApplyUnstructured(resource.GetNamespace(), &resource); err != nil {
if err := kClient.ApplyUnstructured(resource.GetNamespace(), &resource); err != nil {
return results.Failf("failed to apply static resource %s: %v", resource.GetName(), err)
}
}
Expand All @@ -115,7 +117,7 @@ func (c *KubernetesResourceChecker) Check(ctx context.Context, check v1.Kubernet

for i := range check.Resources {
resource := check.Resources[i]
if err := ctx.Kommons().ApplyUnstructured(resource.GetNamespace(), &resource); err != nil {
if err := kClient.ApplyUnstructured(resource.GetNamespace(), &resource); err != nil {
return results.Failf("failed to apply resource (%s/%s/%s): %v", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err)
}
}
Expand Down Expand Up @@ -229,7 +231,7 @@ func (c *KubernetesResourceChecker) evalWaitFor(ctx context.Context, check v1.Ku

ctx.Logger.V(4).Infof("waiting for %d resources to be in the desired state. (attempts: %d)", check.TotalResources(), attempts)

resourceObjs, err := ctx.KubernetesDynamicClient().FetchResources(ctx, append(check.StaticResources, check.Resources...)...)
resourceObjs, err := ctx.KubernetesClient().FetchResources(ctx, append(check.StaticResources, check.Resources...)...)
if err != nil {
if apiErrors.IsNotFound(err) {
return retry.RetryableError(err)
Expand Down Expand Up @@ -319,12 +321,12 @@ func DeleteResources(ctx context.Context, check v1.KubernetesResourceCheck, dele
continue // client already cached
}

namespace := utils.Coalesce(resource.GetNamespace(), ctx.Namespace)
rc, _, _, err := ctx.Kommons().GetDynamicClientFor(namespace, &resource)
rc, err := ctx.KubernetesClient().GetClientByGroupVersionKind(resource.GroupVersionKind().Group, resource.GroupVersionKind().Version, resource.GetKind())
if err != nil {
return fmt.Errorf("failed to get rest client for (%s/%s/%s): %w", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err)
}
clients.Store(gvk, rc)

clients.Store(gvk, rc.Namespace(resource.GetNamespace()))
}

eg, _ := errgroup.WithContext(ctx)
Expand All @@ -333,7 +335,7 @@ func DeleteResources(ctx context.Context, check v1.KubernetesResourceCheck, dele

eg.Go(func() error {
cachedClient, _ := clients.Load(resource.GetObjectKind().GroupVersionKind())
rc := cachedClient.(dynamic.ResourceInterface)
rc := cachedClient.(dynamic.NamespaceableResourceInterface)

deleteOpt := metav1.DeleteOptions{
GracePeriodSeconds: lo.ToPtr(int64(0)),
Expand Down
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.31.0 // indirect
github.com/aws/smithy-go v1.21.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bmatcuk/doublestar/v4 v4.7.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cert-manager/cert-manager v1.16.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down Expand Up @@ -205,7 +203,6 @@ require (
github.com/hirochachacha/go-smb2 v1.1.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/jsonschema v0.12.0 // indirect
github.com/itchyny/gojq v0.12.16 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect
Expand Down Expand Up @@ -281,15 +278,11 @@ require (
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,6 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.31.0/go.mod h1:yMWe0F+XG0DkRZK5ODZhG
github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/aws/smithy-go v1.21.0 h1:H7L8dtDRk0P1Qm6y0ji7MCYMQObJ5R9CRpyPhRUkLYA=
github.com/aws/smithy-go v1.21.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand All @@ -769,8 +767,6 @@ github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0
github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 h1:6lhrsTEnloDPXyeZBvSYvQf8u86jbKehZPVDDlkgDl4=
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
Expand Down Expand Up @@ -1194,8 +1190,6 @@ github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4=
github.com/itchyny/gojq v0.12.16 h1:yLfgLxhIr/6sJNVmYfQjTIv0jGctu6/DgDoivmxTr7g=
github.com/itchyny/gojq v0.12.16/go.mod h1:6abHbdC2uB9ogMS38XsErnfqJ94UlngIJGlRAIj4jTM=
Expand Down Expand Up @@ -1613,8 +1607,6 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
Expand All @@ -1625,12 +1617,6 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xhit/go-str2duration v1.2.0/go.mod h1:3cPSlfZlUHVlneIVfePFWcJZsuwf+P1v2SRTV4cUmp4=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ var CleanupCRDDeleteCanaries = &dutyjob.Job{
return nil
}

canaryClient, err := ctx.KubernetesDynamicClient().
canaryClient, err := ctx.KubernetesClient().
GetClientByGroupVersionKind(v1.GroupVersion.Group, v1.GroupVersion.Version, "Canary")
if err != nil {
return fmt.Errorf("failed to get kubernetes client for canaries: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/jobs/topology/topology_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ var TopologyCRDReconcile = &job.Job{
return err
}

client, err := ctx.KubernetesDynamicClient().GetClientByKind("Topology")
client, err := ctx.KubernetesClient().GetClientByKind("Topology")
if err != nil {
return fmt.Errorf("error creating dynamic client for Topology: %w", err)
}
Expand Down

0 comments on commit 8e1f7f0

Please sign in to comment.