Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
endawkins committed Sep 27, 2024
1 parent 97accdb commit 2f42270
Showing 1 changed file with 61 additions and 81 deletions.
142 changes: 61 additions & 81 deletions pkg/testcase/clusterrestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ import (
var s3Config shared.AwsS3Config
var awsConfig shared.AwsConfig

// func TestSetConfigs(flags *customflag.FlagConfig) {
// setConfigs2()
// }

// func setConfigs1() {
// Region := os.Getenv("region")
// Bucket := customflag.ServiceFlag.S3Flags.Bucket
// Folder := customflag.ServiceFlag.S3Flags.Folder
// AccessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
// SecretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
// }

func setConfigs(flags *customflag.FlagConfig) {

s3Config = shared.AwsS3Config{
Expand All @@ -51,13 +39,9 @@ func TestClusterRestoreS3(
product := cluster.Config.Product
_, version, err := shared.Product()
Expect(err).NotTo(HaveOccurred())
fmt.Println("Length of String: ", len(version), "\nVersion: ", version)
versionCleanUp := strings.TrimPrefix(version, "rke2 version ")
endChar := strings.Index(versionCleanUp, "(")
versionClean := versionCleanUp[:endChar]
fmt.Println(versionClean)

fmt.Println(s3Config.Region)

var workloadErr error
if applyWorkload {
Expand All @@ -67,11 +51,6 @@ func TestClusterRestoreS3(

shared.LogLevel("info", "%s-extra-metadata configmap successfully added", product)

// s3Bucket := os.Getenv("S3_BUCKET")
// s3Folder := os.Getenv("S3_FOLDER")
// accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
// secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")

testTakeS3Snapshot(
cluster,
true,
Expand All @@ -87,84 +66,90 @@ func TestClusterRestoreS3(
onDemandPath, onDemandPathErr := shared.FetchSnapshotOnDemandPath(cluster.Config.Product, cluster.ServerIPs[0])
Expect(onDemandPathErr).NotTo(HaveOccurred())

fmt.Println("\non-demand-path: ", onDemandPath)

clusterToken, clusterTokenErr := shared.FetchToken(cluster.Config.Product, cluster.ServerIPs[0])
Expect(clusterTokenErr).NotTo(HaveOccurred())

fmt.Println("\ntoken: ", clusterToken)

// for i := 0; i < len(cluster.ServerIPs); i++ {
// shared.LogLevel("info", "stopping server instances: %s", cluster.ServerIPs[i])
// }
stopServerInstances(cluster)

// shared.LogLevel("info", "stopping agent instance: %s", cluster.AgentIPs[0])

stopAgentInstance(cluster)

resourceName := os.Getenv("resource_name")
awsDependencies, err := aws.AddEC2Client(cluster)
ec2Client, err := aws.AddEC2Client(cluster)
Expect(err).NotTo(HaveOccurred(), "error adding aws nodes: %s", err)

// create server names.
// oldLeadServerIP := cluster.ServerIPs[0]

// create new server.
var serverName []string

serverName = append(serverName, fmt.Sprintf("%s-server-fresh", resourceName))

externalServerIP, _, _, createErr :=
awsDependencies.CreateInstances(serverName...)
ec2Client.CreateInstances(serverName...)
Expect(createErr).NotTo(HaveOccurred(), createErr)

shared.LogLevel("info", "Created server public ip: %s",
externalServerIP[0])

// createNewServer(externalServerIP)
newServerIP := externalServerIP

shared.LogLevel("info", "overriding previous cluster data with new cluster")

// nodeReplaceServers(
// cluster,
// a,
// resourceName,
// oldLeadServerIP,
// clusterToken,
// versionClean,
// channel,
// )

shared.LogLevel("info", "installing %s on server: %s", product, newServerIP)

installProduct(
cluster,
externalServerIP[0],
newServerIP[0],
versionClean,
)

// how do I delete the instances, bring up a new instance and install K3s/RKE2 using what we currently have?
// shared.LogLevel("info", "running cluster reset on server %s\n", externalServerIP)
shared.LogLevel("info", "running cluster reset on server %s\n", newServerIP)
testRestoreS3Snapshot(
cluster,
onDemandPath,
clusterToken,
externalServerIP[0],
newServerIP[0],
flags,
)

enableAndStartService(
cluster,
externalServerIP[0],
newServerIP[0],
)
// freshNodeErr := ValidateNodeJoin(externalServerIP[0])
//
// if freshNodeErr != nil {
// shared.LogLevel("error", "error validating node join: %w with ip: %s",
// freshNodeErr, externalServerIP)
// }

testValidateClusterPostRestore(
cluster,
newServerIP[0],
resourceName,
)

}

func testS3SnapshotSave(cluster *shared.Cluster, flags *customflag.FlagConfig) {
func testValidateClusterPostRestore(cluster *shared.Cluster, newServerIP string, resourceName string) {
newKubeConfig, newKubeConfigErr := shared.UpdateKubeConfig(newServerIP,
resourceName, cluster.Config.Product)
Expect(newKubeConfigErr).NotTo(HaveOccurred())
shared.LogLevel("info", "kubeconfig updated to %s\n", newKubeConfig)
}

// s3Config := shared.AwsS3Config{
// AccessKey: os.Getenv("access_key"),
// Region: os.Getenv("region"),
// Bucket: flags.S3Flags.Bucket,
// Folder: flags.S3Flags.Folder,
// }
func testS3SnapshotSave(cluster *shared.Cluster, flags *customflag.FlagConfig) {

fmt.Println("Region: ", s3Config.Region)
s3Client, err := aws.AddS3Client(s3Config)
Expect(err).NotTo(HaveOccurred(), "error creating s3 client: %s", err)

s3Client.GetObjects(s3Config)
}

// perform snapshot and list snapshot commands -- deploy workloads after snapshot [apply workload]
func testTakeS3Snapshot(
cluster *shared.Cluster,
applyWorkload,
Expand All @@ -180,64 +165,57 @@ func testTakeS3Snapshot(
awsConfig.SecretAccessKey)

takeSnapshotRes, takeSnapshotErr := shared.RunCommandOnNode(takeSnapshotCmd, cluster.ServerIPs[0])
// Expect(takeSnapshotRes).To(ContainSubstring("Snapshot on-demand"))
Expect(takeSnapshotErr).NotTo(HaveOccurred())
fmt.Println(takeSnapshotRes)
fmt.Println(takeSnapshotErr)

// add validation that the s3 folder has been created
Expect(takeSnapshotRes).To(ContainSubstring("Snapshot on-demand"))

var workloadErr error
if applyWorkload {
workloadErr = shared.ManageWorkload("apply", "daemonset.yaml")
Expect(workloadErr).NotTo(HaveOccurred(), "Daemonset manifest not deployed")
}

// diff command -- comparison of outputs []

}

func stopServerInstances(cluster *shared.Cluster) {

awsDependencies, err := aws.AddEC2Client(cluster)
ec2Client, err := aws.AddEC2Client(cluster)
Expect(err).NotTo(HaveOccurred())
// stop server Instances
for i := 0; i < len(cluster.ServerIPs); i++ {
serverInstanceIDs, serverInstanceIDsErr := awsDependencies.GetInstanceIDByIP(cluster.ServerIPs[i])
serverInstanceIDs, serverInstanceIDsErr := ec2Client.GetInstanceIDByIP(cluster.ServerIPs[i])
Expect(serverInstanceIDsErr).NotTo(HaveOccurred())
fmt.Println(serverInstanceIDs)
awsDependencies.StopInstance(serverInstanceIDs)
ec2Client.StopInstance(serverInstanceIDs)
Expect(serverInstanceIDsErr).NotTo(HaveOccurred())
}

}

func stopAgentInstance(cluster *shared.Cluster) {
// stop agent Instances
awsDependencies, err := aws.AddEC2Client(cluster)
ec2Client, err := aws.AddEC2Client(cluster)
Expect(err).NotTo(HaveOccurred())

for i := 0; i < len(cluster.AgentIPs); i++ {
agentInstanceIDs, agentInstanceIDsErr := awsDependencies.GetInstanceIDByIP(cluster.AgentIPs[i])
agentInstanceIDs, agentInstanceIDsErr := ec2Client.GetInstanceIDByIP(cluster.AgentIPs[i])
Expect(agentInstanceIDsErr).NotTo(HaveOccurred())
fmt.Println(agentInstanceIDs)
awsDependencies.StopInstance(agentInstanceIDs)
ec2Client.StopInstance(agentInstanceIDs)
Expect(agentInstanceIDsErr).NotTo(HaveOccurred())
}

}

func installProduct(
cluster *shared.Cluster,
externalServerIP string,
newClusterIP string,
version string,
) {

if cluster.Config.Product == "k3s" {
installCmd := fmt.Sprintf("curl -sfL https://get.k3s.io/ | sudo INSTALL_K3S_VERSION=%s INSTALL_K3S_SKIP_ENABLE=true sh -", version)
_, installCmdErr := shared.RunCommandOnNode(installCmd, externalServerIP)
_, installCmdErr := shared.RunCommandOnNode(installCmd, newClusterIP)
Expect(installCmdErr).NotTo(HaveOccurred())
} else if cluster.Config.Product == "rke2" {
installCmd := fmt.Sprintf("curl -sfL https://get.rke2.io | sudo INSTALL_RKE2_VERSION=%s sh -", version)
_, installCmdErr := shared.RunCommandOnNode(installCmd, externalServerIP)
_, installCmdErr := shared.RunCommandOnNode(installCmd, newClusterIP)
Expect(installCmdErr).NotTo(HaveOccurred())
} else {
shared.LogLevel("error", "unsupported product")
Expand All @@ -248,24 +226,26 @@ func testRestoreS3Snapshot(
cluster *shared.Cluster,
onDemandPath,
token string,
externalServerIP string,
newClusterIP string,
flags *customflag.FlagConfig,
) {
setConfigs(flags)
fmt.Println("s3Bucket set to ", s3Config.Bucket)
fmt.Println("s3Folder set to ", s3Config.Folder)
fmt.Println("s3Region set to ", s3Config.Region)
// var path string
productLocationCmd, findErr := shared.FindPath(cluster.Config.Product, externalServerIP)
productLocationCmd, findErr := shared.FindPath(cluster.Config.Product, newClusterIP)
Expect(findErr).NotTo(HaveOccurred())
resetCmd := fmt.Sprintf("sudo %s server --cluster-reset --etcd-s3 --cluster-reset-restore-path=%s"+
"--etcd-s3-bucket=%s --etcd-s3-folder=%s --etcd-s3-region=%s --etcd-s3-access-key=%s"+
"--etcd-s3-secret-key=%s --token=%s", productLocationCmd, onDemandPath, s3Config.Bucket, s3Config.Folder,
" --etcd-s3-bucket=%s --etcd-s3-folder=%s --etcd-s3-region=%s --etcd-s3-access-key=%s"+
" --etcd-s3-secret-key=%s --token=%s", productLocationCmd, onDemandPath, s3Config.Bucket, s3Config.Folder,
s3Config.Region, awsConfig.AccessKeyID, awsConfig.SecretAccessKey, token)
resetRes, resetCmdErr := shared.RunCommandOnNode(resetCmd, externalServerIP)
Expect(resetCmdErr).NotTo(HaveOccurred())
Expect(resetRes).To(ContainSubstring("Managed etcd cluster"))
Expect(resetRes).To(ContainSubstring("has been reset"))
resetCmdRes, resetCmdErr := shared.RunCommandOnNode(resetCmd, newClusterIP)
Expect(resetCmdErr).To(HaveOccurred())
Expect(resetCmdErr.Error).To(ContainSubstring("Managed etcd cluster"))
Expect(resetCmdErr.Error).To(ContainSubstring("has been reset"))
fmt.Println("Response: ", resetCmdRes)
fmt.Println("Error: ", resetCmdErr)
}

func enableAndStartService(
Expand Down

0 comments on commit 2f42270

Please sign in to comment.