/
update.go
158 lines (131 loc) · 4.54 KB
/
update.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
// Copyright © 2019 Banzai Cloud
//
// 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 cluster
import (
"context"
"fmt"
"time"
"emperror.dev/errors"
"github.com/AlecAivazis/survey/v2"
"github.com/Masterminds/semver"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/banzaicloud/banzai-cli/.gen/pipeline"
"github.com/banzaicloud/banzai-cli/internal/cli"
clustercontext "github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/context"
"github.com/banzaicloud/banzai-cli/internal/cli/format"
"github.com/banzaicloud/banzai-cli/internal/cli/utils"
)
type updateOptions struct {
file string
wait bool
interval int
clustercontext.Context
}
// NewUpdateCommand creates a new cobra.Command for `banzai cluster update`.
func NewUpdateCommand(banzaiCli cli.Cli) *cobra.Command {
options := updateOptions{}
cmd := &cobra.Command{
Use: "update",
Aliases: []string{"u"},
Short: "Update a cluster",
Long: "Update cluster based on json stdin or interactive session",
SilenceUsage: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdate(banzaiCli, options)
},
}
flags := cmd.Flags()
flags.StringVarP(&options.file, "file", "f", "", "Cluster update descriptor file")
flags.BoolVarP(&options.wait, "wait", "w", false, "Wait for cluster update")
flags.IntVarP(&options.interval, "interval", "i", 10, "Interval in seconds for polling cluster status")
options.Context = clustercontext.NewClusterContext(cmd, banzaiCli, "update")
return cmd
}
func runUpdate(banzaiCli cli.Cli, options updateOptions) error {
client := banzaiCli.Client()
orgID := banzaiCli.Context().OrganizationID()
err := options.Init()
if err != nil {
return err
}
id := options.ClusterID()
var request pipeline.UpdateClusterRequest
if banzaiCli.Interactive() {
cluster, _, err := client.ClustersApi.GetCluster(context.Background(), orgID, id)
if err != nil {
return errors.WrapIf(err, "failed to get cluster details")
}
format.ClusterWrite(banzaiCli, cluster)
confirmed := false
err = survey.AskOne(&survey.Confirm{Message: "Do you want to UPDATE the cluster?"}, &confirmed)
if err != nil {
return errors.WrapIf(err, "failed to read cluster update confirmation")
}
if !confirmed {
return errors.New("update cancelled")
}
var nextVersion string
if cluster.Distribution == "eks" {
version, err := semver.NewVersion(cluster.Version)
if err != nil {
return errors.WrapIf(err, "failed to parse cluster version")
}
updated := version.IncMinor()
nextVersion = fmt.Sprintf("%d.%d", updated.Major(), updated.Minor())
}
err = survey.AskOne(&survey.Input{
Message: "Which version you wish to update to?",
Help: "Possible versions: [1.15, 1.16, 1.17]",
Default: nextVersion,
}, &request.Version)
if err != nil {
return errors.WrapIf(err, "failed to read cluster version")
}
} else {
filename, raw, err := utils.ReadFileOrStdin(options.file)
if err != nil {
return errors.WrapIfWithDetails(err, "failed to read", "filename", filename)
}
log.Debugf("%d bytes read", len(raw))
if err := utils.Unmarshal(raw, &request); err != nil {
return errors.WrapIf(err, "failed to unmarshal update cluster request")
}
}
log.Debugf("update request: %#v", request)
_, err = client.ClustersApi.UpdateCluster(context.Background(), orgID, id, request)
if err != nil {
cli.LogAPIError("update cluster", err, request)
return errors.WrapIf(err, "failed to update cluster")
}
log.Info("cluster is being updated")
if options.wait {
for {
cluster, _, err := client.ClustersApi.GetCluster(context.Background(), orgID, id)
if err != nil {
cli.LogAPIError("get cluster", err, request)
} else {
format.ClusterShortWrite(banzaiCli, cluster)
if cluster.Status != "UPDATING" {
return nil
}
time.Sleep(time.Duration(options.interval) * time.Second)
}
}
} else {
log.Infof("you can check its status with the command `banzai cluster get %q`", options.ClusterName())
}
return nil
}