Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
policyv1 "k8s.io/api/policy/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -1101,6 +1102,26 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
Expect(secret.Data).To(BeComparableTo(data))
Expect(secret.Data).To(BeComparableTo(secretApplyConfiguration.Data))
})

It("should propagate a typed error", func(ctx SpecContext) {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

obj := corev1applyconfigurations.
Secret("typed-secret", "default").
WithType(corev1.SecretTypeDockercfg).
WithStringData(map[string]string{".dockercfg": "{}"})

err = cl.Apply(ctx, obj, &client.ApplyOptions{FieldManager: "test-manager"})
Expect(err).ToNot(HaveOccurred())

obj = corev1applyconfigurations.
Secret(*obj.Name, *obj.Namespace).
WithType(corev1.SecretTypeOpaque)
err = cl.Apply(ctx, obj, &client.ApplyOptions{FieldManager: "test-manager"})
Expect(isImmutableError(err)).To(BeTrue())
})
})
})

Expand Down Expand Up @@ -4433,3 +4454,8 @@ func toUnstructured(o client.Object) (*unstructured.Unstructured, error) {
u := &unstructured.Unstructured{}
return u, json.Unmarshal(serialized, u)
}

func isImmutableError(err error) bool {
return apierrors.IsInvalid(err) &&
strings.Contains(err.Error(), validation.FieldImmutableErrorMsg)
}
22 changes: 16 additions & 6 deletions pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ func (c *typedClient) Apply(ctx context.Context, obj runtime.ApplyConfiguration,
applyOpts := &ApplyOptions{}
applyOpts.ApplyOptions(opts)

var contentType string
body, err := req.
resp := req.
NamespaceIfScoped(o.namespace, o.isNamespaced()).
Resource(o.resource()).
Name(o.name).
VersionedParams(applyOpts.AsPatchOptions(), c.paramCodec).
Do(ctx).
Do(ctx)
if err := resp.Error(); err != nil {
return err
}

var contentType string
body, err := resp.
ContentType(&contentType).
Raw()
if err != nil {
Expand Down Expand Up @@ -332,14 +337,19 @@ func (c *typedClient) ApplySubResource(ctx context.Context, obj runtime.ApplyCon
return fmt.Errorf("failed to create apply request: %w", err)
}

var contentType string
respBody, err := req.
resp := req.
NamespaceIfScoped(o.namespace, o.isNamespaced()).
Resource(o.resource()).
Name(o.name).
SubResource(subResource).
VersionedParams(applyOpts.AsPatchOptions(), c.paramCodec).
Do(ctx).
Do(ctx)
if err := resp.Error(); err != nil {
return err
}

var contentType string
respBody, err := resp.
ContentType(&contentType).
Raw()
if err != nil {
Expand Down