Skip to content

Commit d3eaef3

Browse files
authored
Merge pull request #3475 from alvaroaleman/fixfix
🐛 Update typed Applyconfigurations with server response
2 parents c8b4b9d + 3296f32 commit d3eaef3

3 files changed

Lines changed: 31 additions & 34 deletions

File tree

pkg/client/applyconfigurations.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,6 @@ func ApplyConfigurationFromUnstructured(u *unstructured.Unstructured) runtime.Ap
3939
return &unstructuredApplyConfiguration{Unstructured: u}
4040
}
4141

42-
type applyconfigurationRuntimeObject struct {
43-
runtime.ApplyConfiguration
44-
}
45-
46-
func (a *applyconfigurationRuntimeObject) GetObjectKind() schema.ObjectKind {
47-
return a
48-
}
49-
50-
func (a *applyconfigurationRuntimeObject) GroupVersionKind() schema.GroupVersionKind {
51-
return schema.GroupVersionKind{}
52-
}
53-
54-
func (a *applyconfigurationRuntimeObject) SetGroupVersionKind(gvk schema.GroupVersionKind) {}
55-
56-
func (a *applyconfigurationRuntimeObject) DeepCopyObject() runtime.Object {
57-
panic("applyconfigurationRuntimeObject does not support DeepCopyObject")
58-
}
59-
60-
func runtimeObjectFromApplyConfiguration(ac runtime.ApplyConfiguration) runtime.Object {
61-
return &applyconfigurationRuntimeObject{ApplyConfiguration: ac}
62-
}
63-
6442
func gvkFromApplyConfiguration(ac applyConfiguration) (schema.GroupVersionKind, error) {
6543
var gvk schema.GroupVersionKind
6644
gv, err := schema.ParseGroupVersion(ptr.Deref(ac.GetAPIVersion(), ""))

pkg/client/client_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
943943

944944
err = cl.Apply(ctx, client.ApplyConfigurationFromUnstructured(obj), &client.ApplyOptions{FieldManager: "test-manager"})
945945
Expect(err).NotTo(HaveOccurred())
946+
Expect(obj.GetResourceVersion()).NotTo(BeEmpty())
946947

947948
cm, err := clientset.CoreV1().ConfigMaps(obj.GetNamespace()).Get(ctx, obj.GetName(), metav1.GetOptions{})
948949
Expect(err).NotTo(HaveOccurred())
@@ -1015,6 +1016,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
10151016

10161017
err = cl.Apply(ctx, obj, &client.ApplyOptions{FieldManager: "test-manager"})
10171018
Expect(err).NotTo(HaveOccurred())
1019+
Expect(obj.ResourceVersion).NotTo(BeNil())
10181020

10191021
cm, err := clientset.CoreV1().ConfigMaps(ptr.Deref(obj.GetNamespace(), "")).Get(ctx, ptr.Deref(obj.GetName(), ""), metav1.GetOptions{})
10201022
Expect(err).NotTo(HaveOccurred())
@@ -1289,6 +1291,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
12891291
By("Applying the scale subresurce")
12901292
deploymentAC, err := appsv1applyconfigurations.ExtractDeployment(dep, "foo")
12911293
Expect(err).NotTo(HaveOccurred())
1294+
initialRV := deploymentAC.ResourceVersion
12921295
scale := autoscaling1applyconfigurations.Scale().
12931296
WithSpec(autoscaling1applyconfigurations.ScaleSpec().WithReplicas(replicaCount))
12941297
err = cl.SubResource("scale").Apply(ctx, deploymentAC,
@@ -1297,6 +1300,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
12971300
client.ForceOwnership,
12981301
)
12991302
Expect(err).NotTo(HaveOccurred())
1303+
Expect(deploymentAC.ResourceVersion).ToNot(Equal(initialRV))
13001304

13011305
By("Asserting replicas got updated")
13021306
dep, err = clientset.AppsV1().Deployments(dep.Namespace).Get(ctx, dep.Name, metav1.GetOptions{})

pkg/client/typed_client.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/util/json"
2425
"k8s.io/client-go/util/apply"
2526
)
2627

@@ -146,17 +147,24 @@ func (c *typedClient) Apply(ctx context.Context, obj runtime.ApplyConfiguration,
146147
applyOpts := &ApplyOptions{}
147148
applyOpts.ApplyOptions(opts)
148149

149-
return req.
150+
var contentType string
151+
body, err := req.
150152
NamespaceIfScoped(o.namespace, o.isNamespaced()).
151153
Resource(o.resource()).
152154
Name(o.name).
153155
VersionedParams(applyOpts.AsPatchOptions(), c.paramCodec).
154156
Do(ctx).
155-
// This is hacky, it is required because `Into` takes a `runtime.Object` and
156-
// that is not implemented by the ApplyConfigurations. The generated clients
157-
// don't have this problem because they deserialize into the api type, not the
158-
// apply configuration: https://github.com/kubernetes/kubernetes/blob/22f5e01a37c0bc6a5f494dec14dd4e3688ee1d55/staging/src/k8s.io/client-go/gentype/type.go#L296-L317
159-
Into(runtimeObjectFromApplyConfiguration(obj))
157+
ContentType(&contentType).
158+
Raw()
159+
if err != nil {
160+
return err
161+
}
162+
163+
if contentType != "application/json" {
164+
return fmt.Errorf("unexpected content type %q in apply response, expected application/json", contentType)
165+
}
166+
167+
return json.Unmarshal(body, obj)
160168
}
161169

162170
// Get implements client.Client.
@@ -324,16 +332,23 @@ func (c *typedClient) ApplySubResource(ctx context.Context, obj runtime.ApplyCon
324332
return fmt.Errorf("failed to create apply request: %w", err)
325333
}
326334

327-
return req.
335+
var contentType string
336+
respBody, err := req.
328337
NamespaceIfScoped(o.namespace, o.isNamespaced()).
329338
Resource(o.resource()).
330339
Name(o.name).
331340
SubResource(subResource).
332341
VersionedParams(applyOpts.AsPatchOptions(), c.paramCodec).
333342
Do(ctx).
334-
// This is hacky, it is required because `Into` takes a `runtime.Object` and
335-
// that is not implemented by the ApplyConfigurations. The generated clients
336-
// don't have this problem because they deserialize into the api type, not the
337-
// apply configuration: https://github.com/kubernetes/kubernetes/blob/22f5e01a37c0bc6a5f494dec14dd4e3688ee1d55/staging/src/k8s.io/client-go/gentype/type.go#L296-L317
338-
Into(runtimeObjectFromApplyConfiguration(obj))
343+
ContentType(&contentType).
344+
Raw()
345+
if err != nil {
346+
return err
347+
}
348+
349+
if contentType != "application/json" {
350+
return fmt.Errorf("unexpected content type %q in apply response, expected application/json", contentType)
351+
}
352+
353+
return json.Unmarshal(respBody, obj)
339354
}

0 commit comments

Comments
 (0)