root.go 15 KB
Newer Older
Angus Lees's avatar
Angus Lees committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Copyright 2017 The kubecfg 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.

16
17
18
package cmd

import (
Angus Lees's avatar
Angus Lees committed
19
20
	"bytes"
	"encoding/json"
21
	goflag "flag"
22
	"fmt"
23
	"io"
24
	"os"
25
	"path"
26
	"path/filepath"
27
	"reflect"
Ted Hahn's avatar
Ted Hahn committed
28
	"strings"
29

30
31
	"github.com/ksonnet/ksonnet/metadata/params"

32
33
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

34
	log "github.com/sirupsen/logrus"
35
	"github.com/spf13/cobra"
36
	"golang.org/x/crypto/ssh/terminal"
Angus Lees's avatar
Angus Lees committed
37
38
39
	"k8s.io/client-go/discovery"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/tools/clientcmd"
40

41
42
43
	"github.com/ksonnet/ksonnet/metadata"
	"github.com/ksonnet/ksonnet/template"
	"github.com/ksonnet/ksonnet/utils"
44
45
46

	// Register auth plugins
	_ "k8s.io/client-go/plugin/pkg/client/auth"
47
48
)

Angus Lees's avatar
Angus Lees committed
49
const (
50
	flagVerbose    = "verbose"
51
	flagJpath      = "jpath"
Angus Lees's avatar
Angus Lees committed
52
	flagExtVar     = "ext-str"
Thomas Hahn's avatar
Thomas Hahn committed
53
	flagExtVarFile = "ext-str-file"
54
55
	flagTlaVar     = "tla-str"
	flagTlaVarFile = "tla-str-file"
56
57
	flagResolver   = "resolve-images"
	flagResolvFail = "resolve-images-error"
Jessica Yuen's avatar
Jessica Yuen committed
58
	flagAPISpec    = "api-spec"
59

60
	// For use in the commands (e.g., diff, apply, delete) that require either an
61
	// environment or the -f flag.
62
63
	flagComponent      = "component"
	flagComponentShort = "c"
Angus Lees's avatar
Angus Lees committed
64
65
66
)

var clientConfig clientcmd.ClientConfig
67
var overrides clientcmd.ConfigOverrides
68
var loadingRules clientcmd.ClientConfigLoadingRules
Angus Lees's avatar
Angus Lees committed
69

70
func init() {
71
	RootCmd.PersistentFlags().CountP(flagVerbose, "v", "Increase verbosity. May be given multiple times.")
72
73

	// The "usual" clientcmd/kubectl flags
74
	loadingRules = *clientcmd.NewDefaultClientConfigLoadingRules()
75
	loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
76
	clientConfig = clientcmd.NewInteractiveDeferredLoadingClientConfig(&loadingRules, &overrides, os.Stdin)
77
78

	RootCmd.PersistentFlags().Set("logtostderr", "true")
79
80
}

81
82
83
84
85
86
87
88
89
90
91
92
93
func bindJsonnetFlags(cmd *cobra.Command) {
	cmd.PersistentFlags().StringSliceP(flagJpath, "J", nil, "Additional jsonnet library search path")
	cmd.PersistentFlags().StringSliceP(flagExtVar, "V", nil, "Values of external variables")
	cmd.PersistentFlags().StringSlice(flagExtVarFile, nil, "Read external variable from a file")
	cmd.PersistentFlags().StringSliceP(flagTlaVar, "A", nil, "Values of top level arguments")
	cmd.PersistentFlags().StringSlice(flagTlaVarFile, nil, "Read top level argument from a file")
	cmd.PersistentFlags().String(flagResolver, "noop", "Change implementation of resolveImage native function. One of: noop, registry")
	cmd.PersistentFlags().String(flagResolvFail, "warn", "Action when resolveImage fails. One of ignore,warn,error")
}

func bindClientGoFlags(cmd *cobra.Command) {
	kflags := clientcmd.RecommendedConfigOverrideFlags("")
	ep := &loadingRules.ExplicitPath
Jessica Yao's avatar
Jessica Yao committed
94
	cmd.PersistentFlags().StringVar(ep, "kubeconfig", "", "Path to a kubeconfig file. Alternative to env var $KUBECONFIG.")
95
96
97
	clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags)
}

98
// RootCmd is the root of cobra subcommand tree
99
var RootCmd = &cobra.Command{
Jessica Yuen's avatar
Jessica Yuen committed
100
	Use:           "ks",
Jessica Yao's avatar
Jessica Yao committed
101
102
103
104
105
106
107
	Short:         `Configure your application to deploy to a Kubernetes cluster`,
	Long: `
You can use the ` + "`ks`" + ` commands to write, share, and deploy your Kubernetes
application configuration to remote clusters.

----
`,
108
109
	SilenceErrors: true,
	SilenceUsage:  true,
110
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
111
		goflag.CommandLine.Parse([]string{})
112
113
114
115
116
117
118
119
120
121
122
123
124
125
		flags := cmd.Flags()
		out := cmd.OutOrStderr()
		log.SetOutput(out)

		logFmt := NewLogFormatter(out)
		log.SetFormatter(logFmt)

		verbosity, err := flags.GetCount(flagVerbose)
		if err != nil {
			return err
		}
		log.SetLevel(logLevel(verbosity))

		return nil
126
127
128
	},
}

