diff --git a/pkg/checks/diff/diff.go b/pkg/checks/diff/diff.go index 0738cba8..88facb4a 100644 --- a/pkg/checks/diff/diff.go +++ b/pkg/checks/diff/diff.go @@ -4,6 +4,10 @@ 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" @@ -15,21 +19,18 @@ import ( "github.com/argoproj/gitops-engine/pkg/diff" "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/gitops-engine/pkg/utils/kube" "github.com/zapier/kubechecks/pkg/msg" "github.com/zapier/kubechecks/telemetry" ) diff --git a/pkg/kubectl/apply/apply.go b/pkg/kubectl/apply/apply.go index 6ea921eb..f67722a2 100644 --- a/pkg/kubectl/apply/apply.go +++ b/pkg/kubectl/apply/apply.go @@ -37,7 +37,6 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" "k8s.io/cli-runtime/pkg/printers" - "k8s.io/cli-runtime/pkg/resource" "k8s.io/client-go/dynamic" "k8s.io/client-go/openapi3" "k8s.io/client-go/util/csaupgrade" @@ -52,6 +51,8 @@ import ( "k8s.io/kubectl/pkg/util/prune" "k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/validation" + + "github.com/zapier/kubechecks/pkg/kubectl/cli-runtime/resource" ) // ApplyFlags directly reflect the information that CLI is gathering via flags. They will be converted to Options, which @@ -584,15 +585,25 @@ func (o *ApplyOptions) applyOneObject(info *resource.Info) error { options := metav1.PatchOptions{ Force: &o.ForceConflicts, } - obj, err := helper. + result := helper. WithSubresource(o.Subresource). - Patch( + PatchWithWarnings( info.Namespace, info.Name, types.ApplyPatchType, data, &options, ) + if len(result.Warnings()) > 0 { + warnings := "" + for _, warning := range result.Warnings() { + warnings += fmt.Sprintf("Warning: %s\n", warning.Text) + } + if warnings != "" { + return fmt.Errorf("%s", warnings) + } + } + obj, err := result.Get() if err != nil { if isIncompatibleServerError(err) { err = fmt.Errorf("Server-side apply not available on the server: (%v)", err) diff --git a/pkg/kubectl/cli-runtime/resource/helper.go b/pkg/kubectl/cli-runtime/resource/helper.go index aa400ae0..46f7574d 100644 --- a/pkg/kubectl/cli-runtime/resource/helper.go +++ b/pkg/kubectl/cli-runtime/resource/helper.go @@ -23,6 +23,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/rest" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/runtime" @@ -247,6 +248,10 @@ func (m *Helper) createResource(c RESTClient, resource, namespace string, obj ru Get() } func (m *Helper) Patch(namespace, name string, pt types.PatchType, data []byte, options *metav1.PatchOptions) (runtime.Object, error) { + return m.PatchWithWarnings(namespace, name, pt, data, options).Get() +} + +func (m *Helper) PatchWithWarnings(namespace, name string, pt types.PatchType, data []byte, options *metav1.PatchOptions) rest.Result { if options == nil { options = &metav1.PatchOptions{} } @@ -266,8 +271,7 @@ func (m *Helper) Patch(namespace, name string, pt types.PatchType, data []byte, SubResource(m.Subresource). VersionedParams(options, metav1.ParameterCodec). Body(data). - Do(context.TODO()). - Get() + Do(context.TODO()) } func (m *Helper) Replace(namespace, name string, overwrite bool, obj runtime.Object) (runtime.Object, error) {