Skip to content

Commit 553253f

Browse files
committed
wip: configurable terraform aws version
1 parent 39d9e43 commit 553253f

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

controllers/controlplane/kopscontrolplane_controller.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type KopsControlPlaneReconciler struct {
9595
Recorder record.EventRecorder
9696
TfExecPath string
9797
DryRun bool
98+
AWSProviderVersion string
9899
GetKopsClientSetFactory func(configBase string) (simple.Clientset, error)
99100
BuildCloudFactory func(*kopsapi.Cluster) (fi.Cloud, error)
100101
PopulateClusterSpecFactory func(ctx context.Context, kopsCluster *kopsapi.Cluster, kopsClientset simple.Clientset, cloud fi.Cloud) (*kopsapi.Cluster, error)
@@ -748,7 +749,12 @@ func (r *KopsControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Req
748749
}
749750

750751
// This is needed because this is normally created, but when deleting we don't create the kops resources files
751-
err = utils.CreateTerraformFilesFromTemplate("templates/provider.tf.tpl", "provider.tf", terraformOutputDir, nil)
752+
providerData := struct {
753+
Version string
754+
}{
755+
Version: r.AWSProviderVersion,
756+
}
757+
err = utils.CreateTerraformFilesFromTemplate("templates/provider.tf.tpl", "provider.tf", terraformOutputDir, providerData)
752758
if err != nil {
753759
return resultError, err
754760
}
@@ -939,6 +945,14 @@ func (r *KopsControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Req
939945
return resultError, err
940946
}
941947

948+
// Modify existing Terraform files to add AWS provider version constraint if specified
949+
if r.AWSProviderVersion != "" {
950+
err = utils.ModifyTerraformProviderVersion(terraformOutputDir, r.AWSProviderVersion)
951+
if err != nil {
952+
return resultError, err
953+
}
954+
}
955+
942956
// Only Apply resources if DryRun isn't set from command line
943957
if r.DryRun {
944958
reconciler.log.Info(fmt.Sprintf("planning Terraform for %s", kopsControlPlane.ObjectMeta.GetName()))

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ func main() {
7171
var probeAddr string
7272
var controllerClass string
7373
var dryRun bool
74+
var awsProviderVersion string
7475
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
7576
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
7677
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
7778
"Enable leader election for controller manager. "+
7879
"Enabling this will ensure there is only one active controller manager.")
7980
flag.StringVar(&controllerClass, "controller-class", "", "The name of the controller class to associate with the controller.")
8081
flag.BoolVar(&dryRun, "dry-run", false, "Enable dry-run mode to plan without making actual changes.")
82+
flag.StringVar(&awsProviderVersion, "aws-provider-version", "", "The version of the AWS provider to use in Terraform templates.")
8183

8284
opts := zap.Options{
8385
Development: true,
@@ -162,6 +164,7 @@ func main() {
162164
Recorder: recorder,
163165
TfExecPath: tfExecPath,
164166
DryRun: dryRun,
167+
AWSProviderVersion: awsProviderVersion,
165168
GetKopsClientSetFactory: utils.GetKopsClientset,
166169
BuildCloudFactory: utils.BuildCloud,
167170
PopulateClusterSpecFactory: controlplane.PopulateClusterSpec,

pkg/utils/terraform_utils.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,49 @@ func CreateAdditionalTerraformFiles(tfFiles ...Template) error {
6161
return nil
6262
}
6363

64+
// ModifyTerraformProviderVersion modifies the existing Terraform files to add AWS provider version constraint
65+
func ModifyTerraformProviderVersion(terraformOutputDir, awsProviderVersion string) error {
66+
kubernetesFile := terraformOutputDir + "/kubernetes.tf"
67+
68+
editor, err := hcledit.ReadFile(kubernetesFile)
69+
if err != nil {
70+
return fmt.Errorf("failed to read kubernetes.tf: %w", err)
71+
}
72+
73+
cleanVersion := strings.Trim(awsProviderVersion, `"`)
74+
75+
err = editor.Update("terraform.required_providers.aws.version", fmt.Sprintf(`"%s"`, cleanVersion))
76+
if err != nil {
77+
return fmt.Errorf("failed to update AWS provider version: %w", err)
78+
}
79+
80+
err = editor.OverWriteFile()
81+
if err != nil {
82+
return fmt.Errorf("failed to write modified kubernetes.tf: %w", err)
83+
}
84+
85+
return nil
86+
}
87+
6488
func initTerraform(ctx context.Context, workingDir, terraformExecPath string, credentials aws.Credentials) (*tfexec.Terraform, error) {
6589
tf, err := tfexec.NewTerraform(workingDir, terraformExecPath)
6690
if err != nil {
6791
return nil, err
6892
}
6993

94+
pluginCacheDir := fmt.Sprintf("%s/plugin-cache", filepath.Dir(terraformExecPath))
95+
96+
err = os.MkdirAll(pluginCacheDir, 0755)
97+
if err != nil {
98+
return nil, fmt.Errorf("failed to create plugin cache directory: %w", err)
99+
}
100+
70101
env := map[string]string{
71102
"AWS_ACCESS_KEY_ID": credentials.AccessKeyID,
72103
"AWS_SECRET_ACCESS_KEY": credentials.SecretAccessKey,
73104
"SPOTINST_TOKEN": os.Getenv("SPOTINST_TOKEN"),
74105
"SPOTINST_ACCOUNT": os.Getenv("SPOTINST_ACCOUNT"),
75-
"TF_PLUGIN_CACHE_DIR": fmt.Sprintf("%s/plugin-cache", filepath.Dir(terraformExecPath)),
106+
"TF_PLUGIN_CACHE_DIR": pluginCacheDir,
76107
}
77108

78109
// this overrides all ENVVARs that are passed to Terraform

0 commit comments

Comments
 (0)