delete.go 3.21 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.

bryanl's avatar
bryanl committed
16
package clicmd
Angus Lees's avatar
Angus Lees committed
17 18

import (
bryanl's avatar
bryanl committed
19
	"github.com/spf13/viper"
20

Angus Lees's avatar
Angus Lees committed
21 22
	"github.com/spf13/cobra"

bryanl's avatar
bryanl committed
23
	"github.com/ksonnet/ksonnet/actions"
bryanl's avatar
bryanl committed
24
	"github.com/ksonnet/ksonnet/pkg/client"
Angus Lees's avatar
Angus Lees committed
25 26 27
)

const (
Jessica Yao's avatar
Jessica Yao committed
28
	deleteShortDesc = "Remove component-specified Kubernetes resources from remote clusters"
Angus Lees's avatar
Angus Lees committed
29 30
)

bryanl's avatar
bryanl committed
31 32 33 34 35
const (
	vDeleteComponent   = "delete-components"
	vDeleteGracePeriod = "delete-grace-period"
)

36 37 38 39
var (
	deleteClientConfig *client.Config
)

Angus Lees's avatar
Angus Lees committed
40 41
func init() {
	RootCmd.AddCommand(deleteCmd)
bryanl's avatar
bryanl committed
42

43 44
	deleteClientConfig = client.NewDefaultClientConfig()
	deleteClientConfig.BindClientGoFlags(deleteCmd)
45
	bindJsonnetFlags(deleteCmd)
bryanl's avatar
bryanl committed
46 47 48 49 50 51

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

	deleteCmd.Flags().Int64(flagGracePeriod, -1, "Number of seconds given to resources to terminate gracefully. A negative value is ignored")
	viper.BindPFlag(vDeleteGracePeriod, deleteCmd.Flags().Lookup(flagGracePeriod))
Angus Lees's avatar
Angus Lees committed
52 53 54
}

var deleteCmd = &cobra.Command{
Jessica Yao's avatar
Jessica Yao committed
55 56
	Use:   "delete [env-name] [-c <component-name>]",
	Short: deleteShortDesc,
Angus Lees's avatar
Angus Lees committed
57
	RunE: func(cmd *cobra.Command, args []string) error {
bryanl's avatar
bryanl committed
58 59 60
		var envName string
		if len(args) == 1 {
			envName = args[0]
61
		}
Angus Lees's avatar
Angus Lees committed
62

bryanl's avatar
bryanl committed
63 64 65 66
		m := map[string]interface{}{
			actions.OptionApp:            ka,
			actions.OptionClientConfig:   deleteClientConfig,
			actions.OptionComponentNames: viper.GetStringSlice(vDeleteComponent),
bryanl's avatar
bryanl committed
67
			actions.OptionEnvName:        envName,
bryanl's avatar
bryanl committed
68
			actions.OptionGracePeriod:    viper.GetInt64(vDeleteGracePeriod),
Angus Lees's avatar
Angus Lees committed
69 70
		}

bryanl's avatar
bryanl committed
71
		return runAction(actionDelete, m)
Angus Lees's avatar
Angus Lees committed
72
	},
Jessica Yao's avatar
Jessica Yao committed
73
	Long: `
Jessica Yao's avatar
Jessica Yao committed
74 75
The ` + "`delete`" + ` command removes Kubernetes resources (described in local
*component* manifests) from a cluster. This cluster is determined by the mandatory
Jessica Yao's avatar
Jessica Yao committed
76
` + "`<env-name>`" + `argument.
Alex Clemmer's avatar
Alex Clemmer committed
77

Jessica Yao's avatar
Jessica Yao committed
78 79 80 81
An entire ksonnet application can be removed from a cluster, or just its specific
components.

**This command can be considered the inverse of the ` + "`ks apply`" + ` command.**
Alex Clemmer's avatar
Alex Clemmer committed
82

Jessica Yao's avatar
Jessica Yao committed
83
### Related Commands
Alex Clemmer's avatar
Alex Clemmer committed
84

Jessica Yao's avatar
Jessica Yao committed
85 86 87 88 89 90 91 92 93
* ` + "`ks diff` " + `— Compare manifests, based on environment or location (local or remote)
* ` + "`ks apply` " + `— ` + applyShortDesc + `

### Syntax
`,
	Example: `# Delete resources from the 'dev' environment, based on ALL of the manifests in your
# ksonnet app's 'components/' directory. This command works in any subdirectory
# of the app.
ks delete dev
Alex Clemmer's avatar
Alex Clemmer committed
94

Jessica Yao's avatar
Jessica Yao committed
95 96 97 98
# Delete resources described by the 'nginx' component. $KUBECONFIG is overridden by
# the CLI-specified './kubeconfig', so these changes are deployed to the current
# context's cluster (not the 'default' environment)
ks delete --kubeconfig=./kubeconfig -c nginx`,
Angus Lees's avatar
Angus Lees committed
99
}