diff --git a/kubetest2-tf/data/k8s-ansible/install-k8s-perf.yml b/kubetest2-tf/data/k8s-ansible/install-k8s-perf.yml index 55a8dbc..738eeac 100644 --- a/kubetest2-tf/data/k8s-ansible/install-k8s-perf.yml +++ b/kubetest2-tf/data/k8s-ansible/install-k8s-perf.yml @@ -1,7 +1,7 @@ - hosts: all tasks: - set_fact: - prepull_images: "{{ prepull_images + ['registry.k8s.io/pause:3.10'] }}" + prepull_images: "{{ (prepull_images | from_json if prepull_images is string else prepull_images) + ['registry.k8s.io/pause:3.10'] }}" - name: Install Runtime and Kubernetes hosts: diff --git a/kubetest2-tf/deployer/deployer.go b/kubetest2-tf/deployer/deployer.go index ee91376..525f46a 100644 --- a/kubetest2-tf/deployer/deployer.go +++ b/kubetest2-tf/deployer/deployer.go @@ -76,15 +76,15 @@ type deployer struct { tmpDir string machineIPs []string - 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."` - IgnoreClusterDir bool `desc:"Ignore the cluster folder if exists"` - AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated"` - RetryOnTfFailure int `desc:"Retry on Terraform Apply Failure"` - BreakKubetestOnUpfail bool `desc:"Breaks kubetest2 when up fails"` - Playbook string `desc:"Name of ansible playbook to be run"` - ExtraVars map[string]string `desc:"Passes extra-vars to ansible playbook, enter a string of key=value pairs"` - SetKubeconfig bool `desc:"Flag to set kubeconfig"` - TargetProvider string `desc:"provider value to be used(powervs, vpc)"` + 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."` + IgnoreClusterDir bool `desc:"Ignore the cluster folder if exists"` + AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated"` + RetryOnTfFailure int `desc:"Retry on Terraform Apply Failure"` + BreakKubetestOnUpfail bool `desc:"Breaks kubetest2 when up fails"` + Playbook string `desc:"Name of ansible playbook to be run"` + ExtraVars string `desc:"Passes extra-vars to ansible playbook, enter a comma-separated string of key:value pairs"` + SetKubeconfig bool `desc:"Flag to set kubeconfig"` + TargetProvider string `desc:"provider value to be used(powervs, vpc)"` } func (d *deployer) Version() string { @@ -248,13 +248,23 @@ func (d *deployer) Up() error { } klog.Infof("Ansible params exposed under groupvars: %v", string(ansibleParams)) // Unmarshalling commonJSON into map to add extra-vars - combinedAnsibleVars := map[string]string{} + combinedAnsibleVars := map[string]interface{}{} if err = json.Unmarshal(ansibleParams, &combinedAnsibleVars); err != nil { return fmt.Errorf("failed to unmarshal ansible group variables: %v", err) } + // Since the accepted extravars are comma-separated, in the format of key1:val1,key2:val2, split accordingly and + // combine with the defaults. + extraVarMap := map[string]interface{}{} + vars := strings.Split(d.ExtraVars, ",") + for _, val := range vars { + keyVal := strings.SplitN(val, ":", 2) + if len(keyVal) != 2 { + return fmt.Errorf("malformed ansible group variable %s", val) + } + extraVarMap[keyVal[0]] = keyVal[1] + } - // Add-in the extra-vars set to the final set. - maps.Insert(combinedAnsibleVars, maps.All(d.ExtraVars)) + maps.Insert(combinedAnsibleVars, maps.All(extraVarMap)) klog.Infof("Updated ansible variables with extra vars: %+v", combinedAnsibleVars) if err = ansible.Playbook(d.tmpDir, filepath.Join(d.tmpDir, "hosts"), d.Playbook, combinedAnsibleVars); err != nil { return fmt.Errorf("failed to run ansible playbook: %v", err) diff --git a/kubetest2-tf/pkg/ansible/ansible.go b/kubetest2-tf/pkg/ansible/ansible.go index 6782c37..607de1a 100644 --- a/kubetest2-tf/pkg/ansible/ansible.go +++ b/kubetest2-tf/pkg/ansible/ansible.go @@ -17,20 +17,16 @@ const ( ansibleDataDir = "k8s-ansible" ) -func Playbook(dir, inventory, playbook string, extraVars map[string]string) error { +func Playbook(dir, inventory, playbook string, extraVars map[string]interface{}) error { if err := unpackAnsible(dir); err != nil { return fmt.Errorf("failed to unpack the ansible code: %v", err) } - extraVarsMap := make(map[string]interface{}, len(extraVars)) - for key, val := range extraVars { - extraVarsMap[key] = val - } klog.Infof("Running ansible playbook: %s", playbook) playbookCmd := ansibleplaybook.NewAnsiblePlaybookCmd( ansibleplaybook.WithPlaybooks(filepath.Join(dir, playbook)), ansibleplaybook.WithPlaybookOptions( &ansibleplaybook.AnsiblePlaybookOptions{ - ExtraVars: extraVarsMap, + ExtraVars: extraVars, Inventory: inventory, }), )