root.go 14.7 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
94
95
96
97
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
	cmd.PersistentFlags().StringVar(ep, "kubeconfig", "", "Path to a kube config. Only required if out-of-cluster")
	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",
101
102
103
	Short:         "Synchronise Kubernetes resources with config files",
	SilenceErrors: true,
	SilenceUsage:  true,
104
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
105
		goflag.CommandLine.Parse([]string{})
106
107
108
109
110
111
112
113
114
115
116
117
118
119
		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
120
121
122
	},
}

123
124
// clientConfig.Namespace() is broken in client-go 3.0:
// namespace in config erroneously overrides explicit --namespace
125
func namespace() (string, error) {
126
	return namespaceFor(clientConfig, &overrides)
127
128
}

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

137
138
139
140
// 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) {
141
142
143
144
145
	rawConfig, err := clientConfig.RawConfig()
	if err != nil {
		return "", "", err
	}

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

155
156
157
	ctx := rawConfig.Contexts[context]
	if ctx == nil {
		return "", "", fmt.Errorf("context '%s' does not exist in the kubeconfig file", context)
158
159
	}

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

166
167
168
	return cluster.Server, ctx.Namespace, nil
}

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

220
func newExpander(cmd *cobra.Command) (*template.Expander, error) {
221
	flags := cmd.Flags()
222
223
	spec := template.Expander{}
	var err error
224

225
	spec.EnvJPath = filepath.SplitList(os.Getenv("KUBECFG_JPATH"))
226

Alex Clemmer's avatar
Fix #95    
Alex Clemmer committed
227
	spec.FlagJpath, err = flags.GetStringSlice(flagJpath)
228
229
230
231
	if err != nil {
		return nil, err
	}

232
	spec.ExtVars, err = flags.GetStringSlice(flagExtVar)
Ted Hahn's avatar
Ted Hahn committed
233
234
235
	if err != nil {
		return nil, err
	}
Thomas Hahn's avatar
Thomas Hahn committed
236

237
	spec.ExtVarFiles, err = flags.GetStringSlice(flagExtVarFile)
238
239
240
241
	if err != nil {
		return nil, err
	}

242
	spec.TlaVars, err = flags.GetStringSlice(flagTlaVar)
243
244
245
246
	if err != nil {
		return nil, err
	}

247
	spec.TlaVarFiles, err = flags.GetStringSlice(flagTlaVarFile)
248
249
250
	if err != nil {
		return nil, err
	}
251

252
	spec.Resolver, err = flags.GetString(flagResolver)
253
254
255
	if err != nil {
		return nil, err
	}
256
	spec.FailAction, err = flags.GetString(flagResolvFail)
257
258
259
260
	if err != nil {
		return nil, err
	}

261
	return &spec, nil
262
}
Angus Lees's avatar
Angus Lees committed
263
264
265
266
267
268
269
270
271
272
273
274

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

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

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

301
func restClientPool(cmd *cobra.Command, envName *string) (dynamic.ClientPool, discovery.DiscoveryInterface, error) {
302
	return restClient(cmd, envName, clientConfig, &overrides)
303
304
}

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

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

325
	metadataManager, err := metadata.Find(wd)
326
	if err != nil {
327
		return err
328
329
	}

330
331
332
333
334
	rawConfig, err := clientConfig.RawConfig()
	if err != nil {
		return err
	}

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

		servers[server] = name
343
344
345
346
	}

	//
	// check to ensure that the environment we are trying to deploy to is
347
	// created, and that the server is located in kubeconfig.
348
349
	//

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

356
357
358
359
360
361
362
	server, err := utils.NormalizeURL(env.Server)
	if err != nil {
		return err
	}

	if _, ok := servers[server]; ok {
		clusterName := servers[server]
363
364
		log.Debugf("Overwriting --cluster flag with '%s'", clusterName)
		overrides.Context.Cluster = clusterName
365
366
		log.Debugf("Overwriting --namespace flag with '%s'", env.Namespace)
		overrides.Context.Namespace = env.Namespace
367
368
369
		return nil
	}

370
	return fmt.Errorf("Attempting to deploy to environment '%s' at '%s', but cannot locate a server at that address", envName, env.Server)
371
372
373
374
375
376
377
}

// 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.
378
func expandEnvCmdObjs(cmd *cobra.Command, env string, components []string, cwd metadata.AbsPath) ([]*unstructured.Unstructured, error) {
379
380
381
382
383
	expander, err := newExpander(cmd)
	if err != nil {
		return nil, err
	}

384
	//
385
	// Set up the template expander to be able to expand the ksonnet application.
386
387
	//

388
389
390
	manager, err := metadata.Find(cwd)
	if err != nil {
		return nil, err
391
392
	}

393
394
	libPath, vendorPath, envLibPath, envComponentPath, envParamsPath := manager.LibPaths(env)
	expander.FlagJpath = append([]string{string(libPath), string(vendorPath), string(envLibPath)}, expander.FlagJpath...)
395

396
397
398
399
	componentPaths, err := manager.ComponentPaths()
	if err != nil {
		return nil, err
	}
400

401
402
403
	baseObj, err := constructBaseObj(componentPaths, components)
	if err != nil {
		return nil, err
404
	}
405
406
	params := importParams(string(envParamsPath))
	expander.ExtCodes = append([]string{baseObj, params}, expander.ExtCodes...)
407
408

	//
409
	// Expand the ksonnet app as rendered for environment `env`.
410
411
	//

412
	return expander.Expand([]string{string(envComponentPath)})
413
}
414
415
416
417
418
419

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

471
472
473
474
		// Emit object field. Sanitize the name to guarantee we generate valid
		// Jsonnet.
		componentName = params.SanitizeComponent(componentName)
		fmt.Fprintf(&obj, "  %s: %s,\n", componentName, importExpr)
475
	}
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

	// 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
491
}
492
493
494
495

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