Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move KV and CDI to the new operand pkg #903

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/controller/common/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package common

const (
ReconcileCompleted = "ReconcileCompleted"
ReconcileCompletedMessage = "Reconcile completed successfully"
)
7 changes: 7 additions & 0 deletions pkg/controller/common/hco_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type HcoRequest struct {
Conditions HcoConditions // in-memory conditions
Ctx context.Context // context of this request, to be use for any other call
Instance *hcov1beta1.HyperConverged // the current state of the CR, as read from K8s
UpgradeMode bool // copy of the reconciler upgrade mode
ComponentUpgradeInProgress bool // if in upgrade mode, accumulate the component upgrade status
Dirty bool // is something was changed in the CR
StatusDirty bool // is something was changed in the CR's Status
Expand All @@ -25,8 +26,14 @@ func NewHcoRequest(request reconcile.Request, log logr.Logger, upgradeMode bool)
Logger: log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name),
Conditions: NewHcoConditions(),
Ctx: context.TODO(),
UpgradeMode: upgradeMode,
ComponentUpgradeInProgress: upgradeMode,
Dirty: false,
StatusDirty: false,
}
}

func (req *HcoRequest) SetUpgradeMode(upgradeMode bool) {
req.UpgradeMode = upgradeMode
req.ComponentUpgradeInProgress = upgradeMode
}
118 changes: 118 additions & 0 deletions pkg/controller/commonTestUtils/testClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package commonTestUtils

import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

type HcoTestClient struct {
client client.Client
sw *HcoTestStatusWriter
readErrors TestErrors
writeErrors TestErrors
}

func (c *HcoTestClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
if ok, err := c.readErrors.GetNextError(); ok {
return err
}
return c.client.Get(ctx, key, obj)
}

func (c *HcoTestClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
if ok, err := c.writeErrors.GetNextError(); ok {
return err
}
return c.client.List(ctx, list, opts...)
}

func (c *HcoTestClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
if ok, err := c.writeErrors.GetNextError(); ok {
return err
}
return c.client.Create(ctx, obj, opts...)
}

func (c *HcoTestClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error {
if ok, err := c.writeErrors.GetNextError(); ok {
return err
}
return c.client.Delete(ctx, obj, opts...)
}

func (c *HcoTestClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
if ok, err := c.writeErrors.GetNextError(); ok {
return err
}
return c.client.Update(ctx, obj, opts...)
}

func (c *HcoTestClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
if ok, err := c.writeErrors.GetNextError(); ok {
return err
}
return c.client.Patch(ctx, obj, patch, opts...)
}

func (c *HcoTestClient) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
if ok, err := c.writeErrors.GetNextError(); ok {
return err
}
return c.client.DeleteAllOf(ctx, obj, opts...)
}

func (c *HcoTestClient) Status() client.StatusWriter {
return c.sw
}

func (c *HcoTestClient) InitiateReadErrors(errs ...error) {
c.readErrors = errs
}

func (c *HcoTestClient) InitiateWriteErrors(errs ...error) {
c.writeErrors = errs
}

type HcoTestStatusWriter struct {
client client.Client
errors TestErrors
}

func (sw *HcoTestStatusWriter) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
if ok, err := sw.errors.GetNextError(); ok {
return err
}
return sw.client.Update(ctx, obj, opts...)
}

func (sw *HcoTestStatusWriter) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
if ok, err := sw.errors.GetNextError(); ok {
return err
}
return sw.client.Patch(ctx, obj, patch, opts...)
}

func (sw *HcoTestStatusWriter) InitiateErrors(errs ...error) {
sw.errors = errs
}

type TestErrors []error

func (errs *TestErrors) GetNextError() (bool, error) {
if len(*errs) == 0 {
return false, nil
}

err := (*errs)[0]
*errs = (*errs)[1:]

return true, err
}

func InitClient(clientObjects []runtime.Object) *HcoTestClient {
// Create a fake client to mock API calls
cl := fake.NewFakeClient(clientObjects...)
return &HcoTestClient{client: cl, sw: &HcoTestStatusWriter{client: cl}}
}
112 changes: 112 additions & 0 deletions pkg/controller/commonTestUtils/testUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package commonTestUtils

import (
"context"
networkaddons "github.com/kubevirt/cluster-network-addons-operator/pkg/apis"
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/apis"
sspopv1 "github.com/kubevirt/kubevirt-ssp-operator/pkg/apis"
vmimportv1beta1 "github.com/kubevirt/vm-import-operator/pkg/apis/v2v/v1beta1"
consolev1 "github.com/openshift/api/console/v1"
"k8s.io/apimachinery/pkg/runtime"
cdiv1beta1 "kubevirt.io/containerized-data-importer/pkg/apis/core/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/kubevirt/hyperconverged-cluster-operator/pkg/controller/common"
sdkapi "kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk/api"

hcov1beta1 "github.com/kubevirt/hyperconverged-cluster-operator/pkg/apis/hco/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

. "github.com/onsi/gomega"
)

// Name and Namespace of our primary resource
const (
Name = "kubevirt-hyperconverged"
Namespace = "kubevirt-hyperconverged"
Conversion_image = "quay.io/kubevirt/kubevirt-v2v-conversion:v2.0.0"
Vmware_image = "quay.io/kubevirt/kubevirt-vmware:v2.0.0"
)

var (
TestLogger = logf.Log.WithName("controller_hyperconverged")
TestRequest = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: Name,
Namespace: Namespace,
},
}
)

func NewHco() *hcov1beta1.HyperConverged {
return &hcov1beta1.HyperConverged{
ObjectMeta: metav1.ObjectMeta{
Name: Name,
Namespace: Namespace,
},
Spec: hcov1beta1.HyperConvergedSpec{},
}
}

func NewReq(inst *hcov1beta1.HyperConverged) *common.HcoRequest {
return &common.HcoRequest{
Request: TestRequest,
Logger: TestLogger,
Conditions: common.NewHcoConditions(),
Ctx: context.TODO(),
Instance: inst,
}
}

func NewHyperConvergedConfig() *sdkapi.NodePlacement {
seconds1, seconds2 := int64(1), int64(2)
return &sdkapi.NodePlacement{
NodeSelector: map[string]string{
"key1": "value1",
"key2": "value2",
},
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{Key: "key1", Operator: "operator1", Values: []string{"value11, value12"}},
{Key: "key2", Operator: "operator2", Values: []string{"value21, value22"}},
},
MatchFields: []corev1.NodeSelectorRequirement{
{Key: "key1", Operator: "operator1", Values: []string{"value11, value12"}},
{Key: "key2", Operator: "operator2", Values: []string{"value21, value22"}},
},
},
},
},
},
},
Tolerations: []corev1.Toleration{
{Key: "key1", Operator: "operator1", Value: "value1", Effect: "effect1", TolerationSeconds: &seconds1},
{Key: "key2", Operator: "operator2", Value: "value2", Effect: "effect2", TolerationSeconds: &seconds2},
},
}
}

func GetScheme() *runtime.Scheme {
s := scheme.Scheme

for _, f := range []func(*runtime.Scheme) error{
apis.AddToScheme,
cdiv1beta1.AddToScheme,
networkaddons.AddToScheme,
sspopv1.AddToScheme,
vmimportv1beta1.AddToScheme,
consolev1.AddToScheme,
} {
Expect(f(s)).To(BeNil())
}

return s
}
Loading