Skip to content

Commit

Permalink
feat(operator): introduce container-runtime runner (#1493)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Co-authored-by: Giovanni Liva <[email protected]>
  • Loading branch information
odubajDT and thisthat authored Jun 1, 2023
1 parent 0f28b8c commit 02ce860
Show file tree
Hide file tree
Showing 25 changed files with 1,990 additions and 52 deletions.
12 changes: 12 additions & 0 deletions examples/sample-app/base/container-task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
name: container-sleep
spec:
container:
name: testy-test
image: busybox:1.36.0
command:
- 'sh'
- '-c'
- 'sleep 30'
2 changes: 2 additions & 0 deletions examples/sample-app/version-1/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ spec:
version: 0.1.1
preDeploymentEvaluations:
- app-pre-deploy-eval-1
preDeploymentTasks:
- container-sleep
2 changes: 2 additions & 0 deletions examples/sample-app/version-2/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ spec:
version: 0.1.1
preDeploymentEvaluations:
- app-pre-deploy-eval-2
preDeploymentTasks:
- container-sleep
2 changes: 2 additions & 0 deletions examples/sample-app/version-3/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ spec:
version: 0.1.1
- name: podtato-head-hat
version: 0.1.2
preDeploymentTasks:
- container-sleep
26 changes: 24 additions & 2 deletions operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha3

import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -27,7 +28,12 @@ import (
type KeptnTaskDefinitionSpec struct {
// Function contains the definition for the function that is to be executed in KeptnTasks based on
// the KeptnTaskDefinitions.
Function FunctionSpec `json:"function,omitempty"`
// +optional
Function *FunctionSpec `json:"function,omitempty"`
// Container contains the definition for the container that is to be used in Job based on
// the KeptnTaskDefinitions.
// +optional
Container *ContainerSpec `json:"container,omitempty"`
// Retries specifies how many times a job executing the KeptnTaskDefinition should be restarted in the case
// of an unsuccessful attempt.
// +kubebuilder:default:=10
Expand All @@ -39,7 +45,6 @@ type KeptnTaskDefinitionSpec struct {
// +kubebuilder:default:="5m"
// +kubebuilder:validation:Pattern="^0|([0-9]+(\\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$"
// +kubebuilder:validation:Type:=string
// +optional
Timeout metav1.Duration `json:"timeout,omitempty"`
}

Expand Down Expand Up @@ -85,6 +90,7 @@ type HttpReference struct {
}

type ContainerSpec struct {
*v1.Container `json:",inline"`
}

// KeptnTaskDefinitionStatus defines the observed state of KeptnTaskDefinition
Expand Down Expand Up @@ -125,3 +131,19 @@ type KeptnTaskDefinitionList struct {
func init() {
SchemeBuilder.Register(&KeptnTaskDefinition{}, &KeptnTaskDefinitionList{})
}

func (d KeptnTaskDefinition) SpecExists() bool {
return d.IsJSSpecDefined() || d.IsContainerSpecDefined()
}

func (d KeptnTaskDefinition) IsJSSpecDefined() bool {
return d.Spec.Function != nil
}

func (d KeptnTaskDefinition) IsContainerSpecDefined() bool {
return d.Spec.Container != nil
}

func (d KeptnTaskDefinition) IsVolumeMountPresent() bool {
return d.IsContainerSpecDefined() && d.Spec.Container.VolumeMounts != nil && len(d.Spec.Container.VolumeMounts) > 0
}
167 changes: 167 additions & 0 deletions operator/apis/lifecycle/v1alpha3/keptntaskdefinition_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package v1alpha3

import (
"testing"

"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
)

var jsTaskDef = &KeptnTaskDefinition{
Spec: KeptnTaskDefinitionSpec{
Function: &FunctionSpec{
Inline: Inline{
Code: "some code",
},
},
},
}

var containerTaskDef = &KeptnTaskDefinition{
Spec: KeptnTaskDefinitionSpec{
Container: &ContainerSpec{
Container: &v1.Container{
Image: "image",
},
},
},
}

func Test_SpecExists(t *testing.T) {
tests := []struct {
name string
taskDef *KeptnTaskDefinition
want bool
}{
{
name: "js builder",
taskDef: jsTaskDef,
want: true,
},
{
name: "container builder",
taskDef: containerTaskDef,
want: true,
},
{
name: "empty builder",
taskDef: &KeptnTaskDefinition{
Spec: KeptnTaskDefinitionSpec{},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, tt.taskDef.SpecExists())
})
}
}

func Test_IsJSSpecDefined(t *testing.T) {
tests := []struct {
name string
taskDef *KeptnTaskDefinition
want bool
}{
{
name: "defined",
taskDef: jsTaskDef,
want: true,
},
{
name: "empty",
taskDef: &KeptnTaskDefinition{},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, tt.taskDef.IsJSSpecDefined())
})
}
}

func Test_IsVolumeMountPresent(t *testing.T) {
tests := []struct {
name string
taskDef *KeptnTaskDefinition
want bool
}{
{
name: "defined",
taskDef: &KeptnTaskDefinition{
Spec: KeptnTaskDefinitionSpec{
Container: &ContainerSpec{
Container: &v1.Container{
Image: "image",
VolumeMounts: []v1.VolumeMount{
{
Name: "name",
MountPath: "path",
},
},
},
},
},
},
want: true,
},
{
name: "empty",
taskDef: &KeptnTaskDefinition{
Spec: KeptnTaskDefinitionSpec{
Container: &ContainerSpec{
Container: &v1.Container{
Image: "image",
VolumeMounts: []v1.VolumeMount{},
},
},
},
},
want: false,
},
{
name: "nil",
taskDef: &KeptnTaskDefinition{
Spec: KeptnTaskDefinitionSpec{
Container: &ContainerSpec{
Container: &v1.Container{
Image: "image",
},
},
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, tt.taskDef.IsVolumeMountPresent())
})
}
}

func Test_IsContainerSpecDefined(t *testing.T) {
tests := []struct {
name string
taskDef *KeptnTaskDefinition
want bool
}{
{
name: "defined",
taskDef: containerTaskDef,
want: true,
},
{
name: "empty",
taskDef: &KeptnTaskDefinition{},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, tt.taskDef.IsContainerSpecDefined())
})
}
}
17 changes: 16 additions & 1 deletion operator/apis/lifecycle/v1alpha3/zz_generated.deepcopy.go

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

Loading

0 comments on commit 02ce860

Please sign in to comment.