129
130
// clientConfig.Namespace() is broken in client-go 3.0:
// namespace in config erroneously overrides explicit --namespace
131
func namespace() (string, error) {
132
	return namespaceFor(clientConfig, &overrides)
133
134
}

135
func namespaceFor(c clientcmd.ClientConfig, overrides *clientcmd.ConfigOverrides) (string, error) {
136
137
138
	if overrides.Context.Namespace != "" {
		return overrides.Context.Namespace, nil
	}
139
	ns, _, err := clientConfig.Namespace()
140
141
142
	return ns, err
}

143
144
145
146
// resolveContext returns the server and namespace of the cluster at the
// provided context. If the context string is empty, the default context is
// used.
func resolveContext(context string) (server, namespace string, err error) {
147
148
149
150
151
	rawConfig, err := clientConfig.RawConfig()
	if err != nil {
		return "", "", err
	}

152
153
154
	// use the default context where context is empty
	if context == "" {
		if rawConfig.CurrentContext == "" {
155
156
157
			// User likely does not have a kubeconfig file.
			return "", "", fmt.Errorf("No current context found. Make sure a kubeconfig file is present")
		}
158
159
		context = rawConfig.CurrentContext
	}
160

161
162
163
	ctx := rawConfig.Contexts[context]
	if ctx == nil {
		return "", "", fmt.Errorf("context '%s' does not exist in the kubeconfig file", context)
164
165
	}

166
	log.Infof("Using context '%s' from the kubeconfig file specified at the environment variable $KUBECONFIG", context)
167
168
169
170
	cluster, exists := rawConfig.Clusters[ctx.Cluster]
	if !exists {
		return "", "", fmt.Errorf("No cluster with name '%s' exists", ctx.Cluster)
	}
171

172
173
174
	return cluster.Server, ctx.Namespace, nil
}

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
func logLevel(verbosity int) log.Level {
	switch verbosity {
	case 0:
		return log.InfoLevel
	default:
		return log.DebugLevel
	}
}

type logFormatter struct {
	escapes  *terminal.EscapeCodes
	colorise bool
}

// NewLogFormatter creates a new log.Formatter customised for writer
func NewLogFormatter(out io.Writer) log.Formatter {
	var ret = logFormatter{}
	if f, ok := out.(*os.File); ok {
		ret.colorise = terminal.IsTerminal(int(f.Fd()))
		ret.escapes = terminal.NewTerminal(f, "").Escape
	}
	return &ret
}

func (f *logFormatter) levelEsc(level log.Level) []byte {
	switch level {
	case log.DebugLevel:
		return []byte{}
	case log.WarnLevel:
		return f.escapes.Yellow
	case log.ErrorLevel, log.FatalLevel, log.PanicLevel:
		return f.escapes.Red
	default:
		return f.escapes.Blue
	}
}

func (f *logFormatter) Format(e *log.Entry) ([]byte, error) {
	buf := bytes.Buffer{}
	if f.colorise {
		buf.Write(f.levelEsc(e.Level))
		fmt.Fprintf(&buf, "%-5s ", strings.ToUpper(e.Level.String()))
		buf.Write(f.escapes.Reset)
	}

	buf.WriteString(strings.TrimSpace(e.Message))
	buf.WriteString("\n")

	return buf.Bytes(), nil
}

226
func newExpander(cmd *cobra.Command) (*template.Expander, error) {
227
	flags := cmd.Flags()
228
229
	spec := template.Expander{}
	var err error
230

231
	spec.EnvJPath = filepath.SplitList(os.Getenv("KUBECFG_JPATH"))
232

Alex Clemmer's avatar
Fix #95  
Alex Clemmer committed
233
	spec.FlagJpath, err = flags.GetStringSlice(flagJpath)
234
235
236
237
	if err != nil {
		return nil, err
	}

238
	spec.ExtVars, err = flags.GetStringSlice(flagExtVar)
Ted Hahn's avatar
Ted Hahn committed
239
240
241
	if err != nil {
		return nil, err
	}
Thomas Hahn's avatar
Thomas Hahn committed
242

243
	spec.ExtVarFiles, err = flags.GetStringSlice(flagExtVarFile)
244
245
246
247
	if err != nil {
		return nil, err
	}

248
	spec.TlaVars, err = flags.GetStringSlice(flagTlaVar)
249
250
251
252
	if err != nil {
		return nil, err
	}

253
	spec.TlaVarFiles, err = flags.GetStringSlice(flagTlaVarFile)
254
255
256
	if err != nil {
		return nil, err
	}
257

258
	spec.Resolver, err = flags.GetString(flagResolver)
259
260
261
	if err != nil {
		return nil, err
	}
262
	spec.FailAction, err = flags.GetString(flagResolvFail)
263
264
265
266
	if err != nil {
		return nil, err
	}

267
	return &spec, nil
268
}
Angus Lees's avatar
Angus Lees committed
269
270
271
272
273
274
275
276
277
278
279
280

