Skip to content

Commit 967dcf8

Browse files
committed
feat: Introduce scheme and decoder helpers
1 parent 2a712d3 commit 967dcf8

File tree

14 files changed

+157
-137
lines changed

14 files changed

+157
-137
lines changed

cmd/main.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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"
2728
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
2829
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2930
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/server"
@@ -55,13 +56,13 @@ func main() {
5556
// Creates a logger to be used during the main func.
5657
setupLog := ctrl.Log.WithName("main")
5758

58-
scheme := runtime.NewScheme()
59-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
60-
utilruntime.Must(crsv1.AddToScheme(scheme))
61-
utilruntime.Must(capiv1.AddToScheme(scheme))
59+
clientScheme := runtime.NewScheme()
60+
utilruntime.Must(clientgoscheme.AddToScheme(clientScheme))
61+
utilruntime.Must(crsv1.AddToScheme(clientScheme))
62+
utilruntime.Must(capiv1.AddToScheme(clientScheme))
6263

6364
mgrOptions := &ctrl.Options{
64-
Scheme: scheme,
65+
Scheme: clientScheme,
6566
Metrics: metricsserver.Options{
6667
BindAddress: ":8080",
6768
},
@@ -144,9 +145,13 @@ func main() {
144145
},
145146
genericMetaPatchHandlers...,
146147
)
148+
147149
awsMetaHandlers := []handlers.Named{
148150
awsclusterconfig.NewVariable(),
149-
mutation.NewMetaGeneratePatchesHandler("awsClusterConfigPatch", awsMetaPatchHandlers...),
151+
mutation.NewMetaGeneratePatchesHandler(
152+
"awsClusterConfigPatch",
153+
apis.CAPADecoder(),
154+
awsMetaPatchHandlers...),
150155
}
151156

152157
// dockerMetaPatchHandlers combines all Docker patch and variable handlers under a single handler.
@@ -157,11 +162,14 @@ func main() {
157162
},
158163
genericMetaPatchHandlers...,
159164
)
165+
160166
dockerMetaHandlers := []handlers.Named{
161167
dockerclusterconfig.NewVariable(),
162168
mutation.NewMetaGeneratePatchesHandler(
163169
"dockerClusterConfigPatch",
164-
dockerMetaPatchHandlers...),
170+
apis.CAPDDecoder(),
171+
dockerMetaPatchHandlers...,
172+
),
165173
}
166174

167175
var allHandlers []handlers.Named

common/pkg/capi/apis/decoders.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package apis
5+
6+
import (
7+
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/apimachinery/pkg/runtime/schema"
9+
"k8s.io/apimachinery/pkg/runtime/serializer"
10+
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
11+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
12+
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
13+
14+
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
15+
capdv1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
16+
)
17+
18+
var capiCoreGroups = []schema.GroupVersion{
19+
controlplanev1.GroupVersion,
20+
bootstrapv1.GroupVersion,
21+
capiv1.GroupVersion,
22+
}
23+
24+
func CAPIDecoder() runtime.Decoder {
25+
return serializer.NewCodecFactory(CAPIScheme()).UniversalDecoder(capiCoreGroups...)
26+
}
27+
28+
func CAPDDecoder() runtime.Decoder {
29+
return serializer.NewCodecFactory(CAPDScheme()).UniversalDecoder(
30+
append(
31+
[]schema.GroupVersion{capdv1.GroupVersion},
32+
capiCoreGroups...,
33+
)...,
34+
)
35+
}
36+
37+
func CAPADecoder() runtime.Decoder {
38+
return serializer.NewCodecFactory(CAPAScheme()).UniversalDecoder(
39+
append(
40+
[]schema.GroupVersion{capav1.GroupVersion},
41+
capiCoreGroups...,
42+
)...,
43+
)
44+
}

