forked from kedacore/keda
-
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.
Merge branch 'kedacore:main' into main
- Loading branch information
Showing
44 changed files
with
821 additions
and
86 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,73 @@ | ||
/* | ||
Copyright 2024 The KEDA Authors | ||
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 ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
var scaledjoblog = logf.Log.WithName("scaledjob-validation-webhook") | ||
|
||
func (s *ScaledJob) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(s). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:path=/validate-keda-sh-v1alpha1-scaledjob,mutating=false,failurePolicy=ignore,sideEffects=None,groups=keda.sh,resources=scaledjobs,verbs=create;update,versions=v1alpha1,name=vscaledjob.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &ScaledJob{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (s *ScaledJob) ValidateCreate() (admission.Warnings, error) { | ||
val, _ := json.MarshalIndent(s, "", " ") | ||
scaledjoblog.Info(fmt.Sprintf("validating scaledjob creation for %s", string(val))) | ||
return nil, verifyTriggers(s, "create", false) | ||
} | ||
|
||
func (s *ScaledJob) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { | ||
val, _ := json.MarshalIndent(s, "", " ") | ||
scaledobjectlog.V(1).Info(fmt.Sprintf("validating scaledjob update for %s", string(val))) | ||
|
||
oldTa := old.(*ScaledJob) | ||
if isScaledJobRemovingFinalizer(s.ObjectMeta, oldTa.ObjectMeta, s.Spec, oldTa.Spec) { | ||
scaledjoblog.V(1).Info("finalizer removal, skipping validation") | ||
return nil, nil | ||
} | ||
return nil, verifyTriggers(s, "update", false) | ||
} | ||
|
||
func (s *ScaledJob) ValidateDelete() (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func isScaledJobRemovingFinalizer(om metav1.ObjectMeta, oldOm metav1.ObjectMeta, spec ScaledJobSpec, oldSpec ScaledJobSpec) bool { | ||
taSpec, _ := json.MarshalIndent(spec, "", " ") | ||
oldTaSpec, _ := json.MarshalIndent(oldSpec, "", " ") | ||
taSpecString := string(taSpec) | ||
oldTaSpecString := string(oldTaSpec) | ||
|
||
return len(om.Finalizers) == 0 && len(oldOm.Finalizers) == 1 && taSpecString == oldTaSpecString | ||
} |
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,83 @@ | ||
/* | ||
Copyright 2024 The KEDA Authors | ||
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" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = It("should validate empty triggers in ScaledJob", func() { | ||
|
||
namespaceName := "scaledjob-empty-triggers-set" | ||
namespace := createNamespace(namespaceName) | ||
|
||
err := k8sClient.Create(context.Background(), namespace) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
sj := createScaledJob(sjName, namespaceName, []ScaleTriggers{}) | ||
|
||
Eventually(func() error { | ||
return k8sClient.Create(context.Background(), sj) | ||
}).Should(HaveOccurred()) | ||
}) | ||
|
||
// -------------------------------------------------------------------------- // | ||
// ----------------------------- HELP FUNCTIONS ----------------------------- // | ||
// -------------------------------------------------------------------------- // | ||
func createScaledJob(name string, namespace string, triggers []ScaleTriggers) *ScaledJob { | ||
return &ScaledJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "ScaledJob", | ||
APIVersion: "keda.sh", | ||
}, | ||
Spec: ScaledJobSpec{ | ||
JobTargetRef: &batchv1.JobSpec{ | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"app": name, | ||
}, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"app": name, | ||
}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: name, | ||
Image: name, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Triggers: triggers, | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.