-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
764 additions
and
200 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
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,123 @@ | ||
/* | ||
Copyright 2022 Quentin Lemaire <[email protected]>. | ||
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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"go.opentelemetry.io/otel" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"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" | ||
) | ||
|
||
var ( | ||
// log is for logging in this package. | ||
feedlog = logf.Log.WithName("feed-resource") | ||
tracer = otel.GetTracerProvider().Tracer("webhook") | ||
) | ||
|
||
const defaultParentDirID uint = 0 | ||
|
||
func (r *Feed) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
_, span := tracer.Start(context.Background(), "v1alpha1.Feed.SetupWebhookWithManager") | ||
defer span.End() | ||
|
||
//nolint:wrapcheck | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/mutate-putio-skynewz-dev-v1alpha1-feed,mutating=true,failurePolicy=fail,sideEffects=None,groups=putio.skynewz.dev,resources=feeds,verbs=create;update,versions=v1alpha1,name=mfeed.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Defaulter = &Feed{} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type. | ||
func (r *Feed) Default() { | ||
_, span := tracer.Start(context.Background(), "v1alpha1.Feed.Default") | ||
defer span.End() | ||
|
||
feedlog.Info("default", "name", r.Name) | ||
|
||
if r.Spec.ParentDirID == nil { | ||
r.Spec.ParentDirID = new(uint) | ||
*r.Spec.ParentDirID = defaultParentDirID | ||
} | ||
|
||
if r.Spec.DeleteOldFiles == nil { | ||
r.Spec.DeleteOldFiles = new(bool) | ||
} | ||
|
||
if r.Spec.DontProcessWholeFeed == nil { | ||
r.Spec.DontProcessWholeFeed = new(bool) | ||
} | ||
|
||
if r.Spec.Paused == nil { | ||
r.Spec.Paused = new(bool) | ||
} | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-putio-skynewz-dev-v1alpha1-feed,mutating=false,failurePolicy=fail,sideEffects=None,groups=putio.skynewz.dev,resources=feeds,verbs=create;update,versions=v1alpha1,name=vfeed.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &Feed{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (r *Feed) ValidateCreate() error { | ||
_, span := tracer.Start(context.Background(), "v1alpha1.Feed.ValidateCreate") | ||
defer span.End() | ||
|
||
feedlog.Info("validate create", "name", r.Name) | ||
return r.validateFeedSpec() | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (r *Feed) ValidateUpdate(_ runtime.Object) error { | ||
_, span := tracer.Start(context.Background(), "v1alpha1.Feed.ValidateUpdate") | ||
defer span.End() | ||
|
||
feedlog.Info("validate update", "name", r.Name) | ||
return r.validateFeedSpec() | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||
func (r *Feed) ValidateDelete() error { | ||
_, span := tracer.Start(context.Background(), "v1alpha1.Feed.ValidateDelete") | ||
defer span.End() | ||
|
||
feedlog.Info("validate delete", "name", r.Name) | ||
return nil // nothing to validate on deletion | ||
} | ||
|
||
func (r *Feed) validateFeedSpec() error { | ||
_, span := tracer.Start(context.Background(), "v1alpha1.Feed.validateFeedSpec") | ||
defer span.End() | ||
|
||
// validate URL | ||
return r.validateRSSSourceURL(r.Spec.RssSourceURL, field.NewPath("spec").Child("rss_source_url")) | ||
} | ||
|
||
func (r *Feed) validateRSSSourceURL(u string, fldPath *field.Path) error { | ||
if _, err := url.ParseRequestURI(u); err != nil { | ||
return field.Invalid(fldPath, u, "invalid URL provided") | ||
} | ||
|
||
return 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,134 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func TestFeed_validateRSSSourceURL(t *testing.T) { | ||
type fields struct { | ||
TypeMeta v1.TypeMeta | ||
ObjectMeta v1.ObjectMeta | ||
Spec FeedSpec | ||
Status FeedStatus | ||
} | ||
type args struct { | ||
u string | ||
fldPath *field.Path | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid URL", | ||
fields: fields{}, | ||
args: args{ | ||
u: "https://google.fr", | ||
fldPath: field.NewPath("spec").Child("rss_source_url"), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid url", | ||
fields: fields{}, | ||
args: args{ | ||
u: "foo bar", | ||
fldPath: field.NewPath("spec").Child("rss_source_url"), | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := &Feed{ | ||
TypeMeta: tt.fields.TypeMeta, | ||
ObjectMeta: tt.fields.ObjectMeta, | ||
Spec: tt.fields.Spec, | ||
Status: tt.fields.Status, | ||
} | ||
if err := r.validateRSSSourceURL(tt.args.u, tt.args.fldPath); (err != nil) != tt.wantErr { | ||
t.Errorf("validateRSSSourceURL() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
var _ = Describe("Feed webhook", func() { | ||
// Define utility constants for object names and testing timeouts/durations and intervals. | ||
const ( | ||
FeedName = "test-cronjob" | ||
FeedNamespace = "default" | ||
) | ||
|
||
Context("When creating Feed", func() { | ||
It("Should validate Feed URL", func() { | ||
By("By giving a wrong URL") | ||
ctx := context.Background() | ||
feed := &Feed{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "Feed", | ||
APIVersion: "putio.skynewz.dev/v1alpha1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: FeedName, | ||
Namespace: FeedNamespace, | ||
}, | ||
Spec: FeedSpec{ | ||
Title: "foo", | ||
RssSourceURL: "foo bar", | ||
ParentDirID: nil, | ||
DeleteOldFiles: new(bool), | ||
DontProcessWholeFeed: new(bool), | ||
Keyword: "foo", | ||
UnwantedKeywords: "", | ||
Paused: new(bool), | ||
AuthSecretRef: AuthSecretReference{ | ||
Name: "putio-token", | ||
Key: "token", | ||
}, | ||
}, | ||
Status: FeedStatus{}, | ||
} | ||
|
||
expectedError := "admission webhook \"vfeed.kb.io\" denied the request: spec.rss_source_url: Invalid value: \"foo bar\": invalid URL provided" | ||
Expect(k8sClient.Create(ctx, feed)).Should(MatchError(expectedError)) | ||
|
||
By("By giving a valid URL") | ||
feed = &Feed{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "Feed", | ||
APIVersion: "putio.skynewz.dev/v1alpha1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: FeedName, | ||
Namespace: FeedNamespace, | ||
}, | ||
Spec: FeedSpec{ | ||
Title: "foo", | ||
RssSourceURL: "https://www.google.fr", | ||
ParentDirID: nil, | ||
DeleteOldFiles: new(bool), | ||
DontProcessWholeFeed: new(bool), | ||
Keyword: "foo", | ||
UnwantedKeywords: "", | ||
Paused: new(bool), | ||
AuthSecretRef: AuthSecretReference{ | ||
Name: "putio-token", | ||
Key: "token", | ||
}, | ||
}, | ||
Status: FeedStatus{}, | ||
} | ||
|
||
Expect(k8sClient.Create(ctx, feed)).Should(Succeed()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.