|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/microsoft/retina/test/e2e/common" |
| 12 | + generic "github.com/microsoft/retina/test/e2e/framework/generic" |
| 13 | + "helm.sh/helm/v3/pkg/action" |
| 14 | + "helm.sh/helm/v3/pkg/chart/loader" |
| 15 | + "helm.sh/helm/v3/pkg/cli" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + "k8s.io/client-go/tools/clientcmd" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + HubbleNamespace = "kube-system" |
| 22 | + HubbleUIApp = "hubble-ui" |
| 23 | + HubbleRelayApp = "hubble-relay" |
| 24 | +) |
| 25 | + |
| 26 | +type ValidateHubbleStep struct { |
| 27 | + Namespace string |
| 28 | + ReleaseName string |
| 29 | + KubeConfigFilePath string |
| 30 | + ChartPath string |
| 31 | + TagEnv string |
| 32 | +} |
| 33 | + |
| 34 | +func (v *ValidateHubbleStep) Run() error { |
| 35 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second) |
| 36 | + defer cancel() |
| 37 | + |
| 38 | + settings := cli.New() |
| 39 | + settings.KubeConfig = v.KubeConfigFilePath |
| 40 | + actionConfig := new(action.Configuration) |
| 41 | + |
| 42 | + err := actionConfig.Init(settings.RESTClientGetter(), v.Namespace, os.Getenv("HELM_DRIVER"), log.Printf) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("failed to initialize helm action config: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Creating extra namespace to deploy test pods |
| 48 | + err = CreateNamespaceFn(v.KubeConfigFilePath, common.TestPodNamespace) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to create namespace %s: %w", v.Namespace, err) |
| 51 | + } |
| 52 | + |
| 53 | + tag := os.Getenv(generic.DefaultTagEnv) |
| 54 | + if tag == "" { |
| 55 | + return fmt.Errorf("tag is not set: %w", errEmpty) |
| 56 | + } |
| 57 | + imageRegistry := os.Getenv(generic.DefaultImageRegistry) |
| 58 | + if imageRegistry == "" { |
| 59 | + return fmt.Errorf("image registry is not set: %w", errEmpty) |
| 60 | + } |
| 61 | + |
| 62 | + imageNamespace := os.Getenv(generic.DefaultImageNamespace) |
| 63 | + if imageNamespace == "" { |
| 64 | + return fmt.Errorf("image namespace is not set: %w", errEmpty) |
| 65 | + } |
| 66 | + |
| 67 | + // load chart from the path |
| 68 | + chart, err := loader.Load(v.ChartPath) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to load chart from path %s: %w", v.ChartPath, err) |
| 71 | + } |
| 72 | + |
| 73 | + chart.Values["imagePullSecrets"] = []map[string]interface{}{ |
| 74 | + { |
| 75 | + "name": "acr-credentials", |
| 76 | + }, |
| 77 | + } |
| 78 | + chart.Values["operator"].(map[string]interface{})["enabled"] = true |
| 79 | + chart.Values["operator"].(map[string]interface{})["repository"] = imageRegistry + "/" + imageNamespace + "/retina-operator" |
| 80 | + chart.Values["operator"].(map[string]interface{})["tag"] = tag |
| 81 | + chart.Values["agent"].(map[string]interface{})["enabled"] = true |
| 82 | + chart.Values["agent"].(map[string]interface{})["repository"] = imageRegistry + "/" + imageNamespace + "/retina-agent" |
| 83 | + chart.Values["agent"].(map[string]interface{})["tag"] = tag |
| 84 | + chart.Values["agent"].(map[string]interface{})["init"].(map[string]interface{})["enabled"] = true |
| 85 | + chart.Values["agent"].(map[string]interface{})["init"].(map[string]interface{})["repository"] = imageRegistry + "/" + imageNamespace + "/retina-init" |
| 86 | + chart.Values["agent"].(map[string]interface{})["init"].(map[string]interface{})["tag"] = tag |
| 87 | + chart.Values["hubble"].(map[string]interface{})["tls"].(map[string]interface{})["enabled"] = false |
| 88 | + chart.Values["hubble"].(map[string]interface{})["relay"].(map[string]interface{})["tls"].(map[string]interface{})["server"].(map[string]interface{})["enabled"] = false |
| 89 | + chart.Values["hubble"].(map[string]interface{})["tls"].(map[string]interface{})["auto"].(map[string]interface{})["enabled"] = false |
| 90 | + |
| 91 | + getclient := action.NewGet(actionConfig) |
| 92 | + release, err := getclient.Run(v.ReleaseName) |
| 93 | + if err == nil && release != nil { |
| 94 | + log.Printf("found existing release by same name, removing before installing %s", release.Name) |
| 95 | + delclient := action.NewUninstall(actionConfig) |
| 96 | + delclient.Wait = true |
| 97 | + delclient.Timeout = deleteTimeout |
| 98 | + _, err = delclient.Run(v.ReleaseName) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to delete existing release %s: %w", v.ReleaseName, err) |
| 101 | + } |
| 102 | + } else if err != nil && !strings.Contains(err.Error(), "not found") { |
| 103 | + return fmt.Errorf("failed to get release %s: %w", v.ReleaseName, err) |
| 104 | + } |
| 105 | + |
| 106 | + client := action.NewInstall(actionConfig) |
| 107 | + client.Namespace = v.Namespace |
| 108 | + client.ReleaseName = v.ReleaseName |
| 109 | + client.Timeout = createTimeout |
| 110 | + client.Wait = true |
| 111 | + client.WaitForJobs = true |
| 112 | + |
| 113 | + // install the chart here |
| 114 | + rel, err := client.RunWithContext(ctx, chart, chart.Values) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to install chart: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + log.Printf("installed chart from path: %s in namespace: %s\n", rel.Name, rel.Namespace) |
| 120 | + // this will confirm the values set during installation |
| 121 | + log.Printf("chart values: %v\n", rel.Config) |
| 122 | + |
| 123 | + // ensure all pods are running, since helm doesn't care about windows |
| 124 | + config, err := clientcmd.BuildConfigFromFlags("", v.KubeConfigFilePath) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("error building kubeconfig: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + clientset, err := kubernetes.NewForConfig(config) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("error creating Kubernetes client: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + // Validate Hubble Relay Pod |
| 135 | + if err := WaitForPodReady(ctx, clientset, HubbleNamespace, "k8s-app="+HubbleRelayApp); err != nil { |
| 136 | + return fmt.Errorf("error waiting for Hubble Relay pods to be ready: %w", err) |
| 137 | + } |
| 138 | + log.Printf("Hubble Relay Pod is ready") |
| 139 | + |
| 140 | + // Validate Hubble UI Pod |
| 141 | + if err := WaitForPodReady(ctx, clientset, HubbleNamespace, "k8s-app="+HubbleUIApp); err != nil { |
| 142 | + return fmt.Errorf("error waiting for Hubble UI pods to be ready: %w", err) |
| 143 | + } |
| 144 | + log.Printf("Hubble UI Pod is ready") |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (v *ValidateHubbleStep) Prevalidate() error { |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (v *ValidateHubbleStep) Stop() error { |
| 154 | + return nil |
| 155 | +} |
0 commit comments