Skip to content

Commit a54be60

Browse files
committed
feat: tests changes and comments improvements
Signed-off-by: jzywieck <jzywiecki@splunk.com>
1 parent 1690c1c commit a54be60

5 files changed

Lines changed: 33 additions & 151 deletions

File tree

config/default/kustomization-cluster.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ bases:
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
2323
- ../app-webhook
24-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
24+
# [CERTMANAGER] Required for apps webhook
2525
- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2727
#- ../prometheus
@@ -37,9 +37,7 @@ patchesStrategicMerge:
3737
# crd/kustomization.yaml
3838
#- manager_webhook_patch.yaml
3939

40-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
41-
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
42-
# 'CERTMANAGER' needs to be enabled to use ca injection
40+
# [CERTMANAGER] Enabled for CA injection in the admission webhooks
4341
- webhookcainjection_patch.yaml
4442

4543
# the following config is for teaching kustomize how to do var substitution

config/default/kustomization-namespace.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ bases:
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
2323
- ../app-webhook
24-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
24+
# [CERTMANAGER] Required for apps webhook
2525
- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2727
#- ../prometheus
@@ -37,9 +37,7 @@ patchesStrategicMerge:
3737
# crd/kustomization.yaml
3838
#- manager_webhook_patch.yaml
3939

40-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
41-
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
42-
# 'CERTMANAGER' needs to be enabled to use ca injection
40+
# [CERTMANAGER] Enabled for CA injection in the admission webhooks
4341
- webhookcainjection_patch.yaml
4442

4543
# the following config is for teaching kustomize how to do var substitution

config/default/kustomization.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ bases:
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
2323
- ../app-webhook
24-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
24+
# [CERTMANAGER] Required for apps webhook
2525
- ../certmanager
2626
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2727
#- ../prometheus
@@ -37,9 +37,7 @@ patchesStrategicMerge:
3737
# crd/kustomization.yaml
3838
#- manager_webhook_patch.yaml
3939

40-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
41-
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
42-
# 'CERTMANAGER' needs to be enabled to use ca injection
40+
# [CERTMANAGER] Enabled for CA injection in the admission webhooks
4341
- webhookcainjection_patch.yaml
4442

4543
# the following config is for teaching kustomize how to do var substitution

