Skip to content

Commit

Permalink
Reinstantiate resource validation map on each instance of validation
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Sep 12, 2024
1 parent 413c565 commit 3530a70
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ KIND_CLUSTER_NAME ?= hmc-dev
KIND_NETWORK ?= kind
REGISTRY_NAME ?= hmc-local-registry
REGISTRY_PORT ?= 5001
REGISTRY_REPO ?= oci://$(REGISTRY_NAME):5000/charts
REGISTRY_REPO ?= oci://127.0.0.1:$(REGISTRY_PORT)/charts
DEV_PROVIDER ?= aws
REGISTRY_IS_OCI = $(shell echo $(REGISTRY_REPO) | grep -q oci && echo true || echo false)
CLUSTER_NAME ?= $(shell $(YQ) '.metadata.name' ./config/dev/deployment.yaml)
Expand Down Expand Up @@ -245,9 +245,13 @@ hmc-deploy: helm

.PHONY: dev-deploy
dev-deploy: ## Deploy HMC helm chart to the K8s cluster specified in ~/.kube/config.
$(YQ) eval -i '.image.repository = "$(IMG_REPO)"' config/dev/hmc_values.yaml
$(YQ) eval -i '.image.tag = "$(IMG_TAG)"' config/dev/hmc_values.yaml
$(YQ) eval -i '.controller.defaultRegistryURL = "$(REGISTRY_REPO)"' config/dev/hmc_values.yaml
@$(YQ) eval -i '.image.repository = "$(IMG_REPO)"' config/dev/hmc_values.yaml
@$(YQ) eval -i '.image.tag = "$(IMG_TAG)"' config/dev/hmc_values.yaml
@if [ "$(REGISTRY_REPO)" = "oci://127.0.0.1:$(REGISTRY_PORT)/charts" ]; then \
$(YQ) eval -i '.controller.defaultRegistryURL = "oci://$(REGISTRY_NAME):5000/charts"' config/dev/hmc_values.yaml; \
else \
$(YQ) eval -i '.controller.defaultRegistryURL = "$(REGISTRY_REPO)"' config/dev/hmc_values.yaml; \
fi; \
$(MAKE) hmc-deploy HMC_VALUES=config/dev/hmc_values.yaml
$(KUBECTL) rollout restart -n $(NAMESPACE) deployment/hmc-controller-manager

