-
Notifications
You must be signed in to change notification settings - Fork 124
/
machines.go
203 lines (169 loc) · 7.42 KB
/
machines.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
/*
Copyright 2019 The Machine Controller 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 admission
import (
"context"
"encoding/json"
"fmt"
"github.com/Masterminds/semver/v3"
"golang.org/x/crypto/ssh"
"github.com/kubermatic/machine-controller/pkg/apis/cluster/common"
clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
"github.com/kubermatic/machine-controller/pkg/cloudprovider"
controllerutil "github.com/kubermatic/machine-controller/pkg/controller/util"
"github.com/kubermatic/machine-controller/pkg/providerconfig"
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
admissionv1 "k8s.io/api/admission/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// BypassSpecNoModificationRequirementAnnotation is used to bypass the "no machine.spec modification" allowed
// restriction from the webhook in order to change the spec in some special cases, e.G. for the migration of
// the `providerConfig` field to `providerSpec`.
const BypassSpecNoModificationRequirementAnnotation = "kubermatic.io/bypass-no-spec-mutation-requirement"
func (ad *admissionData) mutateMachines(ctx context.Context, ar admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) {
machine := clusterv1alpha1.Machine{}
if err := json.Unmarshal(ar.Object.Raw, &machine); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
machineOriginal := machine.DeepCopy()
log := ad.log.With("machine", ctrlruntimeclient.ObjectKeyFromObject(&machine))
log.Debug("Defaulting and validating machine")
// Mutating .Spec is never allowed
// Only hidden exception: the machine-controller may set the .Spec.Name to .Metadata.Name
// because otherwise it can never add the delete finalizer as it internally defaults the Name
// as well, since on the CREATE request for machines, there is only Metadata.GenerateName set
// so we can't default it initially.
if ar.Operation == admissionv1.Update {
oldMachine := clusterv1alpha1.Machine{}
if err := json.Unmarshal(ar.OldObject.Raw, &oldMachine); err != nil {
return nil, fmt.Errorf("failed to unmarshal OldObject: %w", err)
}
if oldMachine.Spec.Name != machine.Spec.Name && machine.Spec.Name == machine.Name {
oldMachine.Spec.Name = machine.Spec.Name
}
// Allow mutation when:
// * machine has the `MigrationBypassSpecNoModificationRequirementAnnotation` annotation (used for type migration)
bypassValidationForMigration := machine.Annotations[BypassSpecNoModificationRequirementAnnotation] == "true"
if !bypassValidationForMigration {
if equal := apiequality.Semantic.DeepEqual(machine.Spec, oldMachine.Spec); !equal {
return nil, fmt.Errorf("machine.spec is immutable")
}
}
}
// Delete the `BypassSpecNoModificationRequirementAnnotation` annotation, it should be valid only once.
delete(machine.Annotations, BypassSpecNoModificationRequirementAnnotation)
// Default name
if machine.Spec.Name == "" {
machine.Spec.Name = machine.Name
}
// Default and verify .Spec on CREATE only, its expensive and not required to do it on UPDATE
// as we disallow .Spec changes anyways.
if ar.Operation == admissionv1.Create {
if err := ad.defaultAndValidateMachineSpec(ctx, &machine.Spec); err != nil {
return nil, err
}
common.SetKubeletFeatureGates(&machine, ad.nodeSettings.KubeletFeatureGates)
common.SetKubeletFlags(&machine, map[common.KubeletFlags]string{
common.ExternalCloudProviderKubeletFlag: fmt.Sprintf("%t", ad.nodeSettings.ExternalCloudProvider),
})
providerConfig, err := providerconfigtypes.GetConfig(machine.Spec.ProviderSpec)
if err != nil {
return nil, err
}
common.SetOSLabel(&machine.Spec, string(providerConfig.OperatingSystem))
}
if machine.Labels == nil {
machine.Labels = make(map[string]string)
}
// Set LegacyMachineControllerUserDataLabel to false if external bootstrapping is expected for managing the machine configuration.
if ad.useExternalBootstrap {
machine.Labels[controllerutil.LegacyMachineControllerUserDataLabel] = "false"
} else {
machine.Labels[controllerutil.LegacyMachineControllerUserDataLabel] = "true"
}
return createAdmissionResponse(log, machineOriginal, &machine)
}
func (ad *admissionData) defaultAndValidateMachineSpec(ctx context.Context, spec *clusterv1alpha1.MachineSpec) error {
providerConfig, err := providerconfigtypes.GetConfig(spec.ProviderSpec)
if err != nil {
return fmt.Errorf("failed to read machine.spec.providerSpec: %w", err)
}
// Packet has been renamed to Equinix Metal
if providerConfig.CloudProvider == cloudProviderPacket {
err = migrateToEquinixMetal(providerConfig)
if err != nil {
return fmt.Errorf("failed to migrate packet to equinix metal: %w", err)
}
}
skg := providerconfig.NewConfigVarResolver(ctx, ad.workerClient)
prov, err := cloudprovider.ForProvider(providerConfig.CloudProvider, skg)
if err != nil {
return fmt.Errorf("failed to get cloud provider %q: %w", providerConfig.CloudProvider, err)
}
// Verify operating system.
if _, err := ad.userDataManager.ForOS(providerConfig.OperatingSystem); err != nil {
return fmt.Errorf("failed to get OS '%s': %w", providerConfig.OperatingSystem, err)
}
// Check kubelet version
if spec.Versions.Kubelet == "" {
return fmt.Errorf("Kubelet version must be set")
}
kubeletVer, err := semver.NewVersion(spec.Versions.Kubelet)
if err != nil {
return fmt.Errorf("failed to parse kubelet version: %w", err)
}
if !ad.constraints.Check(kubeletVer) {
return fmt.Errorf("kubernetes version constraint didn't allow %q kubelet version", kubeletVer)
}
// Do not allow usage of config source (dynamic kubelet configuration) since has been removed in k8s v1.24.
if spec.ConfigSource != nil {
return fmt.Errorf("setting spec.ConfigSource is not allowed for kubelet version %q", kubeletVer)
}
// Validate SSH keys
if err := validatePublicKeys(providerConfig.SSHPublicKeys); err != nil {
return fmt.Errorf("Invalid public keys specified: %w", err)
}
defaultedOperatingSystemSpec, err := providerconfig.DefaultOperatingSystemSpec(
providerConfig.OperatingSystem,
providerConfig.CloudProvider,
providerConfig.OperatingSystemSpec,
ad.useExternalBootstrap,
)
if err != nil {
return err
}
providerConfig.OperatingSystemSpec = defaultedOperatingSystemSpec
spec.ProviderSpec.Value.Raw, err = json.Marshal(providerConfig)
if err != nil {
return fmt.Errorf("failed to json marshal machine.spec.providerSpec: %w", err)
}
defaultedSpec, err := prov.AddDefaults(ad.log, *spec)
if err != nil {
return fmt.Errorf("failed to default machineSpec: %w", err)
}
spec = &defaultedSpec
if err := prov.Validate(ctx, ad.log, *spec); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
return nil
}
func validatePublicKeys(keys []string) error {
for _, s := range keys {
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(s))
if err != nil {
return fmt.Errorf("invalid public key %q: %w", s, err)
}
}
return nil
}