Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add restart flag to config set and delete commands #154

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
type k8sConfigImplementation interface {
GetConfigMap(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*corev1.ConfigMap, error)
PatchConfigMap(ctx context.Context, namespace, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*corev1.ConfigMap, error)
DeletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
}

type K8sConfig struct {
Expand All @@ -41,6 +42,7 @@ type K8sConfig struct {

type Parameters struct {
Namespace string
Restart bool
Writer io.Writer
}

Expand All @@ -55,26 +57,32 @@ func (k *K8sConfig) Log(format string, a ...interface{}) {
fmt.Fprintf(k.params.Writer, format+"\n", a...)
}

func (k *K8sConfig) Set(ctx context.Context, key, value string) error {
func (k *K8sConfig) Set(ctx context.Context, key, value string, params Parameters) error {
patch := []byte(`{"data":{"` + key + `":"` + value + `"}}`)

k.Log("✨ Patching ConfigMap %s with %s=%s...", defaults.ConfigMapName, key, value)
_, err := k.client.PatchConfigMap(ctx, k.params.Namespace, defaults.ConfigMapName, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
if err != nil {
return fmt.Errorf("unable to patch ConfigMap %s with patch %q: %w", defaults.ConfigMapName, patch, err)
}
if err = k.restartPodsUponConfigChange(params); err != nil {
return err
}

return nil
}

func (k *K8sConfig) Delete(ctx context.Context, key string) error {
func (k *K8sConfig) Delete(ctx context.Context, key string, params Parameters) error {
patch := []byte(`[{"op": "remove", "path": "/data/` + key + `"}]`)

k.Log("✨ Removing key %s from ConfigMap %s...", key, defaults.ConfigMapName)
_, err := k.client.PatchConfigMap(ctx, k.params.Namespace, defaults.ConfigMapName, types.JSONPatchType, patch, metav1.PatchOptions{})
if err != nil {
return fmt.Errorf("unable to patch ConfigMap %s with patch %q: %w", defaults.ConfigMapName, patch, err)
}
if err = k.restartPodsUponConfigChange(params); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -103,3 +111,18 @@ func (k *K8sConfig) View(ctx context.Context) (string, error) {

return buf.String(), nil
}

func (k *K8sConfig) restartPodsUponConfigChange(params Parameters) error {
if !params.Restart {
fmt.Println("⚠️ Restart Cilium pods for configmap changes to take effect")
return nil
}
if err := k.client.DeletePodCollection(context.Background(), params.Namespace,
metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: defaults.CiliumPodSelector}); err != nil {
return fmt.Errorf("⚠️ unable to restart Cilium pods: %v", err)
} else {
fmt.Println("♻️ Restarted Cilium pods")
}

return nil
}
7 changes: 4 additions & 3 deletions internal/cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ func newCmdConfigSet() *cobra.Command {
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
check := config.NewK8sConfig(k8sClient, params)
if err := check.Set(context.Background(), args[0], args[1]); err != nil {
if err := check.Set(context.Background(), args[0], args[1], params); err != nil {
fatalf("Unable to set config: %s", err)
}
return nil
},
}

cmd.Flags().StringVarP(&params.Namespace, "namespace", "n", "kube-system", "Namespace Cilium is running in")
aditighag marked this conversation as resolved.
Show resolved Hide resolved

cmd.Flags().BoolVarP(&params.Restart, "restart", "r", true, "Restart Cilium pods")
return cmd
}

Expand All @@ -101,14 +101,15 @@ func newCmdConfigDelete() *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
check := config.NewK8sConfig(k8sClient, params)
if err := check.Delete(context.Background(), args[0]); err != nil {
if err := check.Delete(context.Background(), args[0], params); err != nil {
fatalf("Unable to delete config: %s", err)
}
return nil
},
}

cmd.Flags().StringVarP(&params.Namespace, "namespace", "n", "kube-system", "Namespace Cilium is running in")
aditighag marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().BoolVarP(&params.Restart, "restart", "r", true, "Restart Cilium pods")

return cmd
}