-
Notifications
You must be signed in to change notification settings - Fork 40
/
options.go
158 lines (134 loc) · 3.43 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 main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
)
type options struct {
EnableLeaderElection bool
EnableMetricsRecorder bool
LeaderElectionNamespace string
MetricsAddr string
MetricsTlsDir string
Namespace string
PprofAddr string
ProbeAddr string
StatusReportingEnabled bool
EnableUpgradePolicyStatus bool
}
// Process retrieves values from flags, environment values,
// and secret files in order and then validates the provided
// values to determine if any are invalid.
func (o *options) Process() error {
o.parseFlags()
o.processEnv()
o.processSecrets()
o.applyValuesFromOptions()
return o.validate()
}
func (o *options) parseFlags() {
flag.BoolVar(
&o.EnableLeaderElection,
"enable-leader-election",
o.EnableLeaderElection,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.",
)
flag.BoolVar(
&o.EnableMetricsRecorder,
"enable-metrics-recorder",
o.EnableMetricsRecorder,
"Enable recording Addon Metrics",
)
flag.StringVar(
&o.LeaderElectionNamespace,
"leader-election-namspace",
o.LeaderElectionNamespace,
"The namespace in which the Leader Election resource wil be created.",
)
flag.StringVar(
&o.MetricsAddr,
"metrics-addr",
o.MetricsAddr,
"The address the metric endpoint binds to.",
)
flag.StringVar(
&o.MetricsTlsDir,
"metrics-tls-dir",
o.MetricsTlsDir,
strings.Join([]string{
"The directory containing the TLS certificate for secure metrics propagation.",
"If unset metrics will be served without TLS.",
}, " "),
)
flag.StringVar(
&o.Namespace,
"namespace",
o.Namespace,
"The namespace in which the operator is running.",
)
flag.StringVar(
&o.PprofAddr,
"pprof-addr", o.PprofAddr,
"The address the pprof web endpoint binds to.",
)
flag.StringVar(
&o.ProbeAddr,
"health-probe-bind-address",
o.ProbeAddr,
"The address the probe endpoint binds to.",
)
flag.Parse()
}
func (o *options) processEnv() {
if ns := os.Getenv("ADDON_OPERATOR_NAMESPACE"); ns != "" {
if o.Namespace == "" {
o.Namespace = ns
}
}
if ns := os.Getenv("ADDON_OPERATOR_LE_NAMESPACE"); ns != "" {
if o.LeaderElectionNamespace == "" {
o.LeaderElectionNamespace = ns
}
}
enableStatusReporting, ok := os.LookupEnv("ENABLE_STATUS_REPORTING")
if ok && enableStatusReporting == "true" {
o.StatusReportingEnabled = true
} else {
o.StatusReportingEnabled = false
}
enableUpgradePolicyStatus, ok := os.LookupEnv("ENABLE_UPGRADEPOLICY_STATUS")
if ok && enableUpgradePolicyStatus == "true" {
o.EnableUpgradePolicyStatus = true
} else {
o.EnableUpgradePolicyStatus = false
}
}
func (o *options) processSecrets() {
const (
scrtsPath = "/var/run/secrets"
inClusterNamespacePath = scrtsPath + "/kubernetes.io/serviceaccount/namespace"
)
var namespace string
if ns, err := os.ReadFile(inClusterNamespacePath); err == nil {
// Avoid applying a garbage value if an error occurred
namespace = string(ns)
}
if o.Namespace == "" {
o.Namespace = namespace
}
}
func (o *options) applyValuesFromOptions() {
if o.LeaderElectionNamespace == "" {
o.LeaderElectionNamespace = o.Namespace
}
}
var errInvalidOption = errors.New("invalid option")
func (o *options) validate() error {
if o.Namespace == "" {
return fmt.Errorf("'Namespace' must not be empty: %w", errInvalidOption)
}
return nil
}