-
Notifications
You must be signed in to change notification settings - Fork 40.4k
/
Copy pathconfig.go
285 lines (241 loc) · 9.34 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubelet
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clientset "k8s.io/client-go/kubernetes"
kubeletconfig "k8s.io/kubelet/config/v1beta1"
"sigs.k8s.io/yaml"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/features"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
"k8s.io/kubernetes/cmd/kubeadm/app/util/patches"
)
var applyKubeletConfigPatchesFunc = applyKubeletConfigPatches
// WriteConfigToDisk writes the kubelet config object down to a file
// Used at "kubeadm init" and "kubeadm upgrade" time
func WriteConfigToDisk(cfg *kubeadmapi.ClusterConfiguration, kubeletDir, patchesDir string, output io.Writer) error {
kubeletCfg, ok := cfg.ComponentConfigs[componentconfigs.KubeletGroup]
if !ok {
return errors.New("no kubelet component config found")
}
if err := kubeletCfg.Mutate(); err != nil {
return err
}
kubeletBytes, err := kubeletCfg.Marshal()
if err != nil {
return err
}
// Apply patches to the KubeletConfiguration
if len(patchesDir) != 0 {
kubeletBytes, err = applyKubeletConfigPatchesFunc(kubeletBytes, patchesDir, output)
if err != nil {
return errors.Wrap(err, "could not apply patches to the KubeletConfiguration")
}
}
if features.Enabled(cfg.FeatureGates, features.NodeLocalCRISocket) {
file := filepath.Join(kubeletDir, kubeadmconstants.KubeletInstanceConfigurationFileName)
kubeletBytes, err = applyKubeletConfigPatchFromFile(kubeletBytes, file, output)
if err != nil {
return errors.Wrapf(err, "could not apply kubelet instance configuration as a patch from %q", file)
}
}
return writeConfigBytesToDisk(kubeletBytes, kubeletDir, kubeadmconstants.KubeletConfigurationFileName)
}
// WriteInstanceConfigToDisk writes the container runtime endpoint configuration
// to the instance configuration file in the specified kubelet directory.
func WriteInstanceConfigToDisk(cfg *kubeletconfig.KubeletConfiguration, kubeletDir string) error {
instanceFileContent := fmt.Sprintf("containerRuntimeEndpoint: %q\n", cfg.ContainerRuntimeEndpoint)
return writeConfigBytesToDisk([]byte(instanceFileContent), kubeletDir, kubeadmconstants.KubeletInstanceConfigurationFileName)
}
// ApplyPatchesToConfig applies the patches located in patchesDir to the KubeletConfiguration stored
// in the ClusterConfiguration.ComponentConfigs map.
func ApplyPatchesToConfig(cfg *kubeadmapi.ClusterConfiguration, patchesDir string) error {
kubeletCfg, ok := cfg.ComponentConfigs[componentconfigs.KubeletGroup]
if !ok {
return errors.New("no kubelet component config found")
}
if err := kubeletCfg.Mutate(); err != nil {
return err
}
kubeletBytes, err := kubeletCfg.Marshal()
if err != nil {
return err
}
// Apply patches to the KubeletConfiguration. Output is discarded.
if len(patchesDir) != 0 {
kubeletBytes, err = applyKubeletConfigPatchesFunc(kubeletBytes, patchesDir, io.Discard)
if err != nil {
return errors.Wrap(err, "could not apply patches to the KubeletConfiguration")
}
}
gvkmap, err := kubeadmutil.SplitConfigDocuments(kubeletBytes)
if err != nil {
return err
}
if err := kubeletCfg.Unmarshal(gvkmap); err != nil {
return err
}
return nil
}
// CreateConfigMap creates a ConfigMap with the generic kubelet configuration.
// Used at "kubeadm init" and "kubeadm upgrade" time
func CreateConfigMap(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface) error {
configMapName := kubeadmconstants.KubeletBaseConfigurationConfigMap
fmt.Printf("[kubelet] Creating a ConfigMap %q in namespace %s with the configuration for the kubelets in the cluster\n", configMapName, metav1.NamespaceSystem)
kubeletCfg, ok := cfg.ComponentConfigs[componentconfigs.KubeletGroup]
if !ok {
return errors.New("no kubelet component config found in the active component config set")
}
kubeletBytes, err := kubeletCfg.Marshal()
if err != nil {
return err
}
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: metav1.NamespaceSystem,
},
Data: map[string]string{
kubeadmconstants.KubeletBaseConfigurationConfigMapKey: string(kubeletBytes),
},
}
if !kubeletCfg.IsUserSupplied() {
componentconfigs.SignConfigMap(configMap)
}
if err := apiclient.CreateOrUpdate(client.CoreV1().ConfigMaps(configMap.GetNamespace()), configMap); err != nil {
return err
}
if err := createConfigMapRBACRules(client); err != nil {
return errors.Wrap(err, "error creating kubelet configuration configmap RBAC rules")
}
return nil
}
// createConfigMapRBACRules creates the RBAC rules for exposing the base kubelet ConfigMap in the kube-system namespace to unauthenticated users
func createConfigMapRBACRules(client clientset.Interface) error {
if err := apiclient.CreateOrUpdate(client.RbacV1().Roles(metav1.NamespaceSystem), &rbac.Role{
ObjectMeta: metav1.ObjectMeta{
Name: kubeadmconstants.KubeletBaseConfigMapRole,
Namespace: metav1.NamespaceSystem,
},
Rules: []rbac.PolicyRule{
{
Verbs: []string{"get"},
APIGroups: []string{""},
Resources: []string{"configmaps"},
ResourceNames: []string{kubeadmconstants.KubeletBaseConfigurationConfigMap},
},
},
}); err != nil {
return err
}
return apiclient.CreateOrUpdate(client.RbacV1().RoleBindings(metav1.NamespaceSystem), &rbac.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: kubeadmconstants.KubeletBaseConfigMapRole,
Namespace: metav1.NamespaceSystem,
},
RoleRef: rbac.RoleRef{
APIGroup: rbac.GroupName,
Kind: "Role",
Name: kubeadmconstants.KubeletBaseConfigMapRole,
},
Subjects: []rbac.Subject{
{
Kind: rbac.GroupKind,
Name: kubeadmconstants.NodesGroup,
},
{
Kind: rbac.GroupKind,
Name: kubeadmconstants.NodeBootstrapTokenAuthGroup,
},
},
})
}
// writeConfigBytesToDisk writes a byte slice down to disk at the specific location of the kubelet config file
func writeConfigBytesToDisk(b []byte, kubeletDir, fileName string) error {
configFile := filepath.Join(kubeletDir, fileName)
fmt.Printf("[kubelet-start] Writing kubelet configuration to file %q\n", configFile)
// creates target folder if not already exists
if err := os.MkdirAll(kubeletDir, 0700); err != nil {
return errors.Wrapf(err, "failed to create directory %q", kubeletDir)
}
if err := os.WriteFile(configFile, b, 0644); err != nil {
return errors.Wrapf(err, "failed to write kubelet configuration file %q", configFile)
}
return nil
}
// applyKubeletConfigPatches reads patches from a directory and applies them over the input kubeletBytes
func applyKubeletConfigPatches(kubeletBytes []byte, patchesDir string, output io.Writer) ([]byte, error) {
patchManager, err := patches.GetPatchManagerForPath(patchesDir, patches.KnownTargets(), output)
if err != nil {
return nil, err
}
patchTarget := &patches.PatchTarget{
Name: patches.KubeletConfiguration,
StrategicMergePatchObject: kubeletconfig.KubeletConfiguration{},
Data: kubeletBytes,
}
if err := patchManager.ApplyPatchesToTarget(patchTarget); err != nil {
return nil, err
}
kubeletBytes, err = yaml.JSONToYAML(patchTarget.Data)
if err != nil {
return nil, err
}
return kubeletBytes, nil
}
// applyKubeletConfigPatchFromFile applies a single patch file to the kubelet configuration bytes.
func applyKubeletConfigPatchFromFile(kubeletConfigBytes []byte, patchFilePath string, output io.Writer) ([]byte, error) {
// Get the patch data from the file.
data, err := os.ReadFile(patchFilePath)
if err != nil {
return nil, errors.Wrapf(err, "could not read patch file %q", patchFilePath)
}
patchSet, err := patches.CreatePatchSet(patches.KubeletConfiguration, types.StrategicMergePatchType, string(data))
if err != nil {
return nil, err
}
patchManager := patches.NewPatchManager([]*patches.PatchSet{patchSet}, []string{patches.KubeletConfiguration}, output)
// Always convert the target data to JSON.
patchData, err := yaml.YAMLToJSON(kubeletConfigBytes)
if err != nil {
return nil, err
}
// Define the patch target.
patchTarget := &patches.PatchTarget{
Name: patches.KubeletConfiguration,
StrategicMergePatchObject: kubeletconfig.KubeletConfiguration{},
Data: patchData,
}
err = patchManager.ApplyPatchesToTarget(patchTarget)
if err != nil {
return nil, err
}
// Convert the patched data back to YAML and return it.
kubeletConfigBytes, err = yaml.JSONToYAML(patchTarget.Data)
if err != nil {
return nil, errors.Wrap(err, "failed to convert patched data to YAML")
}
return kubeletConfigBytes, nil
}