Skip to content

Commit

Permalink
feat: adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
kqzh committed Oct 13, 2023
1 parent 7b208e0 commit b37d955
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 223 deletions.
27 changes: 23 additions & 4 deletions tests/e2e/e2evalidator/e2evalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@ package e2evalidator

import (
"fmt"
"github.com/go-playground/validator/v10"
corev1 "k8s.io/api/core/v1"
"reflect"
"strings"

"github.com/go-playground/validator/v10"
)

type Ruler interface {
Cmp(val any) error
}

type Rule string

func StructWithRules(actual any, rulesMapping map[string]Rule) (errMessages []string) {
func (r Rule) Cmp(val any) error {
return validator.New().Var(val, string(r))
}

type Resource corev1.ResourceRequirements

func (r Resource) Cmp(val any) error {
if !reflect.DeepEqual(val, corev1.ResourceRequirements(r)) {
return fmt.Errorf("resource not equal, expected %v, got %v", r, val)
}

return nil
}

func StructWithRules(actual any, rulesMapping map[string]Ruler) (errMessages []string) {
for field, rule := range rulesMapping {
actualVal := reflect.ValueOf(actual)

Expand All @@ -32,7 +50,8 @@ func StructWithRules(actual any, rulesMapping map[string]Rule) (errMessages []st
actualVal = actualVal.Elem()
}
val := actualVal.Interface()
if err := validator.New().Var(val, string(rule)); err != nil {

if err := rule.Cmp(val); err != nil {
errMessages = append(errMessages, fmt.Sprintf("field %q(%v) does not match %q\n", field, val, rule))
}
}
Expand Down
65 changes: 62 additions & 3 deletions tests/e2e/envfuncsext/nebulacluster-ready-func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package envfuncsext
import (
"context"
stderrors "errors"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"

"k8s.io/klog/v2"
"sigs.k8s.io/e2e-framework/pkg/envconf"
Expand Down Expand Up @@ -46,7 +49,7 @@ func DefaultNebulaClusterReadyFunc(ctx context.Context, cfg *envconf.Config, nc
return true, nil
}

func NebulaClusterReadyFuncForFields(ignoreValidationErrors bool, rulesMapping map[string]e2evalidator.Rule) NebulaClusterReadyFunc {
func NebulaClusterReadyFuncForFields(ignoreValidationErrors bool, rulesMapping map[string]e2evalidator.Ruler) NebulaClusterReadyFunc {
return func(_ context.Context, _ *envconf.Config, nc *appsv1alpha1.NebulaCluster) (isReady bool, err error) {
if errMessages := e2evalidator.StructWithRules(nc, rulesMapping); len(errMessages) > 0 {
if ignoreValidationErrors {
Expand All @@ -61,7 +64,7 @@ func NebulaClusterReadyFuncForFields(ignoreValidationErrors bool, rulesMapping m
}
}

func defaultNebulaClusterReadyFuncForStatus(_ context.Context, _ *envconf.Config, nc *appsv1alpha1.NebulaCluster) (bool, error) {
func defaultNebulaClusterReadyFuncForStatus(ctx context.Context, cfg *envconf.Config, nc *appsv1alpha1.NebulaCluster) (bool, error) {
isReady := nc.IsReady()
if isReady {
// TODO: Add more checks
Expand Down Expand Up @@ -110,6 +113,24 @@ func defaultNebulaClusterReadyFuncForStatus(_ context.Context, _ *envconf.Config
)
}
}

{ // Metad Resource checks
if !isComponentResourceExpected(ctx, cfg, nc.MetadComponent()) {
isReady = false
}
}

{ // Storaged Resource checks
if !isComponentResourceExpected(ctx, cfg, nc.StoragedComponent()) {
isReady = false
}
}

{ // Graphd Resource checks
if !isComponentResourceExpected(ctx, cfg, nc.GraphdComponent()) {
isReady = false
}
}
}

return isReady, nil
Expand Down Expand Up @@ -152,7 +173,7 @@ func isComponentStatusExpected(
) bool {
if errMessages := e2evalidator.StructWithRules(
componentStatus,
map[string]e2evalidator.Rule{
map[string]e2evalidator.Ruler{
"Version": e2evalidator.Eq(componentSpec.Version),
"Phase": e2evalidator.Eq(appsv1alpha1.RunningPhase),
"Workload.ReadyReplicas": e2evalidator.Eq(*componentSpec.Replicas),
Expand All @@ -173,3 +194,41 @@ func isComponentStatusExpected(

return true
}

func isComponentResourceExpected(ctx context.Context, cfg *envconf.Config, component appsv1alpha1.NebulaClusterComponent) bool {
sts := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: component.GetName(),
Namespace: component.GetNamespace(),
},
}

if err := cfg.Client().Resources().Get(ctx, sts.Name, sts.Namespace, sts); err != nil {
klog.InfoS("Check Component Resource but statefulset not found",
"namespace", sts.Namespace,
"name", sts.Name,
)
return false
}

for _, c := range sts.Spec.Template.Spec.Containers {
if c.Name != component.ComponentType().String() {
continue
}
resource := component.ComponentSpec().Resources()
if reflect.DeepEqual(c.Resources.DeepCopy(), resource) {
return true
} else {
klog.InfoS("Check Component Resource but not expected",
"namespace", sts.Namespace,
"name", sts.Name,
"requests", c.Resources.Requests,
"requestsExpected", resource.Requests,
"limits", c.Resources.Limits,
"limitsExpected", resource.Limits,
)
}
}

return false
}
155 changes: 0 additions & 155 deletions tests/e2e/envfuncsext/resources.go

This file was deleted.

Loading

0 comments on commit b37d955

Please sign in to comment.