Skip to content

Commit f1ba6bb

Browse files
authored
refactor: Remove usage of non-meta handlers (#226)
Remove code for mutation handlers that was used to support individual handlers. As we are only registering the meta handlers, this makes it simpler to add new handlers and only a single place to add variable unit tests. I also removed this assertion since the meta registration code will already fail if `Mutate` is not implemented and `mutation.GeneratePatches` and `commonhandlers.Named` interfaces are no needed if registering in the meta handler. ``` _ commonhandlers.Named = &...PatchHandler{} _ mutation.GeneratePatches = &...PatchHandler{} _ mutation.MetaMutator = &...PatchHandler{} ```
1 parent 0f4b92e commit f1ba6bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+280
-1463
lines changed

api/v1alpha1/constants.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package v1alpha1
5+
6+
const (
7+
// CNIVariableName is the external patch variable name.
8+
CNIVariableName = "cni"
9+
)

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

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,20 @@ import (
88

99
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1010
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11-
"k8s.io/apimachinery/pkg/runtime"
1211
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1312
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
14-
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
1513
ctrl "sigs.k8s.io/controller-runtime"
1614
"sigs.k8s.io/controller-runtime/pkg/client"
1715

1816
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
19-
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
20-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2117
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
2218
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
2319
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2420
)
2521

2622
const (
27-
// HandlerNamePatch is the name of the inject handler.
28-
ControlPlaneHandlerNamePatch = "AWSAMISpecControlPlanePatch"
29-
VariableName = "ami"
30-
)
31-
32-
var (
33-
_ commonhandlers.Named = &awsAMISpecPatchHandler{}
34-
_ mutation.MetaMutator = &awsAMISpecPatchHandler{}
23+
// VariableName is the external patch variable name.
24+
VariableName = "ami"
3525
)
3626

3727
type awsAMISpecPatchHandler struct {
@@ -52,10 +42,6 @@ func newAWSAMISpecPatchHandler(
5242
}
5343
}
5444

55-
func (h *awsAMISpecPatchHandler) Name() string {
56-
return ControlPlaneHandlerNamePatch
57-
}
58-
5945
func (h *awsAMISpecPatchHandler) Mutate(
6046
ctx context.Context,
6147
obj *unstructured.Unstructured,
@@ -113,30 +99,3 @@ func (h *awsAMISpecPatchHandler) Mutate(
11399
},
114100
)
115101
}
116-
117-
func (h *awsAMISpecPatchHandler) GeneratePatches(
118-
ctx context.Context,
119-
req *runtimehooksv1.GeneratePatchesRequest,
120-
resp *runtimehooksv1.GeneratePatchesResponse,
121-
) {
122-
topologymutation.WalkTemplates(
123-
ctx,
124-
unstructured.UnstructuredJSONScheme,
125-
req,
126-
resp,
127-
func(
128-
ctx context.Context,
129-
obj runtime.Object,
130-
vars map[string]apiextensionsv1.JSON,
131-
holderRef runtimehooksv1.HolderReference,
132-
) error {
133-
return h.Mutate(
134-
ctx,
135-
obj.(*unstructured.Unstructured),
136-
vars,
137-
holderRef,
138-
client.ObjectKey{},
139-
)
140-
},
141-
)
142-
}

pkg/handlers/aws/mutation/ami/inject_control_plane_test.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

pkg/handlers/aws/mutation/ami/inject_worker_test.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

pkg/handlers/aws/clusterconfig/variables_test.go renamed to pkg/handlers/aws/mutation/ami/variables_test.go

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2023 D2iQ, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package clusterconfig
4+
package ami
55

66
import (
77
"testing"
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
1212
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
13+
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
1314
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
1415
)
1516

