Skip to content

Commit

Permalink
Update apply to return warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Mmadu Manasseh <[email protected]>
  • Loading branch information
MeNsaaH committed Jan 13, 2025
1 parent ae53c15 commit 70f6053
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
9 changes: 5 additions & 4 deletions pkg/checks/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down
17 changes: 14 additions & 3 deletions pkg/kubectl/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions pkg/kubectl/cli-runtime/resource/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
}
Expand All @@ -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) {
Expand Down

0 comments on commit 70f6053

Please sign in to comment.