forked from openyurtio/openyurt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol-plane.go
103 lines (86 loc) · 3.03 KB
/
control-plane.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
/*
Copyright 2022 The OpenYurt 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 config
import (
"fmt"
"path/filepath"
"strings"
v1 "k8s.io/api/core/v1"
fileutil "github.com/openyurtio/openyurt/pkg/util/file"
)
// ControlPlaneConfig has the information that required by node-servant config control-plane operation
type ControlPlaneConfig struct {
RunMode string
KASStaticPodPath string
}
type Runner interface {
Do() error
}
func NewControlPlaneRunner(o *ControlPlaneOptions) (Runner, error) {
switch o.RunMode {
case "pod":
return newStaticPodRunner(o.PodManifestsPath)
default:
return nil, fmt.Errorf("%s mode is not supported, only static pod mode is implemented", o.RunMode)
}
}
type staticPodRunner struct {
kasStaticPodPath string
}
func newStaticPodRunner(podManifestsPath string) (Runner, error) {
kasStaticPodPath := filepath.Join(podManifestsPath, "kube-apiserver.yaml")
if exist, _ := fileutil.FileExists(kasStaticPodPath); !exist {
return nil, fmt.Errorf("%s file is not exist", kasStaticPodPath)
}
return &staticPodRunner{
kasStaticPodPath: kasStaticPodPath,
}, nil
}
func (spr *staticPodRunner) Do() error {
var kasPodUpdated bool
// read kube-apiserver static pod
kasObj, err := fileutil.ReadObjectFromYamlFile(spr.kasStaticPodPath)
if err != nil {
return err
}
kasPod, ok := kasObj.(*v1.Pod)
if !ok {
return fmt.Errorf("manifest file(%s) is not a static pod", spr.kasStaticPodPath)
}
// remove --kubelet-preferred-address-types parameter in order to make sure kube-apiserver
// to use hostname to access nodes on edge node
for i := range kasPod.Spec.Containers {
for j := range kasPod.Spec.Containers[i].Command {
if strings.Contains(kasPod.Spec.Containers[i].Command[j], "kubelet-preferred-address-types=") {
// remove --kubelet-preferred-address-types parameter setting
kasPod.Spec.Containers[i].Command = append(kasPod.Spec.Containers[i].Command[:j], kasPod.Spec.Containers[i].Command[j+1:]...)
kasPodUpdated = true
break
}
}
}
// set dnsPolicy to ClusterFirstWithHostNet in order to make sure kube-apiserver
// will use coredns to resolve hostname. by the way, hostname of edge nodes will be resolved
// to the service(x-tunnel-server-internal-svc) clusterIP of yurt-tunnel-server
if kasPod.Spec.DNSPolicy != v1.DNSClusterFirstWithHostNet {
kasPod.Spec.DNSPolicy = v1.DNSClusterFirstWithHostNet
kasPodUpdated = true
}
// update static pod files
if kasPodUpdated {
if err := fileutil.WriteObjectToYamlFile(kasPod, spr.kasStaticPodPath); err != nil {
return err
}
}
return nil
}