@@ -11,11 +11,11 @@ import (
11
11
"io"
12
12
"io/fs"
13
13
14
- "github.com/go-logr/logr"
15
14
corev1 "k8s.io/api/core/v1"
16
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
16
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
18
17
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
18
+ "k8s.io/apimachinery/pkg/runtime"
19
19
"k8s.io/apimachinery/pkg/runtime/schema"
20
20
"k8s.io/apimachinery/pkg/runtime/serializer/json"
21
21
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -33,8 +33,6 @@ import (
33
33
34
34
const (
35
35
CNILabelValue = "calico"
36
-
37
- DefaultPodSubnet = "192.168.0.0/16"
38
36
)
39
37
40
38
type CalicoCNI struct {
@@ -88,38 +86,54 @@ func (s *CalicoCNI) AfterControlPlaneInitialized(
88
86
// will update for failure response properly.
89
87
resp .SetStatus (runtimehooksv1 .ResponseStatusSuccess )
90
88
91
- if v , ok := req .Cluster .GetLabels ()[cni .CNILabelKey ]; ! ok || v != CNILabelValue {
92
- log .V (4 ).Info ("Skipping Calico CNI handler, CNI provider is not calico" )
89
+ if v , ok := req .Cluster .GetLabels ()[cni .CNIProviderLabelKey ]; ! ok || v != CNILabelValue {
90
+ log .V (4 ).Info (
91
+ fmt .Sprintf (
92
+ "Skipping Calico CNI handler, cluster does not specify %q as value of CNI provider label %q" ,
93
+ CNILabelValue ,
94
+ cni .CNIProviderLabelKey ,
95
+ ),
96
+ )
93
97
return
94
98
}
95
99
96
100
manifestsFS , ok := providerManifestsFS [req .Cluster .Spec .InfrastructureRef .Kind ]
97
101
if ! ok {
98
- log .V (4 ).Info ("Skipping Calico CNI handler, unknown CNI provider" )
102
+ log .V (4 ).Info (
103
+ fmt .Sprintf (
104
+ "Skipping Calico CNI handler, no default configured for infrastructure provider %q" ,
105
+ req .Cluster .Spec .InfrastructureRef .Kind ,
106
+ ),
107
+ )
99
108
return
100
109
}
101
110
102
- if err := applyTigeraOperatorCRS (ctx , s .client , & req .Cluster , log ); err != nil {
111
+ log .Info ("Ensuring Tigera CRS and manifests ConfigMap exist in cluster namespace" )
112
+ tigeraObjs := generateTigeraOperatorCRS (& req .Cluster )
113
+ if err := client .ServerSideApply (ctx , s .client , tigeraObjs ... ); err != nil {
103
114
log .Error (err , "failed to apply Tigera ClusterResourceSet" )
104
115
resp .SetStatus (runtimehooksv1 .ResponseStatusFailure )
105
116
resp .SetMessage (fmt .Sprintf ("failed to apply Tigera ClusterResourceSet: %v" , err ))
117
+ return
118
+ }
119
+
120
+ log .Info ("Ensuring Calico installation CRS and ConfigMap exist in cluster namespace" )
121
+ calicoCNIObjs , err := generateProviderCNICRS (manifestsFS , & req .Cluster , s .client .Scheme ())
122
+ if err != nil {
123
+ log .Error (err , "failed to generate provider CNI CRS" )
124
+ resp .SetStatus (runtimehooksv1 .ResponseStatusFailure )
125
+ resp .SetMessage (fmt .Sprintf ("failed to generate provider CNI CRS: %v" , err ))
126
+ return
106
127
}
107
128
108
- if err := applyProviderCNICRS (ctx , manifestsFS , s .client , & req . Cluster , log ); err != nil {
129
+ if err := client . ServerSideApply (ctx , s .client , calicoCNIObjs ... ); err != nil {
109
130
log .Error (err , "failed to apply CNI installation ClusterResourceSet" )
110
131
resp .SetStatus (runtimehooksv1 .ResponseStatusFailure )
111
132
resp .SetMessage (fmt .Sprintf ("failed to apply CNI installation ClusterResourceSet: %v" , err ))
112
133
}
113
134
}
114
135
115
- func applyTigeraOperatorCRS (
116
- ctx context.Context ,
117
- c ctrlclient.Client ,
118
- cluster * capiv1.Cluster ,
119
- log logr.Logger ,
120
- ) error {
121
- log .Info ("Ensuring Tigera CRS and manifests ConfigMap exist in cluster namespace" )
122
-
136
+ func generateTigeraOperatorCRS (cluster * capiv1.Cluster ) []ctrlclient.Object {
123
137
// Set the namespace on the tigera configmap to apply by deep copying and then mutating.
124
138
namespacedTigeraConfigMap := & corev1.ConfigMap {}
125
139
tigeraConfigMap .DeepCopyInto (namespacedTigeraConfigMap )
@@ -141,32 +155,24 @@ func applyTigeraOperatorCRS(
141
155
}},
142
156
Strategy : string (crsv1 .ClusterResourceSetStrategyReconcile ),
143
157
ClusterSelector : metav1.LabelSelector {
144
- MatchLabels : map [string ]string {cni .CNILabelKey : CNILabelValue },
158
+ MatchLabels : map [string ]string {cni .CNIProviderLabelKey : CNILabelValue },
145
159
},
146
160
},
147
161
}
148
162
149
- return client . ServerSideApply ( ctx , c , namespacedTigeraConfigMap , tigeraCRS )
163
+ return []ctrlclient. Object { namespacedTigeraConfigMap , tigeraCRS }
150
164
}
151
165
152
- func applyProviderCNICRS (
153
- ctx context.Context ,
154
- manifestsFS fs.FS ,
155
- c ctrlclient.Client ,
156
- cluster * capiv1.Cluster ,
157
- log logr.Logger ,
158
- ) error {
159
- log .Info ("Ensuring Calico installation CRS and ConfigMap exist in cluster namespace" )
160
-
166
+ func generateProviderCNICRS (manifestsFS fs.FS , cluster * capiv1.Cluster , scheme * runtime.Scheme ) ([]ctrlclient.Object , error ) {
161
167
readers , cleanup , err := readersForManifestsInFS (manifestsFS )
162
168
if err != nil {
163
- return fmt .Errorf ("failed to read embedded manifests: %w" , err )
169
+ return nil , fmt .Errorf ("failed to read embedded manifests: %w" , err )
164
170
}
165
171
defer func () { _ = cleanup () }()
166
172
167
173
parsed , err := parser .ReadersToUnstructured (readers ... )
168
174
if err != nil {
169
- return fmt .Errorf ("failed to parse embedded manifests: %w" , err )
175
+ return nil , fmt .Errorf ("failed to parse embedded manifests: %w" , err )
170
176
}
171
177
172
178
cm := & corev1.ConfigMap {
@@ -191,51 +197,55 @@ func applyProviderCNICRS(
191
197
},
192
198
)
193
199
200
+ podSubnet , podSubnetSpecified := cluster .GetAnnotations ()[cni .PodSubnetAnnotationKey ]
201
+
194
202
var b bytes.Buffer
195
203
196
204
for _ , o := range parsed {
197
- if o .GetObjectKind ().GroupVersionKind ().GroupKind () == calicoInstallationGK {
205
+ if podSubnetSpecified &&
206
+ podSubnet != "" &&
207
+ o .GetObjectKind ().GroupVersionKind ().GroupKind () == calicoInstallationGK {
198
208
obj := o .(* unstructured.Unstructured ).Object
199
209
200
210
ipPoolsRef , exists , err := unstructured .NestedFieldNoCopy (
201
211
obj ,
202
212
"spec" , "calicoNetwork" , "ipPools" ,
203
213
)
204
214
if err != nil {
205
- return fmt .Errorf ("failed to get ipPools from unstructured object: %w" , err )
215
+ return nil , fmt .Errorf ("failed to get ipPools from unstructured object: %w" , err )
206
216
}
207
217
if ! exists {
208
- return fmt .Errorf ("missing ipPools in unstructured object" )
218
+ return nil , fmt .Errorf ("missing ipPools in unstructured object" )
209
219
}
210
220
211
221
ipPools := ipPoolsRef .([]interface {})
212
222
213
223
err = unstructured .SetNestedField (
214
224
ipPools [0 ].(map [string ]interface {}),
215
- DefaultPodSubnet ,
225
+ podSubnet ,
216
226
"cidr" ,
217
227
)
218
228
if err != nil {
219
- return fmt .Errorf ("failed to set default pod subnet: %w" , err )
229
+ return nil , fmt .Errorf ("failed to set default pod subnet: %w" , err )
220
230
}
221
231
222
232
err = unstructured .SetNestedSlice (obj , ipPools , "spec" , "calicoNetwork" , "ipPools" )
223
233
if err != nil {
224
- return fmt .Errorf ("failed to update ipPools: %w" , err )
234
+ return nil , fmt .Errorf ("failed to update ipPools: %w" , err )
225
235
}
226
236
}
227
237
228
238
if err := yamlSerializer .Encode (o , & b ); err != nil {
229
- return fmt .Errorf ("failed to serialize manifests: %w" , err )
239
+ return nil , fmt .Errorf ("failed to serialize manifests: %w" , err )
230
240
}
231
241
232
242
_ , _ = b .WriteString ("\n ---\n " )
233
243
}
234
244
235
245
cm .Data ["manifests" ] = b .String ()
236
246
237
- if err := controllerutil .SetOwnerReference (cluster , cm , c . Scheme () ); err != nil {
238
- return fmt .Errorf ("failed to set owner reference: %w" , err )
247
+ if err := controllerutil .SetOwnerReference (cluster , cm , scheme ); err != nil {
248
+ return nil , fmt .Errorf ("failed to set owner reference: %w" , err )
239
249
}
240
250
241
251
crs := & crsv1.ClusterResourceSet {
@@ -259,11 +269,11 @@ func applyProviderCNICRS(
259
269
},
260
270
}
261
271
262
- if err := controllerutil .SetOwnerReference (cluster , crs , c . Scheme () ); err != nil {
263
- return fmt .Errorf ("failed to set owner reference: %w" , err )
272
+ if err := controllerutil .SetOwnerReference (cluster , crs , scheme ); err != nil {
273
+ return nil , fmt .Errorf ("failed to set owner reference: %w" , err )
264
274
}
265
275
266
- return client . ServerSideApply ( ctx , c , cm , crs )
276
+ return []ctrlclient. Object { cm , crs }, nil
267
277
}
268
278
269
279
func readersForManifestsInFS (
0 commit comments