Skip to content

Commit

Permalink
servicemesh memberroll adjustments + unit test added (#423)
Browse files Browse the repository at this point in the history
* memberroll unit test

* memberroll pkg adjustments
  • Loading branch information
elenagerman authored May 15, 2024
1 parent c6f7a7e commit 1bd24d0
Show file tree
Hide file tree
Showing 4 changed files with 542 additions and 18 deletions.
14 changes: 14 additions & 0 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ import (
fakeRuntimeClient "sigs.k8s.io/controller-runtime/pkg/client/fake"

operatorv1 "github.com/openshift/api/operator/v1"
istiov1 "maistra.io/api/core/v1"
istiov2 "maistra.io/api/core/v2"

nvidiagpuv1 "github.com/NVIDIA/gpu-operator/api/v1"
grafanaV4V1Alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
Expand Down Expand Up @@ -366,6 +368,14 @@ func SetScheme(crScheme *runtime.Scheme) error {
return err
}

if err := istiov1.AddToScheme(crScheme); err != nil {
return err
}

if err := istiov2.AddToScheme(crScheme); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -485,6 +495,10 @@ func GetTestClients(tcp TestClientParams) *Settings {
genericClientObjects = append(genericClientObjects, v)
case *ocsoperatorv1.StorageCluster:
genericClientObjects = append(genericClientObjects, v)
case *istiov1.ServiceMeshMemberRoll:
genericClientObjects = append(genericClientObjects, v)
case *istiov2.ServiceMeshControlPlane:
genericClientObjects = append(genericClientObjects, v)
case *clov1.ClusterLogging:
genericClientObjects = append(genericClientObjects, v)
case *clov1.ClusterLogForwarder:
Expand Down
91 changes: 74 additions & 17 deletions pkg/servicemesh/member-roll.go → pkg/servicemesh/memberroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ package servicemesh
import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
istioV1 "maistra.io/api/core/v1"
istiov1 "maistra.io/api/core/v1"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// MemberRollBuilder provides a struct for serviceMeshMemberRoll object from the cluster and
// a serviceMeshMemberRoll definition.
type MemberRollBuilder struct {
// serviceMeshMemberRoll definition, used to create the serviceMeshMemberRoll object.
Definition *istioV1.ServiceMeshMemberRoll
Definition *istiov1.ServiceMeshMemberRoll
// Created serviceMeshMemberRoll object.
Object *istioV1.ServiceMeshMemberRoll
Object *istiov1.ServiceMeshMemberRoll
// Used in functions that define or mutate serviceMeshMemberRoll definition. errorMsg is processed
// before the serviceMeshMemberRoll object is created.
errorMsg string
// api client to interact with the cluster.
apiClient *clients.Settings
apiClient goclient.Client
}

// NewMemberRollBuilder method creates new instance of builder.
Expand All @@ -34,8 +37,8 @@ func NewMemberRollBuilder(apiClient *clients.Settings, name, nsname string) *Mem
"params: name: %s, namespace: %s", name, nsname)

builder := &MemberRollBuilder{
apiClient: apiClient,
Definition: &istioV1.ServiceMeshMemberRoll{
apiClient: apiClient.Client,
Definition: &istiov1.ServiceMeshMemberRoll{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
Expand All @@ -46,26 +49,32 @@ func NewMemberRollBuilder(apiClient *clients.Settings, name, nsname string) *Mem
if name == "" {
glog.V(100).Infof("The name of the serviceMeshMemberRoll is empty")

builder.errorMsg = "The serviceMeshMemberRoll 'name' cannot be empty"
builder.errorMsg = "serviceMeshMemberRoll 'name' cannot be empty"
}

if nsname == "" {
glog.V(100).Infof("The namespace of the serviceMeshMemberRoll is empty")

builder.errorMsg = "The serviceMeshMemberRoll 'namespace' cannot be empty"
builder.errorMsg = "serviceMeshMemberRoll 'nsname' cannot be empty"
}

return builder
}

// PullMemberRole retrieves an existing serviceMeshMemberRoll object from the cluster.
func PullMemberRole(apiClient *clients.Settings, name, nsname string) (*MemberRollBuilder, error) {
// PullMemberRoll retrieves an existing serviceMeshMemberRoll object from the cluster.
func PullMemberRoll(apiClient *clients.Settings, name, nsname string) (*MemberRollBuilder, error) {
glog.V(100).Infof(
"Pulling serviceMeshMemberRoll object name: %s in namespace: %s", name, nsname)

if apiClient == nil {
glog.V(100).Infof("The apiClient is empty")

return nil, fmt.Errorf("serviceMeshMemberRoll 'apiClient' cannot be empty")
}

builder := MemberRollBuilder{
apiClient: apiClient,
Definition: &istioV1.ServiceMeshMemberRoll{
apiClient: apiClient.Client,
Definition: &istiov1.ServiceMeshMemberRoll{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
Expand All @@ -76,13 +85,13 @@ func PullMemberRole(apiClient *clients.Settings, name, nsname string) (*MemberRo
if name == "" {
glog.V(100).Infof("The name of the serviceMeshMemberRoll is empty")

builder.errorMsg = "serviceMeshMemberRoll 'name' cannot be empty"
return nil, fmt.Errorf("serviceMeshMemberRoll 'name' cannot be empty")
}

if nsname == "" {
glog.V(100).Infof("The namespace of the serviceMeshMemberRoll is empty")

builder.errorMsg = "serviceMeshMemberRoll 'nsname' cannot be empty"
return nil, fmt.Errorf("serviceMeshMemberRoll 'nsname' cannot be empty")
}

if !builder.Exists() {
Expand All @@ -95,15 +104,15 @@ func PullMemberRole(apiClient *clients.Settings, name, nsname string) (*MemberRo
}

// Get fetches existing serviceMeshMemberRoll from cluster.
func (builder *MemberRollBuilder) Get() (*istioV1.ServiceMeshMemberRoll, error) {
func (builder *MemberRollBuilder) Get() (*istiov1.ServiceMeshMemberRoll, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof("Fetching existing serviceMeshMemberRoll with name %s under namespace %s from cluster",
builder.Definition.Name, builder.Definition.Namespace)

servicemeshmemberroll := &istioV1.ServiceMeshMemberRoll{}
servicemeshmemberroll := &istiov1.ServiceMeshMemberRoll{}
err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{
Name: builder.Definition.Name,
Namespace: builder.Definition.Namespace,
Expand Down Expand Up @@ -213,7 +222,7 @@ func (builder *MemberRollBuilder) Exists() bool {
return err == nil || !k8serrors.IsNotFound(err)
}

// WithMembersList adds member list section to the ServiceMeshMemberRoleBuilder.
// WithMembersList adds member list section to the MemberRollBuilder.
func (builder *MemberRollBuilder) WithMembersList(membersList []string) *MemberRollBuilder {
glog.V(100).Infof("Adding member list %v section to the MemberRollBuilder", membersList)

Expand All @@ -236,6 +245,54 @@ func (builder *MemberRollBuilder) WithMembersList(membersList []string) *MemberR
return builder
}

// GetMembersList fetches memberRoll's membersList.
func (builder *MemberRollBuilder) GetMembersList() (*[]string, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}

glog.V(100).Infof("Getting memberRoll %s in namespace %s membersList configuration",
builder.Definition.Name, builder.Definition.Namespace)

if !builder.Exists() {
return nil, fmt.Errorf("memberRoll object %s does not exist in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
}

return &builder.Object.Spec.Members, nil
}

// IsReady check if the ServiceMesh MemberRoll is Ready.
func (builder *MemberRollBuilder) IsReady(timeout time.Duration) (bool, error) {
if valid, err := builder.validate(); !valid {
return false, err
}

err := wait.PollUntilContextTimeout(
context.TODO(), time.Second, timeout, true, func(ctx context.Context) (bool, error) {
if !builder.Exists() {
return false, nil
}

for _, condition := range builder.Object.Status.Conditions {
if condition.Type == istiov1.ConditionTypeMemberRollReady {
if condition.Status == corev1.ConditionTrue {
return true, nil
}
}
}

return false, nil
})

if err != nil {
return false, fmt.Errorf("the Ready condition did not reached for the Service Mesh MemberRoll %s in "+
"namespace %s during %v; %v", builder.Definition.Name, builder.Definition.Namespace, timeout, err)
}

return true, nil
}

// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *MemberRollBuilder) validate() (bool, error) {
Expand Down
Loading

0 comments on commit 1bd24d0

Please sign in to comment.