param.go 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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
// 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.

package cmd

import (
	"fmt"

	"github.com/spf13/cobra"

	"github.com/ksonnet/ksonnet/pkg/kubecfg"
)

const (
	flagParamEnv = "env"
)

func init() {
	RootCmd.AddCommand(paramCmd)

	paramCmd.AddCommand(paramSetCmd)

	paramSetCmd.PersistentFlags().String(flagParamEnv, "", "Specify environment to set parameters for")
}

var paramCmd = &cobra.Command{
	Use:   "param",
	Short: `Manage ksonnet component parameters`,
	Long: `Parameters are the customizable fields defining ksonnet components. For
example, replica count, component name, or deployment image.

Parameters are also able to be defined separately across environments. Meaning,
this supports features to allow a "development" environment to only run a
single replication instance for it's components, whereas allowing a "production"
environment to run more replication instances to meet heavier production load
demands.

Environments are ksonnet "named clusters". For more information on environments,
run:
  ks env --help
`,
	RunE: func(cmd *cobra.Command, args []string) error {
		return fmt.Errorf("Command 'param' requires a subcommand\n\n%s", cmd.UsageString())
	},
}

var paramSetCmd = &cobra.Command{
	Use:   "set <component-name> <param-key> <param-value>",
	Short: "Set component or environment parameters such as replica count or name",
	RunE: func(cmd *cobra.Command, args []string) error {
		flags := cmd.Flags()
		if len(args) != 3 {
			return fmt.Errorf("'param set' takes exactly three arguments, the name of the component, and the key and value of the parameter, respectively")
		}

		component := args[0]
		param := args[1]
		value := args[2]

		env, err := flags.GetString(flagParamEnv)
		if err != nil {
			return err
		}

		c := kubecfg.NewParamSetCmd(component, env, param, value)

		return c.Run()
	},
	Long: `"Set component or environment parameters such as replica count or name.

Parameters are set individually, one at a time. If you require customization of
more fields, we suggest that you modify your ksonnet project's
'components/params.libsonnet' file directly. Likewise, for greater customization
of environment parameters, we suggest modifying the
'environments/:name/params.libsonnet' file.
`,
	Example: `  # Updates the replica count of the 'guestbook' component to 4.
  ks param set guestbook replicas 4

  # Updates the replica count of the 'guestbook' component to 2 for the environment
  # 'dev'
  ks param set guestbook replicas 2 --env=dev`,
}