Skip to content

Commit

Permalink
validate managed service
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
BROngineer committed Jan 8, 2025
1 parent d218cad commit b07c28b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
3 changes: 2 additions & 1 deletion test/e2e/clusterdeployment/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const (
// debugging of test failures.
EnvVarNoCleanup = "NO_CLEANUP"

EnvVarServiceNamespace = "SERVICE_NAMESPACE"
EnvVarServiceNamespace = "INGRESS_SERVICE_NAMESPACE"
EnvVarServiceName = "INGRESS_SERVICE_NAME"

// AWS
EnvVarAWSAccessKeyID = "AWS_ACCESS_KEY_ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ spec:
instanceType: ${AWS_INSTANCE_TYPE:=t3.small}
services:
- template: ingress-nginx-4-11-0
name: managed-ingress-nginx
namespace: ${SERVICE_NAMESPACE}
name: ${INGRESS_SERVICE_NAME}
namespace: ${INGRESS_SERVICE_NAMESPACE}
40 changes: 30 additions & 10 deletions test/e2e/clusterdeployment/servicevalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,63 @@ import (
"github.com/K0rdent/kcm/test/e2e/kubeclient"
)

type ManagedServiceResource struct {
// ResourceNameSuffix is the suffix added to the resource name
ResourceNameSuffix string

// ValidationFunc is the validation function for the resource
ValidationFunc resourceValidationFunc
}

func (m ManagedServiceResource) fullResourceName(serviceName string) string {
if m.ResourceNameSuffix == "" {
return serviceName
}
return fmt.Sprintf("%s-%s", serviceName, m.ResourceNameSuffix)
}

type ServiceValidator struct {
// managedClusterName is the name of managed cluster
managedClusterName string

// managedServiceName is the name of managed service
managedServiceName string

// template being validated
template Template

// namespace is a namespace of deployed service
namespace string

// resourcesToValidate is a map of resource names and corresponding validation functions
resourcesToValidate map[string]resourceValidationFunc
// resourcesToValidate is a map of resource names and corresponding resources definitions
resourcesToValidate map[string]ManagedServiceResource
}

func NewServiceValidator(clusterName, namespace string) *ServiceValidator {
func NewServiceValidator(clusterName, serviceName, namespace string) *ServiceValidator {
return &ServiceValidator{
managedClusterName: clusterName,
managedServiceName: serviceName,
namespace: namespace,
resourcesToValidate: make(map[string]resourceValidationFunc),
resourcesToValidate: make(map[string]ManagedServiceResource),
}
}

func (v *ServiceValidator) WithResourceValidation(resourceName string, validationFunc resourceValidationFunc) *ServiceValidator {
v.resourcesToValidate[resourceName] = validationFunc
func (v *ServiceValidator) WithResourceValidation(resourceName string, resource ManagedServiceResource) *ServiceValidator {
v.resourcesToValidate[resourceName] = resource
return v
}

func (v *ServiceValidator) Validate(ctx context.Context, kc *kubeclient.KubeClient) error {
clusterKubeClient := kc.NewFromCluster(ctx, v.namespace, v.managedClusterName)

for res, f := range v.resourcesToValidate {
err := f(ctx, clusterKubeClient, res)
for resourceName, resource := range v.resourcesToValidate {
fullResourceName := resource.fullResourceName(v.managedServiceName)
err := resource.ValidationFunc(ctx, clusterKubeClient, fullResourceName)
if err != nil {
_, _ = fmt.Fprintf(GinkgoWriter, "[%s/%s] validation error: %v\n", v.template, res, err)
_, _ = fmt.Fprintf(GinkgoWriter, "[%s/%s] validation error: %v\n", v.template, resourceName, err)
return err
}
_, _ = fmt.Fprintf(GinkgoWriter, "[%s/%s] validation succeeded\n", v.template, res)
_, _ = fmt.Fprintf(GinkgoWriter, "[%s/%s] validation succeeded\n", v.template, resourceName)
}
return nil
}
19 changes: 19 additions & 0 deletions test/e2e/clusterdeployment/validate_deployed.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,22 @@ func validateCCM(ctx context.Context, kc *kubeclient.KubeClient, clusterName str

return fmt.Errorf("%s Service does not yet have an external hostname", service.Name)
}

func ValidateService(ctx context.Context, kc *kubeclient.KubeClient, name string) error {
_, err := kc.Client.CoreV1().Services(kc.Namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}
return nil
}

func ValidateDeployment(ctx context.Context, kc *kubeclient.KubeClient, name string) error {
dep, err := kc.Client.AppsV1().Deployments(kc.Namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}
if *dep.Spec.Replicas != dep.Status.ReadyReplicas {
return fmt.Errorf("deployment %s has %d ready replicas, expected %d", name, dep.Status.ReadyReplicas, *dep.Spec.Replicas)
}
return nil
}
15 changes: 15 additions & 0 deletions test/e2e/provider_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order
// hosting the hosted cluster.
GinkgoT().Setenv(clusterdeployment.EnvVarAWSInstanceType, "t3.xlarge")
GinkgoT().Setenv(clusterdeployment.EnvVarServiceNamespace, "default")
GinkgoT().Setenv(clusterdeployment.EnvVarServiceName, "managed-ingress-nginx")

templateBy(clusterdeployment.TemplateAWSStandaloneCP, "creating a ClusterDeployment")
sd := clusterdeployment.GetUnstructured(clusterdeployment.TemplateAWSStandaloneCP)
Expand All @@ -96,6 +97,20 @@ var _ = Describe("AWS Templates", Label("provider:cloud", "provider:aws"), Order
return deploymentValidator.Validate(context.Background(), kc)
}).WithTimeout(30 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())

// validating service included in the cluster deployment is deployed
serviceDeployedValidator := clusterdeployment.NewServiceValidator(clusterName, "default", "managed-ingress-nginx").
WithResourceValidation("service", clusterdeployment.ManagedServiceResource{
ResourceNameSuffix: "",
ValidationFunc: clusterdeployment.ValidateService,
}).
WithResourceValidation("deployment", clusterdeployment.ManagedServiceResource{
ResourceNameSuffix: "",
ValidationFunc: clusterdeployment.ValidateDeployment,
})
Eventually(func() error {
return serviceDeployedValidator.Validate(context.Background(), kc)
}).WithTimeout(30 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())

templateBy(clusterdeployment.TemplateAWSHostedCP, "installing controller and templates on standalone cluster")

// Download the KUBECONFIG for the standalone cluster and load it
Expand Down

0 comments on commit b07c28b

Please sign in to comment.