Skip to content

Commit 16c8315

Browse files
committed
Fix parsing of extravars with multiple fields
1 parent 2fdcb58 commit 16c8315

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

kubetest2-tf/data/k8s-ansible/install-k8s-perf.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
- hosts: all
22
tasks:
33
- set_fact:
4-
prepull_images: "{{ prepull_images + ['registry.k8s.io/pause:3.10'] }}"
4+
prepull_images: "{{ (prepull_images | from_json if prepull_images is string else prepull_images) + ['registry.k8s.io/pause:3.10'] }}"
55

66
- name: Install Runtime and Kubernetes
77
hosts:

kubetest2-tf/deployer/deployer.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ type deployer struct {
7676
tmpDir string
7777
machineIPs []string
7878

79-
RepoRoot string `desc:"The path to the root of the local kubernetes repo. Necessary to call certain scripts. Defaults to the current directory. If operating in legacy mode, this should be set to the local kubernetes/kubernetes repo."`
80-
IgnoreClusterDir bool `desc:"Ignore the cluster folder if exists"`
81-
AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated"`
82-
RetryOnTfFailure int `desc:"Retry on Terraform Apply Failure"`
83-
BreakKubetestOnUpfail bool `desc:"Breaks kubetest2 when up fails"`
84-
Playbook string `desc:"Name of ansible playbook to be run"`
85-
ExtraVars map[string]string `desc:"Passes extra-vars to ansible playbook, enter a string of key=value pairs"`
86-
SetKubeconfig bool `desc:"Flag to set kubeconfig"`
87-
TargetProvider string `desc:"provider value to be used(powervs, vpc)"`
79+
RepoRoot string `desc:"The path to the root of the local kubernetes repo. Necessary to call certain scripts. Defaults to the current directory. If operating in legacy mode, this should be set to the local kubernetes/kubernetes repo."`
80+
IgnoreClusterDir bool `desc:"Ignore the cluster folder if exists"`
81+
AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated"`
82+
RetryOnTfFailure int `desc:"Retry on Terraform Apply Failure"`
83+
BreakKubetestOnUpfail bool `desc:"Breaks kubetest2 when up fails"`
84+
Playbook string `desc:"Name of ansible playbook to be run"`
85+
ExtraVars string `desc:"Passes extra-vars to ansible playbook, enter a comma-separated string of key:value pairs"`
86+
SetKubeconfig bool `desc:"Flag to set kubeconfig"`
87+
TargetProvider string `desc:"provider value to be used(powervs, vpc)"`
8888
}
8989

9090
func (d *deployer) Version() string {
@@ -248,13 +248,23 @@ func (d *deployer) Up() error {
248248
}
249249
klog.Infof("Ansible params exposed under groupvars: %v", string(ansibleParams))
250250
// Unmarshalling commonJSON into map to add extra-vars
251-
combinedAnsibleVars := map[string]string{}
251+
combinedAnsibleVars := map[string]interface{}{}
252252
if err = json.Unmarshal(ansibleParams, &combinedAnsibleVars); err != nil {
253253
return fmt.Errorf("failed to unmarshal ansible group variables: %v", err)
254254
}
255+
// Since the accepted extravars are comma-separated, in the format of key1:val1,key2:val2, split accordingly and
256+
// combine with the defaults.
257+
extraVarMap := map[string]interface{}{}
258+
vars := strings.Split(d.ExtraVars, ",")
259+
for _, val := range vars {
260+
keyVal := strings.SplitN(val, ":", 2)
261+
if len(keyVal) != 2 {
262+
return fmt.Errorf("malformed ansible group variable %s", val)
263+
}
264+
extraVarMap[keyVal[0]] = keyVal[1]
265+
}
255266

256-
// Add-in the extra-vars set to the final set.
257-
maps.Insert(combinedAnsibleVars, maps.All(d.ExtraVars))
267+
maps.Insert(combinedAnsibleVars, maps.All(extraVarMap))
258268
klog.Infof("Updated ansible variables with extra vars: %+v", combinedAnsibleVars)
259269
if err = ansible.Playbook(d.tmpDir, filepath.Join(d.tmpDir, "hosts"), d.Playbook, combinedAnsibleVars); err != nil {
260270
return fmt.Errorf("failed to run ansible playbook: %v", err)

kubetest2-tf/pkg/ansible/ansible.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@ const (
1717
ansibleDataDir = "k8s-ansible"
1818
)
1919

20-
func Playbook(dir, inventory, playbook string, extraVars map[string]string) error {
20+
func Playbook(dir, inventory, playbook string, extraVars map[string]interface{}) error {
2121
if err := unpackAnsible(dir); err != nil {
2222
return fmt.Errorf("failed to unpack the ansible code: %v", err)
2323
}
24-
extraVarsMap := make(map[string]interface{}, len(extraVars))
25-
for key, val := range extraVars {
26-
extraVarsMap[key] = val
27-
}
2824
klog.Infof("Running ansible playbook: %s", playbook)
2925
playbookCmd := ansibleplaybook.NewAnsiblePlaybookCmd(
3026
ansibleplaybook.WithPlaybooks(filepath.Join(dir, playbook)),
3127
ansibleplaybook.WithPlaybookOptions(
3228
&ansibleplaybook.AnsiblePlaybookOptions{
33-
ExtraVars: extraVarsMap,
29+
ExtraVars: extraVars,
3430
Inventory: inventory,
3531
}),
3632
)

0 commit comments

Comments
 (0)