-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cmd.go
131 lines (110 loc) · 3.89 KB
/
cmd.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
package cmdutils
import (
"context"
"fmt"
"github.com/kris-nova/logger"
"github.com/spf13/cobra"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/outposts"
)
// Cmd holds attributes that are common between commands;
// not all commands use each attribute, but they can if needed
type Cmd struct {
CobraCommand *cobra.Command
FlagSetGroup *NamedFlagSetGroup
Plan, Wait, Validate bool
NameArg string
ClusterConfigFile string
ProviderConfig api.ProviderConfig
ClusterConfig *api.ClusterConfig
Include, Exclude []string
}
// NewCtl performs common defaulting and validation and constructs a new
// instance of eks.ClusterProvider, it may return an error if configuration
// is invalid or region is not supported
func (c *Cmd) NewCtl() (*eks.ClusterProvider, error) {
if err := c.InitializeClusterConfig(); err != nil {
return nil, err
}
ctl, err := eks.New(context.TODO(), &c.ProviderConfig, c.ClusterConfig)
if err != nil {
return nil, err
}
if !ctl.IsSupportedRegion() {
return nil, ErrUnsupportedRegion(&c.ProviderConfig)
}
return ctl, nil
}
// InitializeClusterConfig validates and initializes the ClusterConfig.
func (c *Cmd) InitializeClusterConfig() error {
api.SetClusterConfigDefaults(c.ClusterConfig)
if err := api.ValidateClusterConfig(c.ClusterConfig); err != nil {
if c.Validate {
return err
}
logger.Warning("ignoring validation error: %s", err.Error())
}
for i, ng := range c.ClusterConfig.NodeGroups {
if err := api.ValidateNodeGroup(i, ng, c.ClusterConfig); err != nil {
if c.Validate {
return err
}
logger.Warning("ignoring validation error: %s", err.Error())
}
// defaulting of nodegroup currently depends on validation;
// that may change, but at present that's how it's meant to work
api.SetNodeGroupDefaults(ng, c.ClusterConfig.Metadata, c.ClusterConfig.IsControlPlaneOnOutposts())
}
for i, ng := range c.ClusterConfig.ManagedNodeGroups {
api.SetManagedNodeGroupDefaults(ng, c.ClusterConfig.Metadata, c.ClusterConfig.IsControlPlaneOnOutposts())
if err := api.ValidateManagedNodeGroup(i, ng); err != nil {
return err
}
}
return nil
}
// NewProviderForExistingCluster is a wrapper for NewCtl that also validates that the cluster exists and is not a
// registered/connected cluster.
func (c *Cmd) NewProviderForExistingCluster(ctx context.Context) (*eks.ClusterProvider, error) {
clusterProvider, err := eks.New(ctx, &c.ProviderConfig, c.ClusterConfig)
if err != nil {
return nil, fmt.Errorf("could not create cluster provider from options: %w", err)
}
if !clusterProvider.IsSupportedRegion() {
return nil, ErrUnsupportedRegion(&c.ProviderConfig)
}
if err := clusterProvider.RefreshClusterStatus(ctx, c.ClusterConfig); err != nil {
return nil, err
}
if err := c.InitializeClusterConfig(); err != nil {
return nil, err
}
if c.ClusterConfig.IsControlPlaneOnOutposts() {
clusterProvider.AWSProvider = outposts.WrapClusterProvider(clusterProvider.AWSProvider)
}
return clusterProvider, nil
}
// AddResourceCmd create a registers a new command under the given verb command
func AddResourceCmd(flagGrouping *FlagGrouping, parentVerbCmd *cobra.Command, newCmd func(*Cmd)) {
c := &Cmd{
CobraCommand: &cobra.Command{},
ProviderConfig: api.ProviderConfig{
WaitTimeout: api.DefaultWaitTimeout,
},
Plan: true, // always on by default
Wait: false, // varies in some commands
Validate: true, // also on by default
}
c.FlagSetGroup = flagGrouping.New(c.CobraCommand)
newCmd(c)
c.FlagSetGroup.AddTo(c.CobraCommand)
parentVerbCmd.AddCommand(c.CobraCommand)
}
// SetDescription sets usage along with short and long descriptions as well as aliases
func (c *Cmd) SetDescription(use, short, long string, aliases ...string) {
c.CobraCommand.Use = use
c.CobraCommand.Short = short
c.CobraCommand.Long = long
c.CobraCommand.Aliases = aliases
}