Skip to content

Commit 5de242d

Browse files
committed
refactor: Use consistent decoder in mutators
Also fixes my typo in `mutater` to `mutator`.
1 parent 1ed01f4 commit 5de242d

File tree

13 files changed

+74
-69
lines changed

13 files changed

+74
-69
lines changed

common/pkg/capi/clustertopology/handlers/mutation/meta.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
1616
)
1717

18-
type MetaMutater interface {
18+
type MetaMutator interface {
1919
Mutate(
2020
ctx context.Context,
2121
obj runtime.Object,
22+
decoder runtime.Decoder,
2223
vars map[string]apiextensionsv1.JSON,
2324
holderRef runtimehooksv1.HolderReference,
2425
clusterKey client.ObjectKey,
@@ -28,18 +29,18 @@ type MetaMutater interface {
2829
type metaGeneratePatches struct {
2930
name string
3031
decoder runtime.Decoder
31-
mutaters []MetaMutater
32+
mutators []MetaMutator
3233
}
3334

3435
func NewMetaGeneratePatchesHandler(
3536
name string,
3637
decoder runtime.Decoder,
37-
mutators ...MetaMutater,
38+
mutators ...MetaMutator,
3839
) handlers.Named {
3940
return metaGeneratePatches{
4041
name: name,
4142
decoder: decoder,
42-
mutaters: mutators,
43+
mutators: mutators,
4344
}
4445
}
4546

@@ -65,8 +66,8 @@ func (mgp metaGeneratePatches) GeneratePatches(
6566
vars map[string]apiextensionsv1.JSON,
6667
holderRef runtimehooksv1.HolderReference,
6768
) error {
68-
for _, h := range mgp.mutaters {
69-
if err := h.Mutate(ctx, obj, vars, holderRef, clusterKey); err != nil {
69+
for _, h := range mgp.mutators {
70+
if err := h.Mutate(ctx, obj, mgp.decoder, vars, holderRef, clusterKey); err != nil {
7071
return err
7172
}
7273
}

common/pkg/capi/clustertopology/handlers/mutation/meta_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ type testHandler struct {
3030
mutateControlPlane bool
3131
}
3232

33-
var _ MetaMutater = &testHandler{}
33+
var _ MetaMutator = &testHandler{}
3434

3535
func (h *testHandler) Mutate(
3636
_ context.Context,
3737
obj runtime.Object,
38+
_ runtime.Decoder,
3839
_ map[string]apiextensionsv1.JSON,
3940
holderRef runtimehooksv1.HolderReference,
4041
_ client.ObjectKey,
@@ -89,7 +90,7 @@ func TestMetaGeneratePatches(t *testing.T) {
8990

9091
tests := []struct {
9192
name string
92-
mutaters []MetaMutater
93+
mutaters []MetaMutator
9394
expectedResponse *runtimehooksv1.GeneratePatchesResponse
9495
}{{
9596
name: "no handlers",
@@ -109,7 +110,7 @@ func TestMetaGeneratePatches(t *testing.T) {
109110
},
110111
}, {
111112
name: "single success handler",
112-
mutaters: []MetaMutater{
113+
mutaters: []MetaMutator{
113114
&testHandler{},
114115
},
115116
expectedResponse: &runtimehooksv1.GeneratePatchesResponse{
@@ -134,7 +135,7 @@ func TestMetaGeneratePatches(t *testing.T) {
134135
},
135136
}, {
136137
name: "single failure handler",
137-
mutaters: []MetaMutater{
138+
mutaters: []MetaMutator{
138139
&testHandler{
139140
returnErr: true,
140141
},
@@ -147,7 +148,7 @@ func TestMetaGeneratePatches(t *testing.T) {
147148
},
148149
}, {
149150
name: "multiple success handlers",
150-
mutaters: []MetaMutater{
151+
mutaters: []MetaMutator{
151152
&testHandler{},
152153
&testHandler{},
153154
&testHandler{mutateControlPlane: true},
@@ -184,7 +185,7 @@ func TestMetaGeneratePatches(t *testing.T) {
184185
},
185186
}, {
186187
name: "success handler followed by failure handler",
187-
mutaters: []MetaMutater{
188+
mutaters: []MetaMutator{
188189
&testHandler{},
189190
&testHandler{
190191
returnErr: true,
@@ -198,7 +199,7 @@ func TestMetaGeneratePatches(t *testing.T) {
198199
},
199200
}, {
200201
name: "failure handler followed by success handler",
201-
mutaters: []MetaMutater{
202+
mutaters: []MetaMutator{
202203
&testHandler{
203204
returnErr: true,
204205
},

pkg/handlers/aws/mutation/metapatch_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616
// MetaPatchHandler returns a meta patch handler for mutating CAPD clusters.
1717
func MetaPatchHandler(mgr manager.Manager) handlers.Named {
1818
patchHandlers := append(
19-
[]mutation.MetaMutater{
19+
[]mutation.MetaMutator{
2020
region.NewMetaPatch(),
2121
},
22-
genericmutation.MetaMutaters(mgr)...,
22+
genericmutation.MetaMutators(mgr)...,
2323
)
2424

2525
return mutation.NewMetaGeneratePatchesHandler(

pkg/handlers/aws/mutation/region/inject.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ const (
3131
)
3232

3333
type awsRegionPatchHandler struct {
34-
decoder runtime.Decoder
3534
variableName string
3635
variableFieldPath []string
3736
}
3837

3938
var (
4039
_ commonhandlers.Named = &awsRegionPatchHandler{}
4140
_ mutation.GeneratePatches = &awsRegionPatchHandler{}
42-
_ mutation.MetaMutater = &awsRegionPatchHandler{}
41+
_ mutation.MetaMutator = &awsRegionPatchHandler{}
4342
)
4443

4544
func NewPatch() *awsRegionPatchHandler {
@@ -55,7 +54,6 @@ func newAWSRegionPatchHandler(
5554
variableFieldPath ...string,
5655
) *awsRegionPatchHandler {
5756
return &awsRegionPatchHandler{
58-
decoder: apis.CAPADecoder(),
5957
variableName: variableName,
6058
variableFieldPath: variableFieldPath,
6159
}
@@ -68,6 +66,7 @@ func (h *awsRegionPatchHandler) Name() string {
6866
func (h *awsRegionPatchHandler) Mutate(
6967
ctx context.Context,
7068
obj runtime.Object,
69+
decoder runtime.Decoder,
7170
vars map[string]apiextensionsv1.JSON,
7271
holderRef runtimehooksv1.HolderReference,
7372
_ client.ObjectKey,
@@ -122,9 +121,11 @@ func (h *awsRegionPatchHandler) GeneratePatches(
122121
req *runtimehooksv1.GeneratePatchesRequest,
123122
resp *runtimehooksv1.GeneratePatchesResponse,
124123
) {
124+
decoder := apis.CAPADecoder()
125+
125126
topologymutation.WalkTemplates(
126127
ctx,
127-
h.decoder,
128+
decoder,
128129
req,
129130
resp,
130131
func(
@@ -133,7 +134,7 @@ func (h *awsRegionPatchHandler) GeneratePatches(
133134
vars map[string]apiextensionsv1.JSON,
134135
holderRef runtimehooksv1.HolderReference,
135136
) error {
136-
return h.Mutate(ctx, obj, vars, holderRef, client.ObjectKey{})
137+
return h.Mutate(ctx, obj, decoder, vars, holderRef, client.ObjectKey{})
137138
},
138139
)
139140
}

pkg/handlers/docker/mutation/customimage/inject.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ const (
3535
)
3636

3737
type customImagePatchHandler struct {
38-
decoder runtime.Decoder
3938
variableName string
4039
variableFieldPath []string
4140
}
4241

4342
var (
4443
_ commonhandlers.Named = &customImagePatchHandler{}
4544
_ mutation.GeneratePatches = &customImagePatchHandler{}
46-
_ mutation.MetaMutater = &customImagePatchHandler{}
45+
_ mutation.MetaMutator = &customImagePatchHandler{}
4746
)
4847

4948
func NewPatch() *customImagePatchHandler {
@@ -59,7 +58,6 @@ func newCustomImagePatchHandler(
5958
variableFieldPath ...string,
6059
) *customImagePatchHandler {
6160
return &customImagePatchHandler{
62-
decoder: apis.CAPDDecoder(),
6361
variableName: variableName,
6462
variableFieldPath: variableFieldPath,
6563
}
@@ -72,6 +70,7 @@ func (h *customImagePatchHandler) Name() string {
7270
func (h *customImagePatchHandler) Mutate(
7371
ctx context.Context,
7472
obj runtime.Object,
73+
decoder runtime.Decoder,
7574
vars map[string]apiextensionsv1.JSON,
7675
holderRef runtimehooksv1.HolderReference,
7776
_ client.ObjectKey,
@@ -193,9 +192,11 @@ func (h *customImagePatchHandler) GeneratePatches(
193192
req *runtimehooksv1.GeneratePatchesRequest,
194193
resp *runtimehooksv1.GeneratePatchesResponse,
195194
) {
195+
decoder := apis.CAPDDecoder()
196+
196197
topologymutation.WalkTemplates(
197198
ctx,
198-
h.decoder,
199+
decoder,
199200
req,
200201
resp,
201202
func(
@@ -204,7 +205,7 @@ func (h *customImagePatchHandler) GeneratePatches(
204205
vars map[string]apiextensionsv1.JSON,
205206
holderRef runtimehooksv1.HolderReference,
206207
) error {
207-
return h.Mutate(ctx, obj, vars, holderRef, client.ObjectKey{})
208+
return h.Mutate(ctx, obj, decoder, vars, holderRef, client.ObjectKey{})
208209
},
209210
)
210211
}

pkg/handlers/docker/mutation/metapatch_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616
// MetaPatchHandler returns a meta patch handler for mutating CAPD clusters.
1717
func MetaPatchHandler(mgr manager.Manager) handlers.Named {
1818
patchHandlers := append(
19-
[]mutation.MetaMutater{
19+
[]mutation.MetaMutator{
2020
customimage.NewMetaPatch(),
2121
},
22-
genericmutation.MetaMutaters(mgr)...,
22+
genericmutation.MetaMutators(mgr)...,
2323
)
2424

2525
return mutation.NewMetaGeneratePatchesHandler(

pkg/handlers/generic/mutation/auditpolicy/inject.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ const (
2828
HandlerNamePatch = "AuditPolicyPatch"
2929
)
3030

31-
type auditPolicyPatchHandler struct {
32-
decoder runtime.Decoder
33-
}
31+
type auditPolicyPatchHandler struct{}
3432

3533
var (
3634
_ handlers.Named = &auditPolicyPatchHandler{}
3735
_ mutation.GeneratePatches = &auditPolicyPatchHandler{}
38-
_ mutation.MetaMutater = &auditPolicyPatchHandler{}
36+
_ mutation.MetaMutator = &auditPolicyPatchHandler{}
3937

4038
//go:embed embedded/apiserver-audit-policy.yaml
4139
auditPolicy string
@@ -44,9 +42,7 @@ var (
4442
const auditPolicyPath = "/etc/kubernetes/audit-policy/apiserver-audit-policy.yaml"
4543

4644
func NewPatch() *auditPolicyPatchHandler {
47-
return &auditPolicyPatchHandler{
48-
decoder: apis.CAPIDecoder(),
49-
}
45+
return &auditPolicyPatchHandler{}
5046
}
5147

5248
func (h *auditPolicyPatchHandler) Name() string {
@@ -56,6 +52,7 @@ func (h *auditPolicyPatchHandler) Name() string {
5652
func (h *auditPolicyPatchHandler) Mutate(
5753
ctx context.Context,
5854
obj runtime.Object,
55+
decoder runtime.Decoder,
5956
vars map[string]apiextensionsv1.JSON,
6057
holderRef runtimehooksv1.HolderReference,
6158
_ client.ObjectKey,
@@ -124,9 +121,11 @@ func (h *auditPolicyPatchHandler) GeneratePatches(
124121
req *runtimehooksv1.GeneratePatchesRequest,
125122
resp *runtimehooksv1.GeneratePatchesResponse,
126123
) {
124+
decoder := apis.CAPIDecoder()
125+
127126
topologymutation.WalkTemplates(
128127
ctx,
129-
h.decoder,
128+
decoder,
130129
req,
131130
resp,
132131
func(
@@ -135,7 +134,7 @@ func (h *auditPolicyPatchHandler) GeneratePatches(
135134
vars map[string]apiextensionsv1.JSON,
136135
holderRef runtimehooksv1.HolderReference,
137136
) error {
138-
return h.Mutate(ctx, obj, vars, holderRef, client.ObjectKey{})
137+
return h.Mutate(ctx, obj, decoder, vars, holderRef, client.ObjectKey{})
139138
},
140139
)
141140
}

pkg/handlers/generic/mutation/etcd/inject.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ const (
3232
)
3333

3434
type etcdPatchHandler struct {
35-
decoder runtime.Decoder
3635
variableName string
3736
variableFieldPath []string
3837
}
3938

4039
var (
4140
_ commonhandlers.Named = &etcdPatchHandler{}
4241
_ mutation.GeneratePatches = &etcdPatchHandler{}
43-
_ mutation.MetaMutater = &etcdPatchHandler{}
42+
_ mutation.MetaMutator = &etcdPatchHandler{}
4443
)
4544

4645
func NewPatch() *etcdPatchHandler {
@@ -56,7 +55,6 @@ func newEtcdPatchHandler(
5655
variableFieldPath ...string,
5756
) *etcdPatchHandler {
5857
return &etcdPatchHandler{
59-
decoder: apis.CAPIDecoder(),
6058
variableName: variableName,
6159
variableFieldPath: variableFieldPath,
6260
}
@@ -69,6 +67,7 @@ func (h *etcdPatchHandler) Name() string {
6967
func (h *etcdPatchHandler) Mutate(
7068
ctx context.Context,
7169
obj runtime.Object,
70+
decoder runtime.Decoder,
7271
vars map[string]apiextensionsv1.JSON,
7372
holderRef runtimehooksv1.HolderReference,
7473
_ client.ObjectKey,
@@ -132,9 +131,11 @@ func (h *etcdPatchHandler) GeneratePatches(
132131
req *runtimehooksv1.GeneratePatchesRequest,
133132
resp *runtimehooksv1.GeneratePatchesResponse,
134133
) {
134+
decoder := apis.CAPIDecoder()
135+
135136
topologymutation.WalkTemplates(
136137
ctx,
137-
h.decoder,
138+
decoder,
138139
req,
139140
resp,
140141
func(
@@ -143,7 +144,7 @@ func (h *etcdPatchHandler) GeneratePatches(
143144
vars map[string]apiextensionsv1.JSON,
144145
holderRef runtimehooksv1.HolderReference,
145146
) error {
146-
return h.Mutate(ctx, obj, vars, holderRef, client.ObjectKey{})
147+
return h.Mutate(ctx, obj, decoder, vars, holderRef, client.ObjectKey{})
147148
},
148149
)
149150
}

0 commit comments

Comments
 (0)