Expand Down
1 change: 1 addition & 0 deletions config/dev/hmc_values.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
image:
repository: hmc/controller
tag: latest
controller:
defaultRegistryURL: oci://hmc-local-registry:5000/charts
insecureRegistry: true
Expand Down
37 changes: 26 additions & 11 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ var _ = Describe("controller", Ordered, func() {

It("should work with an AWS provider", func() {
// Deploy a standalone cluster and verify it is running/ready.
// Deploy standalone with an xlarge instance since it will also be
// hosting the hosted cluster.
GinkgoT().Setenv(managedcluster.EnvVarAWSInstanceType, "t3.xlarge")
GinkgoT().Setenv(managedcluster.EnvVarInstallBeachHeadServices, "false")

Expand All @@ -129,8 +131,9 @@ var _ = Describe("controller", Ordered, func() {
standaloneDeleteFunc = kc.CreateManagedCluster(context.Background(), sd)

templateBy(managedcluster.TemplateAWSStandaloneCP, "waiting for infrastructure to deploy successfully")
resourcesToValidate := managedcluster.NewDeployedValidation()
Eventually(func() error {
return managedcluster.VerifyProviderDeployed(context.Background(), kc, clusterName)
return managedcluster.VerifyProviderDeployed(context.Background(), kc, clusterName, resourcesToValidate)
}).WithTimeout(30 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())

templateBy(managedcluster.TemplateAWSHostedCP, "installing controller and templates on standalone cluster")
Expand Down Expand Up @@ -195,29 +198,41 @@ var _ = Describe("controller", Ordered, func() {

// Verify the hosted cluster is running/ready.
templateBy(managedcluster.TemplateAWSHostedCP, "waiting for infrastructure to deploy successfully")
resourcesToValidate = managedcluster.NewDeployedValidation()
Eventually(func() error {
return managedcluster.VerifyProviderDeployed(context.Background(), standaloneClient, hdName)
return managedcluster.VerifyProviderDeployed(
context.Background(), standaloneClient, hdName, resourcesToValidate,
)
}).WithTimeout(30 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())

// Delete the hosted ManagedCluster and verify it is removed.
// FIXME: Do not test hosted-cp deletion until #242 is
// resolved as it will just get stuck.
// templateBy(managedcluster.TemplateAWSHostedCP, "deleting the ManagedCluster")
// err = hostedDeleteFunc()
// Expect(err).NotTo(HaveOccurred())
// Eventually(func() error {
// return managedcluster.VerifyProviderDeleted(context.Background(), standaloneClient, hdName)
// }).WithTimeout(10 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())
/*
// Delete the hosted ManagedCluster and verify it is removed.
templateBy(managedcluster.TemplateAWSHostedCP, "deleting the ManagedCluster")
err = hostedDeleteFunc()
Expect(err).NotTo(HaveOccurred())
resourcesToValidate = managedcluster.NewDeletionValidation()
Eventually(func() error {
return managedcluster.VerifyProviderDeleted(context.Background(), standaloneClient, hdName)
}).WithTimeout(10 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())
*/

// Now delete the standalone ManagedCluster and verify it is
// removed, it is deleted last since it is the basis for the hosted
// cluster.
templateBy(managedcluster.TemplateAWSStandaloneCP, "deleting the ManagedCluster")
err = standaloneDeleteFunc()
Expect(err).NotTo(HaveOccurred())

resourcesToValidate = managedcluster.NewDeletionValidation()
Eventually(func() error {
return managedcluster.VerifyProviderDeleted(context.Background(), kc, clusterName)
}).WithTimeout(10 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())
return managedcluster.VerifyProviderDeleted(
context.Background(), kc, clusterName, resourcesToValidate,
)
}).WithTimeout(10 * time.Minute).WithPolling(10 *
time.Second).Should(Succeed())
})
})
})
Expand Down
16 changes: 10 additions & 6 deletions test/managedcluster/validate_deleted.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var deletionValidators = map[string]resourceValidationFunc{
"clusters": validateClusterDeleted,
"machinedeployments": validateMachineDeploymentsDeleted,
"control-planes": validateK0sControlPlanesDeleted,
func NewDeletionValidation() map[string]resourceValidationFunc {
return map[string]resourceValidationFunc{
"clusters": validateClusterDeleted,
"machinedeployments": validateMachineDeploymentsDeleted,
"control-planes": validateK0sControlPlanesDeleted,
}
}

// VerifyProviderDeleted is a provider-agnostic verification that checks
// to ensure generic resources managed by the provider have been deleted.
// It is intended to be used in conjunction with an Eventually block.
func VerifyProviderDeleted(ctx context.Context, kc *kubeclient.KubeClient, clusterName string) error {
return verifyProviderAction(ctx, kc, clusterName, deletionValidators,
func VerifyProviderDeleted(
ctx context.Context, kc *kubeclient.KubeClient, clusterName string,
resourcesToValidate map[string]resourceValidationFunc) error {
return verifyProviderAction(ctx, kc, clusterName, resourcesToValidate,
[]string{"clusters", "machinedeployments", "control-planes"})
}

Expand Down
20 changes: 12 additions & 8 deletions test/managedcluster/validate_deployed.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@ import (
// resource.
type resourceValidationFunc func(context.Context, *kubeclient.KubeClient, string) error

var resourceValidators = map[string]resourceValidationFunc{
"clusters": validateCluster,
"machines": validateMachines,
"control-planes": validateK0sControlPlanes,
"csi-driver": validateCSIDriver,
"ccm": validateCCM,
func NewDeployedValidation() map[string]resourceValidationFunc {
return map[string]resourceValidationFunc{
"clusters": validateCluster,
"machines": validateMachines,
"control-planes": validateK0sControlPlanes,
"csi-driver": validateCSIDriver,
"ccm": validateCCM,
}
}

// VerifyProviderDeployed is a provider-agnostic verification that checks
// to ensure generic resources managed by the provider have been deleted.
// It is intended to be used in conjunction with an Eventually block.
func VerifyProviderDeployed(ctx context.Context, kc *kubeclient.KubeClient, clusterName string) error {
return verifyProviderAction(ctx, kc, clusterName, resourceValidators,
func VerifyProviderDeployed(
ctx context.Context, kc *kubeclient.KubeClient, clusterName string,
resourceValidationMap map[string]resourceValidationFunc) error {
return verifyProviderAction(ctx, kc, clusterName, resourceValidationMap,
[]string{"clusters", "machines", "control-planes", "csi-driver", "ccm"})
}

Expand Down

0 comments on commit 3530a70

Please sign in to comment.