env_add.go 3.96 KB
Newer Older
bryanl's avatar
bryanl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2018 The ksonnet 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.

bryanl's avatar
bryanl committed
16
package clicmd
bryanl's avatar
bryanl committed
17 18 19 20 21 22

import (
	"fmt"

	"github.com/spf13/viper"

bryanl's avatar
bryanl committed
23
	"github.com/ksonnet/ksonnet/pkg/actions"
bryanl's avatar
bryanl committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	"github.com/spf13/cobra"
)

const (
	vEnvAddOverride = "env-add-override"
)

var envAddCmd = &cobra.Command{
	Use:   "add <env-name>",
	Short: envShortDesc["add"],
	RunE: func(cmd *cobra.Command, args []string) error {
		flags := cmd.Flags()
		if len(args) != 1 {
			return fmt.Errorf("'env add' takes exactly one argument, which is the name of the environment")
		}

		name := args[0]

		server, namespace, err := resolveEnvFlags(flags)
		if err != nil {
			return err
		}

bryanl's avatar
bryanl committed
47 48
		// TODO: pass envClientConfig to the action so it can pull out the
		// spec flag if it is empty.
bryanl's avatar
bryanl committed
49 50 51 52 53
		specFlag, err := flags.GetString(flagAPISpec)
		if err != nil {
			return err
		}
		if specFlag == "" {
54
			specFlag = envClientConfig.GetAPISpec()
bryanl's avatar
bryanl committed
55 56 57 58
		}

		isOverride := viper.GetBool(vEnvAddOverride)

bryanl's avatar
bryanl committed
59
		m := map[string]interface{}{
60 61 62 63 64 65
			actions.OptionApp:      ka,
			actions.OptionEnvName:  name,
			actions.OptionServer:   server,
			actions.OptionModule:   namespace,
			actions.OptionSpecFlag: specFlag,
			actions.OptionOverride: isOverride,
bryanl's avatar
bryanl committed
66 67 68
		}

		return runAction(actionEnvAdd, m)
bryanl's avatar
bryanl committed
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
	},

	Long: `
The ` + "`add`" + ` command creates a new environment (specifically for the ksonnet app
whose directory it's executed in). This environment is cached with the following
info:

1. **Name** — A string used to uniquely identify the environment.
2. **Server** — The address and port of a Kubernetes API server (i.e. cluster).
3. **Namespace**  — A Kubernetes namespace. *Must already exist on the cluster.*
4. **Kubernetes API Version**  — Used to generate a library with compatible type defs.

(1) is mandatory. (2) and (3) can be inferred from $KUBECONFIG, *or* from the
` + "`--kubeconfig`" + ` or ` + "`--context`" + ` flags. Otherwise, (2), (3), and (4) can all be
specified by individual flags. Unless otherwise specified, (4) defaults to the
latest Kubernetes version that ksonnet supports.

Note that an environment *DOES NOT* contain user-specific data such as private keys.

### Related Commands

* ` + "`ks env list` " + `— ` + protoShortDesc["list"] + `
* ` + "`ks env rm` " + `— ` + protoShortDesc["rm"] + `
* ` + "`ks env set` " + `— ` + protoShortDesc["set"] + `
* ` + "`ks param set` " + `— ` + paramShortDesc["set"] + `
* ` + "`ks apply` " + `— ` + applyShortDesc + `

### Syntax
`,
	Example: `
# Initialize a new environment, called "staging". No flags are set, so 'server'
# and 'namespace' info are pulled from the file specified by $KUBECONFIG.
# 'version' defaults to the latest that ksonnet supports.
ks env add us-west/staging

# Initialize a new environment called "us-west/staging" with the pre-existing
# namespace 'staging'. 'version' is specified, so the OpenAPI spec from the
# Kubernetes v1.7.1 build is used to generate the helper library 'ksonnet-lib'.
#
# NOTE: "us-west/staging" indicates a hierarchical structure, so the env-specific
# files here are saved in "<ksonnet-app-root>/environments/us-west/staging".
ks env add us-west/staging --api-spec=version:v1.7.1 --namespace=staging

# Initialize a new environment "my-env" using the "dev" context in your current
# kubeconfig file ($KUBECONFIG).
ks env add my-env --context=dev

# Initialize a new environment "prod" using the address of a cluster's Kubernetes
# API server.
ks env add prod --server=https://ksonnet-1.us-west.elb.amazonaws.com`,
}