This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 560
/
template.go
191 lines (168 loc) · 7.23 KB
/
template.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
package engine
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/api/vlabs"
"github.com/Azure/acs-engine/pkg/helpers"
"github.com/Azure/acs-engine/test/e2e/config"
"github.com/kelseyhightower/envconfig"
)
// Config represents the configuration values of a template stored as env vars
type Config struct {
ClientID string `envconfig:"CLIENT_ID"`
ClientSecret string `envconfig:"CLIENT_SECRET"`
MasterDNSPrefix string `envconfig:"DNS_PREFIX"`
AgentDNSPrefix string `envconfig:"DNS_PREFIX"`
PublicSSHKey string `envconfig:"PUBLIC_SSH_KEY"`
WindowsAdminPasssword string `envconfig:"WINDOWS_ADMIN_PASSWORD"`
OrchestratorRelease string `envconfig:"ORCHESTRATOR_RELEASE"`
OrchestratorVersion string `envconfig:"ORCHESTRATOR_VERSION"`
OutputDirectory string `envconfig:"OUTPUT_DIR" default:"_output"`
CreateVNET bool `envconfig:"CREATE_VNET" default:"false"`
ClusterDefinitionPath string // The original template we want to use to build the cluster from.
ClusterDefinitionTemplate string // This is the template after we splice in the environment variables
GeneratedDefinitionPath string // Holds the contents of running acs-engine generate
OutputPath string // This is the root output path
DefinitionName string // Unique cluster name
GeneratedTemplatePath string // azuredeploy.json path
GeneratedParametersPath string // azuredeploy.parameters.json path
}
// Engine holds necessary information to interact with acs-engine cli
type Engine struct {
Config *Config
ClusterDefinition *api.VlabsARMContainerService // Holds the parsed ClusterDefinition
}
// ParseConfig will return a new engine config struct taking values from env vars
func ParseConfig(cwd, clusterDefinition, name string) (*Config, error) {
c := new(Config)
if err := envconfig.Process("config", c); err != nil {
return nil, err
}
clusterDefinitionTemplate := fmt.Sprintf("%s/%s.json", c.OutputDirectory, name)
generatedDefinitionPath := fmt.Sprintf("%s/%s", c.OutputDirectory, name)
c.DefinitionName = name
c.ClusterDefinitionPath = filepath.Join(cwd, clusterDefinition)
c.ClusterDefinitionTemplate = filepath.Join(cwd, clusterDefinitionTemplate)
c.OutputPath = filepath.Join(cwd, c.OutputDirectory)
c.GeneratedDefinitionPath = filepath.Join(cwd, generatedDefinitionPath)
c.GeneratedTemplatePath = filepath.Join(cwd, generatedDefinitionPath, "azuredeploy.json")
c.GeneratedParametersPath = filepath.Join(cwd, generatedDefinitionPath, "azuredeploy.parameters.json")
return c, nil
}
// Build takes a template path and will inject values based on provided environment variables
// it will then serialize the structs back into json and save it to outputPath
func Build(cfg *config.Config, subnetID string) (*Engine, error) {
config, err := ParseConfig(cfg.CurrentWorkingDir, cfg.ClusterDefinition, cfg.Name)
if err != nil {
log.Printf("Error while trying to build Engine Configuration:%s\n", err)
}
cs, err := Parse(config.ClusterDefinitionPath)
if err != nil {
return nil, err
}
if config.ClientID != "" && config.ClientSecret != "" {
cs.ContainerService.Properties.ServicePrincipalProfile = &vlabs.ServicePrincipalProfile{
ClientID: config.ClientID,
Secret: config.ClientSecret,
}
}
if config.MasterDNSPrefix != "" {
cs.ContainerService.Properties.MasterProfile.DNSPrefix = config.MasterDNSPrefix
}
if !cfg.IsKubernetes() && config.AgentDNSPrefix != "" {
for idx, pool := range cs.ContainerService.Properties.AgentPoolProfiles {
pool.DNSPrefix = fmt.Sprintf("%v-%v", config.AgentDNSPrefix, idx)
}
}
if config.PublicSSHKey != "" {
cs.ContainerService.Properties.LinuxProfile.SSH.PublicKeys[0].KeyData = config.PublicSSHKey
}
if config.WindowsAdminPasssword != "" {
cs.ContainerService.Properties.WindowsProfile.AdminPassword = config.WindowsAdminPasssword
}
// If the parsed api model input has no expressed version opinion, we check if ENV does have an opinion
if cs.ContainerService.Properties.OrchestratorProfile.OrchestratorRelease == "" &&
cs.ContainerService.Properties.OrchestratorProfile.OrchestratorVersion == "" {
// First, prefer the release string if ENV declares it
if config.OrchestratorRelease != "" {
cs.ContainerService.Properties.OrchestratorProfile.OrchestratorRelease = config.OrchestratorRelease
// Or, choose the version string if ENV declares it
} else if config.OrchestratorVersion != "" {
cs.ContainerService.Properties.OrchestratorProfile.OrchestratorVersion = config.OrchestratorVersion
// If ENV similarly has no version opinion, we will rely upon the acs-engine default
} else {
log.Println("No orchestrator version specified, will use the default.")
}
}
if config.CreateVNET {
cs.ContainerService.Properties.MasterProfile.VnetSubnetID = subnetID
for _, p := range cs.ContainerService.Properties.AgentPoolProfiles {
p.VnetSubnetID = subnetID
}
}
return &Engine{
Config: config,
ClusterDefinition: cs,
}, nil
}
// NodeCount returns the number of nodes that should be provisioned for a given cluster definition
func (e *Engine) NodeCount() int {
expectedCount := e.ClusterDefinition.Properties.MasterProfile.Count
for _, pool := range e.ClusterDefinition.Properties.AgentPoolProfiles {
expectedCount = expectedCount + pool.Count
}
return expectedCount
}
// HasLinuxAgents will return true if there is at least 1 linux agent pool
func (e *Engine) HasLinuxAgents() bool {
for _, ap := range e.ClusterDefinition.Properties.AgentPoolProfiles {
if ap.OSType == "" || ap.OSType == "Linux" {
return true
}
}
return false
}
// HasWindowsAgents will return true is there is at least 1 windows agent pool
func (e *Engine) HasWindowsAgents() bool {
for _, ap := range e.ClusterDefinition.Properties.AgentPoolProfiles {
if ap.OSType == "Windows" {
return true
}
}
return false
}
// OrchestratorVersion1Dot8AndUp will return true if the orchestrator version is 1.8 and up
func (e *Engine) OrchestratorVersion1Dot8AndUp() bool {
return e.ClusterDefinition.ContainerService.Properties.OrchestratorProfile.OrchestratorVersion >= "1.8"
}
// Write will write the cluster definition to disk
func (e *Engine) Write() error {
json, err := helpers.JSONMarshal(e.ClusterDefinition, false)
if err != nil {
log.Printf("Error while trying to serialize Container Service object to json:%s\n%+v\n", err, e.ClusterDefinition)
return err
}
err = ioutil.WriteFile(e.Config.ClusterDefinitionTemplate, json, 0777)
if err != nil {
log.Printf("Error while trying to write container service definition to file (%s):%s\n%s\n", e.Config.ClusterDefinitionTemplate, err, string(json))
}
return nil
}
// Parse takes a template path and will parse that into a api.VlabsARMContainerService
func Parse(path string) (*api.VlabsARMContainerService, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("Error while trying to read cluster definition at (%s):%s\n", path, err)
return nil, err
}
cs := api.VlabsARMContainerService{}
if err = json.Unmarshal(contents, &cs); err != nil {
log.Printf("Error while trying to unmarshal container service json:%s\n%s\n", err, string(contents))
return nil, err
}
return &cs, nil
}