-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
kubecontrollermanager.go
187 lines (149 loc) · 6.17 KB
/
kubecontrollermanager.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
/*
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 components
import (
"fmt"
"net"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/loader"
"k8s.io/kops/upup/pkg/fi/utils"
)
const (
defaultAttachDetachReconcileSyncPeriod = time.Minute
)
// KubeControllerManagerOptionsBuilder adds options for the kubernetes controller manager to the model.
type KubeControllerManagerOptionsBuilder struct {
*OptionsContext
}
var _ loader.OptionsBuilder = &KubeControllerManagerOptionsBuilder{}
// BuildOptions generates the configurations used to create kubernetes controller manager manifest
func (b *KubeControllerManagerOptionsBuilder) BuildOptions(o interface{}) error {
clusterSpec := o.(*kops.ClusterSpec)
if clusterSpec.KubeControllerManager == nil {
clusterSpec.KubeControllerManager = &kops.KubeControllerManagerConfig{}
}
kcm := clusterSpec.KubeControllerManager
// Tune the duration upon which the volume attach detach component is called.
// See https://github.com/kubernetes/kubernetes/pull/39551
// TLDR; set this too low, and have a few EBS Volumes, and you will spam AWS api
{
klog.V(4).Infof("Kubernetes version %q supports AttachDetachReconcileSyncPeriod; will configure", b.KubernetesVersion)
// If not set ... or set to 0s ... which is stupid
if kcm.AttachDetachReconcileSyncPeriod == nil ||
kcm.AttachDetachReconcileSyncPeriod.Duration.String() == "0s" {
klog.V(8).Infof("AttachDetachReconcileSyncPeriod is not set; will set to default %v", defaultAttachDetachReconcileSyncPeriod)
kcm.AttachDetachReconcileSyncPeriod = &metav1.Duration{Duration: defaultAttachDetachReconcileSyncPeriod}
// If less than 1 min and greater than 1 sec ... you get a warning
} else if kcm.AttachDetachReconcileSyncPeriod.Duration < defaultAttachDetachReconcileSyncPeriod &&
kcm.AttachDetachReconcileSyncPeriod.Duration > time.Second {
klog.Infof("KubeControllerManager AttachDetachReconcileSyncPeriod is set lower than recommended: %s", defaultAttachDetachReconcileSyncPeriod)
// If less than 1sec you get an error. Controller is coded to not allow configuration
// less than one second.
} else if kcm.AttachDetachReconcileSyncPeriod.Duration < time.Second {
return fmt.Errorf("AttachDetachReconcileSyncPeriod cannot be set to less than 1 second")
}
}
kcm.ClusterName = b.ClusterName
switch kops.CloudProviderID(clusterSpec.CloudProvider) {
case kops.CloudProviderAWS:
kcm.CloudProvider = "aws"
case kops.CloudProviderGCE:
kcm.CloudProvider = "gce"
kcm.ClusterName = gce.SafeClusterName(b.ClusterName)
case kops.CloudProviderDO:
kcm.CloudProvider = "external"
case kops.CloudProviderOpenstack:
kcm.CloudProvider = "openstack"
case kops.CloudProviderALI:
kcm.CloudProvider = "alicloud"
case kops.CloudProviderAzure:
kcm.CloudProvider = "azure"
default:
return fmt.Errorf("unknown cloudprovider %q", clusterSpec.CloudProvider)
}
if clusterSpec.ExternalCloudControllerManager != nil {
kcm.CloudProvider = "external"
}
kcm.LogLevel = 2
image, err := Image("kube-controller-manager", clusterSpec, b.AssetBuilder)
if err != nil {
return err
}
kcm.Image = image
// Doesn't seem to be any real downside to always doing a leader election
kcm.LeaderElection = &kops.LeaderElectionConfiguration{LeaderElect: fi.Bool(true)}
kcm.AllocateNodeCIDRs = fi.Bool(true)
kcm.ConfigureCloudRoutes = fi.Bool(false)
if kcm.ClusterCIDR == "" {
kcm.ClusterCIDR = clusterSpec.PodCIDR
}
if utils.IsIPv6CIDR(kcm.ClusterCIDR) {
_, clusterNet, _ := net.ParseCIDR(kcm.ClusterCIDR)
clusterSize, _ := clusterNet.Mask.Size()
nodeSize := (128 - clusterSize) / 2
if nodeSize > 16 {
// Kubernetes limitation
nodeSize = 16
}
kcm.NodeCIDRMaskSize = fi.Int32(int32(clusterSize + nodeSize))
}
networking := clusterSpec.Networking
if networking == nil {
kcm.ConfigureCloudRoutes = fi.Bool(true)
} else if networking.Kubenet != nil {
kcm.ConfigureCloudRoutes = fi.Bool(true)
} else if networking.GCE != nil {
kcm.ConfigureCloudRoutes = fi.Bool(false)
kcm.CIDRAllocatorType = fi.String("CloudAllocator")
} else if networking.External != nil {
kcm.ConfigureCloudRoutes = fi.Bool(false)
} else if UsesCNI(networking) {
kcm.ConfigureCloudRoutes = fi.Bool(false)
} else if networking.Kopeio != nil {
// Kopeio is based on kubenet / external
kcm.ConfigureCloudRoutes = fi.Bool(false)
} else {
return fmt.Errorf("no networking mode set")
}
if kcm.UseServiceAccountCredentials == nil {
kcm.UseServiceAccountCredentials = fi.Bool(true)
}
// @check if the node authorization is enabled and if so enable the tokencleaner controller (disabled by default)
// This is responsible for cleaning up bootstrap tokens which have expired
if fi.BoolValue(clusterSpec.KubeAPIServer.EnableBootstrapAuthToken) && len(kcm.Controllers) <= 0 {
kcm.Controllers = []string{"*", "tokencleaner"}
}
if clusterSpec.CloudConfig != nil && clusterSpec.CloudConfig.AWSEBSCSIDriver != nil && fi.BoolValue(clusterSpec.CloudConfig.AWSEBSCSIDriver.Enabled) {
if kcm.FeatureGates == nil {
kcm.FeatureGates = make(map[string]string)
}
if b.IsKubernetesLT("1.21.0") {
if _, found := clusterSpec.Kubelet.FeatureGates["CSIMigrationAWSComplete"]; !found {
clusterSpec.Kubelet.FeatureGates["CSIMigrationAWSComplete"] = "true"
}
} else {
if _, found := clusterSpec.Kubelet.FeatureGates["InTreePluginAWSUnregister"]; !found {
clusterSpec.Kubelet.FeatureGates["InTreePluginAWSUnregister"] = "true"
}
}
if _, found := kcm.FeatureGates["CSIMigrationAWS"]; !found {
kcm.FeatureGates["CSIMigrationAWS"] = "true"
}
}
return nil
}