@@ -27,13 +27,15 @@ import (
27
27
v1 "k8s.io/api/core/v1"
28
28
rbac "k8s.io/api/rbac/v1"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
+ "k8s.io/apimachinery/pkg/types"
30
31
clientset "k8s.io/client-go/kubernetes"
31
32
kubeletconfig "k8s.io/kubelet/config/v1beta1"
32
33
"sigs.k8s.io/yaml"
33
34
34
35
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
35
36
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
36
37
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
38
+ "k8s.io/kubernetes/cmd/kubeadm/app/features"
37
39
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
38
40
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
39
41
"k8s.io/kubernetes/cmd/kubeadm/app/util/patches"
@@ -66,7 +68,21 @@ func WriteConfigToDisk(cfg *kubeadmapi.ClusterConfiguration, kubeletDir, patches
66
68
}
67
69
}
68
70
69
- return writeConfigBytesToDisk (kubeletBytes , kubeletDir )
71
+ if features .Enabled (cfg .FeatureGates , features .NodeLocalCRISocket ) {
72
+ file := filepath .Join (kubeletDir , kubeadmconstants .KubeletInstanceConfigurationFileName )
73
+ kubeletBytes , err = applyKubeletConfigPatchFromFile (kubeletBytes , file , output )
74
+ if err != nil {
75
+ return errors .Wrapf (err , "could not apply kubelet instance configuration as a patch from %q" , file )
76
+ }
77
+ }
78
+ return writeConfigBytesToDisk (kubeletBytes , kubeletDir , kubeadmconstants .KubeletConfigurationFileName )
79
+ }
80
+
81
+ // WriteInstanceConfigToDisk writes the container runtime endpoint configuration
82
+ // to the instance configuration file in the specified kubelet directory.
83
+ func WriteInstanceConfigToDisk (cfg * kubeletconfig.KubeletConfiguration , kubeletDir string ) error {
84
+ instanceFileContent := fmt .Sprintf ("containerRuntimeEndpoint: %q\n " , cfg .ContainerRuntimeEndpoint )
85
+ return writeConfigBytesToDisk ([]byte (instanceFileContent ), kubeletDir , kubeadmconstants .KubeletInstanceConfigurationFileName )
70
86
}
71
87
72
88
// ApplyPatchesToConfig applies the patches located in patchesDir to the KubeletConfiguration stored
@@ -188,8 +204,8 @@ func createConfigMapRBACRules(client clientset.Interface) error {
188
204
}
189
205
190
206
// writeConfigBytesToDisk writes a byte slice down to disk at the specific location of the kubelet config file
191
- func writeConfigBytesToDisk (b []byte , kubeletDir string ) error {
192
- configFile := filepath .Join (kubeletDir , kubeadmconstants . KubeletConfigurationFileName )
207
+ func writeConfigBytesToDisk (b []byte , kubeletDir , fileName string ) error {
208
+ configFile := filepath .Join (kubeletDir , fileName )
193
209
fmt .Printf ("[kubelet-start] Writing kubelet configuration to file %q\n " , configFile )
194
210
195
211
// creates target folder if not already exists
@@ -198,7 +214,7 @@ func writeConfigBytesToDisk(b []byte, kubeletDir string) error {
198
214
}
199
215
200
216
if err := os .WriteFile (configFile , b , 0644 ); err != nil {
201
- return errors .Wrapf (err , "failed to write kubelet configuration to the file %q" , configFile )
217
+ return errors .Wrapf (err , "failed to write kubelet configuration file %q" , configFile )
202
218
}
203
219
return nil
204
220
}
@@ -225,3 +241,45 @@ func applyKubeletConfigPatches(kubeletBytes []byte, patchesDir string, output io
225
241
}
226
242
return kubeletBytes , nil
227
243
}
244
+
245
+ // applyKubeletConfigPatchFromFile applies a single patch file to the kubelet configuration bytes.
246
+ func applyKubeletConfigPatchFromFile (kubeletConfigBytes []byte , patchFilePath string , output io.Writer ) ([]byte , error ) {
247
+ // Get the patch data from the file.
248
+ data , err := os .ReadFile (patchFilePath )
249
+ if err != nil {
250
+ return nil , errors .Wrapf (err , "could not read patch file %q" , patchFilePath )
251
+ }
252
+
253
+ patchSet , err := patches .CreatePatchSet (patches .KubeletConfiguration , types .StrategicMergePatchType , string (data ))
254
+ if err != nil {
255
+ return nil , err
256
+ }
257
+
258
+ patchManager := patches .NewPatchManager ([]* patches.PatchSet {patchSet }, []string {patches .KubeletConfiguration }, output )
259
+
260
+ // Always convert the target data to JSON.
261
+ patchData , err := yaml .YAMLToJSON (kubeletConfigBytes )
262
+ if err != nil {
263
+ return nil , err
264
+ }
265
+
266
+ // Define the patch target.
267
+ patchTarget := & patches.PatchTarget {
268
+ Name : patches .KubeletConfiguration ,
269
+ StrategicMergePatchObject : kubeletconfig.KubeletConfiguration {},
270
+ Data : patchData ,
271
+ }
272
+
273
+ err = patchManager .ApplyPatchesToTarget (patchTarget )
274
+ if err != nil {
275
+ return nil , err
276
+ }
277
+
278
+ // Convert the patched data back to YAML and return it.
279
+ kubeletConfigBytes , err = yaml .JSONToYAML (patchTarget .Data )
280
+ if err != nil {
281
+ return nil , errors .Wrap (err , "failed to convert patched data to YAML" )
282
+ }
283
+
284
+ return kubeletConfigBytes , nil
285
+ }
0 commit comments