|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Copyright 2024 D2iQ, Inc. All rights reserved. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package e2e |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + appsv1 "k8s.io/api/apps/v1" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + "k8s.io/apimachinery/pkg/api/errors" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + capiv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 19 | + "sigs.k8s.io/cluster-api/test/framework" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 21 | + |
| 22 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 23 | +) |
| 24 | + |
| 25 | +const clusterAutoscalerPrefix = "cluster-autoscaler-" |
| 26 | + |
| 27 | +type WaitForClusterAutoscalerToBeReadyInWorkloadClusterInput struct { |
| 28 | + ClusterAutoscaler *v1alpha1.ClusterAutoscaler |
| 29 | + WorkloadCluster *capiv1.Cluster |
| 30 | + ClusterProxy framework.ClusterProxy |
| 31 | + DeploymentIntervals []interface{} |
| 32 | + DaemonSetIntervals []interface{} |
| 33 | + HelmReleaseIntervals []interface{} |
| 34 | + ClusterResourceSetIntervals []interface{} |
| 35 | +} |
| 36 | + |
| 37 | +func WaitForClusterAutoscalerToBeReadyInWorkloadCluster( |
| 38 | + ctx context.Context, |
| 39 | + input WaitForClusterAutoscalerToBeReadyInWorkloadClusterInput, //nolint:gocritic // This hugeParam is OK in tests. |
| 40 | +) { |
| 41 | + if input.ClusterAutoscaler == nil { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + // Only check for ClusterAutoscaler if the cluster is self-managed. Check this by checking if the kubeconfig |
| 46 | + // exists in the workload cluster itself. |
| 47 | + workloadClusterClient := input.ClusterProxy.GetWorkloadCluster( |
| 48 | + ctx, input.WorkloadCluster.Namespace, input.WorkloadCluster.Name, |
| 49 | + ).GetClient() |
| 50 | + err := workloadClusterClient.Get( |
| 51 | + ctx, |
| 52 | + client.ObjectKey{ |
| 53 | + Namespace: input.WorkloadCluster.Namespace, |
| 54 | + Name: input.WorkloadCluster.Name + "-kubeconfig", |
| 55 | + }, |
| 56 | + &corev1.Secret{}, |
| 57 | + ) |
| 58 | + if err != nil && errors.IsNotFound(err) { |
| 59 | + return |
| 60 | + } |
| 61 | + Expect(err).NotTo(HaveOccurred()) |
| 62 | + |
| 63 | + switch input.ClusterAutoscaler.Strategy { |
| 64 | + case v1alpha1.AddonStrategyClusterResourceSet: |
| 65 | + waitForClusterResourceSetToApplyResourcesInCluster( |
| 66 | + ctx, |
| 67 | + waitForClusterResourceSetToApplyResourcesInClusterInput{ |
| 68 | + name: clusterAutoscalerPrefix + input.WorkloadCluster.Name, |
| 69 | + clusterProxy: input.ClusterProxy, |
| 70 | + cluster: input.WorkloadCluster, |
| 71 | + intervals: input.ClusterResourceSetIntervals, |
| 72 | + }, |
| 73 | + ) |
| 74 | + case v1alpha1.AddonStrategyHelmAddon: |
| 75 | + WaitForHelmReleaseProxyReadyForCluster( |
| 76 | + ctx, |
| 77 | + WaitForHelmReleaseProxyReadyForClusterInput{ |
| 78 | + GetLister: input.ClusterProxy.GetClient(), |
| 79 | + Cluster: input.WorkloadCluster, |
| 80 | + HelmChartProxyName: clusterAutoscalerPrefix + input.WorkloadCluster.Name, |
| 81 | + }, |
| 82 | + input.HelmReleaseIntervals..., |
| 83 | + ) |
| 84 | + default: |
| 85 | + Fail( |
| 86 | + fmt.Sprintf( |
| 87 | + "Do not know how to wait for cluster autoscaler using strategy %s to be ready", |
| 88 | + input.ClusterAutoscaler.Strategy, |
| 89 | + ), |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + WaitForDeploymentsAvailable(ctx, framework.WaitForDeploymentsAvailableInput{ |
| 94 | + Getter: workloadClusterClient, |
| 95 | + Deployment: &appsv1.Deployment{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: clusterAutoscalerPrefix + input.WorkloadCluster.Name, |
| 98 | + Namespace: input.WorkloadCluster.Namespace, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, input.DeploymentIntervals...) |
| 102 | +} |
0 commit comments