@@ -12,7 +12,6 @@ import (
12
12
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13
13
"k8s.io/apimachinery/pkg/runtime"
14
14
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
15
- "sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
16
15
ctrl "sigs.k8s.io/controller-runtime"
17
16
"sigs.k8s.io/controller-runtime/pkg/client"
18
17
@@ -24,65 +23,60 @@ import (
24
23
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
25
24
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
26
25
capdv1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
26
+ "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers"
27
27
dockerclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/clusterconfig"
28
28
dockerworkerconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/docker/workerconfig"
29
29
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
30
30
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/workerconfig"
31
31
)
32
32
33
- const (
34
- // HandlerNamePatch is the name of the inject handler.
35
- HandlerNamePatch = "DockerCustomImagePatch"
36
-
37
- defaultKinDImageRepository = "ghcr.io/mesosphere/kind-node"
38
- )
39
-
40
- type customImagePatchHandler struct {
33
+ type customImageWorkerPatchHandler struct {
41
34
variableName string
42
35
variableFieldPath []string
43
36
}
44
37
45
38
var (
46
- _ commonhandlers.Named = & customImagePatchHandler {}
47
- _ mutation.GeneratePatches = & customImagePatchHandler {}
48
- _ mutation.MetaMutator = & customImagePatchHandler {}
39
+ _ commonhandlers.Named = & customImageWorkerPatchHandler {}
40
+ _ mutation.GeneratePatches = & customImageWorkerPatchHandler {}
41
+ _ mutation.MetaMutator = & customImageWorkerPatchHandler {}
49
42
)
50
43
51
- func NewPatch () * customImagePatchHandler {
52
- return newCustomImagePatchHandler (VariableName )
44
+ func NewWorkerPatch () * customImageWorkerPatchHandler {
45
+ return newcustomImageWorkerPatchHandler (VariableName )
53
46
}
54
47
55
- func NewMetaPatch () * customImagePatchHandler {
56
- return newCustomImagePatchHandler (
48
+ func NewWorkerInClusterConfigMetaPatch () * customImageControlPlanePatchHandler {
49
+ return newCustomImageControlPlanePatchHandler (
57
50
clusterconfig .MetaVariableName ,
51
+ clusterconfig .MetaWorkerConfigName ,
58
52
dockerclusterconfig .DockerVariableName ,
59
53
VariableName ,
60
54
)
61
55
}
62
56
63
- func NewMetaWorkerPatch () * customImagePatchHandler {
64
- return newCustomImagePatchHandler (
57
+ func NewMetaWorkerPatch () * customImageWorkerPatchHandler {
58
+ return newcustomImageWorkerPatchHandler (
65
59
workerconfig .MetaVariableName ,
66
60
dockerworkerconfig .DockerVariableName ,
67
61
VariableName ,
68
62
)
69
63
}
70
64
71
- func newCustomImagePatchHandler (
65
+ func newcustomImageWorkerPatchHandler (
72
66
variableName string ,
73
67
variableFieldPath ... string ,
74
- ) * customImagePatchHandler {
75
- return & customImagePatchHandler {
68
+ ) * customImageWorkerPatchHandler {
69
+ return & customImageWorkerPatchHandler {
76
70
variableName : variableName ,
77
71
variableFieldPath : variableFieldPath ,
78
72
}
79
73
}
80
74
81
- func (h * customImagePatchHandler ) Name () string {
75
+ func (h * customImageWorkerPatchHandler ) Name () string {
82
76
return HandlerNamePatch
83
77
}
84
78
85
- func (h * customImagePatchHandler ) Mutate (
79
+ func (h * customImageWorkerPatchHandler ) Mutate (
86
80
ctx context.Context ,
87
81
obj runtime.Object ,
88
82
vars map [string ]apiextensionsv1.JSON ,
@@ -102,7 +96,8 @@ func (h *customImagePatchHandler) Mutate(
102
96
return err
103
97
}
104
98
if ! found {
105
- log .V (5 ).Info ("Docker customImage variable not defined, using default KinD node image" )
99
+ log .V (5 ).
100
+ Info ("Docker customImage variable not defined for workers, using default KinD node image" )
106
101
}
107
102
108
103
log = log .WithValues (
@@ -114,7 +109,7 @@ func (h *customImagePatchHandler) Mutate(
114
109
customImageVar ,
115
110
)
116
111
117
- err = patches .Generate (
112
+ return patches .Generate (
118
113
obj ,
119
114
vars ,
120
115
& holderRef ,
@@ -154,74 +149,12 @@ func (h *customImagePatchHandler) Mutate(
154
149
return nil
155
150
},
156
151
)
157
-
158
- if err != nil {
159
- return err
160
- }
161
-
162
- if h .variableName == workerconfig .MetaVariableName {
163
- return nil
164
- }
165
-
166
- return patches .Generate (
167
- obj ,
168
- vars ,
169
- & holderRef ,
170
- selectors .InfrastructureControlPlaneMachines ("v1beta1" , "DockerMachineTemplate" ),
171
- log ,
172
- func (obj * capdv1.DockerMachineTemplate ) error {
173
- variablePath := []string {"builtin" , "controlPlane" , "version" }
174
-
175
- if customImageVar == "" {
176
- kubernetesVersion , found , err := variables .Get [string ](
177
- vars ,
178
- variablePath [0 ],
179
- variablePath [1 :]... )
180
- if err != nil {
181
- return err
182
- }
183
- if ! found {
184
- return fmt .Errorf (
185
- "missing required variable: %s" ,
186
- strings .Join (variablePath , "." ),
187
- )
188
- }
189
-
190
- customImageVar = v1alpha1 .OCIImage (
191
- defaultKinDImageRepository + ":" + kubernetesVersion ,
192
- )
193
- }
194
-
195
- log .WithValues (
196
- "patchedObjectKind" , obj .GetObjectKind ().GroupVersionKind ().String (),
197
- "patchedObjectName" , client .ObjectKeyFromObject (obj ),
198
- "customImage" , customImageVar ,
199
- ).Info ("setting customImage in control plane DockerMachineTemplate spec" )
200
-
201
- obj .Spec .Template .Spec .CustomImage = string (customImageVar )
202
-
203
- return nil
204
- },
205
- )
206
152
}
207
153
208
- func (h * customImagePatchHandler ) GeneratePatches (
154
+ func (h * customImageWorkerPatchHandler ) GeneratePatches (
209
155
ctx context.Context ,
210
156
req * runtimehooksv1.GeneratePatchesRequest ,
211
157
resp * runtimehooksv1.GeneratePatchesResponse ,
212
158
) {
213
- topologymutation .WalkTemplates (
214
- ctx ,
215
- apis .CAPDDecoder (),
216
- req ,
217
- resp ,
218
- func (
219
- ctx context.Context ,
220
- obj runtime.Object ,
221
- vars map [string ]apiextensionsv1.JSON ,
222
- holderRef runtimehooksv1.HolderReference ,
223
- ) error {
224
- return h .Mutate (ctx , obj , vars , holderRef , client.ObjectKey {})
225
- },
226
- )
159
+ handlers .GeneratePatches (ctx , req , resp , apis .CAPDDecoder (), h .Mutate )
227
160
}
0 commit comments