-
Notifications
You must be signed in to change notification settings - Fork 11
/
cluster.go
316 lines (264 loc) · 8.32 KB
/
cluster.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2022 The Codefresh Authors.
//
// 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 commands
import (
"context"
"fmt"
"os"
"strings"
"github.com/codefresh-io/cli-v2/pkg/log"
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"
cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
"github.com/Masterminds/semver/v3"
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
"github.com/juju/ansiterm"
"github.com/spf13/cobra"
kusttypes "sigs.k8s.io/kustomize/api/types"
)
type (
ClusterAddOptions struct {
runtimeName string
kubeContext string
kubeconfig string
dryRun bool
kubeFactory kube.Factory
}
ClusterRemoveOptions struct {
server string
runtimeName string
}
)
var minAddClusterSupportedVersion = semver.MustParse("0.0.283")
func NewClusterCommand() *cobra.Command {
cmd := &cobra.Command{
Hidden: true, // until app-proxy is working correctly
Use: "cluster",
Short: "Manage clusters of Codefresh runtimes",
PersistentPreRunE: cfConfig.RequireAuthentication,
Args: cobra.NoArgs, // Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
exit(1)
},
}
cmd.AddCommand(NewClusterAddCommand())
cmd.AddCommand(NewClusterRemoveCommand())
cmd.AddCommand(NewClusterListCommand())
return cmd
}
func NewClusterAddCommand() *cobra.Command {
var (
opts ClusterAddOptions
err error
)
cmd := &cobra.Command{
Use: "add RUNTIME_NAME",
Short: "Add a cluster to a given runtime",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`<BIN> cluster add my-runtime --context my-context`),
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error
ctx := cmd.Context()
opts.runtimeName, err = ensureRuntimeName(ctx, args)
if err != nil {
return err
}
opts.kubeContext, err = ensureKubeContextName(cmd.Flag("context"), cmd.Flag("kubeconfig"))
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
return runClusterAdd(cmd.Context(), &opts)
},
}
cmd.Flags().BoolVar(&opts.dryRun, "dry-run", false, "")
opts.kubeFactory = kube.AddFlags(cmd.Flags())
die(err)
return cmd
}
func runClusterAdd(ctx context.Context, opts *ClusterAddOptions) error {
runtime, err := cfConfig.NewClient().V2().Runtime().Get(ctx, opts.runtimeName)
if err != nil {
return err
}
if runtime.RuntimeVersion == nil {
return fmt.Errorf("runtime \"%s\" has no version", opts.runtimeName)
}
version := semver.MustParse(*runtime.RuntimeVersion)
if version.LessThan(minAddClusterSupportedVersion) {
return fmt.Errorf("runtime \"%s\" does not support this command. Minimal required version is %s", opts.runtimeName, minAddClusterSupportedVersion)
}
if runtime.IngressHost == nil {
return fmt.Errorf("runtime \"%s\" is missing an ingress URL", opts.runtimeName)
}
ingressUrl := *runtime.IngressHost
server, err := util.KubeServerByContextName(opts.kubeContext, opts.kubeconfig)
if err != nil {
return fmt.Errorf("failed getting server for context \"%s\": %w", opts.kubeContext, err)
}
csdpToken := cfConfig.GetCurrentContext().Token
k := createAddClusterKustomization(ingressUrl, opts.kubeContext, server, csdpToken, *runtime.RuntimeVersion)
manifests, err := kustutil.BuildKustomization(k)
if err != nil {
return fmt.Errorf("failed building kustomization:%w", err)
}
if opts.dryRun {
fmt.Println(string(manifests))
return nil
}
return opts.kubeFactory.Apply(ctx, manifests)
}
func createAddClusterKustomization(ingressUrl, contextName, server, csdpToken, version string) *kusttypes.Kustomization {
resourceUrl := store.AddClusterDefURL
if strings.HasPrefix(resourceUrl, "http") {
resourceUrl = fmt.Sprintf("%s?ref=v%s", resourceUrl, version)
}
k := &kusttypes.Kustomization{
ConfigMapGenerator: []kusttypes.ConfigMapArgs{
{
GeneratorArgs: kusttypes.GeneratorArgs{
Namespace: "kube-system",
Name: "csdp-add-cluster-cm",
Behavior: "merge",
KvPairSources: kusttypes.KvPairSources{
LiteralSources: []string{
fmt.Sprintf("ingressUrl=" + ingressUrl),
fmt.Sprintf("contextName=" + contextName),
fmt.Sprintf("server=" + server),
},
},
},
},
},
SecretGenerator: []kusttypes.SecretArgs{
{
GeneratorArgs: kusttypes.GeneratorArgs{
Namespace: "kube-system",
Name: "csdp-add-cluster-secret",
Behavior: "merge",
KvPairSources: kusttypes.KvPairSources{
LiteralSources: []string{
fmt.Sprintf("csdpToken=" + csdpToken),
},
},
},
},
},
Resources: []string{
resourceUrl,
},
}
k.FixKustomizationPostUnmarshalling()
util.Die(k.FixKustomizationPreMarshalling())
return k
}
func NewClusterRemoveCommand() *cobra.Command {
var (
opts ClusterRemoveOptions
)
cmd := &cobra.Command{
Use: "remove RUNTIME_NAME",
Short: "Removes a cluster from a given runtime",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`<BIN> cluster remove my-runtime --server-url my-server-url`),
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error
ctx := cmd.Context()
opts.runtimeName, err = ensureRuntimeName(ctx, args)
if err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runClusterRemove(cmd.Context(), &opts)
},
}
cmd.Flags().StringVar(&opts.server, "server-url", "", "The cluster's server url")
util.Die(cobra.MarkFlagRequired(cmd.Flags(), "server-url"))
return cmd
}
func runClusterRemove(ctx context.Context, opts *ClusterRemoveOptions) error {
appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.runtimeName, store.Get().InsecureIngressHost)
if err != nil {
return err
}
err = appProxy.AppProxyClusters().RemoveCluster(ctx, opts.server, opts.runtimeName)
if err != nil {
return fmt.Errorf("failed to remove cluster: %w", err)
}
log.G(ctx).Info("cluster was removed successfully")
return nil
}
func NewClusterListCommand() *cobra.Command {
var runtimeName string
var kubeconfig string
cmd := &cobra.Command{
Use: "list RUNTIME_NAME",
Short: "List all the clusters of a given runtime",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`<BIN> cluster list my-runtime`),
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error
runtimeName, err = ensureRuntimeName(cmd.Context(), args)
return err
},
RunE: func(cmd *cobra.Command, _ []string) error {
return runClusterList(cmd.Context(), runtimeName, kubeconfig)
},
}
cmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "Path to the kubeconfig file")
return cmd
}
func runClusterList(ctx context.Context, runtimeName, kubeconfig string) error {
runtime, err := cfConfig.NewClient().V2().Runtime().Get(ctx, runtimeName)
if err != nil {
return err
}
kubeContext, err := util.KubeContextNameByServer(*runtime.Cluster, kubeconfig)
if err != nil {
return fmt.Errorf("failed getting context for \"%s\": %w", *runtime.Cluster, err)
}
clusters, err := cdutil.GetClusterList(ctx, kubeContext, *runtime.Metadata.Namespace, false)
if err != nil {
return err
}
if len(clusters.Items) == 0 {
log.G(ctx).Info("No clusters were found")
return nil
}
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
_, err = fmt.Fprintln(tb, "SERVER\tNAME\tVERSION\tSTATUS\tMESSAGE")
if err != nil {
return err
}
for _, c := range clusters.Items {
server := c.Server
if len(c.Namespaces) > 0 {
server = fmt.Sprintf("%s (%d namespaces)", c.Server, len(c.Namespaces))
}
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
server,
c.Name,
c.ServerVersion,
c.ConnectionState.Status,
c.ConnectionState.Message,
)
if err != nil {
return err
}
}
return tb.Flush()
}