internal/webhook/apps/v1alpha1/app_webhook.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
/*
2-
Copyright 2021.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// Copyright (c) 2018-2026 Splunk Inc. All rights reserved.
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
1614

1715
package v1alpha1
1816

@@ -26,16 +24,11 @@ import (
2624
"k8s.io/apimachinery/pkg/util/validation/field"
2725
ctrl "sigs.k8s.io/controller-runtime"
2826
"sigs.k8s.io/controller-runtime/pkg/client"
29-
logf "sigs.k8s.io/controller-runtime/pkg/log"
3027
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3128

3229
appsv1alpha1 "github.com/splunk/splunk-operator/api/apps/v1alpha1"
3330
)
3431

35-
// nolint:unused
36-
// log is for logging in this package.
37-
var applog = logf.Log.WithName("app-resource")
38-
3932
const AppValidationPath = "/validate-apps-splunk-com-v1alpha1-app"
4033

4134
// SetupWebhookWithManager registers the webhook for App in the manager.
@@ -46,11 +39,9 @@ func SetupWebhookWithManager(mgr ctrl.Manager) error {
4639
Complete()
4740
}
4841

49-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
50-
// NOTE: If you want to customise the 'path', use the flags '--defaulting-path' or '--validation-path'.
5142
// +kubebuilder:webhook:path=/validate-apps-splunk-com-v1alpha1-app,mutating=false,failurePolicy=fail,sideEffects=None,groups=apps.splunk.com,resources=apps,verbs=create;update,versions=v1alpha1,name=vapp-v1alpha1.kb.io,admissionReviewVersions=v1
5243

53-
// AppValidator is a scaffold validator for the App resource.
44+
// AppValidator is a validator for the App resource.
5445
type AppValidator struct {
5546
Client client.Client
5647
}
@@ -76,8 +67,6 @@ func (v *AppValidator) ValidateCreate(_ context.Context, obj runtime.Object) (ad
7667
return nil, err
7768
}
7869

79-
applog.Info("Validation for App upon creation", "name", app.GetName())
80-
8170
allErrs := ValidateAppCreate(v.Client, app)
8271
if len(allErrs) > 0 {
8372
return GetAppWarningsOnCreate(app), apierrors.NewInvalid(v.GetGroupKind(obj), v.GetName(obj), allErrs)
@@ -98,8 +87,6 @@ func (v *AppValidator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.
9887
return nil, err
9988
}
10089

101-
applog.Info("Validation for App upon update", "name", app.GetName())
102-
10390
allErrs := ValidateAppUpdate(v.Client, app, oldApp)
10491
if len(allErrs) > 0 {
10592
return GetAppWarningsOnUpdate(app, oldApp), apierrors.NewInvalid(v.GetGroupKind(newObj), v.GetName(newObj), allErrs)
@@ -110,13 +97,6 @@ func (v *AppValidator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.
11097

11198
// ValidateDelete implements admission.CustomValidator so a webhook will be registered for the type App.
11299
func (v *AppValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
113-
app, err := appFromObject(obj)
114-
if err != nil {
115-
return nil, err
116-
}
117-
118-
applog.Info("Validation for App upon deletion", "name", app.GetName())
119-
120100
return nil, nil
121101
}
122102

internal/webhook/apps/v1alpha1/app_webhook_test.go

Lines changed: 13 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,28 @@
1-
/*
2-
Copyright 2021.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// Copyright (c) 2018-2026 Splunk Inc. All rights reserved.
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
1614

1715
package v1alpha1
1816

1917
import (
2018
"context"
21-
"net/http"
22-
"net/url"
2319
"testing"
2420

25-
"github.com/go-logr/logr"
2621
appsv1alpha1 "github.com/splunk/splunk-operator/api/apps/v1alpha1"
27-
corev1 "k8s.io/api/core/v1"
2822
apierrors "k8s.io/apimachinery/pkg/api/errors"
29-
"k8s.io/apimachinery/pkg/api/meta"
3023
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3124
"k8s.io/apimachinery/pkg/runtime"
32-
"k8s.io/client-go/rest"
33-
"k8s.io/client-go/tools/record"
34-
"sigs.k8s.io/controller-runtime/pkg/cache"
35-
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
36-
"sigs.k8s.io/controller-runtime/pkg/client"
3725
"sigs.k8s.io/controller-runtime/pkg/client/fake"
38-
"sigs.k8s.io/controller-runtime/pkg/config"
39-
"sigs.k8s.io/controller-runtime/pkg/healthz"
40-
logf "sigs.k8s.io/controller-runtime/pkg/log"
41-
"sigs.k8s.io/controller-runtime/pkg/manager"
42-
"sigs.k8s.io/controller-runtime/pkg/webhook"
4326
)
4427

4528
func TestAppValidatorAllowsValidObjects(t *testing.T) {
@@ -202,78 +185,3 @@ func TestAppValidatorRejectsDuplicateAppTarget(t *testing.T) {
202185
t.Fatalf("expected Invalid validation error, got %v", err)
203186
}
204187
}
205-
206-
func TestAppValidatorRejectsWrongTypes(t *testing.T) {
207-
validator := &AppValidator{}
208-
wrongObj := &corev1.ConfigMap{}
209-
210-
if _, err := validator.ValidateCreate(context.Background(), wrongObj); err == nil {
211-
t.Fatalf("expected create validation to reject wrong object type")
212-
}
213-
if _, err := validator.ValidateUpdate(context.Background(), wrongObj, &appsv1alpha1.App{}); err == nil {
214-
t.Fatalf("expected update validation to reject wrong old object type")
215-
}
216-
if _, err := validator.ValidateUpdate(context.Background(), &appsv1alpha1.App{}, wrongObj); err == nil {
217-
t.Fatalf("expected update validation to reject wrong new object type")
218-
}
219-
if _, err := validator.ValidateDelete(context.Background(), wrongObj); err == nil {
220-
t.Fatalf("expected delete validation to reject wrong object type")
221-
}
222-
}
223-
224-
func TestSetupWebhookWithManager(t *testing.T) {
225-
scheme := runtime.NewScheme()
226-
if err := appsv1alpha1.AddToScheme(scheme); err != nil {
227-
t.Fatalf("failed to add App scheme: %v", err)
228-
}
229-
230-
webhookServer := webhook.NewServer(webhook.Options{Port: 0})
231-
mgr := &testManager{
232-
scheme: scheme,
233-
client: fake.NewClientBuilder().WithScheme(scheme).Build(),
234-
webhookServer: webhookServer,
235-
}
236-
237-
if err := SetupWebhookWithManager(mgr); err != nil {
238-
t.Fatalf("expected setup to succeed, got %v", err)
239-
}
240-
241-
handler, path := webhookServer.WebhookMux().Handler(&http.Request{
242-
URL: &url.URL{Path: AppValidationPath},
243-
})
244-
if path != AppValidationPath {
245-
t.Fatalf("expected handler path %q, got %q", AppValidationPath, path)
246-
}
247-
if handler == nil {
248-
t.Fatalf("expected webhook handler to be registered")
249-
}
250-
}
251-
252-
var _ manager.Manager = &testManager{}
253-
254-
type testManager struct {
255-
scheme *runtime.Scheme
256-
client client.Client
257-
webhookServer webhook.Server
258-
}
259-
260-
func (m *testManager) AddMetricsServerExtraHandler(string, http.Handler) error { return nil }
261-
func (m *testManager) Add(manager.Runnable) error { return nil }
262-
func (m *testManager) Elected() <-chan struct{} { return nil }
263-
func (m *testManager) AddHealthzCheck(string, healthz.Checker) error { return nil }
264-
func (m *testManager) AddReadyzCheck(string, healthz.Checker) error { return nil }
265-
func (m *testManager) Start(context.Context) error { return nil }
266-
func (m *testManager) GetWebhookServer() webhook.Server { return m.webhookServer }
267-
func (m *testManager) GetLogger() logr.Logger { return logf.Log.WithName("test-manager") }
268-
func (m *testManager) GetControllerOptions() config.Controller { return config.Controller{} }
269-
func (m *testManager) GetHTTPClient() *http.Client { return http.DefaultClient }
270-
func (m *testManager) GetConfig() *rest.Config { return &rest.Config{} }
271-
func (m *testManager) GetScheme() *runtime.Scheme { return m.scheme }
272-
func (m *testManager) GetClient() client.Client { return m.client }
273-
func (m *testManager) GetFieldIndexer() client.FieldIndexer { return nil }
274-
func (m *testManager) GetCache() cache.Cache { return &informertest.FakeInformers{} }
275-
func (m *testManager) GetEventRecorderFor(string) record.EventRecorder {
276-
return record.NewFakeRecorder(1)
277-
}
278-
func (m *testManager) GetRESTMapper() meta.RESTMapper { return nil }
279-
func (m *testManager) GetAPIReader() client.Reader { return m.client }

0 commit comments

Comments
 (0)