Skip to content

Commit 18763c2

Browse files
committed
fix: add tests, remove unused variables
1 parent 308bb88 commit 18763c2

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

pkg/client/client.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/apimachinery/pkg/runtime/schema"
2727
"k8s.io/apimachinery/pkg/runtime/serializer"
28-
"k8s.io/client-go/dynamic"
2928
"k8s.io/client-go/kubernetes/scheme"
3029
"k8s.io/client-go/rest"
3130
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
@@ -69,11 +68,6 @@ func New(config *rest.Config, options Options) (Client, error) {
6968
}
7069
}
7170

72-
dynamicClient, err := dynamic.NewForConfig(config)
73-
if err != nil {
74-
return nil, err
75-
}
76-
7771
clientcache := &clientCache{
7872
config: config,
7973
scheme: options.Scheme,
@@ -89,9 +83,7 @@ func New(config *rest.Config, options Options) (Client, error) {
8983
},
9084
unstructuredClient: unstructuredClient{
9185
cache: clientcache,
92-
client: dynamicClient,
93-
restMapper: options.Mapper,
94-
paramCodec: parameterCodec{},
86+
paramCodec: noConversionParamCodec{},
9587
},
9688
}
9789

pkg/client/client_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,51 @@ var _ = Describe("Client", func() {
827827
close(done)
828828
})
829829

830+
It("should update status and preserve type information", func(done Done) {
831+
cl, err := client.New(cfg, client.Options{})
832+
Expect(err).NotTo(HaveOccurred())
833+
Expect(cl).NotTo(BeNil())
834+
835+
By("initially creating a Deployment")
836+
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
837+
Expect(err).NotTo(HaveOccurred())
838+
839+
By("updating the status of Deployment")
840+
u := &unstructured.Unstructured{}
841+
dep.Status.Replicas = 1
842+
Expect(scheme.Convert(dep, u, nil)).To(Succeed())
843+
err = cl.Status().Update(context.TODO(), u)
844+
Expect(err).NotTo(HaveOccurred())
845+
846+
By("validating updated Deployment has type information")
847+
Expect(u.GroupVersionKind()).To(Equal(depGvk))
848+
849+
close(done)
850+
})
851+
852+
It("should patch status and preserve type information", func(done Done) {
853+
cl, err := client.New(cfg, client.Options{})
854+
Expect(err).NotTo(HaveOccurred())
855+
Expect(cl).NotTo(BeNil())
856+
857+
By("initially creating a Deployment")
858+
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
859+
Expect(err).NotTo(HaveOccurred())
860+
861+
By("patching the status of Deployment")
862+
u := &unstructured.Unstructured{}
863+
depPatch := client.MergeFrom(dep.DeepCopy())
864+
dep.Status.Replicas = 1
865+
Expect(scheme.Convert(dep, u, nil)).To(Succeed())
866+
err = cl.Status().Patch(context.TODO(), u, depPatch)
867+
Expect(err).NotTo(HaveOccurred())
868+
869+
By("validating updated Deployment has type information")
870+
Expect(u.GroupVersionKind()).To(Equal(depGvk))
871+
872+
close(done)
873+
})
874+
830875
It("should not update spec of an existing object", func(done Done) {
831876
cl, err := client.New(cfg, client.Options{})
832877
Expect(err).NotTo(HaveOccurred())

pkg/client/codec.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99
"k8s.io/apimachinery/pkg/runtime/schema"
1010
)
1111

12-
type parameterCodec struct{}
12+
var _ runtime.ParameterCodec = noConversionParamCodec{}
1313

14-
func (parameterCodec) EncodeParameters(obj runtime.Object, to schema.GroupVersion) (url.Values, error) {
14+
// noConversionParamCodec is a no-conversion codec for serializing parameters into URL query strings.
15+
// it's useful in scenarios with the unstructured client and arbitrary resouces.
16+
type noConversionParamCodec struct{}
17+
18+
func (noConversionParamCodec) EncodeParameters(obj runtime.Object, to schema.GroupVersion) (url.Values, error) {
1519
return queryparams.Convert(obj)
1620
}
1721

18-
func (parameterCodec) DecodeParameters(parameters url.Values, from schema.GroupVersion, into runtime.Object) error {
19-
return errors.New("DecodeParameters not implemented on dynamic parameterCodec")
22+
func (noConversionParamCodec) DecodeParameters(parameters url.Values, from schema.GroupVersion, into runtime.Object) error {
23+
return errors.New("DecodeParameters not implemented on noConversionParamCodec")
2024
}

pkg/client/unstructured_client.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@ import (
2121
"fmt"
2222
"strings"
2323

24-
"k8s.io/apimachinery/pkg/api/meta"
2524
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2625
"k8s.io/apimachinery/pkg/runtime"
27-
"k8s.io/client-go/dynamic"
2826
)
2927

3028
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
3129
// new clients at the time they are used, and caches the client.
3230
type unstructuredClient struct {
3331
cache *clientCache
34-
client dynamic.Interface
35-
restMapper meta.RESTMapper
3632
paramCodec runtime.ParameterCodec
3733
}
3834

@@ -190,7 +186,9 @@ func (uc *unstructuredClient) Get(ctx context.Context, key ObjectKey, obj runtim
190186
NamespaceIfScoped(key.Namespace, r.isNamespaced()).
191187
Resource(r.resource()).
192188
Context(ctx).
193-
Name(key.Name).Do().Into(obj)
189+
Name(key.Name).
190+
Do().
191+
Into(obj)
194192

195193
u.SetGroupVersionKind(gvk)
196194

@@ -250,11 +248,13 @@ func (uc *unstructuredClient) UpdateStatus(ctx context.Context, obj runtime.Obje
250248
}
251249

252250
func (uc *unstructuredClient) PatchStatus(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error {
253-
_, ok := obj.(*unstructured.Unstructured)
251+
u, ok := obj.(*unstructured.Unstructured)
254252
if !ok {
255253
return fmt.Errorf("unstructured client did not understand object: %T", obj)
256254
}
257255

256+
gvk := u.GroupVersionKind()
257+
258258
o, err := uc.cache.getObjMeta(obj)
259259
if err != nil {
260260
return err
@@ -266,7 +266,7 @@ func (uc *unstructuredClient) PatchStatus(ctx context.Context, obj runtime.Objec
266266
}
267267

268268
patchOpts := &PatchOptions{}
269-
return o.Patch(patch.Type()).
269+
result := o.Patch(patch.Type()).
270270
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
271271
Resource(o.resource()).
272272
Name(o.GetName()).
@@ -275,5 +275,8 @@ func (uc *unstructuredClient) PatchStatus(ctx context.Context, obj runtime.Objec
275275
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
276276
Context(ctx).
277277
Do().
278-
Into(obj)
278+
Into(u)
279+
280+
u.SetGroupVersionKind(gvk)
281+
return result
279282
}

0 commit comments

Comments
 (0)