@@ -19,39 +20,7 @@ func TestVariableValidation(t *testing.T) {
1920
clusterconfig.MetaVariableName,
2021
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
2122
true,
22-
NewVariable,
23-
capitest.VariableTestDef{
24-
Name: "specified region",
25-
Vals: v1alpha1.ClusterConfigSpec{
26-
AWS: &v1alpha1.AWSSpec{
27-
Region: ptr.To(v1alpha1.Region("a-specified-region")),
28-
},
29-
},
30-
},
31-
capitest.VariableTestDef{
32-
Name: "specified IAM instance profile",
33-
Vals: v1alpha1.ClusterConfigSpec{
34-
ControlPlane: &v1alpha1.NodeConfigSpec{
35-
AWS: &v1alpha1.AWSNodeSpec{
36-
IAMInstanceProfile: ptr.To(
37-
v1alpha1.IAMInstanceProfile(
38-
"control-plane.cluster-api-provider-aws.sigs.k8s.io",
39-
),
40-
),
41-
},
42-
},
43-
},
44-
},
45-
capitest.VariableTestDef{
46-
Name: "specified instance type",
47-
Vals: v1alpha1.ClusterConfigSpec{
48-
ControlPlane: &v1alpha1.NodeConfigSpec{
49-
AWS: &v1alpha1.AWSNodeSpec{
50-
InstanceType: ptr.To(v1alpha1.InstanceType("m5.small")),
51-
},
52-
},
53-
},
54-
},
23+
awsclusterconfig.NewVariable,
5524
capitest.VariableTestDef{
5625
Name: "AMI specification",
5726
Vals: v1alpha1.ClusterConfigSpec{

pkg/handlers/aws/mutation/cni/calico/inject.go

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ import (
1111
"github.com/go-logr/logr"
1212
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1313
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14-
"k8s.io/apimachinery/pkg/runtime"
1514
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
16-
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
1715
ctrl "sigs.k8s.io/controller-runtime"
1816
"sigs.k8s.io/controller-runtime/pkg/client"
1917

2018
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
21-
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
22-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2319
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
2420
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
2521
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
2622
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2723
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
28-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/cni"
2924
)
3025

3126
const (
@@ -38,21 +33,11 @@ type calicoPatchHandler struct {
3833
variableFieldPath []string
3934
}
4035

41-
var (
42-
_ commonhandlers.Named = &calicoPatchHandler{}
43-
_ mutation.GeneratePatches = &calicoPatchHandler{}
44-
_ mutation.MetaMutator = &calicoPatchHandler{}
45-
)
46-
4736
func NewPatch() *calicoPatchHandler {
48-
return newCalicoPatchHandler(cni.VariableName)
49-
}
50-
51-
func NewMetaPatch() *calicoPatchHandler {
5237
return newCalicoPatchHandler(
5338
clusterconfig.MetaVariableName,
5439
"addons",
55-
cni.VariableName,
40+
v1alpha1.CNIVariableName,
5641
)
5742
}
5843

@@ -66,10 +51,6 @@ func newCalicoPatchHandler(
6651
}
6752
}
6853

69-
func (h *calicoPatchHandler) Name() string {
70-
return HandlerNamePatch
71-
}
72-
7354
func (h *calicoPatchHandler) Mutate(
7455
ctx context.Context,
7556
obj *unstructured.Unstructured,
@@ -119,33 +100,6 @@ func (h *calicoPatchHandler) Mutate(
119100
)
120101
}
121102

122-
func (h *calicoPatchHandler) GeneratePatches(
123-
ctx context.Context,
124-
req *runtimehooksv1.GeneratePatchesRequest,
125-
resp *runtimehooksv1.GeneratePatchesResponse,
126-
) {
127-
topologymutation.WalkTemplates(
128-
ctx,
129-
unstructured.UnstructuredJSONScheme,
130-
req,
131-
resp,
132-
func(
133-
ctx context.Context,
134-
obj runtime.Object,
135-
vars map[string]apiextensionsv1.JSON,
136-
holderRef runtimehooksv1.HolderReference,
137-
) error {
138-
return h.Mutate(
139-
ctx,
140-
obj.(*unstructured.Unstructured),
141-
vars,
142-
holderRef,
143-
client.ObjectKey{},
144-
)
145-
},
146-
)
147-
}
148-
149103
func mutateAWSClusterTemplateFunc(log logr.Logger) func(obj *capav1.AWSClusterTemplate) error {
150104
return func(obj *capav1.AWSClusterTemplate) error {
151105
log.WithValues(

pkg/handlers/aws/mutation/cni/calico/inject_test.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

pkg/handlers/aws/mutation/iaminstanceprofile/inject_control_plane.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,25 @@ import (
1414
"sigs.k8s.io/controller-runtime/pkg/client"
1515

1616
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
17-
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
18-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
1917
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
2018
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
2119
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
2220
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
23-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers"
2421
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
2522
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
2623
)
2724

2825
const (
2926
// VariableName is the external patch variable name.
3027
VariableName = "iamInstanceProfile"
31-
32-
// ControlPlaneHandlerNamePatch is the name of the inject handler.
33-
ControlPlaneHandlerNamePatch = "AWSIAMInstanceProfileControlPlanePatch"
3428
)
3529

3630
type awsIAMInstanceProfileControlPlanePatchHandler struct {
3731
variableName string
3832
variableFieldPath []string
3933
}
4034

41-
var (
42-
_ commonhandlers.Named = &awsIAMInstanceProfileControlPlanePatchHandler{}
43-
_ mutation.GeneratePatches = &awsIAMInstanceProfileControlPlanePatchHandler{}
44-
_ mutation.MetaMutator = &awsIAMInstanceProfileControlPlanePatchHandler{}
45-
)
46-
47-
func NewControlPlaneMetaPatch() *awsIAMInstanceProfileControlPlanePatchHandler {
35+
func NewControlPlanePatch() *awsIAMInstanceProfileControlPlanePatchHandler {
4836
return newAWSIAMInstanceProfileControlPlanePatchHandler(
4937
clusterconfig.MetaVariableName,
5038
clusterconfig.MetaControlPlaneConfigName,
@@ -63,10 +51,6 @@ func newAWSIAMInstanceProfileControlPlanePatchHandler(
6351
}
6452
}
6553

66-
func (h *awsIAMInstanceProfileControlPlanePatchHandler) Name() string {
67-
return ControlPlaneHandlerNamePatch
68-
}
69-
7054
func (h *awsIAMInstanceProfileControlPlanePatchHandler) Mutate(
7155
ctx context.Context,
7256
obj *unstructured.Unstructured,
@@ -121,11 +105,3 @@ func (h *awsIAMInstanceProfileControlPlanePatchHandler) Mutate(
121105
},
122106
)
123107
}
124-
125-
func (h *awsIAMInstanceProfileControlPlanePatchHandler) GeneratePatches(
126-
ctx context.Context,
127-
req *runtimehooksv1.GeneratePatchesRequest,
128-
resp *runtimehooksv1.GeneratePatchesResponse,
129-
) {
130-
handlers.GeneratePatches(ctx, req, resp, h.Mutate)
131-
}

0 commit comments

Comments
 (0)