-
Notifications
You must be signed in to change notification settings - Fork 525
/
nuctl.go
206 lines (170 loc) · 6.32 KB
/
nuctl.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
/*
Copyright 2023 The Nuclio 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 command
import (
"context"
"os"
"runtime"
"github.com/nuclio/nuclio/pkg/common"
"github.com/nuclio/nuclio/pkg/platform"
"github.com/nuclio/nuclio/pkg/platform/factory"
"github.com/nuclio/nuclio/pkg/platformconfig"
"github.com/nuclio/errors"
"github.com/nuclio/logger"
"github.com/nuclio/zap"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
// load authentication modes
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
type RootCommandeer struct {
loggerInstance logger.Logger
cmd *cobra.Command
platformName string
platform platform.Platform
namespace string
verbose bool
KubeconfigPath string
concurrency int
enableMaskSensitiveFields bool
platformConfiguration *platformconfig.Config
}
func NewRootCommandeer() *RootCommandeer {
commandeer := &RootCommandeer{}
cmd := &cobra.Command{
Use: "nuctl [command]",
Short: "Nuclio command-line interface",
SilenceUsage: true,
SilenceErrors: true,
}
defaultPlatformType := common.GetEnvOrDefaultString("NUCTL_PLATFORM", common.AutoPlatformName)
defaultNamespace := os.Getenv("NUCTL_NAMESPACE")
ctx := context.Background()
cmd.PersistentFlags().BoolVarP(&commandeer.verbose, "verbose", "v", false, "Verbose output")
cmd.PersistentFlags().StringVarP(&commandeer.platformName, "platform", "", defaultPlatformType, "Platform identifier - \"kube\", \"local\", or \"auto\"")
cmd.PersistentFlags().StringVarP(&commandeer.namespace, "namespace", "n", defaultNamespace, "Namespace")
cmd.PersistentFlags().IntVar(&commandeer.concurrency, "concurrency", runtime.NumCPU(), "Max number of parallel patches. The default value is equal to the number of CPUs.")
cmd.PersistentFlags().BoolVar(&commandeer.enableMaskSensitiveFields, "mask-sensitive-fields", false, "Enable sensitive fields masking")
// platform specific
cmd.PersistentFlags().StringVarP(&commandeer.KubeconfigPath, "kubeconfig", "k", "", "Path to a Kubernetes configuration file (admin.conf)")
// add children
cmd.AddCommand(
newBuildCommandeer(commandeer).cmd,
newDeployCommandeer(ctx, commandeer).cmd,
newInvokeCommandeer(ctx, commandeer).cmd,
newGetCommandeer(ctx, commandeer).cmd,
newDeleteCommandeer(ctx, commandeer).cmd,
newUpdateCommandeer(ctx, commandeer).cmd,
newVersionCommandeer(commandeer).cmd,
newCreateCommandeer(ctx, commandeer).cmd,
newExportCommandeer(ctx, commandeer).cmd,
newImportCommandeer(ctx, commandeer).cmd,
newBetaCommandeer(ctx, commandeer).cmd,
newParseCommandeer(ctx, commandeer).cmd,
)
commandeer.cmd = cmd
return commandeer
}
// Execute uses os.Args to execute the command
func (rc *RootCommandeer) Execute() error {
return rc.cmd.Execute()
}
// GetCmd returns the underlying cobra command
func (rc *RootCommandeer) GetCmd() *cobra.Command {
return rc.cmd
}
// CreateMarkdown generates MD files in the target path
func (rc *RootCommandeer) CreateMarkdown(path string) error {
return doc.GenMarkdownTree(rc.cmd, path)
}
func (rc *RootCommandeer) initialize(initPlatform bool) error {
var err error
rc.loggerInstance, err = rc.createLogger()
if err != nil {
return errors.Wrap(err, "Failed to create logger")
}
if !initPlatform {
return nil
}
// TODO: accept platform config path as arg
rc.platformConfiguration, err = platformconfig.NewPlatformConfig("")
if err != nil {
return errors.Wrap(err, "Failed to create platform config")
}
rc.platformConfiguration.Kube.KubeConfigPath = rc.KubeconfigPath
if rc.enableMaskSensitiveFields {
rc.platformConfiguration.EnableSensitiveFieldMasking()
}
// do not let nuctl monitor function containers
// nuctl is a CLI tool, to enable function container healthiness, use Nuclio dashboard
rc.platformConfiguration.Local.FunctionContainersHealthinessEnabled = false
// resolve namespace
if err := rc.resolveDefaultNamespace(); err != nil {
return errors.Wrap(err, "Failed to resolve default namespace")
}
// ask the factory to create the appropriate platform
// TODO: as more platforms are supported, i imagine the last argument will be to some
// sort of configuration provider interface
rc.platform, err = factory.CreatePlatform(context.Background(),
rc.loggerInstance,
rc.platformName,
rc.platformConfiguration,
rc.namespace)
if err != nil {
return errors.Wrap(err, "Failed to create platform")
}
rc.loggerInstance.DebugWith("Created platform",
"name", rc.platform.GetName(),
"namespace", rc.namespace)
return nil
}
func (rc *RootCommandeer) createLogger() (logger.Logger, error) {
var loggerLevel nucliozap.Level
if rc.verbose {
loggerLevel = nucliozap.DebugLevel
} else {
loggerLevel = nucliozap.InfoLevel
}
loggerInstance, err := nucliozap.NewNuclioZapCmd("nuctl",
loggerLevel,
common.GetRedactorInstance(rc.GetCmd().OutOrStdout()))
if err != nil {
return nil, errors.Wrap(err, "Failed to create logger")
}
return loggerInstance, nil
}
func (rc *RootCommandeer) resolveDefaultNamespace() error {
// if namespace is already set, use it
if rc.namespace != "" {
return nil
}
platformType, err := factory.GetPlatformByType(rc.platformName, rc.platformConfiguration)
if err != nil {
return errors.Wrap(err, "Failed to get platform by type")
}
switch platformType {
case common.KubePlatformName:
clientCmd, err := common.GetKubeConfigClientCmdByKubeconfigPath(common.GetKubeconfigPath(rc.KubeconfigPath))
if err != nil {
return errors.Wrap(err, "Failed to load kubeconfig")
}
if clientCmd.CurrentContext == "" {
return errors.New("Failed to get current context - is your kubeconfig points to a valid cluster?")
}
rc.namespace = clientCmd.Contexts[clientCmd.CurrentContext].Namespace
default:
rc.namespace = "nuclio"
}
return nil
}