Skip to content

Commit 70f6053

Browse files
committed
Update apply to return warnings
Signed-off-by: Mmadu Manasseh <manasseh.mmadu@zapier.com>
1 parent ae53c15 commit 70f6053

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

pkg/checks/diff/diff.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"io"
8+
"strings"
9+
"time"
10+
711
cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
812
"github.com/argoproj/argo-cd/v2/controller"
913
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
@@ -15,21 +19,18 @@ import (
1519
"github.com/argoproj/gitops-engine/pkg/diff"
1620
"github.com/argoproj/gitops-engine/pkg/sync/hook"
1721
"github.com/argoproj/gitops-engine/pkg/sync/ignore"
18-
"github.com/argoproj/gitops-engine/pkg/utils/kube"
1922
"github.com/argoproj/gitops-engine/pkg/utils/tracing"
2023
"github.com/ghodss/yaml"
2124
"github.com/go-logr/zerologr"
2225
"github.com/pmezard/go-difflib/difflib"
2326
"github.com/rs/zerolog/log"
24-
"io"
2527
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2628
"k8s.io/apimachinery/pkg/runtime/schema"
2729
"k8s.io/client-go/rest"
2830
"k8s.io/klog/v2/textlogger"
29-
"strings"
30-
"time"
3131

3232
"github.com/zapier/kubechecks/pkg/checks"
33+
"github.com/zapier/kubechecks/pkg/gitops-engine/pkg/utils/kube"
3334
"github.com/zapier/kubechecks/pkg/msg"
3435
"github.com/zapier/kubechecks/telemetry"
3536
)

pkg/kubectl/apply/apply.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"k8s.io/cli-runtime/pkg/genericclioptions"
3838
"k8s.io/cli-runtime/pkg/genericiooptions"
3939
"k8s.io/cli-runtime/pkg/printers"
40-
"k8s.io/cli-runtime/pkg/resource"
4140
"k8s.io/client-go/dynamic"
4241
"k8s.io/client-go/openapi3"
4342
"k8s.io/client-go/util/csaupgrade"
@@ -52,6 +51,8 @@ import (
5251
"k8s.io/kubectl/pkg/util/prune"
5352
"k8s.io/kubectl/pkg/util/templates"
5453
"k8s.io/kubectl/pkg/validation"
54+
55+
"github.com/zapier/kubechecks/pkg/kubectl/cli-runtime/resource"
5556
)
5657

5758
// 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 {
584585
options := metav1.PatchOptions{
585586
Force: &o.ForceConflicts,
586587
}
587-
obj, err := helper.
588+
result := helper.
588589
WithSubresource(o.Subresource).
589-
Patch(
590+
PatchWithWarnings(
590591
info.Namespace,
591592
info.Name,
592593
types.ApplyPatchType,
593594
data,
594595
&options,
595596
)
597+
if len(result.Warnings()) > 0 {
598+
warnings := ""
599+
for _, warning := range result.Warnings() {
600+
warnings += fmt.Sprintf("Warning: %s\n", warning.Text)
601+
}
602+
if warnings != "" {
603+
return fmt.Errorf("%s", warnings)
604+
}
605+
}
606+
obj, err := result.Get()
596607
if err != nil {
597608
if isIncompatibleServerError(err) {
598609
err = fmt.Errorf("Server-side apply not available on the server: (%v)", err)

pkg/kubectl/cli-runtime/resource/helper.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
apierrors "k8s.io/apimachinery/pkg/api/errors"
2424
"k8s.io/apimachinery/pkg/api/meta"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/client-go/rest"
2627

2728
"k8s.io/apimachinery/pkg/fields"
2829
"k8s.io/apimachinery/pkg/runtime"
@@ -247,6 +248,10 @@ func (m *Helper) createResource(c RESTClient, resource, namespace string, obj ru
247248
Get()
248249
}
249250
func (m *Helper) Patch(namespace, name string, pt types.PatchType, data []byte, options *metav1.PatchOptions) (runtime.Object, error) {
251+
return m.PatchWithWarnings(namespace, name, pt, data, options).Get()
252+
}
253+
254+
func (m *Helper) PatchWithWarnings(namespace, name string, pt types.PatchType, data []byte, options *metav1.PatchOptions) rest.Result {
250255
if options == nil {
251256
options = &metav1.PatchOptions{}
252257
}
@@ -266,8 +271,7 @@ func (m *Helper) Patch(namespace, name string, pt types.PatchType, data []byte,
266271
SubResource(m.Subresource).
267272
VersionedParams(options, metav1.ParameterCodec).
268273
Body(data).
269-
Do(context.TODO()).
270-
Get()
274+
Do(context.TODO())
271275
}
272276

273277
func (m *Helper) Replace(namespace, name string, overwrite bool, obj runtime.Object) (runtime.Object, error) {

0 commit comments

Comments
 (0)