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

feat(operator): implement KeptnAppCreationRequest controller #1191

Merged
merged 17 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
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 @@ -17,6 +17,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 @@ -25,12 +26,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 Config struct {
thisthat marked this conversation as resolved.
Show resolved Hide resolved
keptnAppCreationRequestTimeout time.Duration
}

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

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

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

func (o *Config) 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