-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
185 lines (159 loc) · 5.73 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
package k8s
import (
"bytes"
"time"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/cke/op"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
apiserverv1 "k8s.io/apiserver/pkg/apis/config/v1"
"k8s.io/client-go/tools/clientcmd/api"
proxyv1alpha1 "k8s.io/kube-proxy/config/v1alpha1"
schedulerv1beta1 "k8s.io/kube-scheduler/config/v1beta1"
kubeletv1beta1 "k8s.io/kubelet/config/v1beta1"
"k8s.io/utils/pointer"
)
var (
resourceEncoder runtime.Encoder
scm = runtime.NewScheme()
)
func init() {
if err := apiserverv1.AddToScheme(scm); err != nil {
panic(err)
}
if err := kubeletv1beta1.AddToScheme(scm); err != nil {
panic(err)
}
if err := schedulerv1beta1.AddToScheme(scm); err != nil {
panic(err)
}
if err := proxyv1alpha1.AddToScheme(scm); err != nil {
panic(err)
}
resourceEncoder = k8sjson.NewSerializerWithOptions(k8sjson.DefaultMetaFactory, scm, scm,
k8sjson.SerializerOptions{Yaml: true})
}
func encodeToYAML(obj runtime.Object) ([]byte, error) {
unst := &unstructured.Unstructured{}
if err := scm.Convert(obj, unst, nil); err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if err := resourceEncoder.Encode(unst, buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func controllerManagerKubeconfig(cluster string, ca, clientCrt, clientKey string) *api.Config {
return cke.Kubeconfig(cluster, "system:kube-controller-manager", ca, clientCrt, clientKey)
}
func schedulerKubeconfig(cluster string, ca, clientCrt, clientKey string) *api.Config {
return cke.Kubeconfig(cluster, "system:kube-scheduler", ca, clientCrt, clientKey)
}
// GenerateSchedulerConfiguration generates scheduler configuration.
// `params` must be validated beforehand.
func GenerateSchedulerConfiguration(params cke.SchedulerParams) *schedulerv1beta1.KubeSchedulerConfiguration {
// default values
base := schedulerv1beta1.KubeSchedulerConfiguration{}
c, err := params.MergeConfig(&base)
if err != nil {
panic(err)
}
// forced values
c.ClientConnection.Kubeconfig = op.SchedulerKubeConfigPath
c.LeaderElection.LeaderElect = pointer.BoolPtr(true)
return c
}
// GenerateProxyConfiguration generates proxy configuration.
// `params` must be validated beforehand.
func GenerateProxyConfiguration(params cke.ProxyParams, n *cke.Node) *proxyv1alpha1.KubeProxyConfiguration {
// default values
base := proxyv1alpha1.KubeProxyConfiguration{
HostnameOverride: n.Nodename(),
MetricsBindAddress: "0.0.0.0",
Conntrack: proxyv1alpha1.KubeProxyConntrackConfiguration{
TCPEstablishedTimeout: &metav1.Duration{Duration: 24 * time.Hour},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 1 * time.Hour},
},
}
c, err := params.MergeConfig(&base)
if err != nil {
panic(err)
}
// forced values
c.ClientConnection.Kubeconfig = proxyKubeconfigPath
return c
}
func proxyKubeconfig(cluster, ca, clientCrt, clientKey, server string) *api.Config {
if server == "" {
return cke.Kubeconfig(cluster, "system:kube-proxy", ca, clientCrt, clientKey)
}
return cke.UserKubeconfig(cluster, "system:kube-proxy", ca, clientCrt, clientKey, server)
}
func kubeletKubeconfig(cluster string, n *cke.Node, caPath, certPath, keyPath string) *api.Config {
cfg := api.NewConfig()
c := api.NewCluster()
c.Server = "https://localhost:16443"
c.CertificateAuthority = caPath
cfg.Clusters[cluster] = c
auth := api.NewAuthInfo()
auth.ClientCertificate = certPath
auth.ClientKey = keyPath
user := "system:node:" + n.Nodename()
cfg.AuthInfos[user] = auth
ctx := api.NewContext()
ctx.AuthInfo = user
ctx.Cluster = cluster
cfg.Contexts["default"] = ctx
cfg.CurrentContext = "default"
return cfg
}
// GenerateKubeletConfiguration generates kubelet configuration.
// `params` must be validated beforehand.
func GenerateKubeletConfiguration(params cke.KubeletParams, nodeAddress string, running *kubeletv1beta1.KubeletConfiguration) *kubeletv1beta1.KubeletConfiguration {
caPath := op.K8sPKIPath("ca.crt")
tlsCertPath := op.K8sPKIPath("kubelet.crt")
tlsKeyPath := op.K8sPKIPath("kubelet.key")
// default values
base := &kubeletv1beta1.KubeletConfiguration{
ClusterDomain: "cluster.local",
RuntimeRequestTimeout: metav1.Duration{Duration: 15 * time.Minute},
HealthzBindAddress: "0.0.0.0",
VolumePluginDir: "/opt/volume/bin",
}
// This won't raise an error because of prior validation
c, err := params.MergeConfig(base)
if err != nil {
panic(err)
}
// forced values
c.TLSCertFile = tlsCertPath
c.TLSPrivateKeyFile = tlsKeyPath
c.Authentication = kubeletv1beta1.KubeletAuthentication{
X509: kubeletv1beta1.KubeletX509Authentication{ClientCAFile: caPath},
Webhook: kubeletv1beta1.KubeletWebhookAuthentication{Enabled: pointer.BoolPtr(true)},
}
c.Authorization = kubeletv1beta1.KubeletAuthorization{Mode: kubeletv1beta1.KubeletAuthorizationModeWebhook}
c.ClusterDNS = []string{nodeAddress}
if running != nil {
// Keep the running configurations while the node is running.
// All these fields are described as:
// This field should not be updated without a full node
// reboot. It is safest to keep this value the same as the local config.
//
// ref: https://pkg.go.dev/k8s.io/kubelet/config/v1beta1#KubeletConfiguration
c.KubeletCgroups = running.KubeletCgroups
c.SystemCgroups = running.SystemCgroups
c.CgroupRoot = running.CgroupRoot
c.CgroupsPerQOS = running.CgroupsPerQOS
c.CgroupDriver = running.CgroupDriver
c.CPUManagerPolicy = running.CPUManagerPolicy
c.TopologyManagerPolicy = running.TopologyManagerPolicy
c.QOSReserved = running.QOSReserved
c.SystemReservedCgroup = running.SystemReservedCgroup
c.KubeReservedCgroup = running.KubeReservedCgroup
}
return c
}