/
apiserver.go
209 lines (186 loc) · 6.96 KB
/
apiserver.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
/*
Copyright 2021 k0s 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 controller
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"path"
"github.com/sirupsen/logrus"
"github.com/k0sproject/k0s/internal/util"
config "github.com/k0sproject/k0s/pkg/apis/v1beta1"
"github.com/k0sproject/k0s/pkg/assets"
"github.com/k0sproject/k0s/pkg/component"
"github.com/k0sproject/k0s/pkg/constant"
"github.com/k0sproject/k0s/pkg/supervisor"
)
// APIServer implement the component interface to run kube api
type APIServer struct {
ClusterConfig *config.ClusterConfig
K0sVars constant.CfgVars
LogLevel string
Storage component.Component
EnableKonnectivity bool
gid int
supervisor supervisor.Supervisor
uid int
}
var apiDefaultArgs = map[string]string{
"allow-privileged": "true",
"enable-admission-plugins": "NodeRestriction",
"requestheader-extra-headers-prefix": "X-Remote-Extra-",
"requestheader-group-headers": "X-Remote-Group",
"requestheader-username-headers": "X-Remote-User",
"secure-port": "6443",
}
const egressSelectorConfigTemplate = `
apiVersion: apiserver.k8s.io/v1beta1
kind: EgressSelectorConfiguration
egressSelections:
- name: cluster
connection:
proxyProtocol: GRPC
transport:
uds:
udsName: {{ .UDSName }}
`
type egressSelectorConfig struct {
UDSName string
}
// Init extracts needed binaries
func (a *APIServer) Init() error {
var err error
a.uid, err = util.GetUID(constant.ApiserverUser)
if err != nil {
logrus.Warning(fmt.Errorf("Running kube-apiserver as root: %w", err))
}
return assets.Stage(a.K0sVars.BinDir, "kube-apiserver", constant.BinDirMode)
}
// Run runs kube api
func (a *APIServer) Run() error {
logrus.Info("Starting kube-apiserver")
args := map[string]string{
"advertise-address": a.ClusterConfig.Spec.API.Address,
"secure-port": fmt.Sprintf("%d", a.ClusterConfig.Spec.API.Port),
"authorization-mode": "Node,RBAC",
"client-ca-file": path.Join(a.K0sVars.CertRootDir, "ca.crt"),
"enable-bootstrap-token-auth": "true",
"kubelet-client-certificate": path.Join(a.K0sVars.CertRootDir, "apiserver-kubelet-client.crt"),
"kubelet-client-key": path.Join(a.K0sVars.CertRootDir, "apiserver-kubelet-client.key"),
"kubelet-preferred-address-types": "InternalIP,ExternalIP,Hostname",
"proxy-client-cert-file": path.Join(a.K0sVars.CertRootDir, "front-proxy-client.crt"),
"proxy-client-key-file": path.Join(a.K0sVars.CertRootDir, "front-proxy-client.key"),
"requestheader-allowed-names": "front-proxy-client",
"requestheader-client-ca-file": path.Join(a.K0sVars.CertRootDir, "front-proxy-ca.crt"),
"service-account-key-file": path.Join(a.K0sVars.CertRootDir, "sa.pub"),
"service-cluster-ip-range": a.ClusterConfig.Spec.Network.BuildServiceCIDR(a.ClusterConfig.Spec.API.Address),
"tls-cert-file": path.Join(a.K0sVars.CertRootDir, "server.crt"),
"tls-private-key-file": path.Join(a.K0sVars.CertRootDir, "server.key"),
"service-account-signing-key-file": path.Join(a.K0sVars.CertRootDir, "sa.key"),
"service-account-issuer": "api",
"insecure-port": "0",
"profiling": "false",
"v": a.LogLevel,
"kubelet-certificate-authority": path.Join(a.K0sVars.CertRootDir, "ca.crt"),
}
if a.EnableKonnectivity {
err := a.writeKonnectivityConfig()
if err != nil {
return err
}
args["egress-selector-config-file"] = path.Join(a.K0sVars.DataDir, "konnectivity.conf")
args["api-audiences"] = "system:konnectivity-server"
}
for name, value := range a.ClusterConfig.Spec.API.ExtraArgs {
if args[name] != "" && name != "profiling" {
return fmt.Errorf("cannot override apiserver flag: %s", name)
}
args[name] = value
}
a.ClusterConfig.Spec.Network.DualStack.EnableDualStackFeatureGate(args)
for name, value := range apiDefaultArgs {
if args[name] == "" {
args[name] = value
}
}
if a.ClusterConfig.Spec.API.ExternalAddress != "" {
args["endpoint-reconciler-type"] = "none"
}
apiServerArgs := []string{}
for name, value := range args {
apiServerArgs = append(apiServerArgs, fmt.Sprintf("--%s=%s", name, value))
}
a.supervisor = supervisor.Supervisor{
Name: "kube-apiserver",
BinPath: assets.BinPath("kube-apiserver", a.K0sVars.BinDir),
RunDir: a.K0sVars.RunDir,
DataDir: a.K0sVars.DataDir,
Args: apiServerArgs,
UID: a.uid,
GID: a.gid,
}
switch a.ClusterConfig.Spec.Storage.Type {
case config.KineStorageType:
a.supervisor.Args = append(a.supervisor.Args,
fmt.Sprintf("--etcd-servers=unix://%s", a.K0sVars.KineSocketPath)) // kine endpoint
case config.EtcdStorageType:
a.supervisor.Args = append(a.supervisor.Args,
"--etcd-servers=https://127.0.0.1:2379",
fmt.Sprintf("--etcd-cafile=%s", path.Join(a.K0sVars.CertRootDir, "etcd/ca.crt")),
fmt.Sprintf("--etcd-certfile=%s", path.Join(a.K0sVars.CertRootDir, "apiserver-etcd-client.crt")),
fmt.Sprintf("--etcd-keyfile=%s", path.Join(a.K0sVars.CertRootDir, "apiserver-etcd-client.key")))
default:
return fmt.Errorf("invalid storage type: %s", a.ClusterConfig.Spec.Storage.Type)
}
return a.supervisor.Supervise()
}
func (a *APIServer) writeKonnectivityConfig() error {
tw := util.TemplateWriter{
Name: "konnectivity",
Template: egressSelectorConfigTemplate,
Data: egressSelectorConfig{
UDSName: path.Join(a.K0sVars.KonnectivitySocketDir, "konnectivity-server.sock"),
},
Path: path.Join(a.K0sVars.DataDir, "konnectivity.conf"),
}
err := tw.Write()
if err != nil {
return fmt.Errorf("failed to write konnectivity config: %w", err)
}
return nil
}
// Stop stops APIServer
func (a *APIServer) Stop() error {
return a.supervisor.Stop()
}
// Health-check interface
func (a *APIServer) Healthy() error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/readyz?verbose", a.ClusterConfig.Spec.API.Port))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
logrus.Debugf("api server readyz output:\n %s", string(body))
}
return fmt.Errorf("expected 200 for api server ready check, got %d", resp.StatusCode)
}
return nil
}