forked from rancher/tfp-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for air-gapped environments
- Loading branch information
1 parent
f6af762
commit c8e2fed
Showing
61 changed files
with
1,734 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package framework | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/logger" | ||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/rancher/tfp-automation/config" | ||
resources "github.com/rancher/tfp-automation/framework/set/resources/airgap" | ||
) | ||
|
||
// AirgapSetup is a function that will set the Terraform configuration and return the Terraform options. | ||
func AirgapSetup(t *testing.T, terraformConfig *config.TerraformConfig, terratestConfig *config.TerratestConfig) (*terraform.Options, string) { | ||
keyPath := resources.KeyPath() | ||
|
||
var terratestLogger logger.Logger | ||
if terratestConfig.TFLogging { | ||
terratestLogger = *logger.Default | ||
} else { | ||
terratestLogger = *logger.Discard | ||
} | ||
|
||
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ | ||
TerraformDir: keyPath, | ||
NoColor: true, | ||
Logger: &terratestLogger, | ||
}) | ||
|
||
return terraformOptions, keyPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package airgap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/rancher/shepherd/clients/rancher" | ||
"github.com/rancher/shepherd/pkg/config" | ||
"github.com/rancher/tfp-automation/defaults/configs" | ||
) | ||
|
||
// ConfigAirgapCleanup is a function that will run terraform destroy and cleanup Terraform resources. | ||
func ConfigAirgapCleanup(t *testing.T, terraformOptions *terraform.Options) { | ||
rancherConfig := new(rancher.Config) | ||
config.LoadConfig(configs.Rancher, rancherConfig) | ||
|
||
if *rancherConfig.Cleanup { | ||
terraform.Destroy(t, terraformOptions) | ||
ConfigAirgapCleanupTF() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package airgap | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rancher/tfp-automation/defaults/configs" | ||
resources "github.com/rancher/tfp-automation/framework/set/resources/airgap" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// ConfigAirgapCleanupTF is a function that will cleanup the main.tf file and terraform.tfstate files. | ||
func ConfigAirgapCleanupTF() error { | ||
keyPath := resources.KeyPath() | ||
|
||
file, err := os.Create(keyPath + configs.MainTF) | ||
if err != nil { | ||
logrus.Errorf("Failed to overwrite main.tf file. Error: %v", err) | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
_, err = file.WriteString("// Leave blank - main.tf will be set during testing") | ||
if err != nil { | ||
logrus.Errorf("Failed to write to main.tf file. Error: %v", err) | ||
return err | ||
} | ||
|
||
delete_files := [3]string{configs.TFState, configs.TFStateBackup, configs.TFLockHCL} | ||
|
||
for _, delete_file := range delete_files { | ||
delete_file = keyPath + delete_file | ||
err = os.Remove(delete_file) | ||
|
||
if err != nil { | ||
logrus.Errorf("Failed to delete terraform.tfstate, terraform.tfstate.backup, and terraform.lock.hcl files. Error: %v", err) | ||
return err | ||
} | ||
} | ||
|
||
err = os.RemoveAll(keyPath + configs.TerraformFolder) | ||
if err != nil { | ||
logrus.Errorf("Failed to delete .terraform folder. Error: %v", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package airgap | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/rancher/tfp-automation/framework/set/defaults" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
// copyScript is a function that will copy the register-nodes.sh script to the bastion node | ||
func copyScript(provisionerBlockBody *hclwrite.Body) error { | ||
userDir, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
nodesScriptPath := filepath.Join(userDir, "go/src/github.com/rancher/tfp-automation/framework/set/provisioning/airgap/register-nodes.sh") | ||
|
||
nodesScriptContent, err := os.ReadFile(nodesScriptPath) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
provisionerBlockBody.SetAttributeValue(defaults.Inline, cty.ListVal([]cty.Value{ | ||
cty.StringVal("echo '" + string(nodesScriptContent) + "' > /tmp/register-nodes.sh"), | ||
cty.StringVal("chmod +x /tmp/register-nodes.sh"), | ||
})) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package airgap | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/rancher/tfp-automation/config" | ||
"github.com/rancher/tfp-automation/framework/set/defaults" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
const ( | ||
clusterID = "cluster_id" | ||
localCluster = "local" | ||
namespace = "fleet-default" | ||
password = "password" | ||
secretType = "kubernetes.io/basic-auth" | ||
username = "username" | ||
) | ||
|
||
// createRegistrySecret is a function that will set the airgap RKE2/K3s cluster configurations in the main.tf file. | ||
func createRegistrySecret(terraformConfig *config.TerraformConfig, clusterName string, rootBody *hclwrite.Body) { | ||
secretBlock := rootBody.AppendNewBlock(defaults.Resource, []string{defaults.SecretV2, clusterName}) | ||
secretBlockBody := secretBlock.Body() | ||
|
||
secretBlockBody.SetAttributeValue(clusterID, cty.StringVal(localCluster)) | ||
secretBlockBody.SetAttributeValue(defaults.ResourceName, cty.StringVal(terraformConfig.PrivateRegistries.AuthConfigSecretName)) | ||
secretBlockBody.SetAttributeValue(defaults.Namespace, cty.StringVal(namespace)) | ||
secretBlockBody.SetAttributeValue(defaults.Type, cty.StringVal(secretType)) | ||
|
||
dataBlock := secretBlockBody.AppendNewBlock(defaults.Data+" =", nil) | ||
configBlockBody := dataBlock.Body() | ||
|
||
configBlockBody.SetAttributeValue(password, cty.StringVal(terraformConfig.PrivateRegistries.Password)) | ||
configBlockBody.SetAttributeValue(username, cty.StringVal(terraformConfig.PrivateRegistries.Username)) | ||
} |
Oops, something went wrong.