-
Notifications
You must be signed in to change notification settings - Fork 234
/
kubeadm.go
222 lines (194 loc) · 7.33 KB
/
kubeadm.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
/*
Copyright 2019 The KubeOne 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 v1beta1
import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
kubeadmv1beta1 "github.com/kubermatic/kubeone/pkg/apis/kubeadm/v1beta1"
kubeoneapi "github.com/kubermatic/kubeone/pkg/apis/kubeone"
"github.com/kubermatic/kubeone/pkg/features"
"github.com/kubermatic/kubeone/pkg/kubeflags"
"github.com/kubermatic/kubeone/pkg/state"
"github.com/kubermatic/kubeone/pkg/templates/kubeadm/kubeadmargs"
"github.com/kubermatic/kubeone/pkg/templates/nodelocaldns"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
const (
bootstrapTokenTTL = 60 * time.Minute
)
// NewConfig returns all required configs to init a cluster via a set of v1beta1 configs
func NewConfig(s *state.State, host kubeoneapi.HostConfig) ([]runtime.Object, error) {
cluster := s.Cluster
kubeSemVer, err := semver.NewVersion(cluster.Versions.Kubernetes)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse generate config, wrong kubernetes version %s", cluster.Versions.Kubernetes)
}
nodeIP := host.PrivateAddress
if nodeIP == "" {
nodeIP = host.PublicAddress
}
nodeRegistration := kubeadmv1beta1.NodeRegistrationOptions{
Name: host.Hostname,
Taints: []corev1.Taint{
{
Effect: corev1.TaintEffectNoSchedule,
Key: "node-role.kubernetes.io/master",
},
},
KubeletExtraArgs: map[string]string{
"anonymous-auth": "false",
"node-ip": nodeIP,
"read-only-port": "0",
"rotate-certificates": "true",
"cluster-dns": nodelocaldns.VirtualIP,
},
}
bootstrapToken, err := kubeadmv1beta1.NewBootstrapTokenString(s.JoinToken)
if err != nil {
return nil, err
}
controlPlaneEndpoint := fmt.Sprintf("%s:%d", cluster.APIEndpoint.Host, cluster.APIEndpoint.Port)
initConfig := &kubeadmv1beta1.InitConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubeadm.k8s.io/v1beta1",
Kind: "InitConfiguration",
},
BootstrapTokens: []kubeadmv1beta1.BootstrapToken{
{
Token: bootstrapToken,
Groups: []string{
"system:bootstrappers:kubeadm:default-node-token",
},
TTL: &metav1.Duration{
Duration: bootstrapTokenTTL,
},
Usages: []string{
"signing",
"authentication",
},
},
},
LocalAPIEndpoint: kubeadmv1beta1.APIEndpoint{
AdvertiseAddress: nodeIP,
},
}
joinConfig := &kubeadmv1beta1.JoinConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubeadm.k8s.io/v1beta1",
Kind: "JoinConfiguration",
},
ControlPlane: &kubeadmv1beta1.JoinControlPlane{
LocalAPIEndpoint: kubeadmv1beta1.APIEndpoint{
AdvertiseAddress: nodeIP,
},
},
Discovery: kubeadmv1beta1.Discovery{
BootstrapToken: &kubeadmv1beta1.BootstrapTokenDiscovery{
Token: s.JoinToken,
APIServerEndpoint: controlPlaneEndpoint,
UnsafeSkipCAVerification: true,
},
},
}
clusterConfig := &kubeadmv1beta1.ClusterConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubeadm.k8s.io/v1beta1",
Kind: "ClusterConfiguration",
},
Networking: kubeadmv1beta1.Networking{
PodSubnet: cluster.ClusterNetwork.PodSubnet,
ServiceSubnet: cluster.ClusterNetwork.ServiceSubnet,
DNSDomain: cluster.ClusterNetwork.ServiceDomainName,
},
KubernetesVersion: cluster.Versions.Kubernetes,
ControlPlaneEndpoint: controlPlaneEndpoint,
APIServer: kubeadmv1beta1.APIServer{
ControlPlaneComponent: kubeadmv1beta1.ControlPlaneComponent{
ExtraArgs: map[string]string{
"endpoint-reconciler-type": "lease",
"service-node-port-range": cluster.ClusterNetwork.NodePortRange,
"enable-admission-plugins": kubeflags.DefaultAdmissionControllers(kubeSemVer),
},
ExtraVolumes: []kubeadmv1beta1.HostPathMount{},
},
CertSANs: []string{strings.ToLower(cluster.APIEndpoint.Host)},
},
ControllerManager: kubeadmv1beta1.ControlPlaneComponent{
ExtraArgs: map[string]string{},
ExtraVolumes: []kubeadmv1beta1.HostPathMount{},
},
ClusterName: cluster.Name,
}
if cluster.CloudProvider.CloudProviderInTree() {
renderedCloudConfig := "/etc/kubernetes/cloud-config"
cloudConfigVol := kubeadmv1beta1.HostPathMount{
Name: "cloud-config",
HostPath: renderedCloudConfig,
MountPath: renderedCloudConfig,
ReadOnly: true,
PathType: corev1.HostPathFile,
}
provider := string(cluster.CloudProvider.Name)
clusterConfig.APIServer.ExtraArgs["cloud-provider"] = provider
clusterConfig.APIServer.ExtraArgs["cloud-config"] = renderedCloudConfig
clusterConfig.APIServer.ExtraVolumes = append(clusterConfig.APIServer.ExtraVolumes, cloudConfigVol)
clusterConfig.ControllerManager.ExtraArgs["cloud-provider"] = provider
clusterConfig.ControllerManager.ExtraArgs["cloud-config"] = renderedCloudConfig
clusterConfig.ControllerManager.ExtraArgs["cluster-name"] = s.Cluster.Name
clusterConfig.ControllerManager.ExtraVolumes = append(clusterConfig.ControllerManager.ExtraVolumes, cloudConfigVol)
nodeRegistration.KubeletExtraArgs["cloud-provider"] = provider
nodeRegistration.KubeletExtraArgs["cloud-config"] = renderedCloudConfig
switch cluster.CloudProvider.Name {
case kubeoneapi.CloudProviderNameAzure:
clusterConfig.ControllerManager.ExtraArgs["configure-cloud-routes"] = "false"
case kubeoneapi.CloudProviderNameAWS:
clusterConfig.ControllerManager.ExtraArgs["configure-cloud-routes"] = "false"
}
}
if cluster.CloudProvider.External {
delete(clusterConfig.APIServer.ExtraArgs, "cloud-provider")
delete(clusterConfig.ControllerManager.ExtraArgs, "cloud-provider")
nodeRegistration.KubeletExtraArgs["cloud-provider"] = "external"
}
if cluster.Features.StaticAuditLog != nil && cluster.Features.StaticAuditLog.Enable {
auditPolicyVol := kubeadmv1beta1.HostPathMount{
Name: "audit-conf",
HostPath: "/etc/kubernetes/audit",
MountPath: "/etc/kubernetes/audit",
ReadOnly: true,
PathType: corev1.HostPathDirectoryOrCreate,
}
logVol := kubeadmv1beta1.HostPathMount{
Name: "log",
HostPath: filepath.Dir(cluster.Features.StaticAuditLog.Config.LogPath),
MountPath: filepath.Dir(cluster.Features.StaticAuditLog.Config.LogPath),
ReadOnly: false,
PathType: corev1.HostPathDirectoryOrCreate,
}
clusterConfig.APIServer.ExtraVolumes = append(clusterConfig.APIServer.ExtraVolumes, auditPolicyVol)
clusterConfig.APIServer.ExtraVolumes = append(clusterConfig.APIServer.ExtraVolumes, logVol)
}
args := kubeadmargs.NewFrom(clusterConfig.APIServer.ExtraArgs)
features.UpdateKubeadmClusterConfiguration(cluster.Features, args)
clusterConfig.APIServer.ExtraArgs = args.APIServer.ExtraArgs
clusterConfig.FeatureGates = args.FeatureGates
initConfig.NodeRegistration = nodeRegistration
joinConfig.NodeRegistration = nodeRegistration
return []runtime.Object{initConfig, joinConfig, clusterConfig}, nil
}