Skip to content

Commit

Permalink
Update controller pod tests to look for CAP providers
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Aug 29, 2024
1 parent a32fd96 commit 78ea275
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
10 changes: 9 additions & 1 deletion test/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ import (
type ProviderType string

const (
ProviderAWS ProviderType = "aws"
ProviderCAPI ProviderType = "cluster-api"
ProviderAWS ProviderType = "infrastructure-aws"
ProviderAzure ProviderType = "infrastructure-azure"

providerLabel = "cluster.x-k8s.io/provider"
)

type Template string
Expand All @@ -49,6 +53,10 @@ var awsStandaloneCPDeploymentTemplateBytes []byte
//go:embed resources/aws-hosted-cp.yaml.tpl
var awsHostedCPDeploymentTemplateBytes []byte

func GetProviderLabel(provider ProviderType) string {
return fmt.Sprintf("%s=%s", providerLabel, provider)
}

// GetUnstructuredDeployment returns an unstructured deployment object based on
// the provider and template.
func GetUnstructuredDeployment(provider ProviderType, templateName Template) *unstructured.Unstructured {
Expand Down
87 changes: 54 additions & 33 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import (
"github.com/Mirantis/hmc/test/utils"
)

const namespace = "hmc-system"
const (
namespace = "hmc-system"
hmcControllerLabel = "app.kubernetes.io/name=hmc"
)

var _ = Describe("controller", Ordered, func() {
BeforeAll(func() {
Expand All @@ -57,39 +60,35 @@ var _ = Describe("controller", Ordered, func() {
kc, err := kubeclient.NewFromLocal(namespace)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Ensure only one controller pod is running.
By("validating that the hmc-controller and capi provider controllers are running")
verifyControllersUp := func() error {
for _, provider := range []deployment.ProviderType{
deployment.ProviderCAPI,
deployment.ProviderAWS,
deployment.ProviderAzure,
} {
// Ensure only one controller pod is running.
podList, err := kc.Client.CoreV1().Pods(kc.Namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: deployment.GetProviderLabel(provider),
})
if err != nil {
return err
}

verifyControllerUp(podList, string(provider))

Check failure on line 78 in test/e2e/e2e_test.go

View workflow job for this annotation

GitHub Actions / Build

Error return value is not checked (errcheck)
}

podList, err := kc.Client.CoreV1().Pods(kc.Namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: "control-plane=controller-manager,app.kubernetes.io/name=cluster-api",
LabelSelector: hmcControllerLabel,
})
if err != nil {
return err
}

if len(podList.Items) != 1 {
return fmt.Errorf("expected 1 cluster-api-controller pod, got %d", len(podList.Items))
}

controllerPod := podList.Items[0]

// Ensure the pod is not being deleted.
if controllerPod.DeletionTimestamp != nil {
return fmt.Errorf("deletion timestamp should be nil, got: %v", controllerPod)
}

// Ensure the pod is running and has the expected name.
if !strings.Contains(controllerPod.Name, "controller-manager") {
return fmt.Errorf("controller pod name %s does not contain 'controller-manager'", controllerPod.Name)
}
if controllerPod.Status.Phase != "Running" {
return fmt.Errorf("controller pod in %s status", controllerPod.Status.Phase)
}

return nil
return verifyControllerUp(podList, "hmc-controller-manager")
}
Eventually(func() error {
err := verifyControllerUp()
err := verifyControllersUp()
if err != nil {
_, _ = fmt.Fprintf(GinkgoWriter, "Controller pod validation failed: %v\n", err)
return err
Expand Down Expand Up @@ -120,7 +119,7 @@ var _ = Describe("controller", Ordered, func() {
// as well as the output of clusterctl to store as artifacts.
if CurrentSpecReport().Failed() {
By("collecting failure logs from controllers")
collectLogArtifacts(kc, clusterName, deployment.ProviderAWS)
collectLogArtifacts(kc, clusterName, deployment.ProviderAWS, deployment.ProviderCAPI)
}

// Delete the deployments if they were created.
Expand Down Expand Up @@ -168,19 +167,41 @@ var _ = Describe("controller", Ordered, func() {
})
})

func verifyControllerUp(podList *corev1.PodList, name string) error {
if len(podList.Items) != 1 {
return fmt.Errorf("expected 1 %s controller pod, got %d", name, len(podList.Items))
}

controllerPod := podList.Items[0]

// Ensure the pod is not being deleted.
if controllerPod.DeletionTimestamp != nil {
return fmt.Errorf("deletion timestamp should be nil, got: %v", controllerPod)
}
// Ensure the pod is running and has the expected name.
if !strings.Contains(controllerPod.Name, "controller-manager") {
return fmt.Errorf("controller pod name %s does not contain 'controller-manager'", controllerPod.Name)
}
if controllerPod.Status.Phase != "Running" {
return fmt.Errorf("controller pod in %s status", controllerPod.Status.Phase)
}

return nil
}

// collectLogArtfiacts collects log output from each the HMC controller,
// CAPI controller and the provider controller as well as output from clusterctl
// CAPI controller and the provider controller(s) as well as output from clusterctl
// and stores them in the test/e2e directory as artifacts.
// We could do this at the end or we could use Kubernetes' CopyPodLogs from
// https://github.com/kubernetes/kubernetes/blob/v1.31.0/test/e2e/storage/podlogs/podlogs.go#L88
// to stream the logs to GinkgoWriter during the test.
func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, providerType deployment.ProviderType) {
func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, providerTypes ...deployment.ProviderType) {
GinkgoHelper()

filterLabels := []string{
"app.kubernetes.io/name=hmc-controller-manager",
"app.kubernetes.io/name=cluster-api",
fmt.Sprintf("app.kubernetes.io/name=cluster-api-provider-%s", providerType),
filterLabels := []string{hmcControllerLabel}

for _, providerType := range providerTypes {
filterLabels = append(filterLabels, deployment.GetProviderLabel(providerType))
}

for _, label := range filterLabels {
Expand Down

0 comments on commit 78ea275

Please sign in to comment.