Skip to content

Commit

Permalink
feat(operator): implement KeptnAppCreationRequest controller (#1191)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Apr 24, 2023
1 parent c3754b7 commit 79afd83
Show file tree
Hide file tree
Showing 16 changed files with 801 additions and 22 deletions.
1 change: 1 addition & 0 deletions operator/apis/lifecycle/v1alpha1/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations"
const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations"
const TaskNameAnnotation = "keptn.sh/task-name"
const NamespaceEnabledAnnotation = "keptn.sh/lifecycle-toolkit"

const CreateAppTaskSpanName = "create_%s_app_task"
const CreateWorkloadTaskSpanName = "create_%s_deployment_task"
const CreateAppEvalSpanName = "create_%s_app_evaluation"
Expand Down
9 changes: 9 additions & 0 deletions operator/apis/lifecycle/v1alpha3/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const PostDeploymentTaskAnnotation = "keptn.sh/post-deployment-tasks"
const K8sRecommendedWorkloadAnnotations = "app.kubernetes.io/name"
const K8sRecommendedVersionAnnotations = "app.kubernetes.io/version"
const K8sRecommendedAppAnnotations = "app.kubernetes.io/part-of"
const K8sRecommendedManagedByAnnotations = "app.kubernetes.io/managed-by"
const PreDeploymentEvaluationAnnotation = "keptn.sh/pre-deployment-evaluations"
const PostDeploymentEvaluationAnnotation = "keptn.sh/post-deployment-evaluations"
const TaskNameAnnotation = "keptn.sh/task-name"
Expand All @@ -28,12 +29,20 @@ const CreateAppTaskSpanName = "create_%s_app_task"
const CreateWorkloadTaskSpanName = "create_%s_deployment_task"
const CreateAppEvalSpanName = "create_%s_app_evaluation"
const CreateWorkloadEvalSpanName = "create_%s_deployment_evaluation"
const AppTypeAnnotation = "keptn.sh/app-type"

const MaxAppNameLength = 25
const MaxWorkloadNameLength = 25
const MaxTaskNameLength = 25
const MaxVersionLength = 12

type AppType string

const (
AppTypeSingleService AppType = "single-service"
AppTypeMultiService AppType = "multi-service"
)

type KeptnState string

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha3

import (
"github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -59,3 +60,7 @@ type KeptnAppCreationRequestList struct {
func init() {
SchemeBuilder.Register(&KeptnAppCreationRequest{}, &KeptnAppCreationRequestList{})
}

func (kacr *KeptnAppCreationRequest) IsSingleService() bool {
return kacr.Annotations[common.AppTypeAnnotation] == string(common.AppTypeSingleService)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package v1alpha3

import (
"testing"

"github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestKeptnAppCreationRequest_IsSingleService(t *testing.T) {
type fields struct {
ObjectMeta v1.ObjectMeta
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "single-service application",
fields: fields{ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
common.AppTypeAnnotation: string(common.AppTypeSingleService),
},
}},
want: true,
},
{
name: "multi-service application",
fields: fields{ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
common.AppTypeAnnotation: string(common.AppTypeMultiService),
},
}},
want: false,
},
{
name: "anything else",
fields: fields{ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
common.AppTypeAnnotation: "",
},
}},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
kacr := &KeptnAppCreationRequest{
ObjectMeta: tt.fields.ObjectMeta,
}
if got := kacr.IsSingleService(); got != tt.want {
t.Errorf("IsSingleService() = %v, want %v", got, tt.want)
}
})
}
}
36 changes: 36 additions & 0 deletions operator/controllers/common/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

import (
"sync"
"time"
)

const defaultKeptnAppCreationRequestTimeout = 30 * time.Second

//go:generate moq -pkg fake -skip-ensure -out ./fake/config_mock.go . IConfig:MockConfig
type IConfig interface {
SetCreationRequestTimeout(value time.Duration)
GetCreationRequestTimeout() time.Duration
}

type ControllerConfig struct {
keptnAppCreationRequestTimeout time.Duration
}

var instance *ControllerConfig
var once = sync.Once{}

func Instance() *ControllerConfig {
once.Do(func() {
instance = &ControllerConfig{keptnAppCreationRequestTimeout: defaultKeptnAppCreationRequestTimeout}
})
return instance
}

func (o *ControllerConfig) SetCreationRequestTimeout(value time.Duration) {
o.keptnAppCreationRequestTimeout = value
}

func (o *ControllerConfig) GetCreationRequestTimeout() time.Duration {
return o.keptnAppCreationRequestTimeout
}
38 changes: 38 additions & 0 deletions operator/controllers/common/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package config

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestConfig_GetDefaultCreationRequestTimeout(t *testing.T) {
i := Instance()

timeout := i.GetCreationRequestTimeout()

require.Equal(t, defaultKeptnAppCreationRequestTimeout, timeout)
}

func TestConfig_SetDefaultCreationRequestTimeout(t *testing.T) {
i := Instance()

i.SetCreationRequestTimeout(5 * time.Second)

timeout := i.GetCreationRequestTimeout()
require.Equal(t, 5*time.Second, timeout)
}

func TestGetOptionsInstance(t *testing.T) {
o := Instance()
require.NotNil(t, o)

o.SetCreationRequestTimeout(5 * time.Second)

// verify that all sets/gets operator on the same instance
o2 := Instance()

timeout := o2.GetCreationRequestTimeout()
require.Equal(t, 5*time.Second, timeout)
}
108 changes: 108 additions & 0 deletions operator/controllers/common/config/fake/config_mock.go

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

Loading

0 comments on commit 79afd83

Please sign in to comment.