From 3b1ef3c93f698eae9ab5d4bdc460062e39bb3c5a Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Fri, 9 Jun 2023 17:17:31 +0800 Subject: [PATCH 01/25] Add info in kubectl get triggerauthentication By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- CHANGELOG.md | 1 + .../v1alpha1/triggerauthentication_types.go | 61 ++++++++++++++++- ...keda.sh_clustertriggerauthentications.yaml | 18 ++++- .../bases/keda.sh_triggerauthentications.yaml | 18 ++++- controllers/keda/scaledjob_controller.go | 65 +++++++++++++++++++ controllers/keda/scaledjob_finalizer.go | 1 + controllers/keda/scaledobject_controller.go | 63 ++++++++++++++++++ controllers/keda/scaledobject_finalizer.go | 1 + controllers/keda/util/string_lists.go | 34 ++++++++++ 9 files changed, 258 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ab2d6da1f..6479682a247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio ### Improvements - **General**: Metrics Adapter: remove deprecated Prometheus Metrics and non-gRPC code ([#3930](https://github.com/kedacore/keda/issues/3930)) +- **General:**: Add ScaledObject/ScaledJob Names when kubectl get triggerauthentication/clustertriggerauthentication([#796](https://github.com/kedacore/keda/issues/796)) - **AWS DynamoDB:** Add support for `indexName` ([#4680](https://github.com/kedacore/keda/issues/4680)) - **Azure Data Explorer Scaler**: Use azidentity SDK ([#4489](https://github.com/kedacore/keda/issues/4489)) - **External Scaler**: Add tls options in TriggerAuth metadata. ([#3565](https://github.com/kedacore/keda/issues/3565)) diff --git a/apis/keda/v1alpha1/triggerauthentication_types.go b/apis/keda/v1alpha1/triggerauthentication_types.go index d97b827a5a7..c284805093e 100644 --- a/apis/keda/v1alpha1/triggerauthentication_types.go +++ b/apis/keda/v1alpha1/triggerauthentication_types.go @@ -26,15 +26,19 @@ import ( // +genclient // +genclient:nonNamespaced // +kubebuilder:resource:path=clustertriggerauthentications,scope=Cluster,shortName=cta;clustertriggerauth +// +kubebuilder:subresource:status // +kubebuilder:printcolumn:name="PodIdentity",type="string",JSONPath=".spec.podIdentity.provider" // +kubebuilder:printcolumn:name="Secret",type="string",JSONPath=".spec.secretTargetRef[*].name" // +kubebuilder:printcolumn:name="Env",type="string",JSONPath=".spec.env[*].name" // +kubebuilder:printcolumn:name="VaultAddress",type="string",JSONPath=".spec.hashiCorpVault.address" +// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.allscaledobjectnamesstring" +// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.allscaledjobnamesstring" type ClusterTriggerAuthentication struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` - Spec TriggerAuthenticationSpec `json:"spec"` + Spec TriggerAuthenticationSpec `json:"spec"` + Status TriggerAuthenticationStatus `json:"status,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object @@ -51,15 +55,19 @@ type ClusterTriggerAuthenticationList struct { // TriggerAuthentication defines how a trigger can authenticate // +genclient // +kubebuilder:resource:path=triggerauthentications,scope=Namespaced,shortName=ta;triggerauth +// +kubebuilder:subresource:status // +kubebuilder:printcolumn:name="PodIdentity",type="string",JSONPath=".spec.podIdentity.provider" // +kubebuilder:printcolumn:name="Secret",type="string",JSONPath=".spec.secretTargetRef[*].name" // +kubebuilder:printcolumn:name="Env",type="string",JSONPath=".spec.env[*].name" // +kubebuilder:printcolumn:name="VaultAddress",type="string",JSONPath=".spec.hashiCorpVault.address" +// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.allscaledobjectnamesstring" +// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.allscaledjobnamesstring" type TriggerAuthentication struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` - Spec TriggerAuthenticationSpec `json:"spec"` + Spec TriggerAuthenticationSpec `json:"spec"` + Status TriggerAuthenticationStatus `json:"status,omitempty"` } // TriggerAuthenticationSpec defines the various ways to authenticate @@ -80,6 +88,14 @@ type TriggerAuthenticationSpec struct { AzureKeyVault *AzureKeyVault `json:"azureKeyVault,omitempty"` } +// TriggerAuthenticationStatus defines the observed state of TriggerAuthentication +type TriggerAuthenticationStatus struct { + // +optional + ScaledObjectNamesStr string `json:"allscaledobjectnamesstring,omitempty"` + // +optional + ScaledJobNamesStr string `json:"allscaledjobnamesstring,omitempty"` +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // TriggerAuthenticationList contains a list of TriggerAuthentication @@ -231,3 +247,44 @@ func init() { SchemeBuilder.Register(&ClusterTriggerAuthentication{}, &ClusterTriggerAuthenticationList{}) SchemeBuilder.Register(&TriggerAuthentication{}, &TriggerAuthenticationList{}) } + +// interface that can handle TriggerAuthentication/ClusterTriggerAuthentication generically +type TriggerAuthenticationStatusHandler interface { + metav1.Object + GetScaledObjectNameStr() string + SetScaledObjectNameStr(str string) + GetScaledJobNameStr() string + SetScaledJobNameStr(str string) +} + +func (t *TriggerAuthentication) GetScaledObjectNameStr() string { + return t.Status.ScaledObjectNamesStr +} + +func (t *TriggerAuthentication) SetScaledObjectNameStr(str string) { + t.Status.ScaledObjectNamesStr = str +} + +func (t *TriggerAuthentication) GetScaledJobNameStr() string { + return t.Status.ScaledJobNamesStr +} + +func (t *TriggerAuthentication) SetScaledJobNameStr(str string) { + t.Status.ScaledJobNamesStr = str +} + +func (c *ClusterTriggerAuthentication) GetScaledObjectNameStr() string { + return c.Status.ScaledObjectNamesStr +} + +func (c *ClusterTriggerAuthentication) SetScaledObjectNameStr(str string) { + c.Status.ScaledObjectNamesStr = str +} + +func (c *ClusterTriggerAuthentication) GetScaledJobNameStr() string { + return c.Status.ScaledJobNamesStr +} + +func (c *ClusterTriggerAuthentication) SetScaledJobNameStr(str string) { + c.Status.ScaledJobNamesStr = str +} diff --git a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml index 2e0c1e36523..6fecd1bf509 100644 --- a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml +++ b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml @@ -30,6 +30,12 @@ spec: - jsonPath: .spec.hashiCorpVault.address name: VaultAddress type: string + - jsonPath: .status.allscaledobjectnamesstring + name: ScaledObjects + type: string + - jsonPath: .status.allscaledjobnamesstring + name: ScaledJobs + type: string name: v1alpha1 schema: openAPIV3Schema: @@ -222,9 +228,19 @@ spec: type: object type: array type: object + status: + description: TriggerAuthenticationStatus defines the observed state of + TriggerAuthentication + properties: + allscaledjobnamesstring: + type: string + allscaledobjectnamesstring: + type: string + type: object required: - spec type: object served: true storage: true - subresources: {} + subresources: + status: {} diff --git a/config/crd/bases/keda.sh_triggerauthentications.yaml b/config/crd/bases/keda.sh_triggerauthentications.yaml index e97e481f74d..b390d537431 100644 --- a/config/crd/bases/keda.sh_triggerauthentications.yaml +++ b/config/crd/bases/keda.sh_triggerauthentications.yaml @@ -30,6 +30,12 @@ spec: - jsonPath: .spec.hashiCorpVault.address name: VaultAddress type: string + - jsonPath: .status.allscaledobjectnamesstring + name: ScaledObjects + type: string + - jsonPath: .status.allscaledjobnamesstring + name: ScaledJobs + type: string name: v1alpha1 schema: openAPIV3Schema: @@ -221,9 +227,19 @@ spec: type: object type: array type: object + status: + description: TriggerAuthenticationStatus defines the observed state of + TriggerAuthentication + properties: + allscaledjobnamesstring: + type: string + allscaledobjectnamesstring: + type: string + type: object required: - spec type: object served: true storage: true - subresources: {} + subresources: + status: {} diff --git a/controllers/keda/scaledjob_controller.go b/controllers/keda/scaledjob_controller.go index 806f095e455..a4d4cc6bb43 100644 --- a/controllers/keda/scaledjob_controller.go +++ b/controllers/keda/scaledjob_controller.go @@ -39,6 +39,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" + kedacontrollerutil "github.com/kedacore/keda/v2/controllers/keda/util" "github.com/kedacore/keda/v2/pkg/eventreason" "github.com/kedacore/keda/v2/pkg/prommetrics" "github.com/kedacore/keda/v2/pkg/scaling" @@ -155,6 +156,9 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledJob, &conditions); err != nil { return ctrl.Result{}, err } + + r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledJob) + return ctrl.Result{}, err } @@ -313,3 +317,64 @@ func (r *ScaledJobReconciler) updatePromMetricsOnDelete(namespacedName string) { delete(scaledJobPromMetricsMap, namespacedName) } + +func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { + for _, trigger := range scaledJob.Spec.Triggers { + var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler + + if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { + triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} + } else { + triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} + } + + err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledJob.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) + + if err != nil { + if errors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication") + continue + } + + triggerAuthentication.SetScaledJobNameStr(kedacontrollerutil.AppendIntoString(triggerAuthentication.GetScaledJobNameStr(), scaledJob.GetName(), ",")) + err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) + if err != nil { + logger.Error(err, "Error updating TriggerAuthentication Status") + return "Update TriggerAuthentication Status Failed", err + } + } + return "Update TriggerAuthentication Status Successfully", nil +} + +func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { + + for _, trigger := range scaledJob.Spec.Triggers { + var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler + + if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { + triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} + } else { + triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} + } + + err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledJob.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) + + if err != nil { + if errors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication") + continue + } + + triggerAuthentication.SetScaledJobNameStr(kedacontrollerutil.RemoveFromString(triggerAuthentication.GetScaledJobNameStr(), scaledJob.GetName(), ",")) + err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) + if err != nil { + logger.Error(err, "Error updating TriggerAuthentication Status") + return "Update TriggerAuthentication Status Failed", err + } + } + return "Update TriggerAuthentication Status OnDelete Successfully", nil +} diff --git a/controllers/keda/scaledjob_finalizer.go b/controllers/keda/scaledjob_finalizer.go index 4275ceb5c60..9f25067057b 100644 --- a/controllers/keda/scaledjob_finalizer.go +++ b/controllers/keda/scaledjob_finalizer.go @@ -50,6 +50,7 @@ func (r *ScaledJobReconciler) finalizeScaledJob(ctx context.Context, logger logr return err } + r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledJob) r.updatePromMetricsOnDelete(namespacedName) } diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go index 869c51c3a88..c758ff92c99 100644 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -189,6 +189,8 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request conditions.SetReadyCondition(metav1.ConditionTrue, kedav1alpha1.ScaledObjectConditionReadySucccesReason, msg) } + r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledObject) + if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, &conditions); err != nil { return ctrl.Result{}, err } @@ -540,3 +542,64 @@ func (r *ScaledObjectReconciler) updatePromMetricsOnDelete(namespacedName string delete(scaledObjectPromMetricsMap, namespacedName) } + +func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { + for _, trigger := range scaledObject.Spec.Triggers { + var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler + + if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { + triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} + } else { + triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} + } + + err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledObject.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) + + if err != nil { + if errors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication") + continue + } + + triggerAuthentication.SetScaledObjectNameStr(kedacontrollerutil.AppendIntoString(triggerAuthentication.GetScaledObjectNameStr(), scaledObject.GetName(), ",")) + err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) + if err != nil { + logger.Error(err, "Error updating TriggerAuthentication Status") + return "Update TriggerAuthentication Status Failed", err + } + } + return "Update TriggerAuthentication Status Successfully", nil +} + +func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { + + for _, trigger := range scaledObject.Spec.Triggers { + var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler + + if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { + triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} + } else { + triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} + } + + err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledObject.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) + + if err != nil { + if errors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication") + continue + } + + triggerAuthentication.SetScaledObjectNameStr(kedacontrollerutil.RemoveFromString(triggerAuthentication.GetScaledObjectNameStr(), scaledObject.GetName(), ",")) + err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) + if err != nil { + logger.Error(err, "Error updating TriggerAuthentication Status") + return "Update TriggerAuthentication Status Failed", err + } + } + return "Update TriggerAuthentication Status OnDelete Successfully", nil +} diff --git a/controllers/keda/scaledobject_finalizer.go b/controllers/keda/scaledobject_finalizer.go index 4c26caaa844..99c43fd8dc9 100644 --- a/controllers/keda/scaledobject_finalizer.go +++ b/controllers/keda/scaledobject_finalizer.go @@ -79,6 +79,7 @@ func (r *ScaledObjectReconciler) finalizeScaledObject(ctx context.Context, logge return err } + r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledObject) r.updatePromMetricsOnDelete(namespacedName) } diff --git a/controllers/keda/util/string_lists.go b/controllers/keda/util/string_lists.go index 90a26408ff9..2ee58cc055e 100644 --- a/controllers/keda/util/string_lists.go +++ b/controllers/keda/util/string_lists.go @@ -16,6 +16,10 @@ limitations under the License. package util +import ( + "strings" +) + // Contains checks if the passed string is present in the given slice of strings. func Contains(list []string, s string) bool { for _, v := range list { @@ -35,3 +39,33 @@ func Remove(list []string, s string) []string { } return list } + +// Append a new string into a string that has seprator +func AppendIntoString(srcStr string, appendStr string, sep string) string { + splitStrings := []string{} + if srcStr != "" { + splitStrings = strings.Split(srcStr, sep) + } + + if !Contains(splitStrings, appendStr) { + splitStrings = append(splitStrings, appendStr) + srcStr = strings.Join(splitStrings, sep) + } + return srcStr +} + +// Remove a string from src string that has seprator +func RemoveFromString(srcStr string, str string, sep string) string { + if srcStr == "" { + return srcStr + } + + splitStrings := []string{} + if srcStr != "" { + splitStrings = strings.Split(srcStr, sep) + } + + splitStrings = Remove(splitStrings, str) + srcStr = strings.Join(splitStrings, sep) + return srcStr +} From 9fbc4d2327338317c094aad05e63c969fb66fa0e Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Mon, 12 Jun 2023 10:37:24 +0800 Subject: [PATCH 02/25] Update CHANGELOG.md Co-authored-by: Tom Kerkhove Signed-off-by: SpiritZhou --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6479682a247..ada005f5cfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,7 +53,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio ### Improvements - **General**: Metrics Adapter: remove deprecated Prometheus Metrics and non-gRPC code ([#3930](https://github.com/kedacore/keda/issues/3930)) -- **General:**: Add ScaledObject/ScaledJob Names when kubectl get triggerauthentication/clustertriggerauthentication([#796](https://github.com/kedacore/keda/issues/796)) +- **General:**: Add ScaledObject/ScaledJob names to output of `kubectl get triggerauthentication/clustertriggerauthentication` ([#796](https://github.com/kedacore/keda/issues/796)) - **AWS DynamoDB:** Add support for `indexName` ([#4680](https://github.com/kedacore/keda/issues/4680)) - **Azure Data Explorer Scaler**: Use azidentity SDK ([#4489](https://github.com/kedacore/keda/issues/4489)) - **External Scaler**: Add tls options in TriggerAuth metadata. ([#3565](https://github.com/kedacore/keda/issues/3565)) From e7912d186b5db4fce0b85d7e118b1742b8f5e918 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Mon, 12 Jun 2023 10:40:22 +0800 Subject: [PATCH 03/25] Rename names Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- apis/keda/v1alpha1/triggerauthentication_types.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apis/keda/v1alpha1/triggerauthentication_types.go b/apis/keda/v1alpha1/triggerauthentication_types.go index c284805093e..5efe5e3a7bc 100644 --- a/apis/keda/v1alpha1/triggerauthentication_types.go +++ b/apis/keda/v1alpha1/triggerauthentication_types.go @@ -31,8 +31,8 @@ import ( // +kubebuilder:printcolumn:name="Secret",type="string",JSONPath=".spec.secretTargetRef[*].name" // +kubebuilder:printcolumn:name="Env",type="string",JSONPath=".spec.env[*].name" // +kubebuilder:printcolumn:name="VaultAddress",type="string",JSONPath=".spec.hashiCorpVault.address" -// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.allscaledobjectnamesstring" -// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.allscaledjobnamesstring" +// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.scaledobjects" +// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.scaledjobs" type ClusterTriggerAuthentication struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -60,8 +60,8 @@ type ClusterTriggerAuthenticationList struct { // +kubebuilder:printcolumn:name="Secret",type="string",JSONPath=".spec.secretTargetRef[*].name" // +kubebuilder:printcolumn:name="Env",type="string",JSONPath=".spec.env[*].name" // +kubebuilder:printcolumn:name="VaultAddress",type="string",JSONPath=".spec.hashiCorpVault.address" -// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.allscaledobjectnamesstring" -// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.allscaledjobnamesstring" +// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.scaledobjects" +// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.scaledjobs" type TriggerAuthentication struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -91,9 +91,9 @@ type TriggerAuthenticationSpec struct { // TriggerAuthenticationStatus defines the observed state of TriggerAuthentication type TriggerAuthenticationStatus struct { // +optional - ScaledObjectNamesStr string `json:"allscaledobjectnamesstring,omitempty"` + ScaledObjectNamesStr string `json:"scaledobjects,omitempty"` // +optional - ScaledJobNamesStr string `json:"allscaledjobnamesstring,omitempty"` + ScaledJobNamesStr string `json:"scaledjobs,omitempty"` } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object From 954cf93e1e6816f291c7cf44f52a4a628697e835 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Mon, 12 Jun 2023 18:09:29 +0800 Subject: [PATCH 04/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- .../v1alpha1/triggerauthentication_types.go | 41 ------------- apis/keda/v1alpha1/zz_generated.deepcopy.go | 17 ++++++ controllers/keda/scaledjob_controller.go | 56 +++++++++--------- controllers/keda/scaledobject_controller.go | 58 ++++++++++--------- .../keda/triggerauthentication_controller.go | 33 +++++++++++ pkg/util/status.go | 31 ++++++++++ 6 files changed, 140 insertions(+), 96 deletions(-) mode change 100644 => 100755 apis/keda/v1alpha1/triggerauthentication_types.go mode change 100644 => 100755 apis/keda/v1alpha1/zz_generated.deepcopy.go mode change 100644 => 100755 controllers/keda/scaledjob_controller.go mode change 100644 => 100755 controllers/keda/scaledobject_controller.go mode change 100644 => 100755 controllers/keda/triggerauthentication_controller.go mode change 100644 => 100755 pkg/util/status.go diff --git a/apis/keda/v1alpha1/triggerauthentication_types.go b/apis/keda/v1alpha1/triggerauthentication_types.go old mode 100644 new mode 100755 index 5efe5e3a7bc..ba84a732009 --- a/apis/keda/v1alpha1/triggerauthentication_types.go +++ b/apis/keda/v1alpha1/triggerauthentication_types.go @@ -247,44 +247,3 @@ func init() { SchemeBuilder.Register(&ClusterTriggerAuthentication{}, &ClusterTriggerAuthenticationList{}) SchemeBuilder.Register(&TriggerAuthentication{}, &TriggerAuthenticationList{}) } - -// interface that can handle TriggerAuthentication/ClusterTriggerAuthentication generically -type TriggerAuthenticationStatusHandler interface { - metav1.Object - GetScaledObjectNameStr() string - SetScaledObjectNameStr(str string) - GetScaledJobNameStr() string - SetScaledJobNameStr(str string) -} - -func (t *TriggerAuthentication) GetScaledObjectNameStr() string { - return t.Status.ScaledObjectNamesStr -} - -func (t *TriggerAuthentication) SetScaledObjectNameStr(str string) { - t.Status.ScaledObjectNamesStr = str -} - -func (t *TriggerAuthentication) GetScaledJobNameStr() string { - return t.Status.ScaledJobNamesStr -} - -func (t *TriggerAuthentication) SetScaledJobNameStr(str string) { - t.Status.ScaledJobNamesStr = str -} - -func (c *ClusterTriggerAuthentication) GetScaledObjectNameStr() string { - return c.Status.ScaledObjectNamesStr -} - -func (c *ClusterTriggerAuthentication) SetScaledObjectNameStr(str string) { - c.Status.ScaledObjectNamesStr = str -} - -func (c *ClusterTriggerAuthentication) GetScaledJobNameStr() string { - return c.Status.ScaledJobNamesStr -} - -func (c *ClusterTriggerAuthentication) SetScaledJobNameStr(str string) { - c.Status.ScaledJobNamesStr = str -} diff --git a/apis/keda/v1alpha1/zz_generated.deepcopy.go b/apis/keda/v1alpha1/zz_generated.deepcopy.go old mode 100644 new mode 100755 index 07f8596249d..8fa0c66ba67 --- a/apis/keda/v1alpha1/zz_generated.deepcopy.go +++ b/apis/keda/v1alpha1/zz_generated.deepcopy.go @@ -199,6 +199,7 @@ func (in *ClusterTriggerAuthentication) DeepCopyInto(out *ClusterTriggerAuthenti out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterTriggerAuthentication. @@ -827,6 +828,7 @@ func (in *TriggerAuthentication) DeepCopyInto(out *TriggerAuthentication) { out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TriggerAuthentication. @@ -919,6 +921,21 @@ func (in *TriggerAuthenticationSpec) DeepCopy() *TriggerAuthenticationSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TriggerAuthenticationStatus) DeepCopyInto(out *TriggerAuthenticationStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TriggerAuthenticationStatus. +func (in *TriggerAuthenticationStatus) DeepCopy() *TriggerAuthenticationStatus { + if in == nil { + return nil + } + out := new(TriggerAuthenticationStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ValueFromSecret) DeepCopyInto(out *ValueFromSecret) { *out = *in diff --git a/controllers/keda/scaledjob_controller.go b/controllers/keda/scaledjob_controller.go old mode 100644 new mode 100755 index a4d4cc6bb43..669546645d7 --- a/controllers/keda/scaledjob_controller.go +++ b/controllers/keda/scaledjob_controller.go @@ -320,16 +320,7 @@ func (r *ScaledJobReconciler) updatePromMetricsOnDelete(namespacedName string) { func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { for _, trigger := range scaledJob.Spec.Triggers { - var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler - - if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { - triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} - } else { - triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} - } - - err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledJob.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) - + triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledJob.GetNamespace()) if err != nil { if errors.IsNotFound(err) { logger.Info("TriggerAuthentication Not Found") @@ -338,11 +329,20 @@ func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Cont continue } - triggerAuthentication.SetScaledJobNameStr(kedacontrollerutil.AppendIntoString(triggerAuthentication.GetScaledJobNameStr(), scaledJob.GetName(), ",")) - err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) + triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) if err != nil { - logger.Error(err, "Error updating TriggerAuthentication Status") - return "Update TriggerAuthentication Status Failed", err + if errors.IsNotFound(err) { + logger.Info("TriggerAuthenticationStatus Not Found") + } + logger.Error(err, "Failed to get TriggerAuthenticationStatus") + continue + } + + triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() + triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") + + if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { + logger.Error(err, "Failed to update TriggerAuthenticationStatus") } } return "Update TriggerAuthentication Status Successfully", nil @@ -351,29 +351,31 @@ func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Cont func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { for _, trigger := range scaledJob.Spec.Triggers { - var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler + triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledJob.GetNamespace()) - if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { - triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} - } else { - triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} + if err != nil { + if errors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication1111"+err.Error()) + continue } - err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledJob.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) + triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) if err != nil { if errors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") + logger.Info("TriggerAuthenticationStatus Not Found") } - logger.Error(err, "Failed to get TriggerAuthentication") + logger.Error(err, "Failed to get TriggerAuthenticationStatus") continue } - triggerAuthentication.SetScaledJobNameStr(kedacontrollerutil.RemoveFromString(triggerAuthentication.GetScaledJobNameStr(), scaledJob.GetName(), ",")) - err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) - if err != nil { - logger.Error(err, "Error updating TriggerAuthentication Status") - return "Update TriggerAuthentication Status Failed", err + triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() + triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") + + if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { + logger.Error(err, "Failed to update TriggerAuthenticationStatus") } } return "Update TriggerAuthentication Status OnDelete Successfully", nil diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go old mode 100644 new mode 100755 index c758ff92c99..deb94dad3e2 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -545,16 +545,7 @@ func (r *ScaledObjectReconciler) updatePromMetricsOnDelete(namespacedName string func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { for _, trigger := range scaledObject.Spec.Triggers { - var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler - - if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { - triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} - } else { - triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} - } - - err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledObject.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) - + triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledObject.GetNamespace()) if err != nil { if errors.IsNotFound(err) { logger.Info("TriggerAuthentication Not Found") @@ -563,42 +554,53 @@ func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.C continue } - triggerAuthentication.SetScaledObjectNameStr(kedacontrollerutil.AppendIntoString(triggerAuthentication.GetScaledObjectNameStr(), scaledObject.GetName(), ",")) - err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) + triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) if err != nil { - logger.Error(err, "Error updating TriggerAuthentication Status") - return "Update TriggerAuthentication Status Failed", err + if errors.IsNotFound(err) { + logger.Info("TriggerAuthenticationStatus Not Found") + } + logger.Error(err, "Failed to get TriggerAuthenticationStatus") + continue + } + + triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() + triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") + + if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { + logger.Error(err, "Failed to update TriggerAuthenticationStatus") } } return "Update TriggerAuthentication Status Successfully", nil } -func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { +func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject kedav1alpha1.ScaledObject) (string, error) { for _, trigger := range scaledObject.Spec.Triggers { - var triggerAuthentication kedav1alpha1.TriggerAuthenticationStatusHandler + triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledObject.GetNamespace()) - if trigger.AuthenticationRef.Kind == "ClusterTriggerAuthentication" { - triggerAuthentication = &kedav1alpha1.ClusterTriggerAuthentication{} - } else { - triggerAuthentication = &kedav1alpha1.TriggerAuthentication{} + if err != nil { + if errors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication1111"+err.Error()) + continue } - err := r.Client.Get(ctx, client.ObjectKey{Namespace: scaledObject.Namespace, Name: trigger.AuthenticationRef.Name}, triggerAuthentication.(client.Object)) + triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) if err != nil { if errors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") + logger.Info("TriggerAuthenticationStatus Not Found") } - logger.Error(err, "Failed to get TriggerAuthentication") + logger.Error(err, "Failed to get TriggerAuthenticationStatus") continue } - triggerAuthentication.SetScaledObjectNameStr(kedacontrollerutil.RemoveFromString(triggerAuthentication.GetScaledObjectNameStr(), scaledObject.GetName(), ",")) - err = r.Client.Status().Update(ctx, triggerAuthentication.(client.Object)) - if err != nil { - logger.Error(err, "Error updating TriggerAuthentication Status") - return "Update TriggerAuthentication Status Failed", err + triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() + triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") + + if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { + logger.Error(err, "Failed to update TriggerAuthenticationStatus") } } return "Update TriggerAuthentication Status OnDelete Successfully", nil diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go old mode 100644 new mode 100755 index 26265f00232..260860bc078 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -18,10 +18,12 @@ package keda import ( "context" + "fmt" "sync" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -116,3 +118,34 @@ func (r *TriggerAuthenticationReconciler) UpdatePromMetricsOnDelete(namespacedNa delete(triggerAuthPromMetricsMap, namespacedName) } + +func GetTriggerAuth(ctx context.Context, client client.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (client.Object, error) { + + if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { + triggerAuth := &kedav1alpha1.TriggerAuthentication{} + err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth) + if err != nil { + return nil, err + } + return triggerAuth, nil + } else if triggerAuthRef.Kind == "ClusterTriggerAuthentication" { + clusterTriggerAuth := &kedav1alpha1.ClusterTriggerAuthentication{} + err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, clusterTriggerAuth) + if err != nil { + return nil, err + } + return clusterTriggerAuth, nil + } + return nil, fmt.Errorf("unknown trigger auth kind %s", triggerAuthRef.Kind) +} + +func GetTriggerAuthStatus(triggerAuth client.Object) (*kedav1alpha1.TriggerAuthenticationStatus, error) { + switch obj := triggerAuth.(type) { + case *kedav1alpha1.TriggerAuthentication: + return &obj.Status, nil + case *kedav1alpha1.ClusterTriggerAuthentication: + return &obj.Status, nil + default: + return nil, fmt.Errorf("unknown trigger auth status %s", obj) + } +} diff --git a/pkg/util/status.go b/pkg/util/status.go old mode 100644 new mode 100755 index 479ead8b083..9d9a23bff30 --- a/pkg/util/status.go +++ b/pkg/util/status.go @@ -62,6 +62,25 @@ func UpdateScaledObjectStatus(ctx context.Context, client runtimeclient.StatusCl return TransformObject(ctx, client, logger, scaledObject, status, transform) } +// UpdateTriggerAuthenticationStatus patches the given TriggerAuthentication/ClusterTriggerAuthentication with the updated status passed to it or returns an error. +func UpdateTriggerAuthenticationStatus(ctx context.Context, client runtimeclient.StatusClient, logger logr.Logger, object runtimeclient.Object, status *kedav1alpha1.TriggerAuthenticationStatus) error { + transform := func(runtimeObj runtimeclient.Object, target interface{}) error { + status, ok := target.(*kedav1alpha1.TriggerAuthenticationStatus) + if !ok { + return fmt.Errorf("transform target is not kedav1alpha1.TriggerAuthenticationStatus type %v", target) + } + switch obj := runtimeObj.(type) { + case *kedav1alpha1.TriggerAuthentication: + obj.Status = *status + case *kedav1alpha1.ClusterTriggerAuthentication: + obj.Status = *status + default: + } + return nil + } + return TransformObject(ctx, client, logger, object, status, transform) +} + // TransformObject patches the given object with the targeted passed to it through a transformer function or returns an error. func TransformObject(ctx context.Context, client runtimeclient.StatusClient, logger logr.Logger, object interface{}, target interface{}, transform func(runtimeclient.Object, interface{}) error) error { var patch runtimeclient.Patch @@ -80,6 +99,18 @@ func TransformObject(ctx context.Context, client runtimeclient.StatusClient, log logger.Error(err, "failed to patch ScaledJob") return err } + case *kedav1alpha1.TriggerAuthentication: + patch = runtimeclient.MergeFrom(obj.DeepCopy()) + if err := transform(obj, target); err != nil { + logger.Error(err, "failed to patch TriggerAuthentication") + return err + } + case *kedav1alpha1.ClusterTriggerAuthentication: + patch = runtimeclient.MergeFrom(obj.DeepCopy()) + if err := transform(obj, target); err != nil { + logger.Error(err, "failed to patch ClusterTriggerAuthentication") + return err + } default: err := fmt.Errorf("unknown scalable object type %v", obj) logger.Error(err, "failed to patch Objects") From 1eb6a1457de5ffce445a07e75ca00f991991e7e2 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Mon, 12 Jun 2023 18:11:45 +0800 Subject: [PATCH 05/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- controllers/keda/scaledobject_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go index deb94dad3e2..6788856b779 100755 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -573,7 +573,7 @@ func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.C return "Update TriggerAuthentication Status Successfully", nil } -func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject kedav1alpha1.ScaledObject) (string, error) { +func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { for _, trigger := range scaledObject.Spec.Triggers { triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledObject.GetNamespace()) From ffa601ebf2ed1d6a545d2581f1d6986ae1659462 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 11:17:16 +0800 Subject: [PATCH 06/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- controllers/keda/scaledjob_controller.go | 64 +++++++++---------- controllers/keda/scaledjob_finalizer.go | 4 +- controllers/keda/scaledobject_controller.go | 68 ++++++++++----------- controllers/keda/scaledobject_finalizer.go | 4 +- 4 files changed, 66 insertions(+), 74 deletions(-) diff --git a/controllers/keda/scaledjob_controller.go b/controllers/keda/scaledjob_controller.go index 669546645d7..2475f9c4b59 100755 --- a/controllers/keda/scaledjob_controller.go +++ b/controllers/keda/scaledjob_controller.go @@ -18,6 +18,7 @@ package keda import ( "context" + "errors" "fmt" "sync" "time" @@ -25,7 +26,7 @@ import ( "github.com/go-logr/logr" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" corev1listers "k8s.io/client-go/listers/core/v1" @@ -97,7 +98,7 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( scaledJob := &kedav1alpha1.ScaledJob{} err := r.Client.Get(ctx, req.NamespacedName, scaledJob) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue @@ -157,7 +158,9 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return ctrl.Result{}, err } - r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledJob) + if _, err := r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledJob); err != nil { + reqLogger.Error(err, "Error updating TriggerAuthentication Status") + } return ctrl.Result{}, err } @@ -319,64 +322,55 @@ func (r *ScaledJobReconciler) updatePromMetricsOnDelete(namespacedName string) { } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { - for _, trigger := range scaledJob.Spec.Triggers { - triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledJob.GetNamespace()) - if err != nil { - if errors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") - } - logger.Error(err, "Failed to get TriggerAuthentication") - continue - } - - triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) - if err != nil { - if errors.IsNotFound(err) { - logger.Info("TriggerAuthenticationStatus Not Found") - } - logger.Error(err, "Failed to get TriggerAuthenticationStatus") - continue - } - - triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() + return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledJob, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") - - if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { - logger.Error(err, "Failed to update TriggerAuthenticationStatus") - } - } - return "Update TriggerAuthentication Status Successfully", nil + return triggerAuthenticationStatus + }) } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { + return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledJob, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") + return triggerAuthenticationStatus + }) +} + +func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { + var errs error for _, trigger := range scaledJob.Spec.Triggers { triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledJob.GetNamespace()) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { logger.Info("TriggerAuthentication Not Found") } - logger.Error(err, "Failed to get TriggerAuthentication1111"+err.Error()) + logger.Error(err, "Failed to get TriggerAuthentication") + errs = errors.Join(errs, err) continue } triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { logger.Info("TriggerAuthenticationStatus Not Found") } logger.Error(err, "Failed to get TriggerAuthenticationStatus") + errs = errors.Join(errs, err) continue } - triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() - triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") + triggerAuthenticationStatus = statusHandler(triggerAuthenticationStatus.DeepCopy()) if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { logger.Error(err, "Failed to update TriggerAuthenticationStatus") + errs = errors.Join(errs, err) } } - return "Update TriggerAuthentication Status OnDelete Successfully", nil + + if errs != nil { + return "Update TriggerAuthentication Status Failed", errs + } + return "Update TriggerAuthentication Status Successfully", nil } diff --git a/controllers/keda/scaledjob_finalizer.go b/controllers/keda/scaledjob_finalizer.go index 9f25067057b..99636eca2e9 100644 --- a/controllers/keda/scaledjob_finalizer.go +++ b/controllers/keda/scaledjob_finalizer.go @@ -50,7 +50,9 @@ func (r *ScaledJobReconciler) finalizeScaledJob(ctx context.Context, logger logr return err } - r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledJob) + if _, err := r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledJob); err != nil { + logger.Error(err, "Failed to update TriggerAuthentication Status after removing a finalizer") + } r.updatePromMetricsOnDelete(namespacedName) } diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go index 6788856b779..0c9cac594f8 100755 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -18,6 +18,7 @@ package keda import ( "context" + "errors" "fmt" "sync" @@ -25,7 +26,7 @@ import ( autoscalingv1 "k8s.io/api/autoscaling/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -139,7 +140,7 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request scaledObject := &kedav1alpha1.ScaledObject{} err := r.Client.Get(ctx, req.NamespacedName, scaledObject) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue @@ -189,12 +190,14 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request conditions.SetReadyCondition(metav1.ConditionTrue, kedav1alpha1.ScaledObjectConditionReadySucccesReason, msg) } - r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledObject) - if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, &conditions); err != nil { return ctrl.Result{}, err } + if _, err := r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledObject); err != nil { + reqLogger.Error(err, "Failed to update TriggerAuthentication Status after removing a finalizer") + } + return ctrl.Result{}, err } @@ -406,7 +409,7 @@ func (r *ScaledObjectReconciler) ensureHPAForScaledObjectExists(ctx context.Cont foundHpa := &autoscalingv2.HorizontalPodAutoscaler{} // Check if HPA for this ScaledObject already exists err := r.Client.Get(ctx, types.NamespacedName{Name: hpaName, Namespace: scaledObject.Namespace}, foundHpa) - if err != nil && errors.IsNotFound(err) { + if err != nil && k8sErrors.IsNotFound(err) { // HPA wasn't found -> let's create a new one err = r.createAndDeployNewHPA(ctx, logger, scaledObject, gvkr) if err != nil { @@ -544,64 +547,55 @@ func (r *ScaledObjectReconciler) updatePromMetricsOnDelete(namespacedName string } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { - for _, trigger := range scaledObject.Spec.Triggers { - triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledObject.GetNamespace()) - if err != nil { - if errors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") - } - logger.Error(err, "Failed to get TriggerAuthentication") - continue - } - - triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) - if err != nil { - if errors.IsNotFound(err) { - logger.Info("TriggerAuthenticationStatus Not Found") - } - logger.Error(err, "Failed to get TriggerAuthenticationStatus") - continue - } - - triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() + return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledObject, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") - - if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { - logger.Error(err, "Failed to update TriggerAuthenticationStatus") - } - } - return "Update TriggerAuthentication Status Successfully", nil + return triggerAuthenticationStatus + }) } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { + return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledObject, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") + return triggerAuthenticationStatus + }) +} + +func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { + var errs error for _, trigger := range scaledObject.Spec.Triggers { triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledObject.GetNamespace()) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { logger.Info("TriggerAuthentication Not Found") } - logger.Error(err, "Failed to get TriggerAuthentication1111"+err.Error()) + logger.Error(err, "Failed to get TriggerAuthentication") + errs = errors.Join(errs, err) continue } triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { logger.Info("TriggerAuthenticationStatus Not Found") } logger.Error(err, "Failed to get TriggerAuthenticationStatus") + errs = errors.Join(errs, err) continue } - triggerAuthenticationStatus = triggerAuthenticationStatus.DeepCopy() - triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") + triggerAuthenticationStatus = statusHandler(triggerAuthenticationStatus.DeepCopy()) if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { logger.Error(err, "Failed to update TriggerAuthenticationStatus") + errs = errors.Join(errs, err) } } - return "Update TriggerAuthentication Status OnDelete Successfully", nil + + if errs != nil { + return "Updated TriggerAuthentication Status Failed", errs + } + return "Updated TriggerAuthentication Status Successfully", nil } diff --git a/controllers/keda/scaledobject_finalizer.go b/controllers/keda/scaledobject_finalizer.go index 99c43fd8dc9..b3d48adbd77 100644 --- a/controllers/keda/scaledobject_finalizer.go +++ b/controllers/keda/scaledobject_finalizer.go @@ -79,7 +79,9 @@ func (r *ScaledObjectReconciler) finalizeScaledObject(ctx context.Context, logge return err } - r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledObject) + if _, err := r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledObject); err != nil { + logger.Error(err, "Failed to update TriggerAuthentication Status after removing a finalizer") + } r.updatePromMetricsOnDelete(namespacedName) } From 0229907283fc6a47fc612fdb17a54881f53e0a38 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 11:19:31 +0800 Subject: [PATCH 07/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- controllers/keda/util/string_lists.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/keda/util/string_lists.go b/controllers/keda/util/string_lists.go index 2ee58cc055e..56d92ad7dbf 100644 --- a/controllers/keda/util/string_lists.go +++ b/controllers/keda/util/string_lists.go @@ -40,7 +40,7 @@ func Remove(list []string, s string) []string { return list } -// Append a new string into a string that has seprator +// AppendIntoString append a new string into a string that has seprator func AppendIntoString(srcStr string, appendStr string, sep string) string { splitStrings := []string{} if srcStr != "" { @@ -54,7 +54,7 @@ func AppendIntoString(srcStr string, appendStr string, sep string) string { return srcStr } -// Remove a string from src string that has seprator +// RemoveFromString remove a string from src string that has seprator func RemoveFromString(srcStr string, str string, sep string) string { if srcStr == "" { return srcStr From 13cede6226a560cb4e27909d1fb6becf36ab2a44 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 13:16:53 +0800 Subject: [PATCH 08/25] Fix Checking Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- controllers/keda/scaledjob_controller.go | 1 - controllers/keda/scaledobject_controller.go | 1 - controllers/keda/triggerauthentication_controller.go | 1 - 3 files changed, 3 deletions(-) diff --git a/controllers/keda/scaledjob_controller.go b/controllers/keda/scaledjob_controller.go index 2475f9c4b59..b9b42812505 100755 --- a/controllers/keda/scaledjob_controller.go +++ b/controllers/keda/scaledjob_controller.go @@ -329,7 +329,6 @@ func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Cont } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { - return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledJob, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") return triggerAuthenticationStatus diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go index 0c9cac594f8..3a7e86a231a 100755 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -554,7 +554,6 @@ func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.C } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { - return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledObject, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") return triggerAuthenticationStatus diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 260860bc078..8d1b4a68956 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -120,7 +120,6 @@ func (r *TriggerAuthenticationReconciler) UpdatePromMetricsOnDelete(namespacedNa } func GetTriggerAuth(ctx context.Context, client client.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (client.Object, error) { - if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { triggerAuth := &kedav1alpha1.TriggerAuthentication{} err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth) From d2fd93be96fb3b27672612561595727b8f7e9d0c Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 14:00:26 +0800 Subject: [PATCH 09/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- controllers/keda/scaledjob_controller.go | 48 ++------------ controllers/keda/scaledobject_controller.go | 64 ++++--------------- .../keda/triggerauthentication_controller.go | 46 ++++++++++++- 3 files changed, 61 insertions(+), 97 deletions(-) diff --git a/controllers/keda/scaledjob_controller.go b/controllers/keda/scaledjob_controller.go index b9b42812505..8999889d6e0 100755 --- a/controllers/keda/scaledjob_controller.go +++ b/controllers/keda/scaledjob_controller.go @@ -18,7 +18,6 @@ package keda import ( "context" - "errors" "fmt" "sync" "time" @@ -26,7 +25,7 @@ import ( "github.com/go-logr/logr" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" - k8sErrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" corev1listers "k8s.io/client-go/listers/core/v1" @@ -98,7 +97,7 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( scaledJob := &kedav1alpha1.ScaledJob{} err := r.Client.Get(ctx, req.NamespacedName, scaledJob) if err != nil { - if k8sErrors.IsNotFound(err) { + if errors.IsNotFound(err) { // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue @@ -322,54 +321,15 @@ func (r *ScaledJobReconciler) updatePromMetricsOnDelete(namespacedName string) { } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { - return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledJob, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledJob.GetNamespace(), scaledJob.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") return triggerAuthenticationStatus }) } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { - return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledJob, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledJob.GetNamespace(), scaledJob.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") return triggerAuthenticationStatus }) } - -func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { - var errs error - for _, trigger := range scaledJob.Spec.Triggers { - triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledJob.GetNamespace()) - - if err != nil { - if k8sErrors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") - } - logger.Error(err, "Failed to get TriggerAuthentication") - errs = errors.Join(errs, err) - continue - } - - triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) - - if err != nil { - if k8sErrors.IsNotFound(err) { - logger.Info("TriggerAuthenticationStatus Not Found") - } - logger.Error(err, "Failed to get TriggerAuthenticationStatus") - errs = errors.Join(errs, err) - continue - } - - triggerAuthenticationStatus = statusHandler(triggerAuthenticationStatus.DeepCopy()) - - if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { - logger.Error(err, "Failed to update TriggerAuthenticationStatus") - errs = errors.Join(errs, err) - } - } - - if errs != nil { - return "Update TriggerAuthentication Status Failed", errs - } - return "Update TriggerAuthentication Status Successfully", nil -} diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go index 3a7e86a231a..8cfebffa848 100755 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -18,7 +18,6 @@ package keda import ( "context" - "errors" "fmt" "sync" @@ -26,7 +25,7 @@ import ( autoscalingv1 "k8s.io/api/autoscaling/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" - k8sErrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -140,7 +139,7 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request scaledObject := &kedav1alpha1.ScaledObject{} err := r.Client.Get(ctx, req.NamespacedName, scaledObject) if err != nil { - if k8sErrors.IsNotFound(err) { + if errors.IsNotFound(err) { // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue @@ -409,7 +408,7 @@ func (r *ScaledObjectReconciler) ensureHPAForScaledObjectExists(ctx context.Cont foundHpa := &autoscalingv2.HorizontalPodAutoscaler{} // Check if HPA for this ScaledObject already exists err := r.Client.Get(ctx, types.NamespacedName{Name: hpaName, Namespace: scaledObject.Namespace}, foundHpa) - if err != nil && k8sErrors.IsNotFound(err) { + if err != nil && errors.IsNotFound(err) { // HPA wasn't found -> let's create a new one err = r.createAndDeployNewHPA(ctx, logger, scaledObject, gvkr) if err != nil { @@ -547,54 +546,17 @@ func (r *ScaledObjectReconciler) updatePromMetricsOnDelete(namespacedName string } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { - return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledObject, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { - triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") - return triggerAuthenticationStatus - }) + return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledObject.GetNamespace(), scaledObject.Spec.Triggers, + func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") + return triggerAuthenticationStatus + }) } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { - return r.updateTriggerAuthenticationStatusHandler(ctx, logger, scaledObject, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { - triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") - return triggerAuthenticationStatus - }) -} - -func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { - var errs error - for _, trigger := range scaledObject.Spec.Triggers { - triggerAuth, err := GetTriggerAuth(ctx, r.Client, trigger.AuthenticationRef, scaledObject.GetNamespace()) - - if err != nil { - if k8sErrors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") - } - logger.Error(err, "Failed to get TriggerAuthentication") - errs = errors.Join(errs, err) - continue - } - - triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) - - if err != nil { - if k8sErrors.IsNotFound(err) { - logger.Info("TriggerAuthenticationStatus Not Found") - } - logger.Error(err, "Failed to get TriggerAuthenticationStatus") - errs = errors.Join(errs, err) - continue - } - - triggerAuthenticationStatus = statusHandler(triggerAuthenticationStatus.DeepCopy()) - - if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, r.Client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { - logger.Error(err, "Failed to update TriggerAuthenticationStatus") - errs = errors.Join(errs, err) - } - } - - if errs != nil { - return "Updated TriggerAuthentication Status Failed", errs - } - return "Updated TriggerAuthentication Status Successfully", nil + return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledObject.GetNamespace(), scaledObject.Spec.Triggers, + func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") + return triggerAuthenticationStatus + }) } diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 8d1b4a68956..1c03c1819fe 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -18,11 +18,12 @@ package keda import ( "context" + "errors" "fmt" "sync" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" @@ -31,9 +32,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" + "github.com/go-logr/logr" kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" "github.com/kedacore/keda/v2/pkg/eventreason" "github.com/kedacore/keda/v2/pkg/prommetrics" + kedautil "github.com/kedacore/keda/v2/pkg/util" ) // TriggerAuthenticationReconciler reconciles a TriggerAuthentication object @@ -65,7 +68,7 @@ func (r *TriggerAuthenticationReconciler) Reconcile(ctx context.Context, req ctr triggerAuthentication := &kedav1alpha1.TriggerAuthentication{} err := r.Client.Get(ctx, req.NamespacedName, triggerAuthentication) if err != nil { - if errors.IsNotFound(err) { + if k8sErrors.IsNotFound(err) { return ctrl.Result{}, nil } reqLogger.Error(err, "Failed to get TriggerAuthentication") @@ -148,3 +151,42 @@ func GetTriggerAuthStatus(triggerAuth client.Object) (*kedav1alpha1.TriggerAuthe return nil, fmt.Errorf("unknown trigger auth status %s", obj) } } + +func UpdateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, client client.Client, namespace string, scaleTriggers []kedav1alpha1.ScaleTriggers, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { + var errs error + for _, trigger := range scaleTriggers { + triggerAuth, err := GetTriggerAuth(ctx, client, trigger.AuthenticationRef, namespace) + + if err != nil { + if k8sErrors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication") + errs = errors.Join(errs, err) + continue + } + + triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) + + if err != nil { + if k8sErrors.IsNotFound(err) { + logger.Info("TriggerAuthenticationStatus Not Found") + } + logger.Error(err, "Failed to get TriggerAuthenticationStatus") + errs = errors.Join(errs, err) + continue + } + + triggerAuthenticationStatus = statusHandler(triggerAuthenticationStatus.DeepCopy()) + + if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { + logger.Error(err, "Failed to update TriggerAuthenticationStatus") + errs = errors.Join(errs, err) + } + } + + if errs != nil { + return "Update TriggerAuthentication Status Failed", errs + } + return "Update TriggerAuthentication Status Successfully", nil +} From 3e2583415252536f2bb7107dfd6fbc4f36432539 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 16:13:46 +0800 Subject: [PATCH 10/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- .../triggerauthentication_update_test.go | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 tests/internals/triggerauthentication_update/triggerauthentication_update_test.go diff --git a/tests/internals/triggerauthentication_update/triggerauthentication_update_test.go b/tests/internals/triggerauthentication_update/triggerauthentication_update_test.go new file mode 100644 index 00000000000..cd3122cc484 --- /dev/null +++ b/tests/internals/triggerauthentication_update/triggerauthentication_update_test.go @@ -0,0 +1,190 @@ +//go:build e2e +// +build e2e + +package triggerauthentication_update_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/client-go/kubernetes" + + . "github.com/kedacore/keda/v2/tests/helper" +) + +const ( + testName = "triggerauthentication-update-test" +) + +var ( + namespace = fmt.Sprintf("%s-ns", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + scaledObjectName = fmt.Sprintf("%s-so", testName) + secretName = fmt.Sprintf("%s-secret", testName) + triggerAuthName = fmt.Sprintf("%s-ta", testName) + metricsServerDeploymentName = fmt.Sprintf("%s-metrics-server", testName) + metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, namespace) + minReplicas = 0 + midReplicas = 3 + maxReplicas = 5 +) + +type templateData struct { + TestNamespace string + DeploymentName string + ScaledObject string + TriggerAuthName string + SecretName string + MinReplicas string + MaxReplicas string +} + +const ( + secretTemplate = ` +apiVersion: v1 +kind: Secret +metadata: + name: {{.SecretName}} + namespace: {{.TestNamespace}} +data: + AUTH_PASSWORD: U0VDUkVUCg== + AUTH_USERNAME: VVNFUgo= +` + triggerAuthenticationTemplate = ` +apiVersion: keda.sh/v1alpha1 +kind: TriggerAuthentication +metadata: + name: {{.TriggerAuthName}} + namespace: {{.TestNamespace}} +spec: + secretTargetRef: + - parameter: username + name: {{.SecretName}} + key: AUTH_USERNAME + - parameter: password + name: {{.SecretName}} + key: AUTH_PASSWORD +` + deploymentTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + deploy: {{.DeploymentName}} + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} +spec: + selector: + matchLabels: + app: {{.DeploymentName}} + replicas: {{.MinReplicas}} + template: + metadata: + labels: + app: {{.DeploymentName}} + spec: + containers: + - name: nginx + image: nginxinc/nginx-unprivileged + ports: + - containerPort: 80 + resources: + requests: + cpu: "200m" + limits: + cpu: "500m" +` + + scaledObjectTriggerTemplate = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: {{.ScaledObject}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + advanced: + horizontalPodAutoscalerConfig: + behavior: + scaleDown: + stabilizationWindowSeconds: 10 + pollingInterval: 10 + scaleTargetRef: + name: {{.DeploymentName}} + minReplicaCount: {{.MinReplicas}} + maxReplicaCount: {{.MaxReplicas}} + cooldownPeriod: 1 + triggers: + - type: metrics-api + metadata: + targetValue: "2" + url: "{{.MetricsServerEndpoint}}" + valueLocation: 'value' + method: "query" + authenticationRef: + name: {{.TriggerAuthName}} +` + +updateMetricTemplate = ` +apiVersion: batch/v1 +kind: Job +metadata: + name: update-ms-value + namespace: {{.TestNamespace}} +spec: + ttlSecondsAfterFinished: 0 + template: + spec: + containers: + - name: job-curl + image: curlimages/curl + imagePullPolicy: Always + command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"] + restartPolicy: Never +` +) + +func TestTriggerAuthenticationStatus(t *testing.T) { + // setup + t.Log("--- setting up ---") + // Create kubernetes resources + kc := GetKubernetesClient(t) + data, templates := getTemplateData() + CreateKubernetesResources(t, kc, namespace, data, templates) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, namespace, minReplicas, 180, 3), + "replica count should be %d after 3 minutes", minReplicas) + + testStatusValue(t, kc, data) // one trigger target changes + + DeleteKubernetesResources(t, namespace, data, templates) +} + +func testStatusValue(t *testing.T, kc *kubernetes.Clientset, data templateData) { + kctlGetCmd := fmt.Sprintf(`kubectl get ta/%s"`, triggerAuthName) + output, err := ExecuteCommand(kctlGetCmd) + if err != nil { + t.Logf("erroer") + } + t.Logf("output..." + string(output)) +} + +// help function to load template data +func getTemplateData() (templateData, []Template) { + return templateData{ + TestNamespace: namespace, + DeploymentName: deploymentName, + TriggerAuthName: triggerAuthName, + ScaledObject: scaledObjectName, + SecretName: secretName, + MinReplicas: fmt.Sprintf("%v", minReplicas), + MaxReplicas: fmt.Sprintf("%v", maxReplicas), + }, []Template{ + {Name: "deploymentTemplate", Config: deploymentTemplate}, + {Name: "secretTemplate", Config: secretTemplate}, + {Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplate}, + {Name: "scaledObjectTriggerTemplate", Config: scaledObjectTriggerTemplate}, + } +} From 4d14c1c186eb2ea0f460d3585a9271cb37de3576 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 16:18:27 +0800 Subject: [PATCH 11/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- .../keda/triggerauthentication_controller.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 1c03c1819fe..84ebbe749c9 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -18,7 +18,6 @@ package keda import ( "context" - "errors" "fmt" "sync" @@ -37,6 +36,7 @@ import ( "github.com/kedacore/keda/v2/pkg/eventreason" "github.com/kedacore/keda/v2/pkg/prommetrics" kedautil "github.com/kedacore/keda/v2/pkg/util" + "github.com/pkg/errors" ) // TriggerAuthenticationReconciler reconciles a TriggerAuthentication object @@ -123,7 +123,9 @@ func (r *TriggerAuthenticationReconciler) UpdatePromMetricsOnDelete(namespacedNa } func GetTriggerAuth(ctx context.Context, client client.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (client.Object, error) { - if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { + if triggerAuthRef == nil { + return nil, fmt.Errorf("triggerAuthRef is nil") + } else if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { triggerAuth := &kedav1alpha1.TriggerAuthentication{} err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth) if err != nil { @@ -155,6 +157,10 @@ func GetTriggerAuthStatus(triggerAuth client.Object) (*kedav1alpha1.TriggerAuthe func UpdateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, client client.Client, namespace string, scaleTriggers []kedav1alpha1.ScaleTriggers, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { var errs error for _, trigger := range scaleTriggers { + if trigger.AuthenticationRef == nil { + continue + } + triggerAuth, err := GetTriggerAuth(ctx, client, trigger.AuthenticationRef, namespace) if err != nil { @@ -162,7 +168,7 @@ func UpdateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.L logger.Info("TriggerAuthentication Not Found") } logger.Error(err, "Failed to get TriggerAuthentication") - errs = errors.Join(errs, err) + errs = errors.Wrap(errs, err.Error()) continue } @@ -173,7 +179,7 @@ func UpdateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.L logger.Info("TriggerAuthenticationStatus Not Found") } logger.Error(err, "Failed to get TriggerAuthenticationStatus") - errs = errors.Join(errs, err) + errs = errors.Wrap(errs, err.Error()) continue } @@ -181,7 +187,7 @@ func UpdateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.L if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { logger.Error(err, "Failed to update TriggerAuthenticationStatus") - errs = errors.Join(errs, err) + errs = errors.Wrap(errs, err.Error()) } } From b1d6fa6d005383da686a4b350962bdba9e82c16f Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 13 Jun 2023 17:18:56 +0800 Subject: [PATCH 12/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- .../keda/triggerauthentication_controller.go | 17 +- .../triggerauthentication_update_test.go | 190 ------------------ 2 files changed, 9 insertions(+), 198 deletions(-) delete mode 100644 tests/internals/triggerauthentication_update/triggerauthentication_update_test.go diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 84ebbe749c9..3eefdfbabcb 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -21,6 +21,12 @@ import ( "fmt" "sync" + "github.com/go-logr/logr" + kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" + "github.com/kedacore/keda/v2/pkg/eventreason" + "github.com/kedacore/keda/v2/pkg/prommetrics" + kedautil "github.com/kedacore/keda/v2/pkg/util" + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" @@ -30,13 +36,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" - - "github.com/go-logr/logr" - kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" - "github.com/kedacore/keda/v2/pkg/eventreason" - "github.com/kedacore/keda/v2/pkg/prommetrics" - kedautil "github.com/kedacore/keda/v2/pkg/util" - "github.com/pkg/errors" ) // TriggerAuthenticationReconciler reconciles a TriggerAuthentication object @@ -125,7 +124,9 @@ func (r *TriggerAuthenticationReconciler) UpdatePromMetricsOnDelete(namespacedNa func GetTriggerAuth(ctx context.Context, client client.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (client.Object, error) { if triggerAuthRef == nil { return nil, fmt.Errorf("triggerAuthRef is nil") - } else if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { + } + + if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { triggerAuth := &kedav1alpha1.TriggerAuthentication{} err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth) if err != nil { diff --git a/tests/internals/triggerauthentication_update/triggerauthentication_update_test.go b/tests/internals/triggerauthentication_update/triggerauthentication_update_test.go deleted file mode 100644 index cd3122cc484..00000000000 --- a/tests/internals/triggerauthentication_update/triggerauthentication_update_test.go +++ /dev/null @@ -1,190 +0,0 @@ -//go:build e2e -// +build e2e - -package triggerauthentication_update_test - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" - "k8s.io/client-go/kubernetes" - - . "github.com/kedacore/keda/v2/tests/helper" -) - -const ( - testName = "triggerauthentication-update-test" -) - -var ( - namespace = fmt.Sprintf("%s-ns", testName) - deploymentName = fmt.Sprintf("%s-deployment", testName) - scaledObjectName = fmt.Sprintf("%s-so", testName) - secretName = fmt.Sprintf("%s-secret", testName) - triggerAuthName = fmt.Sprintf("%s-ta", testName) - metricsServerDeploymentName = fmt.Sprintf("%s-metrics-server", testName) - metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, namespace) - minReplicas = 0 - midReplicas = 3 - maxReplicas = 5 -) - -type templateData struct { - TestNamespace string - DeploymentName string - ScaledObject string - TriggerAuthName string - SecretName string - MinReplicas string - MaxReplicas string -} - -const ( - secretTemplate = ` -apiVersion: v1 -kind: Secret -metadata: - name: {{.SecretName}} - namespace: {{.TestNamespace}} -data: - AUTH_PASSWORD: U0VDUkVUCg== - AUTH_USERNAME: VVNFUgo= -` - triggerAuthenticationTemplate = ` -apiVersion: keda.sh/v1alpha1 -kind: TriggerAuthentication -metadata: - name: {{.TriggerAuthName}} - namespace: {{.TestNamespace}} -spec: - secretTargetRef: - - parameter: username - name: {{.SecretName}} - key: AUTH_USERNAME - - parameter: password - name: {{.SecretName}} - key: AUTH_PASSWORD -` - deploymentTemplate = ` -apiVersion: apps/v1 -kind: Deployment -metadata: - labels: - deploy: {{.DeploymentName}} - name: {{.DeploymentName}} - namespace: {{.TestNamespace}} -spec: - selector: - matchLabels: - app: {{.DeploymentName}} - replicas: {{.MinReplicas}} - template: - metadata: - labels: - app: {{.DeploymentName}} - spec: - containers: - - name: nginx - image: nginxinc/nginx-unprivileged - ports: - - containerPort: 80 - resources: - requests: - cpu: "200m" - limits: - cpu: "500m" -` - - scaledObjectTriggerTemplate = ` -apiVersion: keda.sh/v1alpha1 -kind: ScaledObject -metadata: - name: {{.ScaledObject}} - namespace: {{.TestNamespace}} - labels: - app: {{.DeploymentName}} -spec: - advanced: - horizontalPodAutoscalerConfig: - behavior: - scaleDown: - stabilizationWindowSeconds: 10 - pollingInterval: 10 - scaleTargetRef: - name: {{.DeploymentName}} - minReplicaCount: {{.MinReplicas}} - maxReplicaCount: {{.MaxReplicas}} - cooldownPeriod: 1 - triggers: - - type: metrics-api - metadata: - targetValue: "2" - url: "{{.MetricsServerEndpoint}}" - valueLocation: 'value' - method: "query" - authenticationRef: - name: {{.TriggerAuthName}} -` - -updateMetricTemplate = ` -apiVersion: batch/v1 -kind: Job -metadata: - name: update-ms-value - namespace: {{.TestNamespace}} -spec: - ttlSecondsAfterFinished: 0 - template: - spec: - containers: - - name: job-curl - image: curlimages/curl - imagePullPolicy: Always - command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"] - restartPolicy: Never -` -) - -func TestTriggerAuthenticationStatus(t *testing.T) { - // setup - t.Log("--- setting up ---") - // Create kubernetes resources - kc := GetKubernetesClient(t) - data, templates := getTemplateData() - CreateKubernetesResources(t, kc, namespace, data, templates) - - assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, namespace, minReplicas, 180, 3), - "replica count should be %d after 3 minutes", minReplicas) - - testStatusValue(t, kc, data) // one trigger target changes - - DeleteKubernetesResources(t, namespace, data, templates) -} - -func testStatusValue(t *testing.T, kc *kubernetes.Clientset, data templateData) { - kctlGetCmd := fmt.Sprintf(`kubectl get ta/%s"`, triggerAuthName) - output, err := ExecuteCommand(kctlGetCmd) - if err != nil { - t.Logf("erroer") - } - t.Logf("output..." + string(output)) -} - -// help function to load template data -func getTemplateData() (templateData, []Template) { - return templateData{ - TestNamespace: namespace, - DeploymentName: deploymentName, - TriggerAuthName: triggerAuthName, - ScaledObject: scaledObjectName, - SecretName: secretName, - MinReplicas: fmt.Sprintf("%v", minReplicas), - MaxReplicas: fmt.Sprintf("%v", maxReplicas), - }, []Template{ - {Name: "deploymentTemplate", Config: deploymentTemplate}, - {Name: "secretTemplate", Config: secretTemplate}, - {Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplate}, - {Name: "scaledObjectTriggerTemplate", Config: scaledObjectTriggerTemplate}, - } -} From 644d73086c897add404cd96c65671bfcbd1bc08e Mon Sep 17 00:00:00 2001 From: SpirtZhou Date: Tue, 13 Jun 2023 20:01:31 +0800 Subject: [PATCH 13/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- .../v1alpha1/clustertriggerauthentication.go | 16 ++++++++++++++++ .../fake/fake_clustertriggerauthentication.go | 11 +++++++++++ .../v1alpha1/fake/fake_triggerauthentication.go | 12 ++++++++++++ .../keda/v1alpha1/triggerauthentication.go | 17 +++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/clustertriggerauthentication.go b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/clustertriggerauthentication.go index b59f2955d9a..3773d69e357 100644 --- a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/clustertriggerauthentication.go +++ b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/clustertriggerauthentication.go @@ -40,6 +40,7 @@ type ClusterTriggerAuthenticationsGetter interface { type ClusterTriggerAuthenticationInterface interface { Create(ctx context.Context, clusterTriggerAuthentication *v1alpha1.ClusterTriggerAuthentication, opts v1.CreateOptions) (*v1alpha1.ClusterTriggerAuthentication, error) Update(ctx context.Context, clusterTriggerAuthentication *v1alpha1.ClusterTriggerAuthentication, opts v1.UpdateOptions) (*v1alpha1.ClusterTriggerAuthentication, error) + UpdateStatus(ctx context.Context, clusterTriggerAuthentication *v1alpha1.ClusterTriggerAuthentication, opts v1.UpdateOptions) (*v1alpha1.ClusterTriggerAuthentication, error) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error Get(ctx context.Context, name string, opts v1.GetOptions) (*v1alpha1.ClusterTriggerAuthentication, error) @@ -128,6 +129,21 @@ func (c *clusterTriggerAuthentications) Update(ctx context.Context, clusterTrigg return } +// UpdateStatus was generated because the type contains a Status member. +// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). +func (c *clusterTriggerAuthentications) UpdateStatus(ctx context.Context, clusterTriggerAuthentication *v1alpha1.ClusterTriggerAuthentication, opts v1.UpdateOptions) (result *v1alpha1.ClusterTriggerAuthentication, err error) { + result = &v1alpha1.ClusterTriggerAuthentication{} + err = c.client.Put(). + Resource("clustertriggerauthentications"). + Name(clusterTriggerAuthentication.Name). + SubResource("status"). + VersionedParams(&opts, scheme.ParameterCodec). + Body(clusterTriggerAuthentication). + Do(ctx). + Into(result) + return +} + // Delete takes name of the clusterTriggerAuthentication and deletes it. Returns an error if one occurs. func (c *clusterTriggerAuthentications) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { return c.client.Delete(). diff --git a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_clustertriggerauthentication.go b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_clustertriggerauthentication.go index 64a12709ee0..7e1d056d622 100644 --- a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_clustertriggerauthentication.go +++ b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_clustertriggerauthentication.go @@ -95,6 +95,17 @@ func (c *FakeClusterTriggerAuthentications) Update(ctx context.Context, clusterT return obj.(*v1alpha1.ClusterTriggerAuthentication), err } +// UpdateStatus was generated because the type contains a Status member. +// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). +func (c *FakeClusterTriggerAuthentications) UpdateStatus(ctx context.Context, clusterTriggerAuthentication *v1alpha1.ClusterTriggerAuthentication, opts v1.UpdateOptions) (*v1alpha1.ClusterTriggerAuthentication, error) { + obj, err := c.Fake. + Invokes(testing.NewRootUpdateSubresourceAction(clustertriggerauthenticationsResource, "status", clusterTriggerAuthentication), &v1alpha1.ClusterTriggerAuthentication{}) + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.ClusterTriggerAuthentication), err +} + // Delete takes name of the clusterTriggerAuthentication and deletes it. Returns an error if one occurs. func (c *FakeClusterTriggerAuthentications) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { _, err := c.Fake. diff --git a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_triggerauthentication.go b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_triggerauthentication.go index 5cecdac7917..a272dcc5912 100644 --- a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_triggerauthentication.go +++ b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/fake/fake_triggerauthentication.go @@ -101,6 +101,18 @@ func (c *FakeTriggerAuthentications) Update(ctx context.Context, triggerAuthenti return obj.(*v1alpha1.TriggerAuthentication), err } +// UpdateStatus was generated because the type contains a Status member. +// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). +func (c *FakeTriggerAuthentications) UpdateStatus(ctx context.Context, triggerAuthentication *v1alpha1.TriggerAuthentication, opts v1.UpdateOptions) (*v1alpha1.TriggerAuthentication, error) { + obj, err := c.Fake. + Invokes(testing.NewUpdateSubresourceAction(triggerauthenticationsResource, "status", c.ns, triggerAuthentication), &v1alpha1.TriggerAuthentication{}) + + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.TriggerAuthentication), err +} + // Delete takes name of the triggerAuthentication and deletes it. Returns an error if one occurs. func (c *FakeTriggerAuthentications) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { _, err := c.Fake. diff --git a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/triggerauthentication.go b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/triggerauthentication.go index 81b9232e0fc..5fe86261af6 100644 --- a/pkg/generated/clientset/versioned/typed/keda/v1alpha1/triggerauthentication.go +++ b/pkg/generated/clientset/versioned/typed/keda/v1alpha1/triggerauthentication.go @@ -40,6 +40,7 @@ type TriggerAuthenticationsGetter interface { type TriggerAuthenticationInterface interface { Create(ctx context.Context, triggerAuthentication *v1alpha1.TriggerAuthentication, opts v1.CreateOptions) (*v1alpha1.TriggerAuthentication, error) Update(ctx context.Context, triggerAuthentication *v1alpha1.TriggerAuthentication, opts v1.UpdateOptions) (*v1alpha1.TriggerAuthentication, error) + UpdateStatus(ctx context.Context, triggerAuthentication *v1alpha1.TriggerAuthentication, opts v1.UpdateOptions) (*v1alpha1.TriggerAuthentication, error) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error Get(ctx context.Context, name string, opts v1.GetOptions) (*v1alpha1.TriggerAuthentication, error) @@ -135,6 +136,22 @@ func (c *triggerAuthentications) Update(ctx context.Context, triggerAuthenticati return } +// UpdateStatus was generated because the type contains a Status member. +// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). +func (c *triggerAuthentications) UpdateStatus(ctx context.Context, triggerAuthentication *v1alpha1.TriggerAuthentication, opts v1.UpdateOptions) (result *v1alpha1.TriggerAuthentication, err error) { + result = &v1alpha1.TriggerAuthentication{} + err = c.client.Put(). + Namespace(c.ns). + Resource("triggerauthentications"). + Name(triggerAuthentication.Name). + SubResource("status"). + VersionedParams(&opts, scheme.ParameterCodec). + Body(triggerAuthentication). + Do(ctx). + Into(result) + return +} + // Delete takes name of the triggerAuthentication and deletes it. Returns an error if one occurs. func (c *triggerAuthentications) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { return c.client.Delete(). From 7d7ea3aa267d61fdd56d5efb5669e23fed819a37 Mon Sep 17 00:00:00 2001 From: spiritzhou Date: Tue, 13 Jun 2023 20:18:52 +0800 Subject: [PATCH 14/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- .../crd/bases/keda.sh_clustertriggerauthentications.yaml | 8 ++++---- config/crd/bases/keda.sh_triggerauthentications.yaml | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml index 6fecd1bf509..6d2e5fadfb4 100644 --- a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml +++ b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml @@ -30,10 +30,10 @@ spec: - jsonPath: .spec.hashiCorpVault.address name: VaultAddress type: string - - jsonPath: .status.allscaledobjectnamesstring + - jsonPath: .status.scaledobjects name: ScaledObjects type: string - - jsonPath: .status.allscaledjobnamesstring + - jsonPath: .status.scaledjobs name: ScaledJobs type: string name: v1alpha1 @@ -232,9 +232,9 @@ spec: description: TriggerAuthenticationStatus defines the observed state of TriggerAuthentication properties: - allscaledjobnamesstring: + scaledjobs: type: string - allscaledobjectnamesstring: + scaledobjects: type: string type: object required: diff --git a/config/crd/bases/keda.sh_triggerauthentications.yaml b/config/crd/bases/keda.sh_triggerauthentications.yaml index b390d537431..47fc5e2abb0 100644 --- a/config/crd/bases/keda.sh_triggerauthentications.yaml +++ b/config/crd/bases/keda.sh_triggerauthentications.yaml @@ -30,10 +30,10 @@ spec: - jsonPath: .spec.hashiCorpVault.address name: VaultAddress type: string - - jsonPath: .status.allscaledobjectnamesstring + - jsonPath: .status.scaledobjects name: ScaledObjects type: string - - jsonPath: .status.allscaledjobnamesstring + - jsonPath: .status.scaledjobs name: ScaledJobs type: string name: v1alpha1 @@ -231,9 +231,9 @@ spec: description: TriggerAuthenticationStatus defines the observed state of TriggerAuthentication properties: - allscaledjobnamesstring: + scaledjobs: type: string - allscaledobjectnamesstring: + scaledobjects: type: string type: object required: From bba59bef2b31c421cafe5f09b9eded1754b72462 Mon Sep 17 00:00:00 2001 From: SpirtZhou Date: Tue, 13 Jun 2023 20:47:40 +0800 Subject: [PATCH 15/25] Update Signed-off-by: spiritzhou Signed-off-by: SpiritZhou --- controllers/keda/triggerauthentication_controller.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 3eefdfbabcb..6639a9aa5ff 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -22,10 +22,6 @@ import ( "sync" "github.com/go-logr/logr" - kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" - "github.com/kedacore/keda/v2/pkg/eventreason" - "github.com/kedacore/keda/v2/pkg/prommetrics" - kedautil "github.com/kedacore/keda/v2/pkg/util" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" @@ -36,6 +32,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" + + kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" + "github.com/kedacore/keda/v2/pkg/eventreason" + "github.com/kedacore/keda/v2/pkg/prommetrics" + kedautil "github.com/kedacore/keda/v2/pkg/util" ) // TriggerAuthenticationReconciler reconciles a TriggerAuthentication object From a077ae9e9b7cd31d7f79bf296aee29fd9f4081e1 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Wed, 14 Jun 2023 16:47:57 +0800 Subject: [PATCH 16/25] Update Signed-off-by: SpiritZhou --- tests/internals/update_ta/update_ta_test.go | 373 ++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 tests/internals/update_ta/update_ta_test.go diff --git a/tests/internals/update_ta/update_ta_test.go b/tests/internals/update_ta/update_ta_test.go new file mode 100644 index 00000000000..8b74679c6f3 --- /dev/null +++ b/tests/internals/update_ta/update_ta_test.go @@ -0,0 +1,373 @@ +//go:build e2e +// +build e2e + +package update_ta_so_test + +import ( + "fmt" + "strings" + "testing" + "time" + + "github.com/joho/godotenv" + "github.com/stretchr/testify/assert" + "k8s.io/client-go/kubernetes" + + . "github.com/kedacore/keda/v2/tests/helper" +) + +const ( + testName = "update-ta-so-test" +) + +// Load environment variables from .env file +var _ = godotenv.Load("../../.env") + +var ( + namespace = fmt.Sprintf("%s-ns", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + deployment2Name = fmt.Sprintf("%s-deployment-2", testName) + scaledObjectName = fmt.Sprintf("%s-so", testName) + scaledObject2Name = fmt.Sprintf("%s-so-2", testName) + secretName = fmt.Sprintf("%s-secret", testName) + triggerAuthName = fmt.Sprintf("%s-ta", testName) + scaledJobName = fmt.Sprintf("%s-sj", testName) + scaledJob2Name = fmt.Sprintf("%s-sj-2", testName) + minReplicas = 1 + midReplicas = 3 + maxReplicas = 5 + triggerAuthKind = "TriggerAuthentication" + clusterTriggerAuthKind = "ClusterTriggerAuthentication" +) + +type templateData struct { + TestNamespace string + TriggerAuthKind string + DeploymentName string + Deployment2Name string + ScaledObject string + ScaledObject2 string + TriggerAuthName string + SecretName string + MinReplicas string + MaxReplicas string + ScaledJob string + ScaledJob2 string +} + +const ( + secretTemplate = `apiVersion: v1 +kind: Secret +metadata: + name: {{.SecretName}} + namespace: {{.TestNamespace}} +data: + AUTH_PASSWORD: U0VDUkVUCg== + AUTH_USERNAME: VVNFUgo= +` + + triggerAuthenticationTemplate = `apiVersion: keda.sh/v1alpha1 +kind: {{.TriggerAuthKind}} +metadata: + name: {{.TriggerAuthName}} + namespace: {{.TestNamespace}} +spec: + secretTargetRef: + - parameter: username + name: {{.SecretName}} + key: AUTH_USERNAME + - parameter: password + name: {{.SecretName}} + key: AUTH_PASSWORD +` + + deploymentTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + deploy: {{.DeploymentName}} + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} +spec: + selector: + matchLabels: + app: {{.DeploymentName}} + replicas: {{.MinReplicas}} + template: + metadata: + labels: + app: {{.DeploymentName}} + spec: + containers: + - name: nginx + image: nginxinc/nginx-unprivileged + ports: + - containerPort: 80 + resources: + requests: + cpu: "200m" + limits: + cpu: "500m" +` + + deployment2Template = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + deploy: {{.Deployment2Name}} + name: {{.Deployment2Name}} + namespace: {{.TestNamespace}} +spec: + selector: + matchLabels: + app: {{.Deployment2Name}} + replicas: {{.MinReplicas}} + template: + metadata: + labels: + app: {{.Deployment2Name}} + spec: + containers: + - name: nginx + image: nginxinc/nginx-unprivileged + ports: + - containerPort: 80 + resources: + requests: + cpu: "200m" + limits: + cpu: "500m" +` + + scaledObjectTriggerTemplate = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: {{.ScaledObject}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + advanced: + horizontalPodAutoscalerConfig: + behavior: + scaleDown: + stabilizationWindowSeconds: 10 + pollingInterval: 10 + scaleTargetRef: + name: {{.DeploymentName}} + minReplicaCount: {{.MinReplicas}} + maxReplicaCount: {{.MaxReplicas}} + cooldownPeriod: 1 + triggers: + - type: cpu + metricType: Utilization + metadata: + value: "50" + authenticationRef: + name: {{.TriggerAuthName}} + kind: {{.TriggerAuthKind}} +` + + scaledObjectTrigger2Template = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: {{.ScaledObject2}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + advanced: + horizontalPodAutoscalerConfig: + behavior: + scaleDown: + stabilizationWindowSeconds: 10 + pollingInterval: 10 + scaleTargetRef: + name: {{.Deployment2Name}} + minReplicaCount: {{.MinReplicas}} + maxReplicaCount: {{.MaxReplicas}} + cooldownPeriod: 1 + triggers: + - type: cpu + metricType: Utilization + metadata: + value: "50" + authenticationRef: + name: {{.TriggerAuthName}} + kind: {{.TriggerAuthKind}} +` + + scaledJobTemplate = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledJob +metadata: + name: {{.ScaledJob}} + namespace: {{.TestNamespace}} +spec: + jobTargetRef: + template: + spec: + containers: + - name: external-executor + image: busybox + command: + - sleep + - "30" + imagePullPolicy: IfNotPresent + restartPolicy: Never + backoffLimit: 1 + pollingInterval: 5 + minReplicaCount: {{.MinReplicas}} + maxReplicaCount: {{.MaxReplicas}} + successfulJobsHistoryLimit: 0 + failedJobsHistoryLimit: 0 + triggers: + - type: cpu + metadata: + type: Utilization + value: "50" + authenticationRef: + name: {{.TriggerAuthName}} + kind: {{.TriggerAuthKind}} +` + + scaledJob2Template = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledJob +metadata: + name: {{.ScaledJob2}} + namespace: {{.TestNamespace}} +spec: + jobTargetRef: + template: + spec: + containers: + - name: external-executor + image: busybox + command: + - sleep + - "30" + imagePullPolicy: IfNotPresent + restartPolicy: Never + backoffLimit: 1 + pollingInterval: 5 + minReplicaCount: {{.MinReplicas}} + maxReplicaCount: {{.MaxReplicas}} + successfulJobsHistoryLimit: 0 + failedJobsHistoryLimit: 0 + triggers: + - type: cpu + metadata: + type: Utilization + value: "50" + authenticationRef: + name: {{.TriggerAuthName}} + kind: {{.TriggerAuthKind}} +` +) + +func TestTriggerAuthenticationGeneral(t *testing.T) { + // setup + t.Log("--- setting up ---") + // Create kubernetes resources + kc := GetKubernetesClient(t) + + t.Log("--- testing triggerauthentication ---") + data, templates := getTemplateData(triggerAuthKind) + CreateKubernetesResources(t, kc, namespace, data, templates) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, namespace, minReplicas, 180, 3), + "replica count should be %d after 3 minutes", minReplicas) + + testTriggerAuthenticationStatusValue(t, kc, data, triggerAuthKind) + + // Clean resources and then testing clustertriggerauthentication + DeleteKubernetesResources(t, namespace, data, templates) + + t.Log("--- testing clustertriggerauthentication ---") + data, templates = getTemplateData(clusterTriggerAuthKind) + CreateKubernetesResources(t, kc, namespace, data, templates) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, namespace, minReplicas, 180, 3), + "replica count should be %d after 3 minutes", minReplicas) + + testTriggerAuthenticationStatusValue(t, kc, data, clusterTriggerAuthKind) + DeleteKubernetesResources(t, namespace, data, templates) +} + +func checkScaledObjectStatusFromKubectl(t *testing.T, kind string, expected string) { + time.Sleep(1 * time.Second) + kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s -o jsonpath="{.status.scaledobjects}"`, kind, triggerAuthName, namespace) + output, err := ExecuteCommand(kctlGetCmd) + assert.NoErrorf(t, err, "cannot get rollout info - %s", err) + + unqoutedOutput := strings.ReplaceAll(string(output), "\"", "") + assert.Equal(t, expected, unqoutedOutput) +} + +func checkScaleJobStatusFromKubectl(t *testing.T, kind string, expected string) { + time.Sleep(1 * time.Second) + kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s -o jsonpath="{.status.scaledjobs}"`, kind, triggerAuthName, namespace) + output, err := ExecuteCommand(kctlGetCmd) + assert.NoErrorf(t, err, "cannot get rollout info - %s", err) + + unqoutedOutput := strings.ReplaceAll(string(output), "\"", "") + assert.Equal(t, expected, unqoutedOutput) +} + +// tests basic scaling with one trigger based on metrics +func testTriggerAuthenticationStatusValue(t *testing.T, kc *kubernetes.Clientset, data templateData, kind string) { + KubectlApplyWithTemplate(t, data, "triggerAuthenticationTemplate", triggerAuthenticationTemplate) + t.Log("--- test one scaledObject ---") + KubectlApplyWithTemplate(t, data, "scaledObjectTriggerTemplate", scaledObjectTriggerTemplate) + checkScaledObjectStatusFromKubectl(t, kind, scaledObjectName) + + t.Log("--- test two scaledObject ---") + KubectlApplyWithTemplate(t, data, "scaledObjectTrigger2Template", scaledObjectTrigger2Template) + checkScaledObjectStatusFromKubectl(t, kind, scaledObjectName+","+scaledObject2Name) + + t.Log("--- test reomve scaledObject ---") + KubectlDeleteWithTemplate(t, data, "scaledObjectTriggerTemplate", scaledObjectTriggerTemplate) + checkScaledObjectStatusFromKubectl(t, kind, scaledObject2Name) + KubectlDeleteWithTemplate(t, data, "scaledObjectTrigger2Template", scaledObjectTrigger2Template) + checkScaledObjectStatusFromKubectl(t, kind, "") + + t.Log("--- test one scaledJob ---") + KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + checkScaleJobStatusFromKubectl(t, kind, scaledJobName) + + t.Log("--- test two scaledJob ---") + KubectlApplyWithTemplate(t, data, "scaledJob2Template", scaledJob2Template) + checkScaleJobStatusFromKubectl(t, kind, scaledJobName+","+scaledJob2Name) + + t.Log("--- test reomve scaledObject ---") + KubectlDeleteWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + checkScaleJobStatusFromKubectl(t, kind, scaledJob2Name) + KubectlDeleteWithTemplate(t, data, "scaledJob2Template", scaledJob2Template) + checkScaleJobStatusFromKubectl(t, kind, "") +} + +// help function to load template data +func getTemplateData(triggerAuthKind string) (templateData, []Template) { + return templateData{ + TestNamespace: namespace, + TriggerAuthKind: triggerAuthKind, + DeploymentName: deploymentName, + Deployment2Name: deployment2Name, + TriggerAuthName: triggerAuthName, + ScaledObject: scaledObjectName, + ScaledObject2: scaledObject2Name, + ScaledJob: scaledJobName, + ScaledJob2: scaledJob2Name, + SecretName: secretName, + MinReplicas: fmt.Sprintf("%v", minReplicas), + MaxReplicas: fmt.Sprintf("%v", maxReplicas), + }, []Template{ + {Name: "deploymentTemplate", Config: deploymentTemplate}, + {Name: "deployment2Template", Config: deployment2Template}, + } +} From 4269d2df1316b3b92cca53ff1d0e7be79116645f Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Wed, 14 Jun 2023 17:04:08 +0800 Subject: [PATCH 17/25] Update Signed-off-by: SpiritZhou --- tests/internals/update_ta/update_ta_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/internals/update_ta/update_ta_test.go b/tests/internals/update_ta/update_ta_test.go index 8b74679c6f3..02fe83ac8f8 100644 --- a/tests/internals/update_ta/update_ta_test.go +++ b/tests/internals/update_ta/update_ta_test.go @@ -11,7 +11,6 @@ import ( "github.com/joho/godotenv" "github.com/stretchr/testify/assert" - "k8s.io/client-go/kubernetes" . "github.com/kedacore/keda/v2/tests/helper" ) @@ -34,7 +33,6 @@ var ( scaledJobName = fmt.Sprintf("%s-sj", testName) scaledJob2Name = fmt.Sprintf("%s-sj-2", testName) minReplicas = 1 - midReplicas = 3 maxReplicas = 5 triggerAuthKind = "TriggerAuthentication" clusterTriggerAuthKind = "ClusterTriggerAuthentication" @@ -283,7 +281,7 @@ func TestTriggerAuthenticationGeneral(t *testing.T) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, namespace, minReplicas, 180, 3), "replica count should be %d after 3 minutes", minReplicas) - testTriggerAuthenticationStatusValue(t, kc, data, triggerAuthKind) + testTriggerAuthenticationStatusValue(t, data, triggerAuthKind) // Clean resources and then testing clustertriggerauthentication DeleteKubernetesResources(t, namespace, data, templates) @@ -295,7 +293,7 @@ func TestTriggerAuthenticationGeneral(t *testing.T) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, namespace, minReplicas, 180, 3), "replica count should be %d after 3 minutes", minReplicas) - testTriggerAuthenticationStatusValue(t, kc, data, clusterTriggerAuthKind) + testTriggerAuthenticationStatusValue(t, data, clusterTriggerAuthKind) DeleteKubernetesResources(t, namespace, data, templates) } @@ -320,7 +318,7 @@ func checkScaleJobStatusFromKubectl(t *testing.T, kind string, expected string) } // tests basic scaling with one trigger based on metrics -func testTriggerAuthenticationStatusValue(t *testing.T, kc *kubernetes.Clientset, data templateData, kind string) { +func testTriggerAuthenticationStatusValue(t *testing.T, data templateData, kind string) { KubectlApplyWithTemplate(t, data, "triggerAuthenticationTemplate", triggerAuthenticationTemplate) t.Log("--- test one scaledObject ---") KubectlApplyWithTemplate(t, data, "scaledObjectTriggerTemplate", scaledObjectTriggerTemplate) @@ -367,6 +365,7 @@ func getTemplateData(triggerAuthKind string) (templateData, []Template) { MinReplicas: fmt.Sprintf("%v", minReplicas), MaxReplicas: fmt.Sprintf("%v", maxReplicas), }, []Template{ + {Name: "secretTemplate", Config: secretTemplate}, {Name: "deploymentTemplate", Config: deploymentTemplate}, {Name: "deployment2Template", Config: deployment2Template}, } From 12db1def49d6186102cb65b1ac4a0d98d6b5a2c7 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 20 Jun 2023 11:19:27 +0800 Subject: [PATCH 18/25] Update Signed-off-by: SpiritZhou --- controllers/keda/util/string_lists.go | 11 ++++ controllers/keda/util/string_lists_test.go | 75 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 controllers/keda/util/string_lists_test.go diff --git a/controllers/keda/util/string_lists.go b/controllers/keda/util/string_lists.go index 56d92ad7dbf..379fb57770e 100644 --- a/controllers/keda/util/string_lists.go +++ b/controllers/keda/util/string_lists.go @@ -41,7 +41,14 @@ func Remove(list []string, s string) []string { } // AppendIntoString append a new string into a string that has seprator +// For example, +// +// -- input: `viewer,editor`, `owner`, `,` output: `viewer,editor,owner` func AppendIntoString(srcStr string, appendStr string, sep string) string { + if appendStr == "" { + return srcStr + } + splitStrings := []string{} if srcStr != "" { splitStrings = strings.Split(srcStr, sep) @@ -55,6 +62,10 @@ func AppendIntoString(srcStr string, appendStr string, sep string) string { } // RemoveFromString remove a string from src string that has seprator +// For example, +// +// -- input: `viewer,editor,owner`, `owner`, `,` output: `viewer,editor` +// -- input: `viewer,editor,owner`, `owner`, `:` output: `viewer,editor,owner` func RemoveFromString(srcStr string, str string, sep string) string { if srcStr == "" { return srcStr diff --git a/controllers/keda/util/string_lists_test.go b/controllers/keda/util/string_lists_test.go new file mode 100644 index 00000000000..8e5b5dd9835 --- /dev/null +++ b/controllers/keda/util/string_lists_test.go @@ -0,0 +1,75 @@ +/* +Copyright 2021 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 util + +import ( + "reflect" + "testing" +) + +func TestAppendString(t *testing.T) { + testData := []struct { + name string + from string + append string + sep string + exp string + }{ + {"success", "viewer,editor", "owner", ",", "viewer,editor,owner"}, + {"single_success", "viewer", "owner", ",", "viewer,owner"}, + {"exist", "viewer,editor,owner", "editor", ",", "viewer,editor,owner"}, + {"no_seperator", "viewer,editor", "owner", "", "viewer,editorowner"}, + {"space_seperator", "viewer,editor", "owner", " ", "viewer,editor owner"}, + {"diff_seperator", "viewer,editor", "owner", ":", "viewer,editor:owner"}, + {"no_from_str", "", "owner", ",", "owner"}, + {"no_append_str", "viewer,editor", "", ",", "viewer,editor"}, + } + + for _, tt := range testData { + got := AppendIntoString(tt.from, tt.append, tt.sep) + + if !reflect.DeepEqual(tt.exp, got) { + t.Errorf("Expected %v but got %v\n", tt.exp, got) + } + } +} + +func TestRemoveFromString(t *testing.T) { + testData := []struct { + name string + from string + delete string + sep string + exp string + }{ + {"success", "viewer,editor,owner", "owner", ",", "viewer,editor"}, + {"no_exist_success", "viewer", "owner", ",", "viewer"}, + {"no_seperator", "viewer,editor,owner", "owner", "", "viewer,editor,owner"}, + {"space_seperator", "viewer editor owner", "editor", " ", "viewer owner"}, + {"diff_seperator", "viewer,editor,owner", "editor", ":", "viewer,editor,owner"}, + {"no_from_str", "", "owner", ",", ""}, + {"no_delete_str", "viewer,editor", "", ",", "viewer,editor"}, + } + + for _, tt := range testData { + got := RemoveFromString(tt.from, tt.delete, tt.sep) + + if !reflect.DeepEqual(tt.exp, got) { + t.Errorf("Expected %v but got %v\n", tt.exp, got) + } + } +} From 71d01a80e1becb9121c61970a781ebd1d5438837 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 20 Jun 2023 14:20:02 +0800 Subject: [PATCH 19/25] Update Signed-off-by: SpiritZhou --- controllers/keda/hpa.go | 6 +- controllers/keda/scaledjob_controller.go | 10 +-- controllers/keda/scaledobject_controller.go | 12 +-- .../keda/triggerauthentication_controller.go | 86 +------------------ pkg/scaling/executor/scale_executor.go | 6 +- pkg/scaling/executor/scale_scaledobjects.go | 4 +- pkg/{util => status}/status.go | 70 ++++++++++++++- 7 files changed, 87 insertions(+), 107 deletions(-) rename pkg/{util => status}/status.go (59%) diff --git a/controllers/keda/hpa.go b/controllers/keda/hpa.go index febeb0964b4..94f97d65670 100644 --- a/controllers/keda/hpa.go +++ b/controllers/keda/hpa.go @@ -32,8 +32,8 @@ import ( kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" kedacontrollerutil "github.com/kedacore/keda/v2/controllers/keda/util" "github.com/kedacore/keda/v2/pkg/scaling/executor" - kedautil "github.com/kedacore/keda/v2/pkg/util" version "github.com/kedacore/keda/v2/version" + kedastatus "github.com/kedacore/keda/v2/pkg/status" ) const ( @@ -61,7 +61,7 @@ func (r *ScaledObjectReconciler) createAndDeployNewHPA(ctx context.Context, logg status := scaledObject.Status.DeepCopy() status.HpaName = hpaName - err = kedautil.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status) + err = kedastatus.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status) if err != nil { logger.Error(err, "Error updating scaledObject status with used hpaName") return err @@ -238,7 +238,7 @@ func (r *ScaledObjectReconciler) getScaledObjectMetricSpecs(ctx context.Context, updateHealthStatus(scaledObject, externalMetricNames, status) - err = kedautil.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status) + err = kedastatus.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status) if err != nil { logger.Error(err, "Error updating scaledObject status with used externalMetricNames") return nil, err diff --git a/controllers/keda/scaledjob_controller.go b/controllers/keda/scaledjob_controller.go index 8999889d6e0..65da42a8747 100755 --- a/controllers/keda/scaledjob_controller.go +++ b/controllers/keda/scaledjob_controller.go @@ -43,7 +43,7 @@ import ( "github.com/kedacore/keda/v2/pkg/eventreason" "github.com/kedacore/keda/v2/pkg/prommetrics" "github.com/kedacore/keda/v2/pkg/scaling" - kedautil "github.com/kedacore/keda/v2/pkg/util" + kedastatus "github.com/kedacore/keda/v2/pkg/status" ) // +kubebuilder:rbac:groups=keda.sh,resources=scaledjobs;scaledjobs/finalizers;scaledjobs/status,verbs="*" @@ -125,7 +125,7 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( // ensure Status Conditions are initialized if !scaledJob.Status.Conditions.AreInitialized() { conditions := kedav1alpha1.GetInitializedConditions() - if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledJob, conditions); err != nil { + if err := kedastatus.SetStatusConditions(ctx, r.Client, reqLogger, scaledJob, conditions); err != nil { return ctrl.Result{}, err } } @@ -153,7 +153,7 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( conditions.SetReadyCondition(metav1.ConditionTrue, "ScaledJobReady", msg) } - if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledJob, &conditions); err != nil { + if err := kedastatus.SetStatusConditions(ctx, r.Client, reqLogger, scaledJob, &conditions); err != nil { return ctrl.Result{}, err } @@ -321,14 +321,14 @@ func (r *ScaledJobReconciler) updatePromMetricsOnDelete(namespacedName string) { } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { - return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledJob.GetNamespace(), scaledJob.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + return kedastatus.UpdateTriggerAuthenticationStatusFromTriggers(ctx, logger, r.Client, scaledJob.GetNamespace(), scaledJob.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") return triggerAuthenticationStatus }) } func (r *ScaledJobReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) { - return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledJob.GetNamespace(), scaledJob.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { + return kedastatus.UpdateTriggerAuthenticationStatusFromTriggers(ctx, logger, r.Client, scaledJob.GetNamespace(), scaledJob.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledJobNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledJobNamesStr, scaledJob.GetName(), ",") return triggerAuthenticationStatus }) diff --git a/controllers/keda/scaledobject_controller.go b/controllers/keda/scaledobject_controller.go index 8cfebffa848..e01b1f206fd 100755 --- a/controllers/keda/scaledobject_controller.go +++ b/controllers/keda/scaledobject_controller.go @@ -46,7 +46,7 @@ import ( "github.com/kedacore/keda/v2/pkg/eventreason" "github.com/kedacore/keda/v2/pkg/prommetrics" "github.com/kedacore/keda/v2/pkg/scaling" - kedautil "github.com/kedacore/keda/v2/pkg/util" + kedastatus "github.com/kedacore/keda/v2/pkg/status" ) // +kubebuilder:rbac:groups=keda.sh,resources=scaledobjects;scaledobjects/finalizers;scaledobjects/status,verbs="*" @@ -167,7 +167,7 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request // ensure Status Conditions are initialized if !scaledObject.Status.Conditions.AreInitialized() { conditions := kedav1alpha1.GetInitializedConditions() - if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, conditions); err != nil { + if err := kedastatus.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, conditions); err != nil { return ctrl.Result{}, err } } @@ -189,7 +189,7 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request conditions.SetReadyCondition(metav1.ConditionTrue, kedav1alpha1.ScaledObjectConditionReadySucccesReason, msg) } - if err := kedautil.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, &conditions); err != nil { + if err := kedastatus.SetStatusConditions(ctx, r.Client, reqLogger, scaledObject, &conditions); err != nil { return ctrl.Result{}, err } @@ -330,7 +330,7 @@ func (r *ScaledObjectReconciler) checkTargetResourceIsScalable(ctx context.Conte status.PausedReplicaCount = nil } - if err := kedautil.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status); err != nil { + if err := kedastatus.UpdateScaledObjectStatus(ctx, r.Client, logger, scaledObject, status); err != nil { return gvkr, err } logger.Info("Detected resource targeted for scaling", "resource", gvkString, "name", scaledObject.Spec.ScaleTargetRef.Name) @@ -546,7 +546,7 @@ func (r *ScaledObjectReconciler) updatePromMetricsOnDelete(namespacedName string } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { - return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledObject.GetNamespace(), scaledObject.Spec.Triggers, + return kedastatus.UpdateTriggerAuthenticationStatusFromTriggers(ctx, logger, r.Client, scaledObject.GetNamespace(), scaledObject.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.AppendIntoString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") return triggerAuthenticationStatus @@ -554,7 +554,7 @@ func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.C } func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) { - return UpdateTriggerAuthenticationStatusHandler(ctx, logger, r.Client, scaledObject.GetNamespace(), scaledObject.Spec.Triggers, + return kedastatus.UpdateTriggerAuthenticationStatusFromTriggers(ctx, logger, r.Client, scaledObject.GetNamespace(), scaledObject.Spec.Triggers, func(triggerAuthenticationStatus *kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus { triggerAuthenticationStatus.ScaledObjectNamesStr = kedacontrollerutil.RemoveFromString(triggerAuthenticationStatus.ScaledObjectNamesStr, scaledObject.GetName(), ",") return triggerAuthenticationStatus diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 6639a9aa5ff..4d85e4387cd 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -18,14 +18,10 @@ package keda import ( "context" - "fmt" "sync" - "github.com/go-logr/logr" - "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" - k8sErrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" + errors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -36,7 +32,6 @@ import ( kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" "github.com/kedacore/keda/v2/pkg/eventreason" "github.com/kedacore/keda/v2/pkg/prommetrics" - kedautil "github.com/kedacore/keda/v2/pkg/util" ) // TriggerAuthenticationReconciler reconciles a TriggerAuthentication object @@ -68,7 +63,7 @@ func (r *TriggerAuthenticationReconciler) Reconcile(ctx context.Context, req ctr triggerAuthentication := &kedav1alpha1.TriggerAuthentication{} err := r.Client.Get(ctx, req.NamespacedName, triggerAuthentication) if err != nil { - if k8sErrors.IsNotFound(err) { + if errors.IsNotFound(err) { return ctrl.Result{}, nil } reqLogger.Error(err, "Failed to get TriggerAuthentication") @@ -121,80 +116,3 @@ func (r *TriggerAuthenticationReconciler) UpdatePromMetricsOnDelete(namespacedNa delete(triggerAuthPromMetricsMap, namespacedName) } - -func GetTriggerAuth(ctx context.Context, client client.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (client.Object, error) { - if triggerAuthRef == nil { - return nil, fmt.Errorf("triggerAuthRef is nil") - } - - if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { - triggerAuth := &kedav1alpha1.TriggerAuthentication{} - err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth) - if err != nil { - return nil, err - } - return triggerAuth, nil - } else if triggerAuthRef.Kind == "ClusterTriggerAuthentication" { - clusterTriggerAuth := &kedav1alpha1.ClusterTriggerAuthentication{} - err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, clusterTriggerAuth) - if err != nil { - return nil, err - } - return clusterTriggerAuth, nil - } - return nil, fmt.Errorf("unknown trigger auth kind %s", triggerAuthRef.Kind) -} - -func GetTriggerAuthStatus(triggerAuth client.Object) (*kedav1alpha1.TriggerAuthenticationStatus, error) { - switch obj := triggerAuth.(type) { - case *kedav1alpha1.TriggerAuthentication: - return &obj.Status, nil - case *kedav1alpha1.ClusterTriggerAuthentication: - return &obj.Status, nil - default: - return nil, fmt.Errorf("unknown trigger auth status %s", obj) - } -} - -func UpdateTriggerAuthenticationStatusHandler(ctx context.Context, logger logr.Logger, client client.Client, namespace string, scaleTriggers []kedav1alpha1.ScaleTriggers, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { - var errs error - for _, trigger := range scaleTriggers { - if trigger.AuthenticationRef == nil { - continue - } - - triggerAuth, err := GetTriggerAuth(ctx, client, trigger.AuthenticationRef, namespace) - - if err != nil { - if k8sErrors.IsNotFound(err) { - logger.Info("TriggerAuthentication Not Found") - } - logger.Error(err, "Failed to get TriggerAuthentication") - errs = errors.Wrap(errs, err.Error()) - continue - } - - triggerAuthenticationStatus, err := GetTriggerAuthStatus(triggerAuth) - - if err != nil { - if k8sErrors.IsNotFound(err) { - logger.Info("TriggerAuthenticationStatus Not Found") - } - logger.Error(err, "Failed to get TriggerAuthenticationStatus") - errs = errors.Wrap(errs, err.Error()) - continue - } - - triggerAuthenticationStatus = statusHandler(triggerAuthenticationStatus.DeepCopy()) - - if err := kedautil.UpdateTriggerAuthenticationStatus(ctx, client, logger, triggerAuth, triggerAuthenticationStatus); err != nil { - logger.Error(err, "Failed to update TriggerAuthenticationStatus") - errs = errors.Wrap(errs, err.Error()) - } - } - - if errs != nil { - return "Update TriggerAuthentication Status Failed", errs - } - return "Update TriggerAuthentication Status Successfully", nil -} diff --git a/pkg/scaling/executor/scale_executor.go b/pkg/scaling/executor/scale_executor.go index 2e0d6a83e31..3e013e5440b 100644 --- a/pkg/scaling/executor/scale_executor.go +++ b/pkg/scaling/executor/scale_executor.go @@ -29,7 +29,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" - kedautil "github.com/kedacore/keda/v2/pkg/util" + kedastatus "github.com/kedacore/keda/v2/pkg/status" ) const ( @@ -78,7 +78,7 @@ func (e *scaleExecutor) updateLastActiveTime(ctx context.Context, logger logr.Lo } return nil } - return kedautil.TransformObject(ctx, e.client, logger, object, now, transform) + return kedastatus.TransformObject(ctx, e.client, logger, object, now, transform) } func (e *scaleExecutor) setCondition(ctx context.Context, logger logr.Logger, object interface{}, status metav1.ConditionStatus, reason string, message string, setCondition func(kedav1alpha1.Conditions, metav1.ConditionStatus, string, string)) error { @@ -103,7 +103,7 @@ func (e *scaleExecutor) setCondition(ctx context.Context, logger logr.Logger, ob reason: reason, message: message, } - return kedautil.TransformObject(ctx, e.client, logger, object, &target, transform) + return kedastatus.TransformObject(ctx, e.client, logger, object, &target, transform) } func (e *scaleExecutor) setReadyCondition(ctx context.Context, logger logr.Logger, object interface{}, status metav1.ConditionStatus, reason string, message string) error { diff --git a/pkg/scaling/executor/scale_scaledobjects.go b/pkg/scaling/executor/scale_scaledobjects.go index 10cbf2470dc..98da83bfc3c 100644 --- a/pkg/scaling/executor/scale_scaledobjects.go +++ b/pkg/scaling/executor/scale_scaledobjects.go @@ -31,7 +31,7 @@ import ( kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" kedacontrollerutil "github.com/kedacore/keda/v2/controllers/keda/util" "github.com/kedacore/keda/v2/pkg/eventreason" - kedautil "github.com/kedacore/keda/v2/pkg/util" + kedastatus "github.com/kedacore/keda/v2/pkg/status" ) func (e *scaleExecutor) RequestScale(ctx context.Context, scaledObject *kedav1alpha1.ScaledObject, isActive bool, isError bool) { @@ -107,7 +107,7 @@ func (e *scaleExecutor) RequestScale(ctx context.Context, scaledObject *kedav1al return } status.PausedReplicaCount = pausedCount - err = kedautil.UpdateScaledObjectStatus(ctx, e.client, logger, scaledObject, status) + err = kedastatus.UpdateScaledObjectStatus(ctx, e.client, logger, scaledObject, status) if err != nil { logger.Error(err, "error updating status paused replica count") return diff --git a/pkg/util/status.go b/pkg/status/status.go similarity index 59% rename from pkg/util/status.go rename to pkg/status/status.go index 9d9a23bff30..33127030fc7 100755 --- a/pkg/util/status.go +++ b/pkg/status/status.go @@ -14,13 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package status import ( "context" "fmt" "github.com/go-logr/logr" + "github.com/pkg/errors" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/types" runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" @@ -62,8 +65,42 @@ func UpdateScaledObjectStatus(ctx context.Context, client runtimeclient.StatusCl return TransformObject(ctx, client, logger, scaledObject, status, transform) } -// UpdateTriggerAuthenticationStatus patches the given TriggerAuthentication/ClusterTriggerAuthentication with the updated status passed to it or returns an error. -func UpdateTriggerAuthenticationStatus(ctx context.Context, client runtimeclient.StatusClient, logger logr.Logger, object runtimeclient.Object, status *kedav1alpha1.TriggerAuthenticationStatus) error { +func getTriggerAuth(ctx context.Context, client runtimeclient.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (runtimeclient.Object, *kedav1alpha1.TriggerAuthenticationStatus, error) { + if triggerAuthRef == nil { + return nil, nil, fmt.Errorf("triggerAuthRef is nil") + } + + if triggerAuthRef.Kind == "" || triggerAuthRef.Kind == "TriggerAuthentication" { + triggerAuth := &kedav1alpha1.TriggerAuthentication{} + err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, triggerAuth) + if err != nil { + return nil, nil, err + } + return triggerAuth, &triggerAuth.Status, nil + } else if triggerAuthRef.Kind == "ClusterTriggerAuthentication" { + clusterTriggerAuth := &kedav1alpha1.ClusterTriggerAuthentication{} + err := client.Get(ctx, types.NamespacedName{Name: triggerAuthRef.Name, Namespace: namespace}, clusterTriggerAuth) + if err != nil { + return nil, nil, err + } + return clusterTriggerAuth, &clusterTriggerAuth.Status, nil + } + return nil, nil, fmt.Errorf("unknown trigger auth kind %s", triggerAuthRef.Kind) +} + +func updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, client runtimeclient.Client, namespace string, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) error { + triggerAuth, triggerAuthStatus, err := getTriggerAuth(ctx, client, triggerAuthRef, namespace) + + if err != nil { + if k8sErrors.IsNotFound(err) { + logger.Info("TriggerAuthentication Not Found") + } + logger.Error(err, "Failed to get TriggerAuthentication") + return err + } + + triggerAuthenticationStatus := statusHandler(triggerAuthStatus.DeepCopy()) + transform := func(runtimeObj runtimeclient.Object, target interface{}) error { status, ok := target.(*kedav1alpha1.TriggerAuthenticationStatus) if !ok { @@ -78,7 +115,32 @@ func UpdateTriggerAuthenticationStatus(ctx context.Context, client runtimeclient } return nil } - return TransformObject(ctx, client, logger, object, status, transform) + + if err := TransformObject(ctx, client, logger, triggerAuth, triggerAuthenticationStatus, transform); err != nil { + logger.Error(err, "Failed to update TriggerAuthenticationStatus") + } + + return err +} + +// UpdateTriggerAuthenticationStatusFromTriggers patches triggerAuthenticationStatus From the given Triggers or returns an error. +func UpdateTriggerAuthenticationStatusFromTriggers(ctx context.Context, logger logr.Logger, client runtimeclient.Client, namespace string, scaleTriggers []kedav1alpha1.ScaleTriggers, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) (string, error) { + var errs error + for _, trigger := range scaleTriggers { + if trigger.AuthenticationRef == nil { + continue + } + + err := updateTriggerAuthenticationStatus(ctx, logger, client, namespace, trigger.AuthenticationRef, statusHandler) + if err != nil { + errs = errors.Wrap(errs, err.Error()) + } + } + + if errs != nil { + return "Update TriggerAuthentication Status Failed", errs + } + return "Update TriggerAuthentication Status Successfully", nil } // TransformObject patches the given object with the targeted passed to it through a transformer function or returns an error. From 55c7a5af403350fbca29c905a0c1a7c4d4b6f0ca Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 20 Jun 2023 15:37:52 +0800 Subject: [PATCH 20/25] Update testing Signed-off-by: SpiritZhou --- tests/helper/helper.go | 11 ++++++ tests/internals/update_ta/update_ta_test.go | 40 ++++++--------------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/tests/helper/helper.go b/tests/helper/helper.go index 12bc9e1cf30..25cf28fedf0 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -796,3 +796,14 @@ func generateCA(t *testing.T) { require.NoErrorf(t, err, "error closing custom CA key file- %s", err) } } + +func CheckKubectlGetResult(t *testing.T, kind string, name string, namespace string, otherparameter string, expected string) { + time.Sleep(1 * time.Second) // wait a second for recource deployment finished + kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s %s"`, kind, name, namespace, otherparameter) + t.Log("Running kubectl cmd:", kctlGetCmd) + output, err := ExecuteCommand(kctlGetCmd) + assert.NoErrorf(t, err, "cannot get rollout info - %s", err) + + unqoutedOutput := strings.ReplaceAll(string(output), "\"", "") + assert.Equal(t, expected, unqoutedOutput) +} diff --git a/tests/internals/update_ta/update_ta_test.go b/tests/internals/update_ta/update_ta_test.go index 02fe83ac8f8..f63c81e9126 100644 --- a/tests/internals/update_ta/update_ta_test.go +++ b/tests/internals/update_ta/update_ta_test.go @@ -5,9 +5,7 @@ package update_ta_so_test import ( "fmt" - "strings" "testing" - "time" "github.com/joho/godotenv" "github.com/stretchr/testify/assert" @@ -297,56 +295,38 @@ func TestTriggerAuthenticationGeneral(t *testing.T) { DeleteKubernetesResources(t, namespace, data, templates) } -func checkScaledObjectStatusFromKubectl(t *testing.T, kind string, expected string) { - time.Sleep(1 * time.Second) - kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s -o jsonpath="{.status.scaledobjects}"`, kind, triggerAuthName, namespace) - output, err := ExecuteCommand(kctlGetCmd) - assert.NoErrorf(t, err, "cannot get rollout info - %s", err) - - unqoutedOutput := strings.ReplaceAll(string(output), "\"", "") - assert.Equal(t, expected, unqoutedOutput) -} - -func checkScaleJobStatusFromKubectl(t *testing.T, kind string, expected string) { - time.Sleep(1 * time.Second) - kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s -o jsonpath="{.status.scaledjobs}"`, kind, triggerAuthName, namespace) - output, err := ExecuteCommand(kctlGetCmd) - assert.NoErrorf(t, err, "cannot get rollout info - %s", err) - - unqoutedOutput := strings.ReplaceAll(string(output), "\"", "") - assert.Equal(t, expected, unqoutedOutput) -} - // tests basic scaling with one trigger based on metrics func testTriggerAuthenticationStatusValue(t *testing.T, data templateData, kind string) { KubectlApplyWithTemplate(t, data, "triggerAuthenticationTemplate", triggerAuthenticationTemplate) t.Log("--- test one scaledObject ---") KubectlApplyWithTemplate(t, data, "scaledObjectTriggerTemplate", scaledObjectTriggerTemplate) - checkScaledObjectStatusFromKubectl(t, kind, scaledObjectName) + otherparameter := `-o jsonpath="{.status.scaledobjects}"` + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, scaledObjectName) t.Log("--- test two scaledObject ---") KubectlApplyWithTemplate(t, data, "scaledObjectTrigger2Template", scaledObjectTrigger2Template) - checkScaledObjectStatusFromKubectl(t, kind, scaledObjectName+","+scaledObject2Name) + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, scaledObjectName+","+scaledObject2Name) t.Log("--- test reomve scaledObject ---") KubectlDeleteWithTemplate(t, data, "scaledObjectTriggerTemplate", scaledObjectTriggerTemplate) - checkScaledObjectStatusFromKubectl(t, kind, scaledObject2Name) + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, scaledObject2Name) KubectlDeleteWithTemplate(t, data, "scaledObjectTrigger2Template", scaledObjectTrigger2Template) - checkScaledObjectStatusFromKubectl(t, kind, "") + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, "") t.Log("--- test one scaledJob ---") + otherparameter = `-o jsonpath="{.status.scaledjobs}"` KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) - checkScaleJobStatusFromKubectl(t, kind, scaledJobName) + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, scaledJobName) t.Log("--- test two scaledJob ---") KubectlApplyWithTemplate(t, data, "scaledJob2Template", scaledJob2Template) - checkScaleJobStatusFromKubectl(t, kind, scaledJobName+","+scaledJob2Name) + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, scaledJobName+","+scaledJob2Name) t.Log("--- test reomve scaledObject ---") KubectlDeleteWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) - checkScaleJobStatusFromKubectl(t, kind, scaledJob2Name) + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, scaledJob2Name) KubectlDeleteWithTemplate(t, data, "scaledJob2Template", scaledJob2Template) - checkScaleJobStatusFromKubectl(t, kind, "") + CheckKubectlGetResult(t, kind, triggerAuthName, namespace, otherparameter, "") } // help function to load template data From 3c00ed8ab988d641d730d352d153b5f1b7a992c9 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 20 Jun 2023 15:55:30 +0800 Subject: [PATCH 21/25] Show so/sj only in wide Signed-off-by: SpiritZhou --- apis/keda/v1alpha1/triggerauthentication_types.go | 8 ++++---- .../crd/bases/keda.sh_clustertriggerauthentications.yaml | 2 ++ config/crd/bases/keda.sh_triggerauthentications.yaml | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/apis/keda/v1alpha1/triggerauthentication_types.go b/apis/keda/v1alpha1/triggerauthentication_types.go index ba84a732009..c2fb5d9ab5f 100755 --- a/apis/keda/v1alpha1/triggerauthentication_types.go +++ b/apis/keda/v1alpha1/triggerauthentication_types.go @@ -31,8 +31,8 @@ import ( // +kubebuilder:printcolumn:name="Secret",type="string",JSONPath=".spec.secretTargetRef[*].name" // +kubebuilder:printcolumn:name="Env",type="string",JSONPath=".spec.env[*].name" // +kubebuilder:printcolumn:name="VaultAddress",type="string",JSONPath=".spec.hashiCorpVault.address" -// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.scaledobjects" -// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.scaledjobs" +// +kubebuilder:printcolumn:name="ScaledObjects",type="string",priority=1,JSONPath=".status.scaledobjects" +// +kubebuilder:printcolumn:name="ScaledJobs",type="string",priority=1,JSONPath=".status.scaledjobs" type ClusterTriggerAuthentication struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -60,8 +60,8 @@ type ClusterTriggerAuthenticationList struct { // +kubebuilder:printcolumn:name="Secret",type="string",JSONPath=".spec.secretTargetRef[*].name" // +kubebuilder:printcolumn:name="Env",type="string",JSONPath=".spec.env[*].name" // +kubebuilder:printcolumn:name="VaultAddress",type="string",JSONPath=".spec.hashiCorpVault.address" -// +kubebuilder:printcolumn:name="ScaledObjects",type="string",JSONPath=".status.scaledobjects" -// +kubebuilder:printcolumn:name="ScaledJobs",type="string",JSONPath=".status.scaledjobs" +// +kubebuilder:printcolumn:name="ScaledObjects",type="string",priority=1,JSONPath=".status.scaledobjects" +// +kubebuilder:printcolumn:name="ScaledJobs",type="string",priority=1,JSONPath=".status.scaledjobs" type TriggerAuthentication struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml index 6d2e5fadfb4..b6e352ce52e 100644 --- a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml +++ b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml @@ -32,9 +32,11 @@ spec: type: string - jsonPath: .status.scaledobjects name: ScaledObjects + priority: 1 type: string - jsonPath: .status.scaledjobs name: ScaledJobs + priority: 1 type: string name: v1alpha1 schema: diff --git a/config/crd/bases/keda.sh_triggerauthentications.yaml b/config/crd/bases/keda.sh_triggerauthentications.yaml index 47fc5e2abb0..04650e8d908 100644 --- a/config/crd/bases/keda.sh_triggerauthentications.yaml +++ b/config/crd/bases/keda.sh_triggerauthentications.yaml @@ -32,9 +32,11 @@ spec: type: string - jsonPath: .status.scaledobjects name: ScaledObjects + priority: 1 type: string - jsonPath: .status.scaledjobs name: ScaledJobs + priority: 1 type: string name: v1alpha1 schema: From e949528485e6ee4e1caa08182571a6badb24619d Mon Sep 17 00:00:00 2001 From: Tom Kerkhove Date: Wed, 21 Jun 2023 16:07:03 +0200 Subject: [PATCH 22/25] Revert Signed-off-by: Tom Kerkhove --- controllers/keda/triggerauthentication_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/keda/triggerauthentication_controller.go b/controllers/keda/triggerauthentication_controller.go index 4d85e4387cd..26265f00232 100755 --- a/controllers/keda/triggerauthentication_controller.go +++ b/controllers/keda/triggerauthentication_controller.go @@ -21,7 +21,7 @@ import ( "sync" corev1 "k8s.io/api/core/v1" - errors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" From e2c1484369886a9c182454ede969fcd3a9ce02f0 Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Fri, 30 Jun 2023 12:11:11 +0800 Subject: [PATCH 23/25] Update Signed-off-by: SpiritZhou --- pkg/status/status.go | 2 ++ tests/helper/helper.go | 1 + 2 files changed, 3 insertions(+) diff --git a/pkg/status/status.go b/pkg/status/status.go index 33127030fc7..ddbb0b14474 100755 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -65,6 +65,7 @@ func UpdateScaledObjectStatus(ctx context.Context, client runtimeclient.StatusCl return TransformObject(ctx, client, logger, scaledObject, status, transform) } +// getTriggerAuth returns TriggerAuthentication/ClusterTriggerAuthentication object and its status from ScaledObjectAuthRef or returns an error. func getTriggerAuth(ctx context.Context, client runtimeclient.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (runtimeclient.Object, *kedav1alpha1.TriggerAuthenticationStatus, error) { if triggerAuthRef == nil { return nil, nil, fmt.Errorf("triggerAuthRef is nil") @@ -88,6 +89,7 @@ func getTriggerAuth(ctx context.Context, client runtimeclient.Client, triggerAut return nil, nil, fmt.Errorf("unknown trigger auth kind %s", triggerAuthRef.Kind) } +// updateTriggerAuthenticationStatus patches TriggerAuthentication/ClusterTriggerAuthentication from ScaledObjectAuthRef with the status that updated by statushanler function or returns an error. func updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, client runtimeclient.Client, namespace string, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) error { triggerAuth, triggerAuthStatus, err := getTriggerAuth(ctx, client, triggerAuthRef, namespace) diff --git a/tests/helper/helper.go b/tests/helper/helper.go index 25cf28fedf0..d01e7d61011 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -797,6 +797,7 @@ func generateCA(t *testing.T) { } } +// CheckKubectlGetResult runs `kubectl get` with paramters and compares output with expected value func CheckKubectlGetResult(t *testing.T, kind string, name string, namespace string, otherparameter string, expected string) { time.Sleep(1 * time.Second) // wait a second for recource deployment finished kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s %s"`, kind, name, namespace, otherparameter) From 532470905d782854ed188e418f9be81107718cbb Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Fri, 30 Jun 2023 12:53:14 +0800 Subject: [PATCH 24/25] Update Signed-off-by: SpiritZhou --- controllers/keda/util/string_lists_test.go | 12 ++++++------ pkg/status/status.go | 8 ++++---- tests/helper/helper.go | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/controllers/keda/util/string_lists_test.go b/controllers/keda/util/string_lists_test.go index 8e5b5dd9835..70dbb51d1bc 100644 --- a/controllers/keda/util/string_lists_test.go +++ b/controllers/keda/util/string_lists_test.go @@ -32,9 +32,9 @@ func TestAppendString(t *testing.T) { {"success", "viewer,editor", "owner", ",", "viewer,editor,owner"}, {"single_success", "viewer", "owner", ",", "viewer,owner"}, {"exist", "viewer,editor,owner", "editor", ",", "viewer,editor,owner"}, - {"no_seperator", "viewer,editor", "owner", "", "viewer,editorowner"}, - {"space_seperator", "viewer,editor", "owner", " ", "viewer,editor owner"}, - {"diff_seperator", "viewer,editor", "owner", ":", "viewer,editor:owner"}, + {"no_separator", "viewer,editor", "owner", "", "viewer,editorowner"}, + {"space_separator", "viewer,editor", "owner", " ", "viewer,editor owner"}, + {"diff_separator", "viewer,editor", "owner", ":", "viewer,editor:owner"}, {"no_from_str", "", "owner", ",", "owner"}, {"no_append_str", "viewer,editor", "", ",", "viewer,editor"}, } @@ -58,9 +58,9 @@ func TestRemoveFromString(t *testing.T) { }{ {"success", "viewer,editor,owner", "owner", ",", "viewer,editor"}, {"no_exist_success", "viewer", "owner", ",", "viewer"}, - {"no_seperator", "viewer,editor,owner", "owner", "", "viewer,editor,owner"}, - {"space_seperator", "viewer editor owner", "editor", " ", "viewer owner"}, - {"diff_seperator", "viewer,editor,owner", "editor", ":", "viewer,editor,owner"}, + {"no_separator", "viewer,editor,owner", "owner", "", "viewer,editor,owner"}, + {"space_separator", "viewer editor owner", "editor", " ", "viewer owner"}, + {"diff_separator", "viewer,editor,owner", "editor", ":", "viewer,editor,owner"}, {"no_from_str", "", "owner", ",", ""}, {"no_delete_str", "viewer,editor", "", ",", "viewer,editor"}, } diff --git a/pkg/status/status.go b/pkg/status/status.go index ddbb0b14474..73a44122141 100755 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -65,8 +65,8 @@ func UpdateScaledObjectStatus(ctx context.Context, client runtimeclient.StatusCl return TransformObject(ctx, client, logger, scaledObject, status, transform) } -// getTriggerAuth returns TriggerAuthentication/ClusterTriggerAuthentication object and its status from ScaledObjectAuthRef or returns an error. -func getTriggerAuth(ctx context.Context, client runtimeclient.Client, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, namespace string) (runtimeclient.Object, *kedav1alpha1.TriggerAuthenticationStatus, error) { +// getTriggerAuth returns TriggerAuthentication/ClusterTriggerAuthentication object and its status from AuthenticationRef or returns an error. +func getTriggerAuth(ctx context.Context, client runtimeclient.Client, triggerAuthRef *kedav1alpha1.AuthenticationRef, namespace string) (runtimeclient.Object, *kedav1alpha1.TriggerAuthenticationStatus, error) { if triggerAuthRef == nil { return nil, nil, fmt.Errorf("triggerAuthRef is nil") } @@ -89,8 +89,8 @@ func getTriggerAuth(ctx context.Context, client runtimeclient.Client, triggerAut return nil, nil, fmt.Errorf("unknown trigger auth kind %s", triggerAuthRef.Kind) } -// updateTriggerAuthenticationStatus patches TriggerAuthentication/ClusterTriggerAuthentication from ScaledObjectAuthRef with the status that updated by statushanler function or returns an error. -func updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, client runtimeclient.Client, namespace string, triggerAuthRef *kedav1alpha1.ScaledObjectAuthRef, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) error { +// updateTriggerAuthenticationStatus patches TriggerAuthentication/ClusterTriggerAuthentication from AuthenticationRef with the status that updated by statushanler function or returns an error. +func updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, client runtimeclient.Client, namespace string, triggerAuthRef *kedav1alpha1.AuthenticationRef, statusHandler func(*kedav1alpha1.TriggerAuthenticationStatus) *kedav1alpha1.TriggerAuthenticationStatus) error { triggerAuth, triggerAuthStatus, err := getTriggerAuth(ctx, client, triggerAuthRef, namespace) if err != nil { diff --git a/tests/helper/helper.go b/tests/helper/helper.go index d01e7d61011..f300caaa624 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -797,7 +797,7 @@ func generateCA(t *testing.T) { } } -// CheckKubectlGetResult runs `kubectl get` with paramters and compares output with expected value +// CheckKubectlGetResult runs `kubectl get` with parameters and compares output with expected value func CheckKubectlGetResult(t *testing.T, kind string, name string, namespace string, otherparameter string, expected string) { time.Sleep(1 * time.Second) // wait a second for recource deployment finished kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s %s"`, kind, name, namespace, otherparameter) From 140a3ccde1f8511f9c4cf9ea7d1a64b1f4ffee7c Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Fri, 30 Jun 2023 13:27:41 +0800 Subject: [PATCH 25/25] Update Signed-off-by: SpiritZhou --- controllers/keda/hpa.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/keda/hpa.go b/controllers/keda/hpa.go index cf73e15448d..be2edeefed3 100644 --- a/controllers/keda/hpa.go +++ b/controllers/keda/hpa.go @@ -32,8 +32,8 @@ import ( kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" kedacontrollerutil "github.com/kedacore/keda/v2/controllers/keda/util" "github.com/kedacore/keda/v2/pkg/scaling/executor" - version "github.com/kedacore/keda/v2/version" kedastatus "github.com/kedacore/keda/v2/pkg/status" + version "github.com/kedacore/keda/v2/version" ) const (