-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathconfig.go
120 lines (98 loc) · 3.59 KB
/
config.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
/*
Portions Copyright (c) Microsoft Corporation.
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 auth
import (
"fmt"
"os"
"strings"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
)
type cfgField struct {
val string
name string
}
// ClientConfig contains all essential information to create an Azure client.
type ClientConfig struct {
CloudName string
Location string
SubscriptionID string
ResourceManagerEndpoint string
Authorizer autorest.Authorizer
UserAgent string
}
// Config holds the configuration parsed from the --cloud-config flag
type Config struct {
Cloud string `json:"cloud" yaml:"cloud"`
Location string `json:"location" yaml:"location"`
TenantID string `json:"tenantId" yaml:"tenantId"`
SubscriptionID string `json:"subscriptionId" yaml:"subscriptionId"`
ResourceGroup string `json:"resourceGroup" yaml:"resourceGroup"`
// Managed identity for Kubelet (not to be confused with Azure cloud authorization)
KubeletIdentityClientID string `json:"kubeletIdentityClientID" yaml:"kubeletIdentityClientID"`
// Configs only for AKS
NodeResourceGroup string `json:"nodeResourceGroup" yaml:"nodeResourceGroup"`
}
// BuildAzureConfig returns a Config object for the Azure clients
func BuildAzureConfig() (*Config, error) {
cfg := &Config{}
if err := cfg.Build(); err != nil {
return nil, err
}
if err := cfg.Default(); err != nil {
return nil, err
}
if err := cfg.Validate(); err != nil {
return nil, err
}
return cfg, nil
}
func (cfg *Config) GetAzureClientConfig(authorizer autorest.Authorizer, env *azure.Environment) *ClientConfig {
azClientConfig := &ClientConfig{
Location: cfg.Location,
SubscriptionID: cfg.SubscriptionID,
ResourceManagerEndpoint: env.ResourceManagerEndpoint,
Authorizer: authorizer,
}
return azClientConfig
}
func (cfg *Config) Build() error {
// May require more than this behind the scenes: https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/azidentity/README.md#defaultazurecredential
cfg.Cloud = strings.TrimSpace(os.Getenv("ARM_CLOUD"))
cfg.Location = strings.TrimSpace(os.Getenv("LOCATION"))
cfg.ResourceGroup = strings.TrimSpace(os.Getenv("ARM_RESOURCE_GROUP"))
cfg.TenantID = strings.TrimSpace(os.Getenv("ARM_TENANT_ID"))
cfg.SubscriptionID = strings.TrimSpace(os.Getenv("ARM_SUBSCRIPTION_ID"))
cfg.NodeResourceGroup = strings.TrimSpace(os.Getenv("AZURE_NODE_RESOURCE_GROUP"))
cfg.KubeletIdentityClientID = strings.TrimSpace(os.Getenv("KUBELET_IDENTITY_CLIENT_ID"))
return nil
}
func (cfg *Config) Default() error {
// Nothing to default, for now.
return nil
}
func (cfg *Config) Validate() error {
// Setup fields and validate all of them are not empty
fields := []cfgField{
{cfg.SubscriptionID, "subscription ID"},
{cfg.NodeResourceGroup, "node resource group"},
// Even though the config doesnt use some of these,
// its good to validate they were set in the environment
}
for _, field := range fields {
if field.val == "" {
return fmt.Errorf("%s not set", field.name)
}
}
return nil
}