-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
kubecontrollermanager.go
162 lines (131 loc) · 5.29 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
/*
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/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.ClusterOptionsBuilder = &KubeControllerManagerOptionsBuilder{}
// BuildOptions generates the configurations used to create kubernetes controller manager manifest
func (b *KubeControllerManagerOptionsBuilder) BuildOptions(o *kops.Cluster) error {
clusterSpec := &o.Spec
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
kcm.CloudProvider = "external"
if kcm.LogLevel == 0 {
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.PtrTo(true)}
kcm.AllocateNodeCIDRs = fi.PtrTo(!clusterSpec.IsKopsControllerIPAM())
if kcm.ClusterCIDR == "" && !clusterSpec.IsKopsControllerIPAM() {
kcm.ClusterCIDR = clusterSpec.Networking.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.PtrTo(int32(clusterSize + nodeSize))
}
networking := &clusterSpec.Networking
if networking.Kubenet != nil {
kcm.ConfigureCloudRoutes = fi.PtrTo(true)
} else if networking.GCP != nil {
kcm.ConfigureCloudRoutes = fi.PtrTo(false)
if kcm.CloudProvider == "external" {
// kcm should not allocate node cidrs with the CloudAllocator if we're using the external CCM
kcm.AllocateNodeCIDRs = fi.PtrTo(false)
} else {
kcm.CIDRAllocatorType = fi.PtrTo("CloudAllocator")
}
} else if networking.External != nil {
kcm.ConfigureCloudRoutes = fi.PtrTo(false)
} else if UsesCNI(networking) {
kcm.ConfigureCloudRoutes = fi.PtrTo(false)
} else if networking.Kopeio != nil {
// Kopeio is based on kubenet / external
kcm.ConfigureCloudRoutes = fi.PtrTo(false)
} else {
return fmt.Errorf("no networking mode set")
}
if kcm.UseServiceAccountCredentials == nil {
kcm.UseServiceAccountCredentials = fi.PtrTo(true)
}
if len(kcm.Controllers) == 0 {
var changes []string
if clusterSpec.IsKopsControllerIPAM() {
changes = append(changes, "-nodeipam")
}
if len(changes) != 0 {
kcm.Controllers = append([]string{"*"}, changes...)
}
}
if clusterSpec.CloudProvider.AWS != nil {
if kcm.FeatureGates == nil {
kcm.FeatureGates = make(map[string]string)
}
if _, found := kcm.FeatureGates["InTreePluginAWSUnregister"]; !found && b.IsKubernetesLT("1.31") {
kcm.FeatureGates["InTreePluginAWSUnregister"] = "true"
}
if _, found := kcm.FeatureGates["CSIMigrationAWS"]; !found && b.IsKubernetesLT("1.27") {
kcm.FeatureGates["CSIMigrationAWS"] = "true"
}
}
return nil
}