Skip to content

Commit 4af5c70

Browse files
committed
refactor: client package
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
1 parent eaf5860 commit 4af5c70

18 files changed

+185
-185
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.2
55
require (
66
github.com/Masterminds/sprig v2.22.0+incompatible
77
github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0
8+
github.com/evanphx/json-patch v5.9.0+incompatible
89
github.com/fatih/color v1.17.0
910
github.com/go-logr/logr v1.4.2
1011
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
@@ -58,7 +59,6 @@ require (
5859
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
5960
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
6061
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
61-
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
6262
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
6363
github.com/felixge/httpsnoop v1.0.4 // indirect
6464
github.com/fsnotify/fsnotify v1.7.0 // indirect

pkg/client/client.go

-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"k8s.io/apimachinery/pkg/api/meta"
77
"k8s.io/apimachinery/pkg/runtime"
8-
"k8s.io/client-go/rest"
98
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
109
)
1110

@@ -41,8 +40,3 @@ type Client interface {
4140
// RESTMapper returns the rest this client is using.
4241
RESTMapper() meta.RESTMapper
4342
}
44-
45-
func New(cfg *rest.Config) (Client, error) {
46-
var opts ctrlclient.Options
47-
return ctrlclient.New(cfg, opts)
48-
}

pkg/client/dry_run.go

-50
This file was deleted.

pkg/client/dryrun/dry_run.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package dryrun
2+
3+
import (
4+
"context"
5+
6+
"github.com/kyverno/chainsaw/pkg/client"
7+
"k8s.io/apimachinery/pkg/api/meta"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/apimachinery/pkg/types"
10+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
11+
)
12+
13+
type Client = client.Client
14+
15+
type dryRunClient struct {
16+
inner Client
17+
}
18+
19+
func (c *dryRunClient) Create(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.CreateOption) error {
20+
return c.inner.Create(ctx, obj, append(opts, ctrlclient.DryRunAll)...)
21+
}
22+
23+
func (c *dryRunClient) Update(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.UpdateOption) error {
24+
return c.inner.Update(ctx, obj, append(opts, ctrlclient.DryRunAll)...)
25+
}
26+
27+
func (c *dryRunClient) Delete(ctx context.Context, obj ctrlclient.Object, opts ...ctrlclient.DeleteOption) error {
28+
return c.inner.Delete(ctx, obj, append(opts, ctrlclient.DryRunAll)...)
29+
}
30+
31+
func (c *dryRunClient) Get(ctx context.Context, key types.NamespacedName, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error {
32+
return c.inner.Get(ctx, key, obj, opts...)
33+
}
34+
35+
func (c *dryRunClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
36+
return c.inner.IsObjectNamespaced(obj)
37+
}
38+
39+
func (c *dryRunClient) List(ctx context.Context, list ctrlclient.ObjectList, opts ...ctrlclient.ListOption) error {
40+
return c.inner.List(ctx, list, opts...)
41+
}
42+
43+
func (c *dryRunClient) Patch(ctx context.Context, obj ctrlclient.Object, patch ctrlclient.Patch, opts ...ctrlclient.PatchOption) error {
44+
return c.inner.Patch(ctx, obj, patch, append(opts, ctrlclient.DryRunAll)...)
45+
}
46+
47+
func (c *dryRunClient) RESTMapper() meta.RESTMapper {
48+
return c.inner.RESTMapper()
49+
}
50+
51+
func New(inner Client) Client {
52+
return &dryRunClient{inner: inner}
53+
}

pkg/client/dry_run_test.go renamed to pkg/client/dryrun/dry_run_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package dryrun
22

33
import (
44
"context"
@@ -14,7 +14,7 @@ import (
1414
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1515
)
1616

17-
func TestDryRun(t *testing.T) {
17+
func TestNew(t *testing.T) {
1818
tests := []struct {
1919
name string
2020
inner Client
@@ -28,7 +28,7 @@ func TestDryRun(t *testing.T) {
2828
}}
2929
for _, tt := range tests {
3030
t.Run(tt.name, func(t *testing.T) {
31-
got := DryRun(tt.inner)
31+
got := New(tt.inner)
3232
assert.Equal(t, tt.want, got)
3333
})
3434
}

pkg/client/patch.go

-25
This file was deleted.

pkg/client/patch_test.go

-84
This file was deleted.

pkg/client/auth.go renamed to pkg/client/simple/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package simple
22

33
import (
44
_ "k8s.io/client-go/plugin/pkg/client/auth" // package needed for auth providers like GCP

pkg/client/simple/client.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package simple
2+
3+
import (
4+
"github.com/kyverno/chainsaw/pkg/client"
5+
"k8s.io/client-go/rest"
6+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
7+
)
8+
9+
func New(cfg *rest.Config) (client.Client, error) {
10+
var opts ctrlclient.Options
11+
return ctrlclient.New(cfg, opts)
12+
}

pkg/client/client_test.go renamed to pkg/client/simple/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package simple
22

33
import (
44
"testing"

pkg/client/key.go renamed to pkg/client/utils.go

+20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package client
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/kyverno/pkg/ext/output/color"
8+
"k8s.io/apimachinery/pkg/api/meta"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/runtime"
811
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
912
)
1013

@@ -34,3 +37,20 @@ func ColouredName(key ctrlclient.ObjectKey, color *color.Color) string {
3437
}
3538
return name
3639
}
40+
41+
func PatchObject(actual, expected runtime.Object) (runtime.Object, error) {
42+
if actual == nil || expected == nil {
43+
return nil, errors.New("actual and expected objects must not be nil")
44+
}
45+
actualMeta, err := meta.Accessor(actual)
46+
if err != nil {
47+
return nil, err
48+
}
49+
copy := expected.DeepCopyObject()
50+
expectedMeta, err := meta.Accessor(copy)
51+
if err != nil {
52+
return nil, err
53+
}
54+
expectedMeta.SetResourceVersion(actualMeta.GetResourceVersion())
55+
return copy, nil
56+
}

0 commit comments

Comments
 (0)