From 3a7f43781efb498342e079a285ad4fdbc7859100 Mon Sep 17 00:00:00 2001 From: nicktming <2279563278@qq.com> Date: Tue, 11 Aug 2020 10:08:58 +0800 Subject: [PATCH] fix configmap name hardcode --- deploy/chart/README.md | 5 +++-- deploy/chart/templates/configmap.yaml | 6 +++--- deploy/chart/templates/deployment.yaml | 2 +- deploy/chart/values.yaml | 23 ++++++++++++++--------- main.go | 20 +++++++++++++++----- provisioner.go | 22 ++++++++++++---------- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/deploy/chart/README.md b/deploy/chart/README.md index c611bb0c..e1bc502c 100644 --- a/deploy/chart/README.md +++ b/deploy/chart/README.md @@ -71,8 +71,9 @@ default values. | `nodeSelector` | Node labels for Local Path Provisioner pod assignment | `{}` | | `tolerations` | Node taints to tolerate | `[]` | | `affinity` | Pod affinity | `{}` | -| `setup` | Configuration of script to execute setup operations on each node | #!/bin/sh
path=$1
mkdir -m 0777 -p ${path} | -| `teardown` | Configuration of script to execute teardown operations on each node | #!/bin/sh
path=$1
rm -rf ${path} | +| `configmap.setup` | Configuration of script to execute setup operations on each node | #!/bin/sh
path=$1
mkdir -m 0777 -p ${path} | +| `configmap.teardown` | Configuration of script to execute teardown operations on each node | #!/bin/sh
path=$1
rm -rf ${path} | +| `configmap.name` | configmap name | `local-path-config` | Specify each parameter using the `--set key=value[,key=value]` argument to `helm install`. For example, diff --git a/deploy/chart/templates/configmap.yaml b/deploy/chart/templates/configmap.yaml index 672c8d52..ad9f2bc7 100644 --- a/deploy/chart/templates/configmap.yaml +++ b/deploy/chart/templates/configmap.yaml @@ -1,7 +1,7 @@ apiVersion: v1 kind: ConfigMap metadata: - name: {{ include "local-path-provisioner.fullname" . }} + name: {{ .Values.configmap.name }} labels: {{ include "local-path-provisioner.labels" . | indent 4 }} data: @@ -12,6 +12,6 @@ data: "nodePathMap": {{ .Values.nodePathMap | toPrettyJson | nindent 8 }} } setup: |- - {{ .Values.setup | nindent 4 }} + {{ .Values.configmap.setup | nindent 4 }} teardown: |- - {{ .Values.teardown | nindent 4 }} + {{ .Values.configmap.teardown | nindent 4 }} diff --git a/deploy/chart/templates/deployment.yaml b/deploy/chart/templates/deployment.yaml index 2c3cb769..fecb14d4 100644 --- a/deploy/chart/templates/deployment.yaml +++ b/deploy/chart/templates/deployment.yaml @@ -46,7 +46,7 @@ spec: volumes: - name: config-volume configMap: - name: {{ include "local-path-provisioner.fullname" . }} + name: {{ .Values.configmap.name }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} diff --git a/deploy/chart/values.yaml b/deploy/chart/values.yaml index a49c7bac..6465961e 100644 --- a/deploy/chart/values.yaml +++ b/deploy/chart/values.yaml @@ -79,12 +79,17 @@ tolerations: [] affinity: {} -setup: |- - #!/bin/sh - path=$1 - mkdir -m 0777 -p ${path} - -teardown: |- - #!/bin/sh - path=$1 - rm -rf ${path} +configmap: + # specify the config map name + name: local-path-config + # specify the custom script for setup and teardown + setup: |- + #!/bin/sh + path=$1 + mkdir -m 0777 -p ${path} + + teardown: |- + #!/bin/sh + path=$1 + rm -rf ${path} + diff --git a/main.go b/main.go index fd4792f9..dfb56011 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ var ( DefaultKubeConfigFilePath = ".kube/config" DefaultConfigFileKey = "config.json" DefaultConfigMapName = "local-path-config" + FlagConfigMapName = "configmap-name" ) func cmdNotFound(c *cli.Context, command string) { @@ -87,6 +88,11 @@ func StartCmd() cli.Command { Usage: "Paths to a kubeconfig. Only required when it is out-of-cluster.", Value: "", }, + cli.StringFlag{ + Name: FlagConfigMapName, + Usage: "Required. Specify configmap name.", + Value: DefaultConfigMapName, + }, }, Action: func(c *cli.Context) { if err := startDaemon(c); err != nil { @@ -118,8 +124,8 @@ func loadConfig(kubeconfig string) (*rest.Config, error) { return clientcmd.BuildConfigFromFlags("", kubeconfig) } -func findConfigFileFromConfigMap(kubeClient clientset.Interface, namespace string) (string, error) { - cm, err := kubeClient.CoreV1().ConfigMaps(namespace).Get(DefaultConfigMapName, metav1.GetOptions{}) +func findConfigFileFromConfigMap(kubeClient clientset.Interface, namespace, configMapName string) (string, error) { + cm, err := kubeClient.CoreV1().ConfigMaps(namespace).Get(configMapName, metav1.GetOptions{}) if err != nil { return "", err } @@ -157,11 +163,15 @@ func startDaemon(c *cli.Context) error { if namespace == "" { return fmt.Errorf("invalid empty flag %v", FlagNamespace) } + configMapName := c.String(FlagConfigMapName) + if configMapName == "" { + return fmt.Errorf("invalid empty flag %v", FlagConfigMapName) + } configFile := c.String(FlagConfigFile) if configFile == "" { - configFile, err = findConfigFileFromConfigMap(kubeClient, namespace) + configFile, err = findConfigFileFromConfigMap(kubeClient, namespace, configMapName) if err != nil { - return fmt.Errorf("invalid empty flag %v and it also does not exist at ConfigMap %v/%v", FlagConfigFile, namespace, DefaultConfigMapName) + return fmt.Errorf("invalid empty flag %v and it also does not exist at ConfigMap %v/%v with err: %v", FlagConfigFile, namespace, configMapName, err) } } helperImage := c.String(FlagHelperImage) @@ -169,7 +179,7 @@ func startDaemon(c *cli.Context) error { return fmt.Errorf("invalid empty flag %v", FlagHelperImage) } - provisioner, err := NewProvisioner(stopCh, kubeClient, configFile, namespace, helperImage) + provisioner, err := NewProvisioner(stopCh, kubeClient, configFile, namespace, helperImage, configMapName) if err != nil { return err } diff --git a/provisioner.go b/provisioner.go index 6c2260f5..d36f2945 100644 --- a/provisioner.go +++ b/provisioner.go @@ -44,10 +44,11 @@ type LocalPathProvisioner struct { namespace string helperImage string - config *Config - configData *ConfigData - configFile string - configMutex *sync.RWMutex + config *Config + configData *ConfigData + configFile string + configMapName string + configMutex *sync.RWMutex } type NodePathMapData struct { @@ -67,7 +68,7 @@ type Config struct { NodePathMap map[string]*NodePathMap } -func NewProvisioner(stopCh chan struct{}, kubeClient *clientset.Clientset, configFile, namespace, helperImage string) (*LocalPathProvisioner, error) { +func NewProvisioner(stopCh chan struct{}, kubeClient *clientset.Clientset, configFile, namespace, helperImage, configMapName string) (*LocalPathProvisioner, error) { p := &LocalPathProvisioner{ stopCh: stopCh, @@ -76,10 +77,11 @@ func NewProvisioner(stopCh chan struct{}, kubeClient *clientset.Clientset, confi helperImage: helperImage, // config will be updated shortly by p.refreshConfig() - config: nil, - configFile: configFile, - configData: nil, - configMutex: &sync.RWMutex{}, + config: nil, + configFile: configFile, + configData: nil, + configMapName: configMapName, + configMutex: &sync.RWMutex{}, } if err := p.refreshConfig(); err != nil { return nil, err @@ -368,7 +370,7 @@ func (p *LocalPathProvisioner) createHelperPod(action ActionType, cmdsForPath [] VolumeSource: v1.VolumeSource{ ConfigMap: &v1.ConfigMapVolumeSource{ LocalObjectReference: v1.LocalObjectReference{ - Name: "local-path-config", + Name: p.configMapName, }, Items: []v1.KeyToPath{ {