Skip to content

Commit

Permalink
config: Add flag to set config command to restart Cilium pods
Browse files Browse the repository at this point in the history
After config map changes are made, Cilium pods
need to be restarted for the changes to take effect.
Currently, this is a manual step. Add a flag with
default value to restart Cilium pods.

Signed-off-by: Aditi Ghag <[email protected]>
  • Loading branch information
aditighag committed Apr 13, 2021
1 parent d293ae3 commit 8cf61c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
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: %s\n", err)
} else {
fmt.Println("♻️ Restarted Cilium pods")
}

return nil
}
8 changes: 4 additions & 4 deletions internal/cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"

"github.com/cilium/cilium-cli/config"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -77,15 +76,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")

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

Expand All @@ -101,14 +100,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")
cmd.Flags().BoolVarP(&params.Restart, "restart", "r", true, "Restart Cilium pods")

return cmd
}

0 comments on commit 8cf61c6

Please sign in to comment.