Skip to content

Commit e447ee6

Browse files
authored
refactor: Move meta handlers to provider packages (#193)
Also add tests for meta mutators to ensure decoding works properly.
1 parent d1cffda commit e447ee6

40 files changed

+979
-549
lines changed

cmd/main.go

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/manager"
2525
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2626

27-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis"
2827
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
29-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
3028
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/server"
3129
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
32-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/region"
30+
awsmutation "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation"
3331
dockerclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/clusterconfig"
34-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/mutation/customimage"
35-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/cni/calico"
36-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/nfd"
37-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle/servicelbgc"
38-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/auditpolicy"
39-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/etcd"
40-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/extraapiservercertsans"
41-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/httpproxy"
42-
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/kubernetesimagerepository"
32+
dockermutation "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/mutation"
33+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/lifecycle"
4334
)
4435

4536
// Flags.
@@ -87,16 +78,14 @@ func main() {
8778
pflag.CommandLine.StringVar(&mgrOptions.PprofBindAddress, "profiler-address", "",
8879
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
8980

90-
calicoCNIConfig := &calico.CalicoCNIConfig{}
91-
nfdConfig := &nfd.NFDConfig{}
92-
9381
runtimeWebhookServerOpts := server.NewServerOptions()
9482

83+
genericLifecycleHandlers := lifecycle.New()
84+
9585
// Initialize and parse command line flags.
9686
initFlags(pflag.CommandLine)
9787
runtimeWebhookServerOpts.AddFlags(pflag.CommandLine)
98-
nfdConfig.AddFlags("nfd", pflag.CommandLine)
99-
calicoCNIConfig.AddFlags("calicocni", pflag.CommandLine)
88+
genericLifecycleHandlers.AddFlags(pflag.CommandLine)
10089
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
10190
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
10291
pflag.Parse()
@@ -105,7 +94,7 @@ func main() {
10594

10695
// Validates logs flags using Kubernetes component-base machinery and applies them
10796
if err := logsv1.ValidateAndApply(logOptions, nil); err != nil {
108-
setupLog.Error(err, "unable to start extension")
97+
setupLog.Error(err, "unable to apply logging configuration")
10998
os.Exit(1)
11099
}
111100

@@ -120,60 +109,22 @@ func main() {
120109
os.Exit(1)
121110
}
122111

123-
// Handlers for lifecycle hooks.
124-
genericLifecycleHandlers := []handlers.Named{
125-
calico.New(mgr.GetClient(), calicoCNIConfig),
126-
nfd.New(mgr.GetClient(), nfdConfig),
127-
servicelbgc.New(mgr.GetClient()),
128-
}
129-
130-
// This genericMetaPatchHandlers combines all other patch and variable handlers under a single handler.
131-
// It allows to specify configuration under a single variable.
132-
genericMetaPatchHandlers := []mutation.MetaMutater{
133-
auditpolicy.NewPatch(),
134-
etcd.NewMetaPatch(),
135-
extraapiservercertsans.NewMetaPatch(),
136-
httpproxy.NewMetaPatch(mgr.GetClient()),
137-
kubernetesimagerepository.NewMetaPatch(),
138-
}
139-
140-
// awsMetaPatchHandlers combines all AWS patch and variable handlers under a single handler.
112+
// awsMetaHandlers combines all AWS patch and variable handlers under a single handler.
141113
// It allows to specify configuration under a single variable.
142-
awsMetaPatchHandlers := append(
143-
[]mutation.MetaMutater{
144-
region.NewMetaPatch(),
145-
},
146-
genericMetaPatchHandlers...,
147-
)
148-
149114
awsMetaHandlers := []handlers.Named{
150115
awsclusterconfig.NewVariable(),
151-
mutation.NewMetaGeneratePatchesHandler(
152-
"awsClusterConfigPatch",
153-
apis.CAPADecoder(),
154-
awsMetaPatchHandlers...),
116+
awsmutation.MetaPatchHandler(mgr),
155117
}
156118

157-
// dockerMetaPatchHandlers combines all Docker patch and variable handlers under a single handler.
119+
// dockerMetaHandlers combines all Docker patch and variable handlers under a single handler.
158120
// It allows to specify configuration under a single variable.
159-
dockerMetaPatchHandlers := append(
160-
[]mutation.MetaMutater{
161-
customimage.NewMetaPatch(),
162-
},
163-
genericMetaPatchHandlers...,
164-
)
165-
166121
dockerMetaHandlers := []handlers.Named{
167122
dockerclusterconfig.NewVariable(),
168-
mutation.NewMetaGeneratePatchesHandler(
169-
"dockerClusterConfigPatch",
170-
apis.CAPDDecoder(),
171-
dockerMetaPatchHandlers...,
172-
),
123+
dockermutation.MetaPatchHandler(mgr),
173124
}
174125

175126
var allHandlers []handlers.Named
176-
allHandlers = append(allHandlers, genericLifecycleHandlers...)
127+
allHandlers = append(allHandlers, genericLifecycleHandlers.AllHandlers(mgr)...)
177128
allHandlers = append(allHandlers, awsMetaHandlers...)
178129
allHandlers = append(allHandlers, dockerMetaHandlers...)
179130

common/pkg/testutils/capitest/patches.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
3939
) {
4040
t.Helper()
4141

42-
t.Parallel()
43-
4442
for testIdx := range testDefs {
4543
tt := testDefs[testIdx]
4644

@@ -91,10 +89,26 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
9189
}
9290
}
9391

94-
// v returns a runtimehooksv1.Variable with the passed name and value.
95-
func VariableWithValue(name string, value any) runtimehooksv1.Variable {
92+
// VariableWithValue returns a runtimehooksv1.Variable with the passed name and value.
93+
func VariableWithValue(
94+
variableName string,
95+
value any,
96+
variablePath ...string,
97+
) runtimehooksv1.Variable {
98+
if len(variablePath) > 0 {
99+
rootValue := make(map[string]any, 1)
100+
nestedValue := rootValue
101+
102+
for _, p := range variablePath[:len(variablePath)-1] {
103+
nestedValue[p] = make(map[string]any, 1)
104+
}
105+
106+
nestedValue[variablePath[len(variablePath)-1]] = value
107+
value = rootValue
108+
}
109+
96110
return runtimehooksv1.Variable{
97-
Name: name,
111+
Name: variableName,
98112
Value: apiextensionsv1.JSON{Raw: serializer.ToJSON(value)},
99113
}
100114
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package mutation
5+
6+
import (
7+
"sigs.k8s.io/controller-runtime/pkg/manager"
8+
9+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis"
10+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
11+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
12+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/region"
13+
genericmutation "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation"
14+
)
15+
16+
// MetaPatchHandler returns a meta patch handler for mutating CAPD clusters.
17+
func MetaPatchHandler(mgr manager.Manager) handlers.Named {
18+
patchHandlers := append(
19+
[]mutation.MetaMutater{
20+
region.NewMetaPatch(),
21+
},
22+
genericmutation.MetaMutaters(mgr)...,
23+
)
24+
25+
return mutation.NewMetaGeneratePatchesHandler(
26+
"awsClusterConfigPatch",
27+
apis.CAPADecoder(),
28+
patchHandlers...,
29+
)
30+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package mutation
5+
6+
import (
7+
"testing"
8+
9+
"k8s.io/client-go/rest"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
12+
"sigs.k8s.io/controller-runtime/pkg/manager"
13+
14+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
15+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/region"
16+
regiontests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/region/tests"
17+
auditpolicytests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/auditpolicy/tests"
18+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/etcd"
19+
etcdtests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/etcd/tests"
20+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/extraapiservercertsans"
21+
extraapiservercertsanstests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/extraapiservercertsans/tests"
22+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/httpproxy"
23+
httpproxytests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/httpproxy/tests"
24+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/kubernetesimagerepository"
25+
kubernetesimagerepositorytests "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/kubernetesimagerepository/tests"
26+
)
27+
28+
func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches {
29+
return func() mutation.GeneratePatches {
30+
return MetaPatchHandler(mgr).(mutation.GeneratePatches)
31+
}
32+
}
33+
34+
func TestGeneratePatches(t *testing.T) {
35+
t.Parallel()
36+
37+
mgr, _ := manager.New(
38+
&rest.Config{},
39+
manager.Options{
40+
NewClient: func(_ *rest.Config, _ client.Options) (client.Client, error) {
41+
return fake.NewClientBuilder().Build(), nil
42+
},
43+
},
44+
)
45+
46+
regiontests.TestGeneratePatches(
47+
t,
48+
metaPatchGeneratorFunc(mgr),
49+
"clusterConfig",
50+
region.VariableName,
51+
)
52+
53+
auditpolicytests.TestGeneratePatches(
54+
t,
55+
metaPatchGeneratorFunc(mgr),
56+
)
57+
58+
httpproxytests.TestGeneratePatches(
59+
t,
60+
metaPatchGeneratorFunc(mgr),
61+
"clusterConfig",
62+
httpproxy.VariableName,
63+
)
64+
65+
etcdtests.TestGeneratePatches(
66+
t,
67+
metaPatchGeneratorFunc(mgr),
68+
"clusterConfig",
69+
etcd.VariableName,
70+
)
71+
72+
extraapiservercertsanstests.TestGeneratePatches(
73+
t,
74+
metaPatchGeneratorFunc(mgr),
75+
"clusterConfig",
76+
extraapiservercertsans.VariableName,
77+
)
78+
79+
kubernetesimagerepositorytests.TestGeneratePatches(
80+
t,
81+
metaPatchGeneratorFunc(mgr),
82+
"clusterConfig",
83+
kubernetesimagerepository.VariableName,
84+
)
85+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ var (
4343
)
4444

4545
func NewPatch() *awsRegionPatchHandler {
46-
return newAWSRegionPatchHandler(variableName)
46+
return newAWSRegionPatchHandler(VariableName)
4747
}
4848

4949
func NewMetaPatch() *awsRegionPatchHandler {
50-
return newAWSRegionPatchHandler(clusterconfig.MetaVariableName, variableName)
50+
return newAWSRegionPatchHandler(clusterconfig.MetaVariableName, VariableName)
5151
}
5252

5353
func newAWSRegionPatchHandler(

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,16 @@ package region
66
import (
77
"testing"
88

9-
. "github.com/onsi/gomega"
10-
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
11-
129
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
13-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
14-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest/request"
10+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/mutation/region/tests"
1511
)
1612

1713
func TestGeneratePatches(t *testing.T) {
18-
capitest.ValidateGeneratePatches(
14+
t.Parallel()
15+
16+
tests.TestGeneratePatches(
1917
t,
2018
func() mutation.GeneratePatches { return NewPatch() },
21-
capitest.PatchTestDef{
22-
Name: "unset variable",
23-
},
24-
capitest.PatchTestDef{
25-
Name: "region set",
26-
Vars: []runtimehooksv1.Variable{
27-
capitest.VariableWithValue(
28-
variableName,
29-
"a-specific-region",
30-
),
31-
},
32-
RequestItem: request.NewAWSClusterTemplateRequestItem("1234"),
33-
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
34-
Operation: "add",
35-
Path: "/spec/template/spec/region",
36-
ValueMatcher: Equal("a-specific-region"),
37-
}},
38-
},
19+
VariableName,
3920
)
4021
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package tests
5+
6+
import (
7+
"testing"
8+
9+
"github.com/onsi/gomega"
10+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
11+
12+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
13+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
14+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest/request"
15+
)
16+
17+
func TestGeneratePatches(
18+
t *testing.T,
19+
generatorFunc func() mutation.GeneratePatches,
20+
variableName string,
21+
variablePath ...string,
22+
) {
23+
t.Helper()
24+
25+
capitest.ValidateGeneratePatches(
26+
t,
27+
generatorFunc,
28+
capitest.PatchTestDef{
29+
Name: "unset variable",
30+
},
31+
capitest.PatchTestDef{
32+
Name: "region set",
33+
Vars: []runtimehooksv1.Variable{
34+
capitest.VariableWithValue(
35+
variableName,
36+
"a-specific-region",
37+
variablePath...,
38+
),
39+
},
40+
RequestItem: request.NewAWSClusterTemplateRequestItem("1234"),
41+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
42+
Operation: "add",
43+
Path: "/spec/template/spec/region",
44+
ValueMatcher: gomega.Equal("a-specific-region"),
45+
}},
46+
},
47+
)
48+
}

0 commit comments

Comments
 (0)