// For debugging
func dumpJSON(v interface{}) string {
	buf := bytes.NewBuffer(nil)
	enc := json.NewEncoder(buf)
	enc.SetIndent("", "  ")
	if err := enc.Encode(v); err != nil {
		return err.Error()
	}
	return string(buf.Bytes())
}

281
func restClient(cmd *cobra.Command, envName *string, config clientcmd.ClientConfig, overrides *clientcmd.ConfigOverrides) (dynamic.ClientPool, discovery.DiscoveryInterface, error) {
282
	if envName != nil {
283
		err := overrideCluster(*envName, config, overrides)
284
285
286
287
288
		if err != nil {
			return nil, nil, err
		}
	}

289
	conf, err := config.ClientConfig()
Angus Lees's avatar
Angus Lees committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
	if err != nil {
		return nil, nil, err
	}

	disco, err := discovery.NewDiscoveryClientForConfig(conf)
	if err != nil {
		return nil, nil, err
	}

	discoCache := utils.NewMemcachedDiscoveryClient(disco)
	mapper := discovery.NewDeferredDiscoveryRESTMapper(discoCache, dynamic.VersionInterfaces)
	pathresolver := dynamic.LegacyAPIPathResolverFunc

	pool := dynamic.NewClientPool(conf, mapper, pathresolver)
	return pool, discoCache, nil
}
306

307
func restClientPool(cmd *cobra.Command, envName *string) (dynamic.ClientPool, discovery.DiscoveryInterface, error) {
308
	return restClient(cmd, envName, clientConfig, &overrides)
309
310
}

311
// addEnvCmdFlags adds the flags that are common to the family of commands
312
// whose form is `[<env>|-f <file-name>]`, e.g., `apply` and `delete`.
313
func addEnvCmdFlags(cmd *cobra.Command) {
314
	cmd.PersistentFlags().StringArrayP(flagComponent, flagComponentShort, nil, "Name of a specific component (multiple -c flags accepted, allows YAML, JSON, and Jsonnet)")
315
316
}

317
// overrideCluster ensures that the server specified in the environment is
318
319
320
// associated in the user's kubeconfig file during deployment to a ksonnet
// environment. We will error out if it is not.
//
321
// If the environment server the user is attempting to deploy to is not the current
322
// kubeconfig context, we must manually override the client-go --cluster flag
323
// to ensure we are deploying to the correct cluster.
324
func overrideCluster(envName string, clientConfig clientcmd.ClientConfig, overrides *clientcmd.ConfigOverrides) error {
325
	cwd, err := os.Getwd()
326
	if err != nil {
327
		return err
328
	}
329
	wd := metadata.AbsPath(cwd)
330

331
	metadataManager, err := metadata.Find(wd)
332
	if err != nil {
333
		return err
334
335
	}

336
337
338
339
340
	rawConfig, err := clientConfig.RawConfig()
	if err != nil {
		return err
	}

341
	var servers = make(map[string]string)
342
	for name, cluster := range rawConfig.Clusters {
343
344
345
346
347
348
		server, err := utils.NormalizeURL(cluster.Server)
		if err != nil {
			return err
		}

		servers[server] = name
349
350
351
352
	}

	//
	// check to ensure that the environment we are trying to deploy to is
353
	// created, and that the server is located in kubeconfig.
354
355
	//

356
	log.Debugf("Validating deployment at '%s' with server '%v'", envName, reflect.ValueOf(servers).MapKeys())
357
358
359
360
361
	env, err := metadataManager.GetEnvironment(envName)
	if err != nil {
		return err
	}

362
363
364
365
366
367
368
	server, err := utils.NormalizeURL(env.Server)
	if err != nil {
		return err
	}

	if _, ok := servers[server]; ok {
		clusterName := servers[server]
369
370
371
372
373
374
375
376
		if overrides.Context.Cluster == "" {
			log.Debugf("Overwriting --cluster flag with '%s'", clusterName)
			overrides.Context.Cluster = clusterName
		}
		if overrides.Context.Namespace == "" {
			log.Debugf("Overwriting --namespace flag with '%s'", env.Namespace)
			overrides.Context.Namespace = env.Namespace
		}
377
378
379
		return nil
	}

380
	return fmt.Errorf("Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address", envName, env.Server)
381
382
383
384
385
386
387
}

