apply.go 4.96 KB
Newer Older
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.

bryanl's avatar
bryanl committed
16
package clicmd
17 18

import (
bryanl's avatar
bryanl committed
19
	"github.com/ksonnet/ksonnet/pkg/actions"
bryanl's avatar
bryanl committed
20
	"github.com/ksonnet/ksonnet/pkg/client"
bryanl's avatar
bryanl committed
21 22 23
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
24 25
)

26 27 28 29
var (
	applyClientConfig *client.Config
)

30
const (
Jessica Yao's avatar
Jessica Yao committed
31 32
	applyShortDesc = "Apply local Kubernetes manifests (components) to remote clusters"
)
Jessica Yao's avatar
Jessica Yao committed
33

bryanl's avatar
bryanl committed
34 35 36 37 38 39 40 41
const (
	vApplyComponent = "apply-components"
	vApplyCreate    = "apply-create"
	vApplyGcTag     = "apply-gc-tag"
	vApplyDryRun    = "apply-dry-run"
	vApplySkipGc    = "apply-skip-gc"
)

42 43 44
func init() {
	RootCmd.AddCommand(applyCmd)

45
	applyClientConfig = client.NewDefaultClientConfig(ka)
46
	applyClientConfig.BindClientGoFlags(applyCmd)
bryanl's avatar
bryanl committed
47
	bindJsonnetFlags(applyCmd, "apply")
bryanl's avatar
bryanl committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

	applyCmd.Flags().StringSliceP(flagComponent, shortComponent, nil, "Name of a specific component (multiple -c flags accepted, allows YAML, JSON, and Jsonnet)")
	viper.BindPFlag(vApplyComponent, applyCmd.Flags().Lookup(flagComponent))

	applyCmd.Flags().Bool(flagCreate, true, "Option to create resources if they do not already exist on the cluster")
	viper.BindPFlag(vApplyCreate, applyCmd.Flags().Lookup(flagCreate))

	applyCmd.Flags().Bool(flagSkipGc, false, "Option to skip garbage collection, even with --"+flagGcTag+" specified")
	viper.BindPFlag(vApplySkipGc, applyCmd.Flags().Lookup(flagSkipGc))

	applyCmd.Flags().String(flagGcTag, "", "A tag that's (1) added to all updated objects (2) used to garbage collect existing objects that are no longer in the manifest")
	viper.BindPFlag(vApplyGcTag, applyCmd.Flags().Lookup(flagGcTag))

	applyCmd.Flags().Bool(flagDryRun, false, "Option to preview the list of operations without changing the cluster state")
	viper.BindPFlag(vApplyDryRun, applyCmd.Flags().Lookup(flagDryRun))
63 64 65
}

var applyCmd = &cobra.Command{
Jessica Yao's avatar
Jessica Yao committed
66 67
	Use:   "apply <env-name> [-c <component-name>] [--dry-run]",
	Short: applyShortDesc,
68
	RunE: func(cmd *cobra.Command, args []string) error {
bryanl's avatar
bryanl committed
69 70 71
		var envName string
		if len(args) == 1 {
			envName = args[0]
72
		}
73

bryanl's avatar
bryanl committed
74 75 76 77 78 79
		m := map[string]interface{}{
			actions.OptionApp:            ka,
			actions.OptionClientConfig:   applyClientConfig,
			actions.OptionComponentNames: viper.GetStringSlice(vApplyComponent),
			actions.OptionCreate:         viper.GetBool(vApplyCreate),
			actions.OptionDryRun:         viper.GetBool(vApplyDryRun),
bryanl's avatar
bryanl committed
80
			actions.OptionEnvName:        envName,
bryanl's avatar
bryanl committed
81 82
			actions.OptionGcTag:          viper.GetString(vApplyGcTag),
			actions.OptionSkipGc:         viper.GetBool(vApplySkipGc),
83 84
		}

bryanl's avatar
bryanl committed
85 86 87 88
		if err := extractJsonnetFlags("apply"); err != nil {
			return errors.Wrap(err, "handle jsonnet flags")
		}

bryanl's avatar
bryanl committed
89
		return runAction(actionApply, m)
90
	},
Jessica Yuen's avatar
Jessica Yuen committed
91
	Long: `
Jessica Yao's avatar
Jessica Yao committed
92 93 94 95 96 97 98
The ` + "`apply`" + `command uses local manifest(s) to update (and optionally create)
Kubernetes resources on a remote cluster. This cluster is determined by the
mandatory ` + "`<env-name>`" + ` argument.

The manifests themselves correspond to the components of your app, and reside
in your app's ` + "`components/`" + ` directory. When applied, the manifests are fully
expanded using the parameters of the specified environment.
99

Jessica Yao's avatar
Jessica Yao committed
100 101
By default, all component manifests are applied. To apply a subset of components,
use the ` + "`--component` " + `flag, as seen in the examples below.
Jessica Yuen's avatar
Jessica Yuen committed
102

Jessica Yao's avatar
Jessica Yao committed
103
Note that this command needs to be run *within* a ksonnet app directory.
104

Jessica Yuen's avatar
Jessica Yuen committed
105
### Related Commands
106

Jessica Yao's avatar
Jessica Yao committed
107 108
* ` + "`ks diff` " + `— ` + diffShortDesc + `
* ` + "`ks delete` " + `— ` + deleteShortDesc + `
109

Jessica Yuen's avatar
Jessica Yuen committed
110 111 112
### Syntax
`,
	Example: `
Jessica Yao's avatar
Jessica Yao committed
113 114 115
# Create or update all resources described in the ksonnet application, specifically
# the ones running in the 'dev' environment. This command works in any subdirectory
# of the app.
Jessica Yuen's avatar
Jessica Yuen committed
116
#
Jessica Yao's avatar
Jessica Yao committed
117
# This essentially deploys all components in the 'components/' directory.
Jessica Yuen's avatar
Jessica Yuen committed
118
ks apply dev
119

Jessica Yao's avatar
Jessica Yao committed
120 121 122 123 124 125
# Similar to the previous command, but does not immediately execute. Use this to
# see a preview of the cluster-changing actions.
ks apply dev --dry-run

# Create or update the single 'guestbook-ui' component of a ksonnet app, specifically
# the instance running in the 'dev' environment.
Jessica Yuen's avatar
Jessica Yuen committed
126
#
Jessica Yao's avatar
Jessica Yao committed
127
# This essentially deploys 'components/guestbook-ui.jsonnet'.
Jessica Yuen's avatar
Jessica Yuen committed
128 129
ks apply dev -c guestbook-ui

Jessica Yao's avatar
Jessica Yao committed
130 131 132
# Create or update multiple components in a ksonnet application (e.g. 'guestbook-ui'
# and 'ngin-depl') for the 'dev' environment. Does not create resources that are
# not already present on the cluster.
Jessica Yuen's avatar
Jessica Yuen committed
133
#
Jessica Yao's avatar
Jessica Yao committed
134 135 136
# This essentially deploys 'components/guestbook-ui.jsonnet' and
# 'components/nginx-depl.jsonnet'.
ks apply dev -c guestbook-ui -c nginx-depl --create false
Jessica Yuen's avatar
Jessica Yuen committed
137
`,
138
}