forked from virtual-kubelet/virtual-kubelet
/
opts.go
146 lines (120 loc) · 3.84 KB
/
opts.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
// Copyright © 2017 The virtual-kubelet 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 root
import (
"os"
"path/filepath"
"strconv"
"time"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
)
// Defaults for root command options
const (
DefaultNodeName = "virtual-kubelet"
DefaultOperatingSystem = "Linux"
DefaultInformerResyncPeriod = 1 * time.Minute
DefaultMetricsAddr = ":10255"
DefaultListenPort = 10250 // TODO(cpuguy83)(VK1.0): Change this to an addr instead of just a port.. we should not be listening on all interfaces.
DefaultPodSyncWorkers = 10
DefaultKubeNamespace = corev1.NamespaceAll
DefaultTaintEffect = string(corev1.TaintEffectNoSchedule)
DefaultTaintKey = "virtual-kubelet.io/provider"
)
// Opts stores all the options for configuring the root virtual-kubelet command.
// It is used for setting flag values.
//
// You can set the default options by creating a new `Opts` struct and passing
// it into `SetDefaultOpts`
type Opts struct {
// Path to the kubeconfig to use to connect to the Kubernetes API server.
KubeConfigPath string
// Namespace to watch for pods and other resources
KubeNamespace string
// Sets the port to listen for requests from the Kubernetes API server
ListenPort int32
// Node name to use when creating a node in Kubernetes
NodeName string
// Operating system to run pods for
OperatingSystem string
Provider string
ProviderConfigPath string
TaintKey string
TaintEffect string
DisableTaint bool
MetricsAddr string
// Number of workers to use to handle pod notifications
PodSyncWorkers int
InformerResyncPeriod time.Duration
// Use node leases when supported by Kubernetes (instead of node status updates)
EnableNodeLease bool
TraceExporters []string
TraceSampleRate string
TraceConfig TracingExporterOptions
// Startup Timeout is how long to wait for the kubelet to start
StartupTimeout time.Duration
}
// SetDefaultOpts sets default options for unset values on the passed in option struct.
// Fields tht are already set will not be modified.
func SetDefaultOpts(c *Opts) error {
if c.OperatingSystem == "" {
c.OperatingSystem = DefaultOperatingSystem
}
if c.NodeName == "" {
c.NodeName = getEnv("DEFAULT_NODE_NAME", DefaultNodeName)
}
if c.InformerResyncPeriod == 0 {
c.InformerResyncPeriod = DefaultInformerResyncPeriod
}
if c.MetricsAddr == "" {
c.MetricsAddr = DefaultMetricsAddr
}
if c.PodSyncWorkers == 0 {
c.PodSyncWorkers = DefaultPodSyncWorkers
}
if c.TraceConfig.ServiceName == "" {
c.TraceConfig.ServiceName = DefaultNodeName
}
if c.ListenPort == 0 {
if kp := os.Getenv("KUBELET_PORT"); kp != "" {
p, err := strconv.Atoi(kp)
if err != nil {
return errors.Wrap(err, "error parsing KUBELET_PORT environment variable")
}
c.ListenPort = int32(p)
} else {
c.ListenPort = DefaultListenPort
}
}
if c.KubeNamespace == "" {
c.KubeNamespace = DefaultKubeNamespace
}
if c.TaintKey == "" {
c.TaintKey = DefaultTaintKey
}
if c.TaintEffect == "" {
c.TaintEffect = DefaultTaintEffect
}
if c.KubeConfigPath == "" {
c.KubeConfigPath = os.Getenv("KUBECONFIG")
if c.KubeConfigPath == "" {
home, _ := homedir.Dir()
if home != "" {
c.KubeConfigPath = filepath.Join(home, ".kube", "config")
}
}
}
return nil
}