Skip to content

Conversation

@kishen-v
Copy link
Contributor

@kishen-v kishen-v commented Mar 17, 2025

This PR simplifies implementations for executing terraform/ansible sections through modules and has been tested end-to-end with available options.

Fixes: #9

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 17, 2025
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 17, 2025
fmt.Sprintf("-state=%s", filepath.Join(dir, StateFileName)),
fmt.Sprintf("-state-out=%s", filepath.Join(dir, StateFileName)),
}
if autoApprove {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The module has no support for the -auto-approve option and is baked into the module for all terraform operations.
For e.g: tf.Apply

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:"Terraform Auto Approve"`
AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since auto-approved is no longer supported, setting/unsetting will not have any effect on the execution.

Copy link
Member

Choose a reason for hiding this comment

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

interesting to see that they are always setting the auto-approve in the code - https://github.com/hashicorp/terraform-exec/blob/7ae12dd2176573ec55c9b5c84542d1b466efce54/tfexec/apply.go#L176

Maybe for later: explore whether we can influence the community to support this option, as it would be helpful for a dry‑run test to see what changes are occurring before the actual run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an issue on the terraform exec repo just to get an opinion!

@kishen-v kishen-v changed the title [WIP] Use ansible/terraform modules over go/exec Use ansible/terraform modules over go/exec Mar 18, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 18, 2025
@kishen-v kishen-v marked this pull request as draft March 18, 2025 09:11
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 18, 2025
@mkumatag
Copy link
Member

@kishen-v can you please create an issue and add all the information about the change you are trying to make and why?

@kishen-v kishen-v force-pushed the tf-ansible-modules branch from 646bb99 to 7dfad1b Compare March 18, 2025 13:22
@kishen-v kishen-v marked this pull request as ready for review March 18, 2025 14:02
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 18, 2025
@mkumatag
Copy link
Member

/hold
wait till we have some conformance job running

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 25, 2025
@kishen-v kishen-v force-pushed the tf-ansible-modules branch from d18dec0 to e9373c4 Compare April 15, 2025 10:39
@kishen-v kishen-v changed the title Use ansible/terraform modules over go/exec [WIP]Use ansible/terraform modules over go/exec Apr 15, 2025
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 15, 2025
@kishen-v kishen-v force-pushed the tf-ansible-modules branch from e9373c4 to e5cc4b6 Compare April 15, 2025 15:49
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 17, 2025
@kishen-v kishen-v force-pushed the tf-ansible-modules branch from e5cc4b6 to e6e7cd8 Compare May 5, 2025 08:23
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 5, 2025
@kishen-v kishen-v force-pushed the tf-ansible-modules branch from e6e7cd8 to 4997a7b Compare July 8, 2025 14:23
@kishen-v kishen-v changed the title [WIP]Use ansible/terraform modules over go/exec Use ansible/terraform modules over go/exec Jul 8, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 8, 2025
@kishen-v
Copy link
Contributor Author

kishen-v commented Jul 9, 2025

/test pull-provider-ibmcloud-test-infra-kubernetes

@kishen-v
Copy link
Contributor Author

kishen-v commented Jul 9, 2025

/cc @Prajyot-Parab
Thanks!

@Prajyot-Parab
Copy link
Member

Prajyot-Parab commented Aug 21, 2025

/lgtm
/cc @Amulyam24

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 21, 2025
Copy link
Contributor

@Amulyam24 Amulyam24 left a comment

Choose a reason for hiding this comment

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

Golang changes are okay, I might not be the right person to review the flow, will defer it to @Rajalakshmi-Girish, Thanks @kishen-v!

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:"Terraform Auto Approve"`
AutoApprove bool `desc:"Auto-approve the deployment of infrastructure through terraform" flag:",deprecated"`
Copy link
Member

Choose a reason for hiding this comment

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

interesting to see that they are always setting the auto-approve in the code - https://github.com/hashicorp/terraform-exec/blob/7ae12dd2176573ec55c9b5c84542d1b466efce54/tfexec/apply.go#L176

Maybe for later: explore whether we can influence the community to support this option, as it would be helpful for a dry‑run test to see what changes are occurring before the actual run.

// successful execution of ansible playbook
return 0, nil
extraVarsMap := make(map[string]interface{}, len(extraVars))
for key, val := range extraVars {
Copy link
Member

Choose a reason for hiding this comment

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

this mapping can be avoid if change the datatype for the extraVars to map[string]any.

Here is the diff, see if this works:

diff --git a/kubetest2-tf/deployer/deployer.go b/kubetest2-tf/deployer/deployer.go
index dd1887a..c1936b3 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             map[string]any `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)"`
 }
 
 func (d *deployer) Version() string {
@@ -247,7 +247,7 @@ 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]any{}
 	if err = json.Unmarshal(ansibleParams, &combinedAnsibleVars); err != nil {
 		return fmt.Errorf("failed to unmarshal ansible group variables: %v", err)
 	}
diff --git a/kubetest2-tf/pkg/ansible/ansible.go b/kubetest2-tf/pkg/ansible/ansible.go
index 6782c37..31f7ef8 100644
--- a/kubetest2-tf/pkg/ansible/ansible.go
+++ b/kubetest2-tf/pkg/ansible/ansible.go
@@ -17,20 +17,20 @@ const (
 	ansibleDataDir = "k8s-ansible"
 )
 
-func Playbook(dir, inventory, playbook string, extraVars map[string]string) error {
+func Playbook(dir, inventory, playbook string, extraVars map[string]any) 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
-	}
+	// 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,
 			}),
 	)