common/pkg/capi/apis/schemes.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package apis
5+
6+
import (
7+
"k8s.io/apimachinery/pkg/runtime"
8+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
9+
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
10+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
11+
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
12+
13+
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
14+
capdv1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
15+
)
16+
17+
func CAPIScheme() *runtime.Scheme {
18+
scheme := runtime.NewScheme()
19+
20+
utilruntime.Must(bootstrapv1.AddToScheme(scheme))
21+
utilruntime.Must(controlplanev1.AddToScheme(scheme))
22+
utilruntime.Must(capiv1.AddToScheme(scheme))
23+
24+
return scheme
25+
}
26+
27+
func CAPAScheme() *runtime.Scheme {
28+
scheme := runtime.NewScheme()
29+
30+
utilruntime.Must(bootstrapv1.AddToScheme(scheme))
31+
utilruntime.Must(controlplanev1.AddToScheme(scheme))
32+
utilruntime.Must(capiv1.AddToScheme(scheme))
33+
utilruntime.Must(capav1.AddToScheme(scheme))
34+
35+
return scheme
36+
}
37+
38+
func CAPDScheme() *runtime.Scheme {
39+
scheme := runtime.NewScheme()
40+
41+
utilruntime.Must(bootstrapv1.AddToScheme(scheme))
42+
utilruntime.Must(controlplanev1.AddToScheme(scheme))
43+
utilruntime.Must(capiv1.AddToScheme(scheme))
44+
utilruntime.Must(capdv1.AddToScheme(scheme))
45+
46+
return scheme
47+
}

common/pkg/capi/decoder/unstructured_fallback.go renamed to common/pkg/capi/apis/unstructured_fallback.go

Lines changed: 1 addition & 1 deletion
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 decoder
4+
package apis
55

