Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/clusters-update.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"encoding/base64"
10
"fmt"
11
"io"
12
"io/ioutil"
13
"path"
14
"strconv"
15
"strings"
16
17
"github.com/spf13/cobra"
18
19
"github.com/gitpod-io/gitpod/common-go/log"
20
"github.com/gitpod-io/gitpod/ws-manager-bridge/api"
21
)
22
23
// clustersUpdateCmd represents the clustersUpdateCmd command
24
var clustersUpdateCmd = &cobra.Command{
25
Use: "update --name [cluster name]",
26
Short: "Update a cluster",
27
Args: cobra.ExactArgs(1),
28
}
29
30
var clustersUpdateScoreCmd = &cobra.Command{
31
Use: "score <value>",
32
Short: "Update a cluster's score",
33
Args: cobra.ExactArgs(1),
34
Run: func(cmd *cobra.Command, args []string) {
35
name := getClusterName()
36
37
ctx, cancel := context.WithCancel(context.Background())
38
defer cancel()
39
40
conn, client, err := getClustersClient(ctx)
41
if err != nil {
42
log.WithError(err).Fatal("cannot connect")
43
}
44
defer conn.Close()
45
46
value, err := strconv.Atoi(args[0])
47
if err != nil {
48
log.Fatal(err)
49
}
50
request := &api.UpdateRequest{Name: name, Property: &api.UpdateRequest_Score{Score: int32(value)}}
51
52
_, err = client.Update(ctx, request)
53
if err != nil && err != io.EOF {
54
log.Fatal(err)
55
}
56
57
fmt.Printf("cluster '%s' updated with score=%s\n", name, args[0])
58
},
59
}
60
61
var clustersUpdateMaxScoreCmd = &cobra.Command{
62
Use: "max-score <value>",
63
Short: "Update a cluster's max score",
64
Args: cobra.ExactArgs(1),
65
Run: func(cmd *cobra.Command, args []string) {
66
name := getClusterName()
67
68
ctx, cancel := context.WithCancel(context.Background())
69
defer cancel()
70
71
conn, client, err := getClustersClient(ctx)
72
if err != nil {
73
log.WithError(err).Fatal("cannot connect")
74
}
75
defer conn.Close()
76
77
value, err := strconv.Atoi(args[0])
78
if err != nil {
79
log.Fatal(err)
80
}
81
request := &api.UpdateRequest{Name: name, Property: &api.UpdateRequest_MaxScore{MaxScore: int32(value)}}
82
83
_, err = client.Update(ctx, request)
84
if err != nil && err != io.EOF {
85
log.Fatal(err)
86
}
87
88
fmt.Printf("cluster '%s' updated with max_score=%s\n", name, args[0])
89
},
90
}
91
92
var clustersUpdateAdmissionConstraintCmd = &cobra.Command{
93
Use: "admission-constraint add|remove has-feature-preview|has-permission=<permission>",
94
Short: "Updates a cluster's admission constraints",
95
Args: cobra.ExactArgs(2),
96
Run: func(cmd *cobra.Command, args []string) {
97
name := getClusterName()
98
99
var add bool
100
switch args[0] {
101
case "add":
102
add = true
103
case "remove":
104
add = false
105
default:
106
log.Fatalf("must be add or remove instead of \"%s\"", args[0])
107
}
108
109
request := &api.UpdateRequest{Name: name}
110
if args[1] == "has-feature-preview" {
111
request.Property = &api.UpdateRequest_AdmissionConstraint{
112
AdmissionConstraint: &api.ModifyAdmissionConstraint{
113
Add: add,
114
Constraint: &api.AdmissionConstraint{
115
Constraint: &api.AdmissionConstraint_HasFeaturePreview{},
116
},
117
},
118
}
119
} else if strings.HasPrefix(args[1], "has-permission=") {
120
request.Property = &api.UpdateRequest_AdmissionConstraint{
121
AdmissionConstraint: &api.ModifyAdmissionConstraint{
122
Add: add,
123
Constraint: &api.AdmissionConstraint{
124
Constraint: &api.AdmissionConstraint_HasPermission_{
125
HasPermission: &api.AdmissionConstraint_HasPermission{
126
Permission: strings.TrimPrefix(args[1], "has-permission="),
127
},
128
},
129
},
130
},
131
}
132
} else {
133
log.Fatalf("unknown constraint: %s", args[1])
134
}
135
136
ctx, cancel := context.WithCancel(context.Background())
137
defer cancel()
138
139
conn, client, err := getClustersClient(ctx)
140
if err != nil {
141
log.WithError(err).Fatal("cannot connect")
142
}
143
defer conn.Close()
144
145
_, err = client.Update(ctx, request)
146
if err != nil && err != io.EOF {
147
log.Fatal(err)
148
}
149
150
fmt.Printf("cluster '%s' updated with admission constraint %s\n", name, request.GetAdmissionConstraint())
151
},
152
}
153
154
var clustersUpdateTLSConfigCmd = &cobra.Command{
155
Use: "tls",
156
Short: "Updates a cluster's TLS configuration",
157
Args: cobra.ExactArgs(0),
158
Run: func(cmd *cobra.Command, args []string) {
159
name := getClusterName()
160
161
ctx, cancel := context.WithCancel(context.Background())
162
defer cancel()
163
164
conn, client, err := getClustersClient(ctx)
165
if err != nil {
166
log.WithError(err).Fatal("cannot connect")
167
}
168
defer conn.Close()
169
170
tlsPath, err := cmd.Flags().GetString("tls-path")
171
if err != nil {
172
log.Fatal(err)
173
}
174
if tlsPath == "" {
175
log.Fatal("tls-path is required")
176
}
177
178
readFileToBase64Str := func(filename string) string {
179
filepath := path.Join(tlsPath, filename)
180
content, err := ioutil.ReadFile(filepath)
181
if err != nil {
182
log.WithError(err).Fatalf("unable to read from: '%s'", filepath)
183
}
184
return base64.StdEncoding.EncodeToString(content)
185
}
186
request := &api.UpdateRequest{Name: name, Property: &api.UpdateRequest_Tls{Tls: &api.TlsConfig{
187
Ca: readFileToBase64Str("ca.crt"),
188
Crt: readFileToBase64Str("tls.crt"),
189
Key: readFileToBase64Str("tls.key"),
190
}}}
191
192
_, err = client.Update(ctx, request)
193
if err != nil && err != io.EOF {
194
log.Fatal(err)
195
}
196
197
fmt.Printf("cluster '%s' tls config updated\n", name)
198
},
199
}
200
201
func init() {
202
clustersCmd.AddCommand(clustersUpdateCmd)
203
clustersUpdateCmd.AddCommand(clustersUpdateScoreCmd)
204
clustersUpdateCmd.AddCommand(clustersUpdateMaxScoreCmd)
205
clustersUpdateCmd.AddCommand(clustersUpdateAdmissionConstraintCmd)
206
clustersUpdateTLSConfigCmd.Flags().String("tls-path", "", "folder containing the ws cluster's ca.crt, tls.crt and tls.key")
207
clustersUpdateCmd.AddCommand(clustersUpdateTLSConfigCmd)
208
}
209
210