forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
146 lines (115 loc) · 4.23 KB
/
upgrade.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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/acs-engine/pkg/operations/kubernetesupgrade"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
upgradeName = "upgrade"
upgradeShortDescription = "upgrades an existing Kubernetes cluster"
upgradeLongDescription = "upgrades an existing Kubernetes cluster, first replacing masters, then nodes"
)
type upgradeCmd struct {
authArgs
// user input
resourceGroupName string
deploymentDirectory string
upgradeModelFile string
containerService *api.ContainerService
apiVersion string
// derived
upgradeContainerService *api.UpgradeContainerService
upgradeAPIVersion string
client armhelpers.ACSEngineClient
nameSuffix string
}
// NewUpgradeCmd run a command to upgrade a Kubernetes cluster
func newUpgradeCmd() *cobra.Command {
uc := upgradeCmd{}
upgradeCmd := &cobra.Command{
Use: upgradeName,
Short: upgradeShortDescription,
Long: upgradeLongDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return uc.run(cmd, args)
},
}
f := upgradeCmd.Flags()
f.StringVar(&uc.resourceGroupName, "resource-group", "", "the resource group where the cluster is deployed")
f.StringVar(&uc.deploymentDirectory, "deployment-dir", "", "the location of the output from `generate`")
f.StringVar(&uc.upgradeModelFile, "upgrademodel-file", "", "file path to upgrade API model")
addAuthFlags(&uc.authArgs, f)
return upgradeCmd
}
func (uc *upgradeCmd) validate(cmd *cobra.Command, args []string) {
log.Infoln("validating...")
var err error
if uc.resourceGroupName == "" {
cmd.Usage()
log.Fatal("--resource-group must be specified")
}
// TODO(colemick): add in the cmd annotation to help enable autocompletion
if uc.upgradeModelFile == "" {
cmd.Usage()
log.Fatal("--upgrademodel-file must be specified")
}
if uc.client, err = uc.authArgs.getClient(); err != nil {
log.Error("Failed to get client:", err)
}
if uc.deploymentDirectory == "" {
cmd.Usage()
log.Fatal("--deployment-dir must be specified")
}
// load apimodel from the deployment directory
apiModelPath := path.Join(uc.deploymentDirectory, "apimodel.json")
if _, err := os.Stat(apiModelPath); os.IsNotExist(err) {
log.Fatalf("specified api model does not exist (%s)", apiModelPath)
}
uc.containerService, uc.apiVersion, err = api.LoadContainerServiceFromFile(apiModelPath, true)
if err != nil {
log.Fatalf("error parsing the api model: %s", err.Error())
}
if _, err := os.Stat(uc.upgradeModelFile); os.IsNotExist(err) {
log.Fatalf("specified upgrade model file does not exist (%s)", uc.upgradeModelFile)
}
uc.upgradeContainerService, uc.upgradeAPIVersion, err = api.LoadUpgradeContainerServiceFromFile(uc.upgradeModelFile)
if err != nil {
log.Fatalf("error parsing the upgrade api model: %s", err.Error())
}
uc.client, err = uc.authArgs.getClient()
if err != nil {
log.Fatalf("failed to get client") // TODO: cleanup
}
// Read name suffix to identify nodes in the resource group that belong
// to this cluster.
// TODO: Also update to read namesuffix from the parameters file as
// user could have specified a name suffix instead of using the default
// value generated by ACS Engine
templatePath := path.Join(uc.deploymentDirectory, "azuredeploy.json")
contents, _ := ioutil.ReadFile(templatePath)
var template interface{}
json.Unmarshal(contents, &template)
templateMap := template.(map[string]interface{})
templateParameters := templateMap["parameters"].(map[string]interface{})
nameSuffixParam := templateParameters["nameSuffix"].(map[string]interface{})
uc.nameSuffix = nameSuffixParam["defaultValue"].(string)
log.Infoln(fmt.Sprintf("Name suffix: %s", uc.nameSuffix))
}
func (uc *upgradeCmd) run(cmd *cobra.Command, args []string) error {
uc.validate(cmd, args)
upgradeCluster := kubernetesupgrade.UpgradeCluster{
Client: uc.client,
}
if err := upgradeCluster.UpgradeCluster(uc.authArgs.SubscriptionID, uc.resourceGroupName,
uc.containerService, uc.upgradeContainerService, uc.nameSuffix); err != nil {
log.Fatalf("Error upgrading cluster: %s \n", err.Error())
}
return nil
}