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: add validating webhook for KeptnTaskDefinition #1514

Merged
merged 47 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e66f2a5
feat: integrate python-runtime into pipelines (#1505)
odubajDT Jun 2, 2023
2af1475
added validating webhook for TaskDefinitions
geoffrey1330 Jun 2, 2023
1f6d09c
added validating webhook for TaskDefinitions
geoffrey1330 Jun 2, 2023
cee0114
Added TaskDefinition validation logic to allow either Function spec o…
geoffrey1330 Jun 2, 2023
184673d
Merge branch 'main' into feat/1486/validating_webhook
geoffrey1330 Jun 2, 2023
7dd8a8c
Merge branch 'feat/1486/validating_webhook' of https://github.com/geo…
geoffrey1330 Jun 2, 2023
42c8193
Added TaskDefinition validation logic to allow either Function spec o…
geoffrey1330 Jun 2, 2023
caca04d
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 2, 2023
adf509c
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 2, 2023
658e71e
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 2, 2023
2f46086
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 2, 2023
3c45ddb
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 2, 2023
101f20a
Added TaskDefinition validation logic to allow either Function spec o…
geoffrey1330 Jun 2, 2023
1fd9ff4
added ValidatingWebhookConfiguration manifest
geoffrey1330 Jun 2, 2023
95c8234
added ValidatingWebhookConfiguration manifest
geoffrey1330 Jun 2, 2023
609d75a
deleted webhook_suite_test.go file
geoffrey1330 Jun 2, 2023
dcb52c7
Added neccessary import for keptntaskdefinition_webhook
geoffrey1330 Jun 2, 2023
74f7d39
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 4, 2023
8ccfa03
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 4, 2023
f410f6d
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
geoffrey1330 Jun 4, 2023
fdff589
Added testcase keptntaskdefinition_webhook_test.go for keptntaskdefin…
geoffrey1330 Jun 4, 2023
17a0736
Added testcase keptntaskdefinition_webhook_test.go for keptntaskdefin…
geoffrey1330 Jun 4, 2023
47ecfcd
Added ObjectMeta to the testcase keptntaskdefinition_webhook_test.go …
geoffrey1330 Jun 4, 2023
21cf221
deleted certificate.yaml and webhookcainjection_patch.yaml
geoffrey1330 Jun 4, 2023
b6fa939
deleted config directory
geoffrey1330 Jun 4, 2023
fe86366
reverted back the changes in the imports in main.go and fixed yaml issue
geoffrey1330 Jun 5, 2023
b5e4d78
reverted back the changes in the imports in main.go and fixed yaml issue
geoffrey1330 Jun 5, 2023
afe5d32
reverted back the changes in the imports in main.go and fixed yaml issue
geoffrey1330 Jun 5, 2023
1bf4bbf
reverted back the changes in the imports in main.go and fixed yaml issue
geoffrey1330 Jun 5, 2023
5ab6bc2
Added integration test for validate taskdefinition
geoffrey1330 Jun 5, 2023
6244d04
Added integration test for validate taskdefinition
geoffrey1330 Jun 5, 2023
5870802
Added comment to the integration test for validating taskdefinition
geoffrey1330 Jun 5, 2023
a9e05b4
Added comment to the integration test for validating taskdefinition
geoffrey1330 Jun 5, 2023
16cc385
Added comment to the integration test for validating taskdefinition
geoffrey1330 Jun 5, 2023
c2d4129
Added comment to the integration test for validating taskdefinition
geoffrey1330 Jun 5, 2023
ba845ec
Added comment to the integration test for validating taskdefinition
geoffrey1330 Jun 5, 2023
4528ea8
Added comment to the integration test for validating taskdefinition
geoffrey1330 Jun 5, 2023
682151d
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
eb83821
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
f3cf46a
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
7480936
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
2cc657a
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
1605588
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
8de7acf
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
9b464d8
Added tests to cover the Validate[Create/Update/Delete]() methods
geoffrey1330 Jun 5, 2023
330e3c4
Update operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook_t…
geoffrey1330 Jun 6, 2023
0d9daea
Added fixes for tests that covers the Validate[Create/Update/Delete](…
geoffrey1330 Jun 6, 2023
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
3 changes: 3 additions & 0 deletions operator/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ resources:
kind: KeptnTaskDefinition
path: github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3
version: v1alpha3
webhooks:
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down
98 changes: 98 additions & 0 deletions operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha3

import (
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var keptntaskdefinitionlog = logf.Log.WithName("keptntaskdefinition-resource")

func (r *KeptnTaskDefinition) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

//+kubebuilder:webhook:path=/validate-lifecycle-keptn-sh-v1alpha3-keptntaskdefinition,mutating=false,failurePolicy=fail,sideEffects=None,groups=lifecycle.keptn.sh,resources=keptntaskdefinitions,verbs=create;update,versions=v1alpha3,name=vkeptntaskdefinition.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &KeptnTaskDefinition{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *KeptnTaskDefinition) ValidateCreate() error {
keptntaskdefinitionlog.Info("validate create", "name", r.Name)

return r.validateKeptnTaskDefinition()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *KeptnTaskDefinition) ValidateUpdate(old runtime.Object) error {
keptntaskdefinitionlog.Info("validate update", "name", r.Name)

return r.validateKeptnTaskDefinition()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *KeptnTaskDefinition) ValidateDelete() error {
keptntaskdefinitionlog.Info("validate delete", "name", r.Name)

return nil
}

func (r *KeptnTaskDefinition) validateKeptnTaskDefinition() error {
var allErrs field.ErrorList //defined as a list to allow returning multiple validation errors
var err *field.Error
if err = r.validateFields(); err != nil {
allErrs = append(allErrs, err)
}
if len(allErrs) == 0 {
return nil
}

return apierrors.NewInvalid(
schema.GroupKind{Group: "lifecycle.keptn.sh", Kind: "KeptnTaskDefinition"},
r.Name,
allErrs)
}
func (r *KeptnTaskDefinition) validateFields() *field.Error {

if r.Spec.Function == nil && r.Spec.Container == nil {
return field.Invalid(
field.NewPath("spec"),
r.Spec,
errors.New("Forbidden! Either Function or Container field must be defined").Error(),
)
}

if r.Spec.Function != nil && r.Spec.Container != nil {
return field.Invalid(
field.NewPath("spec"),
r.Spec,
errors.New("Forbidden! Both Function and Container fields cannot be defined simultaneously").Error(),
)
}

return nil
}
103 changes: 103 additions & 0 deletions operator/apis/lifecycle/v1alpha3/keptntaskdefinition_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package v1alpha3

import (
"reflect"
"testing"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func TestKeptnTaskDefinition_ValidateFields(t *testing.T) {
bacherfl marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
name string
spec KeptnTaskDefinitionSpec
want *field.Error
verb string
oldSpec runtime.Object
}{
{
name: "with-no-function-or-container",
spec: KeptnTaskDefinitionSpec{},
want: field.Invalid(
field.NewPath("spec"),
KeptnTaskDefinitionSpec{},
errors.New("Forbidden! Either Function or Container field must be defined").Error(),
),
verb: "create",
},
{
name: "with-both-function-and-container",
spec: KeptnTaskDefinitionSpec{
Function: &FunctionSpec{},
Container: &ContainerSpec{},
},
want: field.Invalid(
field.NewPath("spec"),
KeptnTaskDefinitionSpec{
Function: &FunctionSpec{},
Container: &ContainerSpec{},
},
errors.New("Forbidden! Both Function and Container fields cannot be defined simultaneously").Error(),
),
verb: "create",
},
{
name: "with-function-only",
spec: KeptnTaskDefinitionSpec{
Function: &FunctionSpec{},
},
verb: "create",
},
{
name: "with-container-only",
spec: KeptnTaskDefinitionSpec{
Container: &ContainerSpec{},
},
verb: "create",
},
{
name: "update-with-both-function-and-container",
spec: KeptnTaskDefinitionSpec{
Function: &FunctionSpec{},
},
want: field.Invalid(
field.NewPath("spec"),
KeptnTaskDefinitionSpec{Function: &FunctionSpec{}},
errors.New("Forbidden! Both Function and Container fields cannot be defined simultaneously").Error()),
oldSpec: KeptnTaskDefinitionSpec{
RealAnna marked this conversation as resolved.
Show resolved Hide resolved
Function: &FunctionSpec{},
},
verb: "update",
},
{
name: "delete",
verb: "delete",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ktd := &KeptnTaskDefinition{
ObjectMeta: metav1.ObjectMeta{Name: tt.name},
Spec: tt.spec,
}

var got error
switch tt.verb {
case "create":
got = ktd.ValidateCreate()
case "update":
got = ktd.ValidateUpdate(tt.oldSpec)
case "delete":
got = ktd.ValidateDelete()
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("validateFields() = %v, want %v", got, tt.want)
}
})
}
}

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

23 changes: 23 additions & 0 deletions operator/config/default/manager_webhook_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
ports:
- containerPort: 9443
name: webhook-server
protocol: TCP
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true
volumes:
- name: cert
secret:
defaultMode: 420
secretName: webhook-server-cert
29 changes: 29 additions & 0 deletions operator/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,32 @@ webhooks:
resources:
- pods
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
creationTimestamp: null
name: lifecycle-validating-webhook-configuration
labels:
keptn.sh/inject-cert: "true"
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: lifecycle-webhook-service
namespace: system
path: /validate-lifecycle-keptn-sh-v1alpha3-keptntaskdefinition
failurePolicy: Fail
name: vkeptntaskdefinition.kb.io
rules:
- apiGroups:
- lifecycle.keptn.sh
apiVersions:
- v1alpha3
operations:
- CREATE
- UPDATE
resources:
- keptntaskdefinitions
sideEffects: None
Loading