-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation webhook for PromotionTemplate
Signed-off-by: Hidde Beydals <[email protected]>
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package promotiontemplate | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
kargoapi "github.com/akuity/kargo/api/v1alpha1" | ||
libWebhook "github.com/akuity/kargo/internal/webhook" | ||
) | ||
|
||
var promotionTemplateGroupKind = schema.GroupKind{ | ||
Group: kargoapi.GroupVersion.Group, | ||
Kind: "PromotionTemplate", | ||
} | ||
|
||
type webhook struct { | ||
client client.Client | ||
} | ||
|
||
func SetupWebhookWithManager( | ||
mgr ctrl.Manager, | ||
) error { | ||
w := &webhook{ | ||
client: mgr.GetClient(), | ||
} | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&kargoapi.PromotionTemplate{}). | ||
WithValidator(w). | ||
Complete() | ||
} | ||
|
||
func (w *webhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
template := obj.(*kargoapi.PromotionTemplate) // nolint: forcetypeassert | ||
if err := libWebhook.ValidateProject(ctx, w.client, promotionTemplateGroupKind, template); err != nil { | ||
return nil, err | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (w *webhook) ValidateUpdate(context.Context, runtime.Object, runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func (w *webhook) ValidateDelete(context.Context, runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package promotiontemplate | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
kargoapi "github.com/akuity/kargo/api/v1alpha1" | ||
) | ||
|
||
func Test_webhook_ValidateCreate(t *testing.T) { | ||
scheme := runtime.NewScheme() | ||
require.NoError(t, corev1.AddToScheme(scheme)) | ||
require.NoError(t, kargoapi.AddToScheme(scheme)) | ||
|
||
tests := []struct { | ||
name string | ||
objects []client.Object | ||
template *kargoapi.PromotionTemplate | ||
assertions func(*testing.T, admission.Warnings, error) | ||
}{ | ||
{ | ||
name: "project does not exist", | ||
objects: []client.Object{ | ||
&corev1.Namespace{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "fake-project", | ||
}, | ||
}, | ||
}, | ||
template: &kargoapi.PromotionTemplate{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "fake-template", | ||
Namespace: "fake-project", | ||
}, | ||
}, | ||
assertions: func(t *testing.T, warnings admission.Warnings, err error) { | ||
assert.Empty(t, warnings) | ||
assert.ErrorContains(t, err, "namespace \"fake-project\" is not a project") | ||
}, | ||
}, | ||
{ | ||
name: "project exists", | ||
objects: []client.Object{ | ||
&corev1.Namespace{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "fake-project", | ||
Labels: map[string]string{ | ||
kargoapi.ProjectLabelKey: kargoapi.LabelTrueValue, | ||
}, | ||
}, | ||
}, | ||
&kargoapi.Project{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "fake-project", | ||
}, | ||
}, | ||
}, | ||
template: &kargoapi.PromotionTemplate{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "fake-template", | ||
Namespace: "fake-project", | ||
}, | ||
}, | ||
assertions: func(t *testing.T, warnings admission.Warnings, err error) { | ||
assert.Empty(t, warnings) | ||
assert.NoError(t, err) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := fake.NewClientBuilder(). | ||
WithObjects(tt.objects...). | ||
WithScheme(scheme). | ||
Build() | ||
|
||
w := &webhook{ | ||
client: c, | ||
} | ||
|
||
got, err := w.ValidateCreate(context.Background(), tt.template) | ||
tt.assertions(t, got, err) | ||
}) | ||
} | ||
} |