|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/aws/amazon-cloudwatch-agent-test/environment" |
| 14 | + "github.com/aws/amazon-cloudwatch-agent-test/util/awsservice" |
| 15 | +) |
| 16 | + |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | +// Environment Setup |
| 19 | +//------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +func InitializeEnvironment(env *environment.MetaData) error { |
| 22 | + if env.Region != "us-west-2" { |
| 23 | + if err := awsservice.ConfigureAWSClients(env.Region); err != nil { |
| 24 | + return fmt.Errorf("failed to reconfigure AWS clients: %v", err) |
| 25 | + } |
| 26 | + fmt.Printf("AWS clients reconfigured to use region: %s\n", env.Region) |
| 27 | + } else { |
| 28 | + fmt.Printf("Using default testing region: us-west-2\n") |
| 29 | + } |
| 30 | + |
| 31 | + fmt.Println("Applying K8s resources...") |
| 32 | + if err := ApplyResources(env); err != nil { |
| 33 | + return fmt.Errorf("failed to apply K8s resources: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +//------------------------------------------------------------------------------ |
| 40 | +// K8s Resource Management Functions |
| 41 | +//------------------------------------------------------------------------------ |
| 42 | + |
| 43 | +func ApplyResources(env *environment.MetaData) error { |
| 44 | + updateKubeconfig := exec.Command("aws", "eks", "update-kubeconfig", "--name", env.EKSClusterName) |
| 45 | + output, err := updateKubeconfig.CombinedOutput() |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("failed to update kubeconfig: %w\nOutput: %s", err, output) |
| 48 | + } |
| 49 | + |
| 50 | + fmt.Println("Installing Helm release...") |
| 51 | + helm := []string{ |
| 52 | + "helm", "upgrade", "--install", "amazon-cloudwatch-observability", |
| 53 | + filepath.Join("..", "..", "..", "terraform", "eks", "e2e", "helm-charts", "charts", "amazon-cloudwatch-observability"), |
| 54 | + "--set", fmt.Sprintf("clusterName=%s", env.EKSClusterName), |
| 55 | + "--set", fmt.Sprintf("region=%s", env.Region), |
| 56 | + "--set", fmt.Sprintf("agent.image.repository=%s", env.CloudwatchAgentRepository), |
| 57 | + "--set", fmt.Sprintf("agent.image.tag=%s", env.CloudwatchAgentTag), |
| 58 | + "--set", fmt.Sprintf("agent.image.repositoryDomainMap.public=%s", env.CloudwatchAgentRepositoryURL), |
| 59 | + "--set", fmt.Sprintf("manager.image.repository=%s", env.CloudwatchAgentOperatorRepository), |
| 60 | + "--set", fmt.Sprintf("manager.image.tag=%s", env.CloudwatchAgentOperatorTag), |
| 61 | + "--set", fmt.Sprintf("manager.image.repositoryDomainMap.public=%s", env.CloudwatchAgentOperatorRepositoryURL), |
| 62 | + "--namespace", "amazon-cloudwatch", |
| 63 | + "--create-namespace", |
| 64 | + } |
| 65 | + |
| 66 | + if env.AgentConfig != "" { |
| 67 | + agentConfigContent, err := os.ReadFile(env.AgentConfig) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to read agent config file: %w", err) |
| 70 | + } |
| 71 | + helm = append(helm, "--set-json", fmt.Sprintf("agent.config=%s", string(agentConfigContent))) |
| 72 | + } |
| 73 | + |
| 74 | + helmUpgrade := exec.Command(helm[0], helm[1:]...) |
| 75 | + helmUpgrade.Stdout = os.Stdout |
| 76 | + helmUpgrade.Stderr = os.Stderr |
| 77 | + if err := helmUpgrade.Run(); err != nil { |
| 78 | + return fmt.Errorf("failed to install Helm release: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + fmt.Println("Waiting for CloudWatch Agent Operator to initialize...") |
| 82 | + wait := exec.Command("kubectl", "wait", "--for=condition=available", "--timeout=60s", "deployment/amazon-cloudwatch-observability-controller-manager", "-n", "amazon-cloudwatch") |
| 83 | + output, err = wait.CombinedOutput() |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed to wait for operator deployment: %w\nOutput: %s", err, output) |
| 86 | + } |
| 87 | + |
| 88 | + deploymentName := strings.TrimSuffix(filepath.Base(env.SampleApp), ".yaml") |
| 89 | + |
| 90 | + apply := exec.Command("kubectl", "apply", "-f", env.SampleApp) |
| 91 | + output, err = apply.CombinedOutput() |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("failed to apply sample app: %w\nOutput: %s", err, output) |
| 94 | + } |
| 95 | + |
| 96 | + fmt.Println("Waiting for Sample Application to initialize...") |
| 97 | + wait = exec.Command("kubectl", "wait", "--for=condition=available", "--timeout=300s", fmt.Sprintf("deployment/%s", deploymentName), "-n", "test") |
| 98 | + output, err = wait.CombinedOutput() |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to wait for deployment %s: %w\nOutput: %s", deploymentName, err, output) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func DestroyResources(env *environment.MetaData) error { |
| 107 | + updateKubeconfig := exec.Command("aws", "eks", "update-kubeconfig", "--name", env.EKSClusterName) |
| 108 | + output, err := updateKubeconfig.CombinedOutput() |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed to update kubeconfig: %w\nOutput: %s", err, output) |
| 111 | + } |
| 112 | + |
| 113 | + var errors []error |
| 114 | + |
| 115 | + fmt.Println("Deleting test namespace...") |
| 116 | + deleteCmd := exec.Command("kubectl", "delete", "namespace", "test", "--timeout=60s") |
| 117 | + output, err = deleteCmd.CombinedOutput() |
| 118 | + |
| 119 | + // We don't want to consider not finding the namespace to be an error since that's the outcome we want |
| 120 | + if err != nil && !strings.Contains(string(output), "not found") { |
| 121 | + errors = append(errors, fmt.Errorf("failed to delete test namespace: %w\nOutput: %s", err, output)) |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Println("Uninstalling Helm release...") |
| 125 | + helm := []string{ |
| 126 | + "helm", "uninstall", "amazon-cloudwatch-observability", "--namespace", "amazon-cloudwatch", |
| 127 | + } |
| 128 | + |
| 129 | + helmUninstall := exec.Command(helm[0], helm[1:]...) |
| 130 | + helmUninstall.Stdout = os.Stdout |
| 131 | + helmUninstall.Stderr = os.Stderr |
| 132 | + if err := helmUninstall.Run(); err != nil { |
| 133 | + errors = append(errors, fmt.Errorf("failed to uninstall Helm release: %w", err)) |
| 134 | + } |
| 135 | + |
| 136 | + if len(errors) > 0 { |
| 137 | + return fmt.Errorf("cleanup errors: %v", errors) |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | +} |
0 commit comments