@@ -6,10 +6,11 @@ package controlplanevirtualip
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "slices"
9
10
10
- "github.com/spf13/pflag"
11
11
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
12
12
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13
+ bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
13
14
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
14
15
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
15
16
ctrl "sigs.k8s.io/controller-runtime"
@@ -31,23 +32,9 @@ const (
31
32
32
33
type Config struct {
33
34
* options.GlobalOptions
34
-
35
- defaultKubeVIPConfigMapName string
36
- }
37
-
38
- func (c * Config ) AddFlags (prefix string , flags * pflag.FlagSet ) {
39
- flags .StringVar (
40
- & c .defaultKubeVIPConfigMapName ,
41
- prefix + ".default-kube-vip-template-configmap-name" ,
42
- "default-kube-vip-template" ,
43
- "default ConfigMap name that holds the kube-vip template used for the control-plane virtual IP" ,
44
- )
45
35
}
46
36
47
37
type ControlPlaneVirtualIP struct {
48
- client client.Reader
49
- config * Config
50
-
51
38
variableName string
52
39
variableFieldPath []string
53
40
}
@@ -56,14 +43,10 @@ type ControlPlaneVirtualIP struct {
56
43
// It requires variableName and variableFieldPath to be passed from another provider specific handler.
57
44
// The code is here to be shared across different providers.
58
45
func NewControlPlaneVirtualIP (
59
- cl client.Reader ,
60
- config * Config ,
61
46
variableName string ,
62
47
variableFieldPath ... string ,
63
48
) * ControlPlaneVirtualIP {
64
49
return & ControlPlaneVirtualIP {
65
- client : cl ,
66
- config : config ,
67
50
variableName : variableName ,
68
51
variableFieldPath : variableFieldPath ,
69
52
}
@@ -103,11 +86,6 @@ func (h *ControlPlaneVirtualIP) Mutate(
103
86
controlPlaneEndpointVar ,
104
87
)
105
88
106
- if controlPlaneEndpointVar .VirtualIPSpec == nil {
107
- log .V (5 ).Info ("ControlPlane VirtualIP not set" )
108
- return nil
109
- }
110
-
111
89
cluster , err := clusterGetter (ctx )
112
90
if err != nil {
113
91
log .Error (
@@ -117,23 +95,27 @@ func (h *ControlPlaneVirtualIP) Mutate(
117
95
return err
118
96
}
119
97
120
- var virtualIPProvider providers.Provider
121
- // only kube-vip is supported, but more providers can be added in the future
122
- if controlPlaneEndpointVar .VirtualIPSpec .Provider == v1alpha1 .VirtualIPProviderKubeVIP {
123
- virtualIPProvider = providers .NewKubeVIPFromConfigMapProvider (
124
- h .client ,
125
- h .config .defaultKubeVIPConfigMapName ,
126
- h .config .DefaultsNamespace (),
127
- )
128
- }
129
-
130
98
return patches .MutateIfApplicable (
131
99
obj ,
132
100
vars ,
133
101
& holderRef ,
134
102
selectors .ControlPlane (),
135
103
log ,
136
104
func (obj * controlplanev1.KubeadmControlPlaneTemplate ) error {
105
+ if controlPlaneEndpointVar .VirtualIPSpec == nil {
106
+ log .V (5 ).Info ("ControlPlane VirtualIP not set" )
107
+ // if VirtualIPSpec is not set, delete all template files
108
+ // as we do not want them to end up in the generated KCP
109
+ deleteFiles (obj , providers .TemplateFileNames ... )
110
+ return nil
111
+ }
112
+
113
+ var virtualIPProvider providers.Provider
114
+ // only kube-vip is supported, but more providers can be added in the future
115
+ if controlPlaneEndpointVar .VirtualIPSpec .Provider == v1alpha1 .VirtualIPProviderKubeVIP {
116
+ virtualIPProvider = providers .NewKubeVIPFromKCPTemplateProvider (obj )
117
+ }
118
+
137
119
files , preKubeadmCommands , postKubeadmCommands , generateErr := virtualIPProvider .GenerateFilesAndCommands (
138
120
ctx ,
139
121
controlPlaneEndpointVar ,
@@ -150,10 +132,8 @@ func (h *ControlPlaneVirtualIP) Mutate(
150
132
"adding %s static Pod file to control plane kubeadm config spec" ,
151
133
virtualIPProvider .Name (),
152
134
))
153
- obj .Spec .Template .Spec .KubeadmConfigSpec .Files = append (
154
- obj .Spec .Template .Spec .KubeadmConfigSpec .Files ,
155
- files ... ,
156
- )
135
+
136
+ mergeFiles (obj , files ... )
157
137
158
138
if len (preKubeadmCommands ) > 0 {
159
139
log .WithValues (
@@ -187,3 +167,36 @@ func (h *ControlPlaneVirtualIP) Mutate(
187
167
},
188
168
)
189
169
}
170
+
171
+ func deleteFiles (obj * controlplanev1.KubeadmControlPlaneTemplate , filePathsToDelete ... string ) {
172
+ for i := len (obj .Spec .Template .Spec .KubeadmConfigSpec .Files ) - 1 ; i >= 0 ; i -- {
173
+ for _ , path := range filePathsToDelete {
174
+ if obj .Spec .Template .Spec .KubeadmConfigSpec .Files [i ].Path == path {
175
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files = slices .Delete (
176
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files , i , i + 1 ,
177
+ )
178
+ break
179
+ }
180
+ }
181
+ }
182
+ }
183
+
184
+ // mergeFiles will merge the files into the KubeadmControlPlaneTemplate,
185
+ // overriding any file with the same path and appending the rest.
186
+ func mergeFiles (obj * controlplanev1.KubeadmControlPlaneTemplate , filesToMerge ... bootstrapv1.File ) {
187
+ // replace any existing files with the same path
188
+ for i := len (filesToMerge ) - 1 ; i >= 0 ; i -- {
189
+ for j := range obj .Spec .Template .Spec .KubeadmConfigSpec .Files {
190
+ if obj .Spec .Template .Spec .KubeadmConfigSpec .Files [j ].Path == filesToMerge [i ].Path {
191
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files [j ] = filesToMerge [i ]
192
+ filesToMerge = slices .Delete (filesToMerge , i , i + 1 )
193
+ break
194
+ }
195
+ }
196
+ }
197
+ // append the remaining files
198
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files = append (
199
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files ,
200
+ filesToMerge ... ,
201
+ )
202
+ }
0 commit comments