Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kubetest2-tf/data/k8s-ansible/install-k8s-perf.yml
Original file line number Diff line number Diff line change
@@ -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'] }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change part of the extravars modification? I might be missing something here. :|


- name: Install Runtime and Kubernetes
hosts:
Expand Down
34 changes: 22 additions & 12 deletions kubetest2-tf/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions kubetest2-tf/pkg/ansible/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
)
Expand Down