Copy link
Member

Choose a reason for hiding this comment

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

same can be used in the other place as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had tried this approach earlier, It ends up with the CLI returning an error that --extra-vars is an unknown flag:
spf13/pflag#375 - Generics aren't supported yet.😞 Given that the repo uses pflag as the underlying package to support flags..

Flag --auto-approve has been deprecated, Auto-approve the deployment of infrastructure through terraform
Error: unknown flag: --extra-vars

Usage:
  kubetest2 tf [Flags] [DeployerFlags] -- [TesterArgs]

@kishen-v
Copy link
Contributor Author

/hold for addressing review comments and testing.

@kishen-v
Copy link
Contributor Author

/test pull-provider-ibmcloud-test-infra-kubernetes

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 1, 2025
@kishen-v
Copy link
Contributor Author

kishen-v commented Sep 1, 2025

/test pull-provider-ibmcloud-test-infra-kubernetes

@kishen-v
Copy link
Contributor Author

kishen-v commented Sep 2, 2025

/test pull-provider-ibmcloud-test-infra-kubernetes
Squashed all changes with a bit of idiomatic handling of errs.

@mkumatag
Copy link
Member

mkumatag commented Sep 2, 2025

@kishen-v: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-provider-ibmcloud-test-infra-kubernetes 2f30e64 link true /test pull-provider-ibmcloud-test-infra-kubernetes
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

@kishen-v I see some error while tf apply retry flow, not sure if this is because of this change, ptal

@kishen-v
Copy link
Contributor Author

kishen-v commented Sep 2, 2025

Hi @mkumatag, I hadn't changed much around the Apply/Retry logic.. I did run the deployer in my local with the latest changes, it worked as expected.. However in the prow logs, I see that the LPAR deployment timed out.. Should it be worth retesting?

@kishen-v
Copy link
Contributor Author

kishen-v commented Sep 2, 2025

/test pull-provider-ibmcloud-test-infra-kubernetes

@kishen-v kishen-v requested a review from mkumatag September 2, 2025 12:40
@kishen-v
Copy link
Contributor Author

kishen-v commented Sep 2, 2025

/unhold

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 2, 2025
Copy link
Member

@mkumatag mkumatag left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 3, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kishen-v, mkumatag

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 3, 2025
@k8s-ci-robot k8s-ci-robot merged commit de48b01 into kubernetes-sigs:main Sep 3, 2025
4 of 5 checks passed
@kishen-v kishen-v deleted the tf-ansible-modules branch September 3, 2025 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate from go.exec to Go Modules for Ansible and Terraform Integration

5 participants