-
Notifications
You must be signed in to change notification settings - Fork 203
/
vars.go
267 lines (228 loc) · 9.78 KB
/
vars.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package config
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/pkg/errors"
)
const (
// #nosec
ClientSecretVar = "AZURE_CLIENT_SECRET"
SubscriptionIDVar = "AZURE_SUBSCRIPTION_ID"
TenantIDVar = "AZURE_TENANT_ID"
ClientIDVar = "AZURE_CLIENT_ID"
ClientCertificateVar = "AZURE_CLIENT_CERTIFICATE"
// #nosec
ClientCertificatePasswordVar = "AZURE_CLIENT_CERTIFICATE_PASSWORD"
targetNamespacesVar = "AZURE_TARGET_NAMESPACES"
operatorModeVar = "AZURE_OPERATOR_MODE"
syncPeriodVar = "AZURE_SYNC_PERIOD"
resourceManagerEndpointVar = "AZURE_RESOURCE_MANAGER_ENDPOINT"
resourceManagerAudienceVar = "AZURE_RESOURCE_MANAGER_AUDIENCE"
azureAuthorityHostVar = "AZURE_AUTHORITY_HOST"
podNamespaceVar = "POD_NAMESPACE"
useWorkloadIdentityAuth = "USE_WORKLOAD_IDENTITY_AUTH"
// #nosec
FederatedTokenFilePath = "/var/run/secrets/tokens/azure-identity"
)
// These are hardcoded because the init function that initializes them in azcore isn't in /cloud it's in /arm which
// we don't import.
var DefaultEndpoint = "https://management.azure.com"
var DefaultAudience = "https://management.core.windows.net/"
var DefaultAADAuthorityHost = "https://login.microsoftonline.com/"
// NOTE: Changes to documentation or available values here should be documented in Helm values.yaml as well
// Values stores configuration values that are set for the operator.
type Values struct {
// SubscriptionID is the Azure subscription the operator will use
// for ARM communication.
SubscriptionID string
// TenantID is the Azure tenantID the operator will use
// for ARM communication.
TenantID string
// ClientID is the Azure clientID the operator will use
// for ARM communication.
ClientID string
// PodNamespace is the namespace the operator pods are running in.
PodNamespace string
// OperatorMode determines whether the operator should run
// watchers, webhooks or both.
OperatorMode OperatorMode
// TargetNamespaces lists the namespaces the operator will watch
// for Azure resources (if the mode includes running watchers). If
// it's empty the operator will watch all namespaces.
TargetNamespaces []string
// SyncPeriod is the frequency at which resources are re-reconciled with Azure
// when there have been no triggering changes in the Kubernetes resources. This sync
// exists to detect and correct changes that happened in Azure that Kubernetes is not
// aware about. BE VERY CAREFUL setting this value low - even a modest number of resources
// can cause subscription level throttling if they are re-synced frequently.
// If nil, no sync is performed. Durations are specified as "1h", "15m", or "60s". See
// https://pkg.go.dev/time#ParseDuration for more details.
//
// This can be set to nil by specifying empty string for AZURE_SYNC_PERIOD explicitly in
// the config.
SyncPeriod *time.Duration
// ResourceManagerEndpoint is the Azure Resource Manager endpoint.
// If not specified, the default is the Public cloud resource manager endpoint.
// See https://docs.microsoft.com/cli/azure/manage-clouds-azure-cli#list-available-clouds for details
// about how to find available resource manager endpoints for your cloud. Note that the resource manager
// endpoint is referred to as "resourceManager" in the Azure CLI.
ResourceManagerEndpoint string
// ResourceManagerAudience is the Azure Resource Manager AAD audience.
// If not specified, the default is the Public cloud resource manager audience https://management.core.windows.net/.
// See https://docs.microsoft.com/cli/azure/manage-clouds-azure-cli#list-available-clouds for details
// about how to find available resource manager audiences for your cloud. Note that the resource manager
// audience is referred to as "activeDirectoryResourceId" in the Azure CLI.
ResourceManagerAudience string
// AzureAuthorityHost is the URL of the AAD authority. If not specified, the default
// is the AAD URL for the public cloud: https://login.microsoftonline.com/. See
// https://docs.microsoft.com/azure/active-directory/develop/authentication-national-cloud
AzureAuthorityHost string
// UseWorkloadIdentityAuth boolean is used to determine if we're using Workload Identity authentication for global credential
UseWorkloadIdentityAuth bool
}
var _ fmt.Stringer = Values{}
// Returns the configuration as a string
func (v Values) String() string {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("SubscriptionID:%s/", v.SubscriptionID))
builder.WriteString(fmt.Sprintf("TenantID:%s/", v.TenantID))
builder.WriteString(fmt.Sprintf("ClientID:%s/", v.ClientID))
builder.WriteString(fmt.Sprintf("PodNamespace:%s/", v.PodNamespace))
builder.WriteString(fmt.Sprintf("OperatorMode:%s/", v.OperatorMode))
builder.WriteString(fmt.Sprintf("TargetNamespaces:%s/", strings.Join(v.TargetNamespaces, "|")))
builder.WriteString(fmt.Sprintf("SyncPeriod:%s/", v.SyncPeriod))
builder.WriteString(fmt.Sprintf("ResourceManagerEndpoint:%s/", v.ResourceManagerEndpoint))
builder.WriteString(fmt.Sprintf("ResourceManagerAudience:%s/", v.ResourceManagerAudience))
builder.WriteString(fmt.Sprintf("AzureAuthorityHost:%s/", v.AzureAuthorityHost))
builder.WriteString(fmt.Sprintf("UseWorkloadIdentityAuth:%t", v.UseWorkloadIdentityAuth))
return builder.String()
}
// Cloud returns the cloud the configuration is using
func (v Values) Cloud() cloud.Configuration {
// Special handling if we've got all the defaults just return the official public cloud
// configuration
hasDefaultAzureAuthorityHost := v.AzureAuthorityHost == "" || v.AzureAuthorityHost == DefaultAADAuthorityHost
hasDefaultResourceManagerEndpoint := v.ResourceManagerEndpoint == "" || v.ResourceManagerEndpoint == DefaultEndpoint
hasDefaultResourceManagerAudience := v.ResourceManagerAudience == "" || v.ResourceManagerAudience == DefaultAudience
if hasDefaultResourceManagerEndpoint && hasDefaultResourceManagerAudience && hasDefaultAzureAuthorityHost {
return cloud.AzurePublic
}
// We default here too to more easily support empty Values objects
azureAuthorityHost := v.AzureAuthorityHost
resourceManagerEndpoint := v.ResourceManagerEndpoint
resourceManagerAudience := v.ResourceManagerAudience
if azureAuthorityHost == "" {
azureAuthorityHost = DefaultAADAuthorityHost
}
if resourceManagerAudience == "" {
resourceManagerAudience = DefaultAudience
}
if resourceManagerEndpoint == "" {
resourceManagerEndpoint = DefaultEndpoint
}
return cloud.Configuration{
ActiveDirectoryAuthorityHost: azureAuthorityHost,
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{
cloud.ResourceManager: {
Endpoint: resourceManagerEndpoint,
Audience: resourceManagerAudience,
},
},
}
}
// ReadFromEnvironment loads configuration values from the AZURE_*
// environment variables.
func ReadFromEnvironment() (Values, error) {
var result Values
modeValue := os.Getenv(operatorModeVar)
if modeValue == "" {
result.OperatorMode = OperatorModeBoth
} else {
mode, err := ParseOperatorMode(modeValue)
if err != nil {
return Values{}, err
}
result.OperatorMode = mode
}
var err error
result.SubscriptionID = os.Getenv(SubscriptionIDVar)
result.PodNamespace = os.Getenv(podNamespaceVar)
result.TargetNamespaces = parseTargetNamespaces(os.Getenv(targetNamespacesVar))
result.SyncPeriod, err = parseSyncPeriod()
result.ResourceManagerEndpoint = envOrDefault(resourceManagerEndpointVar, DefaultEndpoint)
result.ResourceManagerAudience = envOrDefault(resourceManagerAudienceVar, DefaultAudience)
result.AzureAuthorityHost = envOrDefault(azureAuthorityHostVar, DefaultAADAuthorityHost)
result.ClientID = os.Getenv(ClientIDVar)
result.TenantID = os.Getenv(TenantIDVar)
// Ignoring error here, as any other value or empty value means we should default to false
result.UseWorkloadIdentityAuth, _ = strconv.ParseBool(os.Getenv(useWorkloadIdentityAuth))
if err != nil {
return result, errors.Wrapf(err, "parsing %q", syncPeriodVar)
}
// Not calling validate here to support using from tests where we
// don't require consistent settings.
return result, nil
}
// ReadAndValidate loads the configuration values and checks that
// they're consistent.
func ReadAndValidate() (Values, error) {
result, err := ReadFromEnvironment()
if err != nil {
return Values{}, err
}
err = result.Validate()
if err != nil {
return Values{}, err
}
return result, nil
}
// Validate checks whether the configuration settings are consistent.
func (v Values) Validate() error {
if v.PodNamespace == "" {
return errors.Errorf("missing value for %s", podNamespaceVar)
}
if !v.OperatorMode.IncludesWatchers() && len(v.TargetNamespaces) > 0 {
return errors.Errorf("%s must include watchers to specify target namespaces", targetNamespacesVar)
}
return nil
}
// parseTargetNamespaces splits a comma-separated string into a slice
// of strings with spaces trimmed.
func parseTargetNamespaces(fromEnv string) []string {
if len(strings.TrimSpace(fromEnv)) == 0 {
return nil
}
items := strings.Split(fromEnv, ",")
// Remove any whitespace used to separate items.
for i, item := range items {
items[i] = strings.TrimSpace(item)
}
return items
}
// parseSyncPeriod parses the sync period from the environment
func parseSyncPeriod() (*time.Duration, error) {
syncPeriodStr := envOrDefault(syncPeriodVar, "1h")
if syncPeriodStr == "" {
return nil, nil
}
syncPeriod, err := time.ParseDuration(syncPeriodStr)
if err != nil {
return nil, err
}
return &syncPeriod, nil
}
// envOrDefault returns the value of the specified env variable or the default value if
// the env variable was not set.
func envOrDefault(env string, def string) string {
result, specified := os.LookupEnv(env)
if !specified {
return def
}
return result
}