Skip to content

Commit

Permalink
fix configmap name hardcode
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktming authored and yasker committed Aug 13, 2020
1 parent fc79f07 commit 3a7f437
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
5 changes: 3 additions & 2 deletions deploy/chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>path=$1<br>mkdir -m 0777 -p ${path} |
| `teardown` | Configuration of script to execute teardown operations on each node | #!/bin/sh<br>path=$1<br>rm -rf ${path} |
| `configmap.setup` | Configuration of script to execute setup operations on each node | #!/bin/sh<br>path=$1<br>mkdir -m 0777 -p ${path} |
| `configmap.teardown` | Configuration of script to execute teardown operations on each node | #!/bin/sh<br>path=$1<br>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,

Expand Down
6 changes: 3 additions & 3 deletions deploy/chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 }}
2 changes: 1 addition & 1 deletion deploy/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
23 changes: 14 additions & 9 deletions deploy/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -157,19 +163,23 @@ 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)
if helperImage == "" {
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
}
Expand Down
22 changes: 12 additions & 10 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,

Expand All @@ -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
Expand Down Expand Up @@ -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{
{
Expand Down

0 comments on commit 3a7f437

Please sign in to comment.