66
import (
77
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,11 @@ import (
88

99
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1010
"k8s.io/apimachinery/pkg/runtime"
11-
"k8s.io/apimachinery/pkg/runtime/serializer"
12-
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
13-
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
1411
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1512
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
1613
"sigs.k8s.io/controller-runtime/pkg/client"
1714

1815
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
19-
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/decoder"
20-
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
21-
capdv1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
2216
)
2317

2418
type MetaMutater interface {
@@ -37,24 +31,15 @@ type metaGeneratePatches struct {
3731
mutaters []MetaMutater
3832
}
3933

40-
func NewMetaGeneratePatchesHandler(name string, m ...MetaMutater) handlers.Named {
41-
scheme := runtime.NewScheme()
42-
_ = bootstrapv1.AddToScheme(scheme)
43-
_ = controlplanev1.AddToScheme(scheme)
44-
_ = capav1.AddToScheme(scheme)
45-
_ = capdv1.AddToScheme(scheme)
46-
47-
capiDecoder := serializer.NewCodecFactory(scheme).UniversalDecoder(
48-
controlplanev1.GroupVersion,
49-
bootstrapv1.GroupVersion,
50-
capav1.GroupVersion,
51-
capdv1.GroupVersion,
52-
)
53-
34+
func NewMetaGeneratePatchesHandler(
35+
name string,
36+
decoder runtime.Decoder,
37+
mutators ...MetaMutater,
38+
) handlers.Named {
5439
return metaGeneratePatches{
5540
name: name,
56-
decoder: decoder.NewDecoderWithUnstructuredFallback(capiDecoder),
57-
mutaters: m,
41+
decoder: decoder,
42+
mutaters: mutators,
5843
}
5944
}
6045

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
2121

22+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis"
2223
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
2324
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
2425
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest/request"
@@ -219,7 +220,7 @@ func TestMetaGeneratePatches(t *testing.T) {
219220

220221
g := gomega.NewWithT(t)
221222

222-
h := NewMetaGeneratePatchesHandler("", tt.mutaters...).(GeneratePatches)
223+
h := NewMetaGeneratePatchesHandler("", apis.CAPIDecoder(), tt.mutaters...).(GeneratePatches)
223224

224225
resp := &runtimehooksv1.GeneratePatchesResponse{}
225226
h.GeneratePatches(context.Background(), &runtimehooksv1.GeneratePatchesRequest{

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1111
"k8s.io/apimachinery/pkg/runtime"
12-
"k8s.io/apimachinery/pkg/runtime/serializer"
1312
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1413
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
1514
ctrl "sigs.k8s.io/controller-runtime"
1615
"sigs.k8s.io/controller-runtime/pkg/client"
1716

1817
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
18+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis"
1919
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
2020
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2121
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
@@ -54,12 +54,8 @@ func newAWSRegionPatchHandler(
5454
variableName string,
5555
variableFieldPath ...string,
5656
) *awsRegionPatchHandler {
57-
scheme := runtime.NewScheme()
58-
_ = capav1.AddToScheme(scheme)
5957
return &awsRegionPatchHandler{
60-
decoder: serializer.NewCodecFactory(scheme).UniversalDecoder(
61-
capav1.GroupVersion,
62-
),
58+
decoder: apis.CAPADecoder(),
6359
variableName: variableName,
6460
variableFieldPath: variableFieldPath,
6561
}

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ import (
1010
"strings"
1111

1212
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1413
"k8s.io/apimachinery/pkg/runtime"
1514
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1615
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
1716
ctrl "sigs.k8s.io/controller-runtime"
1817
"sigs.k8s.io/controller-runtime/pkg/client"
1918

2019
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
20+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/apis"
2121
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
2222
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2323
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
2424
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
2525
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
26+
capdv1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
2627
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
2728
)
2829

@@ -34,6 +35,7 @@ const (
3435
)
3536

3637
type customImagePatchHandler struct {
38+
decoder runtime.Decoder
3739
variableName string
3840
variableFieldPath []string
3941
}
@@ -57,6 +59,7 @@ func newCustomImagePatchHandler(
5759
variableFieldPath ...string,
5860
) *customImagePatchHandler {
5961
return &customImagePatchHandler{
62+
decoder: apis.CAPDDecoder(),
6063
variableName: variableName,
6164
variableFieldPath: variableFieldPath,
6265
}
@@ -104,7 +107,7 @@ func (h *customImagePatchHandler) Mutate(
104107
&holderRef,
105108
selectors.InfrastructureWorkerMachineTemplates("v1beta1", "DockerMachineTemplate"),
106109
log,
107-
func(obj *unstructured.Unstructured) error {
110+
func(obj *capdv1.DockerMachineTemplate) error {
108111
fieldPath := []string{"builtin", "machineDeployment", "version"}
109112

110113
if customImageVar == "" {
@@ -133,14 +136,9 @@ func (h *customImagePatchHandler) Mutate(
133136
"customImage", customImageVar,
134137
).Info("setting customImage in workers DockerMachineTemplate spec")
135138

136-
return unstructured.SetNestedField(
137-
obj.Object,
138-
string(customImageVar),
139-
"spec",
140-
"template",
141-
"spec",
142-
"customImage",
143-
)
139+
obj.Spec.Template.Spec.CustomImage = string(customImageVar)
140+
141+
return nil
144142
},
145143
)
146144

@@ -154,21 +152,21 @@ func (h *customImagePatchHandler) Mutate(
154152
&holderRef,
155153
selectors.InfrastructureControlPlaneMachines("v1beta1", "DockerMachineTemplate"),
156154
log,
157-
func(obj *unstructured.Unstructured) error {
158-
fieldPath := []string{"builtin", "controlPlane", "version"}
155+
func(obj *capdv1.DockerMachineTemplate) error {
156+
variablePath := []string{"builtin", "controlPlane", "version"}
159157

160158
if customImageVar == "" {
161159
kubernetesVersion, found, err := variables.Get[string](
162160
vars,
163-
fieldPath[0],
164-
fieldPath[1:]...)
161+
variablePath[0],
162+
variablePath[1:]...)
165163
if err != nil {
166164
return err
167165
}
168166
if !found {
169167
return fmt.Errorf(
170168
"missing required variable: %s",
171-
strings.Join(fieldPath, "."),
169+
strings.Join(variablePath, "."),
172170
)
173171
}
174172

@@ -183,14 +181,9 @@ func (h *customImagePatchHandler) Mutate(
183181
"customImage", customImageVar,
184182
).Info("setting customImage in control plane DockerMachineTemplate spec")
185183

186-
return unstructured.SetNestedField(
187-
obj.Object,
188-
string(customImageVar),
189-
"spec",
190-
"template",
191-
"spec",
192-
"customImage",
193-
)
184+
obj.Spec.Template.Spec.CustomImage = string(customImageVar)
185+
186+
return nil
194187
},
195188
)
196189
}
@@ -202,7 +195,7 @@ func (h *customImagePatchHandler) GeneratePatches(
202195
) {
203196
topologymutation.WalkTemplates(
204197
ctx,
205-
unstructured.UnstructuredJSONScheme,
198+
h.decoder,
206199
req,
207200
resp,
208201
func(

0 commit comments

Comments
 (0)