Skip to content

Commit

Permalink
feat(operator): refactor existing interfaces (#419)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Nov 21, 2022
1 parent 7f15baf commit f9c28a8
Show file tree
Hide file tree
Showing 24 changed files with 827 additions and 254 deletions.
9 changes: 9 additions & 0 deletions operator/api/v1alpha1/keptnevaluation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -97,6 +98,14 @@ func init() {
SchemeBuilder.Register(&KeptnEvaluation{}, &KeptnEvaluationList{})
}

func (e KeptnEvaluationList) GetItems() []client.Object {
var b []client.Object
for _, i := range e.Items {
b = append(b, &i)
}
return b
}

func (e *KeptnEvaluation) SetStartTime() {
if e.Status.StartTime.IsZero() {
e.Status.StartTime = metav1.NewTime(time.Now().UTC())
Expand Down
9 changes: 9 additions & 0 deletions operator/api/v1alpha1/keptntask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -100,6 +101,14 @@ func init() {
SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{})
}

func (t KeptnTaskList) GetItems() []client.Object {
var b []client.Object
for _, i := range t.Items {
b = append(b, &i)
}
return b
}

func (t *KeptnTask) SetStartTime() {
if t.Status.StartTime.IsZero() {
t.Status.StartTime = metav1.NewTime(time.Now().UTC())
Expand Down
19 changes: 19 additions & 0 deletions operator/api/v1alpha1/tests/keptnappversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,24 @@ func TestKeptnAppVersion_CancelRemainingPhases(t *testing.T) {
require.Equal(t, tt.want, tt.app)
})
}
}

func TestKeptnAppVersionList(t *testing.T) {
list := v1alpha1.KeptnAppVersionList{
Items: []v1alpha1.KeptnAppVersion{
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj1",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj2",
},
},
},
}

got := list.GetItems()
require.Len(t, got, 2)
}
20 changes: 20 additions & 0 deletions operator/api/v1alpha1/tests/keptnevaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ func TestKeptnEvaluation(t *testing.T) {
}, evaluation.GetSpanAttributes())

}

func TestKeptnEvaluationList(t *testing.T) {
list := v1alpha1.KeptnEvaluationList{
Items: []v1alpha1.KeptnEvaluation{
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj1",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj2",
},
},
},
}

got := list.GetItems()
require.Len(t, got, 2)
}
20 changes: 20 additions & 0 deletions operator/api/v1alpha1/tests/keptntask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ func TestKeptnTask(t *testing.T) {
}, task.GetSpanAttributes())

}

func TestKeptnTaskList(t *testing.T) {
list := v1alpha1.KeptnTaskList{
Items: []v1alpha1.KeptnTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj1",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj2",
},
},
},
}

got := list.GetItems()
require.Len(t, got, 2)
}
19 changes: 19 additions & 0 deletions operator/api/v1alpha1/tests/keptnworkloadinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,24 @@ func TestKeptnWorkloadInstance_CancelRemainingPhases(t *testing.T) {
require.Equal(t, tt.want, tt.workloadInstance)
})
}
}

func TestKeptnWorkloadInstanceList(t *testing.T) {
list := v1alpha1.KeptnWorkloadInstanceList{
Items: []v1alpha1.KeptnWorkloadInstance{
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj1",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "obj2",
},
},
},
}

got := list.GetItems()
require.Len(t, got, 2)
}
33 changes: 33 additions & 0 deletions operator/controllers/common/activemetricsobject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import (
"go.opentelemetry.io/otel/attribute"
"sigs.k8s.io/controller-runtime/pkg/client"
)

//go:generate moq -pkg fake --skip-ensure -out ./fake/activemetricsobject_mock.go . ActiveMetricsObject
//ActiveMetricsObject represents an object whose active metrics are stored
type ActiveMetricsObject interface {
GetActiveMetricsAttributes() []attribute.KeyValue
IsEndTimeSet() bool
}

type ActiveMetricsObjectWrapper struct {
Obj ActiveMetricsObject
}

func NewActiveMetricsObjectWrapperFromClientObject(object client.Object) (*ActiveMetricsObjectWrapper, error) {
amo, ok := object.(ActiveMetricsObject)
if !ok {
return nil, ErrCannotWrapToActiveMetricsObject
}
return &ActiveMetricsObjectWrapper{Obj: amo}, nil
}

func (amo ActiveMetricsObjectWrapper) GetActiveMetricsAttributes() []attribute.KeyValue {
return amo.Obj.GetActiveMetricsAttributes()
}

func (amo ActiveMetricsObjectWrapper) IsEndTimeSet() bool {
return amo.Obj.IsEndTimeSet()
}
44 changes: 44 additions & 0 deletions operator/controllers/common/activemetricsobject_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common

import (
"testing"

"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1"
"github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1/common"
"github.com/keptn/lifecycle-toolkit/operator/controllers/common/fake"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
)

func TestActiveMetricsObjectWrapper(t *testing.T) {
appVersion := v1alpha1.KeptnAppVersion{
Status: v1alpha1.KeptnAppVersionStatus{
Status: common.StateFailed,
CurrentPhase: "test",
},
}

object, err := NewActiveMetricsObjectWrapperFromClientObject(&appVersion)
require.Nil(t, err)

require.False(t, object.IsEndTimeSet())
}

func TestActiveMetricsObject(t *testing.T) {
activeMetricsObjectMock := fake.ActiveMetricsObjectMock{
GetActiveMetricsAttributesFunc: func() []attribute.KeyValue {
return nil
},
IsEndTimeSetFunc: func() bool {
return true
},
}

wrapper := ActiveMetricsObjectWrapper{Obj: &activeMetricsObjectMock}

_ = wrapper.GetActiveMetricsAttributes()
require.Len(t, activeMetricsObjectMock.GetActiveMetricsAttributesCalls(), 1)

_ = wrapper.IsEndTimeSet()
require.Len(t, activeMetricsObjectMock.IsEndTimeSetCalls(), 1)
}
2 changes: 2 additions & 0 deletions operator/controllers/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "fmt"

var ErrCannotWrapToPhaseItem = fmt.Errorf("provided object does not implement PhaseItem interface")
var ErrCannotWrapToListItem = fmt.Errorf("provided object does not implement ListItem interface")
var ErrCannotWrapToMetricsObject = fmt.Errorf("provided object does not implement MetricsObject interface")
var ErrCannotWrapToActiveMetricsObject = fmt.Errorf("provided object does not implement ActiveMetricsObject interface")
var ErrRetryCountExceeded = fmt.Errorf("retryCount for evaluation exceeded")
var ErrNoValues = fmt.Errorf("no values")
var ErrInvalidOperator = fmt.Errorf("invalid operator")
Expand Down
99 changes: 99 additions & 0 deletions operator/controllers/common/fake/activemetricsobject_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f9c28a8

Please sign in to comment.