From 31eca372c9a228ca825c2dbc93a7880a8b06946f Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 7 Dec 2016 22:02:39 -0800 Subject: [PATCH] Add the ability to edit fields within a config map. --- .generated_docs | 2 + docs/man/man1/kubectl-edit-configmap.1 | 3 + .../kubectl/kubectl_edit_configmap.md | 7 ++ hack/verify-flags/known-flags.txt | 1 + pkg/kubectl/cmd/BUILD | 1 + pkg/kubectl/cmd/edit.go | 9 +- pkg/kubectl/cmd/edit_configmap.go | 96 +++++++++++++++++++ 7 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 docs/man/man1/kubectl-edit-configmap.1 create mode 100644 docs/user-guide/kubectl/kubectl_edit_configmap.md create mode 100644 pkg/kubectl/cmd/edit_configmap.go diff --git a/.generated_docs b/.generated_docs index 1d24a4e88ad..1ce648ed0d0 100644 --- a/.generated_docs +++ b/.generated_docs @@ -58,6 +58,7 @@ docs/man/man1/kubectl-create.1 docs/man/man1/kubectl-delete.1 docs/man/man1/kubectl-describe.1 docs/man/man1/kubectl-drain.1 +docs/man/man1/kubectl-edit-configmap.1 docs/man/man1/kubectl-edit.1 docs/man/man1/kubectl-exec.1 docs/man/man1/kubectl-explain.1 @@ -142,6 +143,7 @@ docs/user-guide/kubectl/kubectl_delete.md docs/user-guide/kubectl/kubectl_describe.md docs/user-guide/kubectl/kubectl_drain.md docs/user-guide/kubectl/kubectl_edit.md +docs/user-guide/kubectl/kubectl_edit_configmap.md docs/user-guide/kubectl/kubectl_exec.md docs/user-guide/kubectl/kubectl_explain.md docs/user-guide/kubectl/kubectl_expose.md diff --git a/docs/man/man1/kubectl-edit-configmap.1 b/docs/man/man1/kubectl-edit-configmap.1 new file mode 100644 index 00000000000..b6fd7a0f989 --- /dev/null +++ b/docs/man/man1/kubectl-edit-configmap.1 @@ -0,0 +1,3 @@ +This file is autogenerated, but we've stopped checking such files into the +repository to reduce the need for rebases. Please run hack/generate-docs.sh to +populate this file. diff --git a/docs/user-guide/kubectl/kubectl_edit_configmap.md b/docs/user-guide/kubectl/kubectl_edit_configmap.md new file mode 100644 index 00000000000..b80751781ef --- /dev/null +++ b/docs/user-guide/kubectl/kubectl_edit_configmap.md @@ -0,0 +1,7 @@ +This file is autogenerated, but we've stopped checking such files into the +repository to reduce the need for rebases. Please run hack/generate-docs.sh to +populate this file. + + +[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_edit_configmap.md?pixel)]() + diff --git a/hack/verify-flags/known-flags.txt b/hack/verify-flags/known-flags.txt index 124127c4ce2..51c6858863b 100644 --- a/hack/verify-flags/known-flags.txt +++ b/hack/verify-flags/known-flags.txt @@ -97,6 +97,7 @@ concurrent-resource-quota-syncs concurrent-serviceaccount-token-syncs concurrent-service-syncs config-map +config-map-data config-map-namespace config-sync-period configure-cloud-routes diff --git a/pkg/kubectl/cmd/BUILD b/pkg/kubectl/cmd/BUILD index 0d00ccdebb4..59b42011188 100644 --- a/pkg/kubectl/cmd/BUILD +++ b/pkg/kubectl/cmd/BUILD @@ -38,6 +38,7 @@ go_library( "describe.go", "drain.go", "edit.go", + "edit_configmap.go", "exec.go", "explain.go", "expose.go", diff --git a/pkg/kubectl/cmd/edit.go b/pkg/kubectl/cmd/edit.go index 28f188676a3..ca871a67178 100644 --- a/pkg/kubectl/cmd/edit.go +++ b/pkg/kubectl/cmd/edit.go @@ -100,12 +100,20 @@ func NewCmdEdit(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { Long: editLong, Example: fmt.Sprintf(editExample), Run: func(cmd *cobra.Command, args []string) { + args = append([]string{"configmap"}, args...) err := RunEdit(f, out, errOut, cmd, args, options) cmdutil.CheckErr(err) }, ValidArgs: validArgs, ArgAliases: argAliases, } + addEditFlags(cmd, options) + + cmd.AddCommand(NewCmdEditConfigMap(f, out, errOut)) + return cmd +} + +func addEditFlags(cmd *cobra.Command, options *resource.FilenameOptions) { usage := "to use to edit the resource" cmdutil.AddFilenameOptionFlags(cmd, options, usage) cmdutil.AddValidateFlags(cmd) @@ -115,7 +123,6 @@ func NewCmdEdit(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { cmdutil.AddApplyAnnotationFlags(cmd) cmdutil.AddRecordFlag(cmd) cmdutil.AddInclude3rdPartyFlags(cmd) - return cmd } func RunEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error { diff --git a/pkg/kubectl/cmd/edit_configmap.go b/pkg/kubectl/cmd/edit_configmap.go new file mode 100644 index 00000000000..60cc804d6fd --- /dev/null +++ b/pkg/kubectl/cmd/edit_configmap.go @@ -0,0 +1,96 @@ +/* +Copyright 2016 The Kubernetes 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 ( + "bytes" + "fmt" + "io" + "os" + + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/apis/meta/v1" + cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" + "k8s.io/kubernetes/pkg/kubectl/cmd/util/editor" + "k8s.io/kubernetes/pkg/kubectl/resource" +) + +// NewCmdEditConfigMap is a macro command to edit config maps +func NewCmdEditConfigMap(f cmdutil.Factory, cmdOut, errOut io.Writer) *cobra.Command { + options := &resource.FilenameOptions{} + + cmd := &cobra.Command{ + Use: "configmap", + Aliases: []string{"cm"}, + Short: "Edit a config map object.", + Long: "Edit and update a config map object", + Run: func(cmd *cobra.Command, args []string) { + RunEditConfigMap(cmd, f, args, cmdOut, errOut, options) + }, + } + + addEditFlags(cmd, options) + cmd.Flags().String("config-map-data", "", "If non-empty, specify the name of a data slot in a config map to edit.") + return cmd +} + +// RunEditConfigMap runs the edit command for config maps. It either edits the complete map +// or it edits individual files inside the config map. +func RunEditConfigMap(cmd *cobra.Command, f cmdutil.Factory, args []string, cmdOut, errOut io.Writer, options *resource.FilenameOptions) error { + dataFile := cmdutil.GetFlagString(cmd, "config-map-data") + if len(dataFile) == 0 { + // We need to add the resource type back on to the front + args = append([]string{"configmap"}, args...) + return RunEdit(f, cmdOut, errOut, cmd, args, options) + } + cmdNamespace, _, err := f.DefaultNamespace() + if err != nil { + return err + } + + cs, err := f.ClientSet() + if err != nil { + return err + } + configMap, err := cs.Core().ConfigMaps(cmdNamespace).Get(args[0], v1.GetOptions{}) + if err != nil { + return err + } + + value, found := configMap.Data[dataFile] + if !found { + keys := []string{} + for key := range configMap.Data { + keys = append(keys, key) + } + return fmt.Errorf("No such data file (%s), filenames are: %v\n", dataFile, keys) + } + edit := editor.NewDefaultEditor(os.Environ()) + data, file, err := edit.LaunchTempFile(fmt.Sprintf("%s-edit-", dataFile), "", bytes.NewBuffer([]byte(value))) + defer func() { + os.Remove(file) + }() + if err != nil { + return err + } + configMap.Data[dataFile] = string(data) + + if _, err := cs.Core().ConfigMaps(cmdNamespace).Update(configMap); err != nil { + return err + } + return nil +}