-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner.go
167 lines (146 loc) · 5.55 KB
/
runner.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
package login
import (
"context"
"fmt"
"io"
"strings"
"github.com/fatih/color"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/giantswarm/kubectl-gs/v2/pkg/commonconfig"
"github.com/giantswarm/kubectl-gs/v2/pkg/kubeconfig"
)
type runner struct {
flag *flag
logger micrologger.Logger
fs afero.Fs
commonConfig *commonconfig.CommonConfig
loginOptions LoginOptions
stdout io.Writer
stderr io.Writer
}
type LoginOptions struct {
selfContained bool
selfContainedWC bool // used only if both MC and WC are specified on command line
isWC bool // used only if both MC and WC are specified on command line
switchToContext bool
switchToWCContext bool // used only if both MC and WC are specified on command line
originContext string
contextOverride string
}
func (r *runner) Run(cmd *cobra.Command, args []string) error {
ctx := context.Background()
err := r.flag.Validate()
if err != nil {
return microerror.Mask(err)
}
r.setLoginOptions(ctx, &args)
err = r.run(ctx, cmd, args)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) error {
switch len(args) {
// No arguments given - we try to reuse the existing context.
case 0:
err := r.tryToReuseExistingContext(ctx)
if err != nil {
return microerror.Mask(err)
}
// One argument - This can be an existing context, an installation name or a login URL
case 1:
installationIdentifier := strings.ToLower(args[0])
foundContext, err := r.findContext(ctx, installationIdentifier)
if IsContextDoesNotExist(err) && !strings.HasSuffix(installationIdentifier, kubeconfig.ClientCertSuffix) {
clientCertContext := kubeconfig.GetClientCertContextName(installationIdentifier)
fmt.Fprint(r.stdout, color.YellowString("No context named %s was found: %s\nLooking for context %s.\n", installationIdentifier, err, clientCertContext))
foundContext, err = r.findContext(ctx, clientCertContext)
}
if err != nil {
return microerror.Mask(err)
}
if !foundContext {
err = r.loginWithURL(ctx, installationIdentifier, true, r.commonConfig.GetTokenOverride())
if err != nil {
return microerror.Mask(err)
}
}
// Two arguments - This is interpreted as an installation and a workload cluster
case 2:
installationIdentifier := strings.ToLower(strings.Join(args, "-"))
foundContext, err := r.findContext(ctx, installationIdentifier)
if IsContextDoesNotExist(err) && !strings.HasSuffix(installationIdentifier, kubeconfig.ClientCertSuffix) {
clientCertContext := kubeconfig.GetClientCertContextName(installationIdentifier)
fmt.Fprint(r.stdout, color.YellowString("No context named %s was found: %s\nLooking for context %s.\n", installationIdentifier, err, clientCertContext))
foundContext, err = r.findContext(ctx, clientCertContext)
}
if err != nil {
return microerror.Mask(err)
}
if !foundContext {
return microerror.Maskf(contextDoesNotExistError, "Could not find context for identifier %s", installationIdentifier)
}
default:
return microerror.Maskf(invalidConfigError, "Invalid number of arguments.")
}
// used only if both MC and WC are specified on command line
if r.loginOptions.isWC {
err := r.handleWCKubeconfig(ctx)
if err != nil {
return microerror.Mask(err)
}
}
return nil
}
func (r *runner) tryToGetCurrentContexts(ctx context.Context) (string, string, error) {
config, err := r.commonConfig.GetConfigAccess().GetStartingConfig()
if err != nil {
return "", "", microerror.Mask(err)
}
contextOverride := r.commonConfig.GetContextOverride()
if _, ok := config.Contexts[contextOverride]; !ok {
contextOverride = ""
}
return config.CurrentContext, contextOverride, nil
}
func (r *runner) setLoginOptions(ctx context.Context, args *[]string) {
originContext, contextOverride, err := r.tryToGetCurrentContexts(ctx)
if err != nil {
fmt.Fprintln(r.stdout, color.YellowString("Failed trying to determine current context. %s", err))
}
hasWCNameFlag := r.flag.WCName != ""
hasSelfContainedFlag := r.flag.SelfContained != ""
hasContextOverride := contextOverride != ""
// indicates whether it is desired to update current context in the kubeconfig file
shouldSwitchContextInConfig := !hasContextOverride && (hasWCNameFlag || !(hasSelfContainedFlag || r.flag.KeepContext))
// indicates whether it is desired to update current context in the kubeconfig file to the wc client context
shouldSwitchToWCContextInConfig := hasWCNameFlag && !(hasSelfContainedFlag || r.flag.KeepContext)
r.loginOptions = LoginOptions{
originContext: originContext,
contextOverride: contextOverride,
isWC: hasWCNameFlag,
selfContained: hasSelfContainedFlag && !hasWCNameFlag,
selfContainedWC: hasSelfContainedFlag && hasWCNameFlag,
switchToContext: shouldSwitchContextInConfig,
switchToWCContext: shouldSwitchToWCContextInConfig,
}
}
func (r *runner) tryToReuseExistingContext(ctx context.Context) error {
currentContext := r.commonConfig.GetContextOverride()
if currentContext == "" {
config, err := r.commonConfig.GetConfigAccess().GetStartingConfig()
if err != nil {
return microerror.Mask(err)
}
currentContext = config.CurrentContext
}
if currentContext != "" {
return r.loginWithKubeContextName(ctx, currentContext)
} else {
return microerror.Maskf(selectedContextNonCompatibleError, "The current context does not seem to belong to a Giant Swarm cluster.\nPlease run 'kubectl gs login --help' to find out how to log in to a particular cluster.")
}
}