From 8175c8750eb196488b07e8d20485cb13dad0c52b Mon Sep 17 00:00:00 2001 From: Dave Winiarski Date: Thu, 5 Dec 2024 07:02:35 -0700 Subject: [PATCH 1/2] add incluster kubectl client to test serverside with mutatation --- pkg/checks/diff/diff.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/pkg/checks/diff/diff.go b/pkg/checks/diff/diff.go index 8d84d46c..1afb593b 100644 --- a/pkg/checks/diff/diff.go +++ b/pkg/checks/diff/diff.go @@ -4,10 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io" - "strings" - "time" - cmdutil "github.com/argoproj/argo-cd/v2/cmd/util" "github.com/argoproj/argo-cd/v2/controller" "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" @@ -20,12 +16,18 @@ import ( "github.com/argoproj/gitops-engine/pkg/sync/hook" "github.com/argoproj/gitops-engine/pkg/sync/ignore" "github.com/argoproj/gitops-engine/pkg/utils/kube" + "github.com/argoproj/gitops-engine/pkg/utils/tracing" "github.com/ghodss/yaml" "github.com/go-logr/zerologr" "github.com/pmezard/go-difflib/difflib" "github.com/rs/zerolog/log" + "io" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/rest" + "k8s.io/klog/v2/textlogger" + "strings" + "time" "github.com/zapier/kubechecks/pkg/checks" "github.com/zapier/kubechecks/pkg/msg" @@ -201,11 +203,32 @@ func generateDiff(ctx context.Context, request checks.Request, argoSettings *set ignoreNormalizerOpts := normalizers.IgnoreNormalizerOpts{ JQExecutionTimeout: 1 * time.Second, } + kubeCtl := &kube.KubectlCmd{ + Tracer: tracing.NopTracer{}, + Log: textlogger.NewLogger(textlogger.NewConfig()), + } + config, err := rest.InClusterConfig() + if err != nil { + return diff.DiffResult{}, err + } + apiRes, _, err := kubeCtl.LoadOpenAPISchema(config) + if err != nil { + return diff.DiffResult{}, err + } + resources, _, err := kubeCtl.ManageResources(config, apiRes) + if err != nil { + return diff.DiffResult{}, err + } + dryRunner := diff.NewK8sServerSideDryRunner(resources) + diffConfig, err := argodiff.NewDiffConfigBuilder(). WithLogger(zerologr.New(&log.Logger)). WithDiffSettings(request.App.Spec.IgnoreDifferences, overrides, ignoreAggregatedRoles, ignoreNormalizerOpts). WithTracking(argoSettings.AppLabelKey, argoSettings.TrackingMethod). WithNoCache(). + WithIgnoreMutationWebhook(false). + WithServerSideDiff(true). + WithServerSideDryRunner(dryRunner). Build() if err != nil { telemetry.SetError(span, err, "Build Diff") From 2a7a65293d8ce429014758b741a6f45941f03419 Mon Sep 17 00:00:00 2001 From: Dave Winiarski Date: Thu, 5 Dec 2024 12:08:28 -0700 Subject: [PATCH 2/2] update k8s dep --- go.mod | 4 ++-- pkg/checks/diff/diff.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 67f8d190..96e446eb 100644 --- a/go.mod +++ b/go.mod @@ -60,7 +60,8 @@ require ( k8s.io/api v0.31.3 k8s.io/apiextensions-apiserver v0.31.2 k8s.io/apimachinery v0.31.3 - k8s.io/client-go v0.31.3 + k8s.io/client-go v1.5.2 + k8s.io/klog/v2 v2.130.1 sigs.k8s.io/controller-runtime v0.19.3 sigs.k8s.io/yaml v1.4.0 ) @@ -295,7 +296,6 @@ require ( k8s.io/cli-runtime v0.31.3 // indirect k8s.io/component-base v0.31.3 // indirect k8s.io/component-helpers v0.31.3 // indirect - k8s.io/klog/v2 v2.130.1 // indirect k8s.io/kube-aggregator v0.31.2 // indirect k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect k8s.io/kubectl v0.31.2 // indirect diff --git a/pkg/checks/diff/diff.go b/pkg/checks/diff/diff.go index 1afb593b..2301b5a0 100644 --- a/pkg/checks/diff/diff.go +++ b/pkg/checks/diff/diff.go @@ -229,6 +229,7 @@ func generateDiff(ctx context.Context, request checks.Request, argoSettings *set WithIgnoreMutationWebhook(false). WithServerSideDiff(true). WithServerSideDryRunner(dryRunner). + WithManager("application/apply-patch"). Build() if err != nil { telemetry.SetError(span, err, "Build Diff")