// expandEnvCmdObjs finds and expands templates for the family of commands of
// the form `[<env>|-f <file-name>]`, e.g., `apply` and `delete`. That is, if
// the user passes a list of files, we will expand all templates in those files,
// while if a user passes an environment name, we will expand all component
// files using that environment.
388
func expandEnvCmdObjs(cmd *cobra.Command, env string, components []string, cwd metadata.AbsPath) ([]*unstructured.Unstructured, error) {
389
390
391
392
393
	expander, err := newExpander(cmd)
	if err != nil {
		return nil, err
	}

394
	//
395
	// Set up the template expander to be able to expand the ksonnet application.
396
397
	//

398
399
400
	manager, err := metadata.Find(cwd)
	if err != nil {
		return nil, err
401
402
	}

403
404
	libPath, vendorPath, envLibPath, envComponentPath, envParamsPath := manager.LibPaths(env)
	expander.FlagJpath = append([]string{string(libPath), string(vendorPath), string(envLibPath)}, expander.FlagJpath...)
405

406
407
408
409
	componentPaths, err := manager.ComponentPaths()
	if err != nil {
		return nil, err
	}
410

411
412
413
	baseObj, err := constructBaseObj(componentPaths, components)
	if err != nil {
		return nil, err
414
	}
415
416
	params := importParams(string(envParamsPath))
	expander.ExtCodes = append([]string{baseObj, params}, expander.ExtCodes...)
417
418

	//
419
	// Expand the ksonnet app as rendered for environment `env`.
420
421
	//

422
	return expander.Expand([]string{string(envComponentPath)})
423
}
424
425
426
427
428
429

// constructBaseObj constructs the base Jsonnet object that represents k-v
// pairs of component name -> component imports. For example,
//
//   {
//      foo: import "components/foo.jsonnet"
430
//      "foo-bar": import "components/foo-bar.jsonnet"
431
//   }
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
func constructBaseObj(componentPaths, componentNames []string) (string, error) {
	// IMPLEMENTATION NOTE: If one or more `componentNames` exist, it is
	// sufficient to simply omit every name that does not appear in the list. This
	// is because we know every field of the base object will contain _only_ an
	// `import` node (see example object in the function-heading comment). This
	// would not be true in cases where one field can reference another field; in
	// this case, one would need to generate the entire object, and filter that.
	//
	// Hence, a word of caution: if the base object ever becomes more complex, you
	// will need to change the way this function performs filtering, as it will
	// lead to very confusing bugs.

	shouldFilter := len(componentNames) > 0
	filter := map[string]string{}
	for _, name := range componentNames {
		filter[name] = ""
	}

	// Add every component we know about to the base object.
451
452
	var obj bytes.Buffer
	obj.WriteString("{\n")
453
	for _, p := range componentPaths {
454
		ext := path.Ext(p)
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
		componentName := strings.TrimSuffix(path.Base(p), ext)

		// Filter! If the filter has more than 1 element and the component name is
		// not in the filter, skip.
		if _, exists := filter[componentName]; shouldFilter && !exists {
			continue
		} else if shouldFilter && exists {
			delete(filter, componentName)
		}

		// Generate import statement.
		var importExpr string
		switch ext {
		case ".jsonnet":
			importExpr = fmt.Sprintf(`import "%s"`, p)

		// TODO: Pull in YAML and JSON when we build the base object.
		//
		// case ".yaml", ".yml":
		// 	importExpr = fmt.Sprintf(`util.parseYaml("%s")`, p)
		// case ".json":
		// 	importExpr = fmt.Sprintf(`util.parseJson("%s")`, p)
		default:
478
479
480
			continue
		}

481
482
483
484
		// Emit object field. Sanitize the name to guarantee we generate valid
		// Jsonnet.
		componentName = params.SanitizeComponent(componentName)
		fmt.Fprintf(&obj, "  %s: %s,\n", componentName, importExpr)
485
	}
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

	// Check that we found all the components the user asked for.
	if shouldFilter && len(filter) != 0 {
		names := []string{}
		for name := range filter {
			names = append(names, "'"+name+"'")
		}
		return "", fmt.Errorf("Failed to filter components; the following components don't exist: [ %s ]", strings.Join(names, ","))
	}

	// Terminate object.
	fmt.Fprintf(&obj, "}\n")

	// Emit `base.libsonnet`.
	return fmt.Sprintf("%s=%s", metadata.ComponentsExtCodeKey, obj.String()), nil
501
}
502
503
504
505

func importParams(path string) string {
	return fmt.Sprintf(`%s=import "%s"`, metadata.ParamsExtCodeKey, path)
}