root.go 14.9 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
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

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

39
40
41
	"github.com/ksonnet/ksonnet/metadata"
	"github.com/ksonnet/ksonnet/template"
	"github.com/ksonnet/ksonnet/utils"
42
43
44

	// Register auth plugins
	_ "k8s.io/client-go/plugin/pkg/client/auth"
45
46
)

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

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

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

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

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

	RootCmd.PersistentFlags().Set("logtostderr", "true")
77
78
}

79
80
81
82
83
84
85
86
87
88
89
90
91
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
92
	cmd.PersistentFlags().StringVar(ep, "kubeconfig", "", "Path to a kubeconfig file. Alternative to env var $KUBECONFIG.")
93
94
95
	clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags)
}

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

----
`,
106
107
	SilenceErrors: true,
	SilenceUsage:  true,
108
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
109
		goflag.CommandLine.Parse([]string{})
110
111
112
113
114
115
116
117
118
119
120
121
122
123
		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
124
125
126
	},
}

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

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

141
142
143
144
// 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) {
145
146
147
148
149
	rawConfig, err := clientConfig.RawConfig()
	if err != nil {
		return "", "", err
	}

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

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

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

170
171
172
	return cluster.Server, ctx.Namespace, nil
}

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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
}

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

229
	spec.EnvJPath = filepath.SplitList(os.Getenv("KUBECFG_JPATH"))
230

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

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

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

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

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

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

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

// 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())
}

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

287
	conf, err := config.ClientConfig()
Angus Lees's avatar
Angus Lees committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	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
}
304

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

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

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

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

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

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

		servers[server] = name
347
348
349
350
	}

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

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

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

	if _, ok := servers[server]; ok {
		clusterName := servers[server]
367
368
369
370
371
372
373
374
		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
		}
375
376
377
		return nil
	}

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

// 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.
386
func expandEnvCmdObjs(cmd *cobra.Command, env string, components []string, cwd metadata.AbsPath) ([]*unstructured.Unstructured, error) {
387
388
389
390
391
	expander, err := newExpander(cmd)
	if err != nil {
		return nil, err
	}

392
	//
393
	// Set up the template expander to be able to expand the ksonnet application.
394
395
	//

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

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

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

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

	//
417
	// Expand the ksonnet app as rendered for environment `env`.
418
419
	//

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

// constructBaseObj constructs the base Jsonnet object that represents k-v
// pairs of component name -> component imports. For example,
//
//   {
//      foo: import "components/foo.jsonnet"
428
//      "foo-bar": import "components/foo-bar.jsonnet"
429
//   }
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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.
449
450
	var obj bytes.Buffer
	obj.WriteString("{\n")
451
	for _, p := range componentPaths {
452
		ext := path.Ext(p)
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
		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:
476
477
478
			continue
		}

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

	// 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
499
}
500
501
502
503

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