-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathoptions.go
158 lines (143 loc) · 5.92 KB
/
options.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
package node
import (
"fmt"
"net"
"strconv"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilfeature "k8s.io/apiserver/pkg/util/feature"
kubeletoptions "k8s.io/kubernetes/cmd/kubelet/app/options"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/kubelet/apis/kubeletconfig"
kubeletcni "k8s.io/kubernetes/pkg/kubelet/network/cni"
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
cmdflags "github.com/openshift/origin/pkg/cmd/util/flags"
"github.com/openshift/origin/pkg/cmd/util/variable"
"github.com/openshift/origin/pkg/network"
)
// Build creates the core Kubernetes component configs for a given NodeConfig, or returns
// an error
func Build(options configapi.NodeConfig) (*kubeletoptions.KubeletServer, error) {
imageTemplate := variable.NewDefaultImageTemplate()
imageTemplate.Format = options.ImageConfig.Format
imageTemplate.Latest = options.ImageConfig.Latest
var path string
var fileCheckInterval int64
if options.PodManifestConfig != nil {
path = options.PodManifestConfig.Path
fileCheckInterval = options.PodManifestConfig.FileCheckIntervalSeconds
}
kubeAddressStr, kubePortStr, err := net.SplitHostPort(options.ServingInfo.BindAddress)
if err != nil {
return nil, fmt.Errorf("cannot parse node address: %v", err)
}
kubePort, err := strconv.Atoi(kubePortStr)
if err != nil {
return nil, fmt.Errorf("cannot parse node port: %v", err)
}
// Defaults are tested in TestKubeletDefaults
server, err := kubeletoptions.NewKubeletServer()
if err != nil {
return nil, fmt.Errorf("cannot create kubelet server: %v", err)
}
// Adjust defaults
server.RequireKubeConfig = true
server.KubeConfig.Default(options.MasterKubeConfig)
server.PodManifestPath = path
server.RootDirectory = options.VolumeDirectory
server.NodeIP = options.NodeIP
server.HostnameOverride = options.NodeName
server.AllowPrivileged = true
server.RegisterNode = true
server.Address = kubeAddressStr
server.Port = int32(kubePort)
server.ReadOnlyPort = 0 // no read only access
server.CAdvisorPort = 0 // no unsecured cadvisor access
server.HealthzPort = 0 // no unsecured healthz access
server.HealthzBindAddress = "" // no unsecured healthz access
server.ClusterDNS = []string{options.DNSIP}
server.ClusterDomain = options.DNSDomain
server.NetworkPluginName = options.NetworkConfig.NetworkPluginName
server.HostNetworkSources = []string{kubelettypes.ApiserverSource, kubelettypes.FileSource}
server.HostPIDSources = []string{kubelettypes.ApiserverSource, kubelettypes.FileSource}
server.HostIPCSources = []string{kubelettypes.ApiserverSource, kubelettypes.FileSource}
server.HTTPCheckFrequency = metav1.Duration{Duration: time.Duration(0)} // no remote HTTP pod creation access
server.FileCheckFrequency = metav1.Duration{Duration: time.Duration(fileCheckInterval) * time.Second}
server.KubeletFlags.ContainerRuntimeOptions.PodSandboxImage = imageTemplate.ExpandOrDie("pod")
server.MaxPods = 250
server.PodsPerCore = 10
server.CgroupDriver = "systemd"
server.DockerExecHandlerName = string(options.DockerConfig.ExecHandlerName)
server.RemoteRuntimeEndpoint = options.DockerConfig.DockerShimSocket
server.RemoteImageEndpoint = options.DockerConfig.DockerShimSocket
server.DockershimRootDirectory = options.DockerConfig.DockershimRootDirectory
// prevents kube from generating certs
server.TLSCertFile = options.ServingInfo.ServerCert.CertFile
server.TLSPrivateKeyFile = options.ServingInfo.ServerCert.KeyFile
containerized := cmdutil.Env("OPENSHIFT_CONTAINERIZED", "") == "true"
server.Containerized = containerized
// force the authentication and authorization
// Setup auth
authnTTL, err := time.ParseDuration(options.AuthConfig.AuthenticationCacheTTL)
if err != nil {
return nil, err
}
server.Authentication = kubeletconfig.KubeletAuthentication{
X509: kubeletconfig.KubeletX509Authentication{
ClientCAFile: options.ServingInfo.ClientCA,
},
Webhook: kubeletconfig.KubeletWebhookAuthentication{
Enabled: true,
CacheTTL: metav1.Duration{Duration: authnTTL},
},
Anonymous: kubeletconfig.KubeletAnonymousAuthentication{
Enabled: true,
},
}
authzTTL, err := time.ParseDuration(options.AuthConfig.AuthorizationCacheTTL)
if err != nil {
return nil, err
}
server.Authorization = kubeletconfig.KubeletAuthorization{
Mode: kubeletconfig.KubeletAuthorizationModeWebhook,
Webhook: kubeletconfig.KubeletWebhookAuthorization{
CacheAuthorizedTTL: metav1.Duration{Duration: authzTTL},
CacheUnauthorizedTTL: metav1.Duration{Duration: authzTTL},
},
}
// resolve extended arguments
// TODO: this should be done in config validation (along with the above) so we can provide
// proper errors
if err := cmdflags.Resolve(options.KubeletArguments, server.AddFlags); len(err) > 0 {
return nil, kerrors.NewAggregate(err)
}
// terminate early if feature gate is incorrect on the node
if len(server.FeatureGates) > 0 {
if err := utilfeature.DefaultFeatureGate.Set(server.FeatureGates); err != nil {
return nil, err
}
}
if utilfeature.DefaultFeatureGate.Enabled(features.RotateKubeletServerCertificate) {
// Server cert rotation is ineffective if a cert is hardcoded.
if len(server.CertDirectory) > 0 {
server.TLSCertFile = ""
server.TLSPrivateKeyFile = ""
}
}
if network.IsOpenShiftNetworkPlugin(options.NetworkConfig.NetworkPluginName) {
// SDN plugin pod setup/teardown is implemented as a CNI plugin
server.NetworkPluginName = kubeletcni.CNIPluginName
server.NetworkPluginDir = kubeletcni.DefaultNetDir
server.CNIConfDir = kubeletcni.DefaultNetDir
server.CNIBinDir = kubeletcni.DefaultCNIDir
server.HairpinMode = kubeletconfig.HairpinNone
}
return server, nil
}
func ToFlags(config *kubeletoptions.KubeletServer) []string {
server, _ := kubeletoptions.NewKubeletServer()
return cmdflags.AsArgs(config.AddFlags, server.AddFlags)
}