forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgradecluster.go
211 lines (183 loc) · 7.4 KB
/
upgradecluster.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
package kubernetesupgrade
import (
"encoding/json"
"fmt"
"path"
"strconv"
"strings"
"github.com/Azure/acs-engine/pkg/acsengine"
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/acs-engine/pkg/i18n"
"github.com/Azure/azure-sdk-for-go/arm/compute"
log "github.com/Sirupsen/logrus"
"github.com/satori/go.uuid"
)
// ClusterTopology contains resources of the cluster the upgrade operation
// is targeting
type ClusterTopology struct {
DataModel *api.ContainerService
ResourceGroup string
NameSuffix string
AgentPools map[string]*AgentPoolTopology
MasterVMs *[]compute.VirtualMachine
UpgradedMasterVMs *[]compute.VirtualMachine
}
// AgentPoolTopology contains agent VMs in a single pool
type AgentPoolTopology struct {
Identifier *string
Name *string
AgentVMs *[]compute.VirtualMachine
UpgradedAgentVMs *[]compute.VirtualMachine
}
// UpgradeCluster upgrades a cluster with Orchestrator version X
// (or X.X or X.X.X) to version y (or Y.Y or X.X.X). RIght now
// upgrades are supported for Kubernetes cluster only.
type UpgradeCluster struct {
Translator *i18n.Translator
ClusterTopology
Client armhelpers.ACSEngineClient
UpgradeModel *api.UpgradeContainerService
}
// MasterVMNamePrefix is the prefix for all master VM names for Kubernetes clusters
const MasterVMNamePrefix = "k8s-master-"
// UpgradeCluster runs the workflow to upgrade a Kubernetes cluster.
// UpgradeContainerService contains target state of the cluster that
// the operation will drive towards.
func (uc *UpgradeCluster) UpgradeCluster(subscriptionID uuid.UUID, resourceGroup string,
cs *api.ContainerService, ucs *api.UpgradeContainerService, nameSuffix string) error {
uc.ClusterTopology = ClusterTopology{}
uc.ResourceGroup = resourceGroup
uc.DataModel = cs
uc.NameSuffix = nameSuffix
uc.MasterVMs = &[]compute.VirtualMachine{}
uc.UpgradedMasterVMs = &[]compute.VirtualMachine{}
uc.AgentPools = make(map[string]*AgentPoolTopology)
uc.UpgradeModel = ucs
if err := uc.getClusterNodeStatus(subscriptionID, resourceGroup); err != nil {
return uc.Translator.Errorf("Error while querying ARM for resources: %+v", err)
}
switch ucs.OrchestratorProfile.OrchestratorVersion {
case api.Kubernetes162:
log.Infoln(fmt.Sprintf("Upgrading to Kubernetes 1.6.2"))
upgrader := Kubernetes162upgrader{
Translator: uc.Translator,
}
upgrader.ClusterTopology = uc.ClusterTopology
upgrader.Client = uc.Client
if err := upgrader.RunUpgrade(); err != nil {
return err
}
default:
return uc.Translator.Errorf("Upgrade to Kubernetes version: %s is not supported from version: %s",
ucs.OrchestratorProfile.OrchestratorVersion,
uc.DataModel.Properties.OrchestratorProfile.OrchestratorVersion)
}
log.Infoln(fmt.Sprintf("Cluster upraded successfully to Kubernetes version: %s",
ucs.OrchestratorProfile.OrchestratorVersion))
return nil
}
func (uc *UpgradeCluster) getClusterNodeStatus(subscriptionID uuid.UUID, resourceGroup string) error {
vmListResult, err := uc.Client.ListVirtualMachines(resourceGroup)
if err != nil {
return err
}
orchestratorTypeVersion := fmt.Sprintf("%s:%s", uc.DataModel.Properties.OrchestratorProfile.OrchestratorType,
uc.DataModel.Properties.OrchestratorProfile.OrchestratorVersion)
targetOrchestratorTypeVersion := fmt.Sprintf("%s:%s", uc.UpgradeModel.OrchestratorProfile.OrchestratorType,
uc.UpgradeModel.OrchestratorProfile.OrchestratorVersion)
for _, vm := range *vmListResult.Value {
if vm.Tags == nil {
log.Infoln(fmt.Sprintf("No tags found for VM: %s skipping.", *vm.Name))
continue
}
vmOrchestratorTypeAndVersion := *(*vm.Tags)["orchestrator"]
if vmOrchestratorTypeAndVersion == orchestratorTypeVersion {
if strings.Contains(*(vm.Name), MasterVMNamePrefix) {
if !strings.Contains(*(vm.Name), uc.NameSuffix) {
log.Infoln(fmt.Sprintf("Skipping VM: %s for upgrade as it does not belong to cluster with expected name suffix: %s",
*vm.Name, uc.NameSuffix))
continue
}
log.Infoln(fmt.Sprintf("Master VM name: %s, orchestrator: %s (MasterVMs)", *vm.Name, vmOrchestratorTypeAndVersion))
*uc.MasterVMs = append(*uc.MasterVMs, vm)
} else {
uc.addVMToAgentPool(vm, true)
}
} else if vmOrchestratorTypeAndVersion == targetOrchestratorTypeVersion {
if !strings.Contains(*(vm.Name), uc.NameSuffix) {
log.Infoln(fmt.Sprintf("Not adding VM: %s to upgraded list as it does not belong to cluster with expected name suffix: %s",
*vm.Name, uc.NameSuffix))
continue
}
if strings.Contains(*(vm.Name), MasterVMNamePrefix) {
log.Infoln(fmt.Sprintf("Master VM name: %s, orchestrator: %s (UpgradedMasterVMs)", *vm.Name, vmOrchestratorTypeAndVersion))
*uc.UpgradedMasterVMs = append(*uc.UpgradedMasterVMs, vm)
} else {
uc.addVMToAgentPool(vm, false)
}
}
}
return nil
}
func (uc *UpgradeCluster) addVMToAgentPool(vm compute.VirtualMachine, isUpgradableVM bool) error {
var poolIdentifier string
var poolPrefix string
var err error
if vm.StorageProfile.OsDisk.OsType == compute.Linux {
_, poolIdentifier, poolPrefix, _, err = armhelpers.LinuxVMNameParts(*vm.Name)
if err != nil {
log.Errorln(err)
return err
}
} else if vm.StorageProfile.OsDisk.OsType == compute.Windows {
poolPrefix, acsStr, poolIndex, _, err := armhelpers.WindowsVMNameParts(*vm.Name)
if err != nil {
log.Errorln(err)
return err
}
poolIdentifier = poolPrefix + acsStr + strconv.Itoa(poolIndex)
}
if !strings.Contains(uc.NameSuffix, poolPrefix) {
log.Infoln(fmt.Sprintf("Skipping VM: %s for upgrade as it does not belong to cluster with expected name suffix: %s",
*vm.Name, uc.NameSuffix))
return nil
}
if uc.AgentPools[poolIdentifier] == nil {
uc.AgentPools[poolIdentifier] =
&AgentPoolTopology{&poolIdentifier, (*vm.Tags)["poolName"], &[]compute.VirtualMachine{}, &[]compute.VirtualMachine{}}
}
if isUpgradableVM {
log.Infoln(fmt.Sprintf("Adding Agent VM: %s, orchestrator: %s to pool: %s (AgentVMs)",
*vm.Name, *(*vm.Tags)["orchestrator"], poolIdentifier))
*uc.AgentPools[poolIdentifier].AgentVMs = append(*uc.AgentPools[poolIdentifier].AgentVMs, vm)
} else {
log.Infoln(fmt.Sprintf("Adding Agent VM: %s, orchestrator: %s to pool: %s (UpgradedAgentVMs)",
*vm.Name, *(*vm.Tags)["orchestrator"], poolIdentifier))
*uc.AgentPools[poolIdentifier].UpgradedAgentVMs = append(*uc.AgentPools[poolIdentifier].UpgradedAgentVMs, vm)
}
return nil
}
// WriteTemplate writes upgrade template to a folder
func WriteTemplate(
translator *i18n.Translator,
upgradeContainerService *api.ContainerService,
templateMap map[string]interface{}, parametersMap map[string]interface{}) {
updatedTemplateJSON, _ := json.Marshal(templateMap)
parametersJSON, _ := json.Marshal(parametersMap)
templateapp, err := acsengine.PrettyPrintArmTemplate(string(updatedTemplateJSON))
if err != nil {
log.Fatalf("error pretty printing template: %s \n", err.Error())
}
parametersapp, e := acsengine.PrettyPrintJSON(string(parametersJSON))
if e != nil {
log.Fatalf("error pretty printing template parameters: %s \n", e.Error())
}
outputDirectory := path.Join("_output", upgradeContainerService.Properties.MasterProfile.DNSPrefix, "Upgrade")
writer := &acsengine.ArtifactWriter{
Translator: translator,
}
if err := writer.WriteTLSArtifacts(upgradeContainerService, "vlabs", templateapp, parametersapp, outputDirectory, false, false); err != nil {
log.Fatalf("error writing artifacts: %s \n", err.Error())
}
}