-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws.go
80 lines (70 loc) · 2.2 KB
/
aws.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
// Package cmd offers CLI functionality
package cmd
import (
"fmt"
"github.com/mateimicu/kdiscover/internal/aws"
"github.com/mateimicu/kdiscover/internal/kubeconfig"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
var (
awsPartitions []string
awsRegions []string
alias string
)
func newAWSCommand() *cobra.Command {
AWSCommand := &cobra.Command{
Use: "aws",
Short: "Work with AWS EKS clusters",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Because cobra will only run the last PersistentPreRunE
// we need to search for the root and run the function.
// This is not a robust solution as there may be multiple
// PersistentPreRunE function between the leaf command and root
// also this assumes that this is the root one
// An issue about this https://github.com/spf13/cobra/issues/252
root := cmd
for ; root.HasParent(); root = root.Parent() {
} //revive:disable-line:empty-block
err := root.PersistentPreRunE(cmd, args)
if err != nil {
return err
}
log.WithFields(log.Fields{
"partitions": awsPartitions,
}).Debug("Search regions for partitions")
awsRegions = aws.GetRegions(awsPartitions)
if len(awsRegions) == 0 {
log.WithFields(log.Fields{
"partitions": awsPartitions,
}).Error("Can't find regions for partitions")
return fmt.Errorf("can't find regions for partitions %v", awsPartitions)
}
log.WithFields(log.Fields{
"regions": awsRegions,
}).Info("Founds regions")
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.HelpFunc()(cmd, args)
return nil
},
}
AWSCommand.PersistentFlags().StringSliceVar(
&awsPartitions,
"aws-partitions",
[]string{"aws"},
fmt.Sprintf("In what partitions to search for clusters. Supported %v", aws.AllowedParitions()))
AWSCommand.PersistentFlags().StringVar(
&kubeconfigPath,
"kubeconfig-path",
kubeconfig.GetDefaultKubeconfigPath(),
"Path to the kubeconfig to work with")
AWSCommand.PersistentFlags().StringVar(
&alias,
"context-name-alias",
"{{.Name}}",
"Template for the context name. Has acces to Cluster type")
AWSCommand.AddCommand(newListCommand(), newUpdateCommand())
return AWSCommand
}