Skip to content
Snippets Groups Projects
Commit 8fc6c76c authored by Jessica Yuen's avatar Jessica Yuen
Browse files

Introduce commands: 'ks param' and 'ks param set'

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.

'ks param set' is defined as follows:
  'ks param set <component-name> <param-key> <param-value>'

Examples:

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'
parent e2d770a7
No related branches found
No related tags found
No related merge requests found
// 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`,
}
// 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 kubecfg
import (
"fmt"
"strconv"
log "github.com/sirupsen/logrus"
)
// ParamSetCmd stores the information necessary to set component and
// environment params.
type ParamSetCmd struct {
component string
env string
param string
value string
}
// NewParamSetCmd acts as a constructor for ParamSetCmd. It will also sanitize
// or "quote" the param value first if necessary.
func NewParamSetCmd(component, env, param, value string) *ParamSetCmd {
return &ParamSetCmd{component: component, env: env, param: param, value: sanitizeParamValue(value)}
}
// Run executes the setting of params.
func (c *ParamSetCmd) Run() error {
manager, err := manager()
if err != nil {
return err
}
if len(c.env) == 0 {
if err = manager.SetComponentParams(c.component, param.Params{c.param: c.value}); err == nil {
log.Infof("Parameter '%s' successfully set to '%s' for component '%s'", c.param, c.value, c.component)
}
} else {
if err = manager.SetEnvironmentParams(c.env, c.component, param.Params{c.param: c.value}); err == nil {
log.Infof("Parameter '%s' successfully set to '%s' for component '%s' in environment '%s'",
c.param, c.value, c.component, c.env)
}
}
return err
}
// sanitizeParamValue does a best effort to identify value types. It will put
// quotes around values which it categorizes as strings.
func sanitizeParamValue(value string) string {
// boolean
if value == "true" || value == "false" {
return value
}
// numeric
if _, err := strconv.ParseFloat(value, 64); err == nil {
return value
}
// string
return fmt.Sprintf(`"%s"`, value)
}
// 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 kubecfg
import (
"os"
"github.com/ksonnet/ksonnet/metadata"
)
func manager() (metadata.Manager, error) {
appDir, err := os.Getwd()
if err != nil {
return nil, err
}
appRoot := metadata.AbsPath(appDir)
return metadata.Find(appRoot)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment