Skip to content

Commit

Permalink
Add info in kubectl get triggerauthentication (#4668)
Browse files Browse the repository at this point in the history
Signed-off-by: spiritzhou <iammrzhouzhenghan@gmail.com>
Signed-off-by: SpiritZhou <iammrzhouzhenghan@gmail.com>
Signed-off-by: Tom Kerkhove <kerkhove.tom@gmail.com>
Co-authored-by: Tom Kerkhove <kerkhove.tom@gmail.com>
  • Loading branch information
SpiritZhou and tomkerkhove authored Jul 3, 2023
1 parent 9d51ac8 commit 9632275
Show file tree
Hide file tree
Showing 23 changed files with 863 additions and 114 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

### Improvements

- TODO ([#XXX](https://github.com/kedacore/keda/issue/XXX))
- **General:**: Add ScaledObject/ScaledJob names to output of `kubectl get triggerauthentication/clustertriggerauthentication` ([#796](https://github.com/kedacore/keda/issues/796))

### Fixes

Expand Down
20 changes: 18 additions & 2 deletions apis/keda/v1alpha1/triggerauthentication_types.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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",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"`

Spec TriggerAuthenticationSpec `json:"spec"`
Spec TriggerAuthenticationSpec `json:"spec"`
Status TriggerAuthenticationStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -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",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"`

Spec TriggerAuthenticationSpec `json:"spec"`
Spec TriggerAuthenticationSpec `json:"spec"`
Status TriggerAuthenticationStatus `json:"status,omitempty"`
}

// TriggerAuthenticationSpec defines the various ways to authenticate
Expand All @@ -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:"scaledobjects,omitempty"`
// +optional
ScaledJobNamesStr string `json:"scaledjobs,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// TriggerAuthenticationList contains a list of TriggerAuthentication
Expand Down
17 changes: 17 additions & 0 deletions apis/keda/v1alpha1/zz_generated.deepcopy.go
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion config/crd/bases/keda.sh_clustertriggerauthentications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ spec:
- jsonPath: .spec.hashiCorpVault.address
name: VaultAddress
type: string
- jsonPath: .status.scaledobjects
name: ScaledObjects
priority: 1
type: string
- jsonPath: .status.scaledjobs
name: ScaledJobs
priority: 1
type: string
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -222,9 +230,19 @@ spec:
type: object
type: array
type: object
status:
description: TriggerAuthenticationStatus defines the observed state of
TriggerAuthentication
properties:
scaledjobs:
type: string
scaledobjects:
type: string
type: object
required:
- spec
type: object
served: true
storage: true
subresources: {}
subresources:
status: {}
20 changes: 19 additions & 1 deletion config/crd/bases/keda.sh_triggerauthentications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ spec:
- jsonPath: .spec.hashiCorpVault.address
name: VaultAddress
type: string
- jsonPath: .status.scaledobjects
name: ScaledObjects
priority: 1
type: string
- jsonPath: .status.scaledjobs
name: ScaledJobs
priority: 1
type: string
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -221,9 +229,19 @@ spec:
type: object
type: array
type: object
status:
description: TriggerAuthenticationStatus defines the observed state of
TriggerAuthentication
properties:
scaledjobs:
type: string
scaledobjects:
type: string
type: object
required:
- spec
type: object
served: true
storage: true
subresources: {}
subresources:
status: {}
6 changes: 3 additions & 3 deletions controllers/keda/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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/scaling/executor"
kedautil "github.com/kedacore/keda/v2/pkg/util"
kedastatus "github.com/kedacore/keda/v2/pkg/status"
version "github.com/kedacore/keda/v2/version"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -246,7 +246,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
Expand Down
25 changes: 22 additions & 3 deletions controllers/keda/scaledjob_controller.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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="*"
Expand Down Expand Up @@ -129,7 +129,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
}
}
Expand Down Expand Up @@ -157,9 +157,14 @@ 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
}

if _, err := r.updateTriggerAuthenticationStatus(ctx, reqLogger, scaledJob); err != nil {
reqLogger.Error(err, "Error updating TriggerAuthentication Status")
}

return ctrl.Result{}, err
}

Expand Down Expand Up @@ -351,3 +356,17 @@ func (r *ScaledJobReconciler) updatePromMetricsOnDelete(namespacedName string) {

delete(scaledJobPromMetricsMap, namespacedName)
}

func (r *ScaledJobReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) (string, error) {
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 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
})
}
3 changes: 3 additions & 0 deletions controllers/keda/scaledjob_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (r *ScaledJobReconciler) finalizeScaledJob(ctx context.Context, logger logr
return err
}

if _, err := r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledJob); err != nil {
logger.Error(err, "Failed to update TriggerAuthentication Status after removing a finalizer")
}
r.updatePromMetricsOnDelete(namespacedName)
}

Expand Down
28 changes: 24 additions & 4 deletions controllers/keda/scaledobject_controller.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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="*"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -189,10 +189,14 @@ 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
}

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
}

Expand Down Expand Up @@ -354,7 +358,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)
Expand Down Expand Up @@ -552,3 +556,19 @@ func (r *ScaledObjectReconciler) updatePromMetricsOnDelete(namespacedName string

delete(scaledObjectPromMetricsMap, namespacedName)
}

func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatus(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) {
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
})
}

func (r *ScaledObjectReconciler) updateTriggerAuthenticationStatusOnDelete(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) {
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
})
}
3 changes: 3 additions & 0 deletions controllers/keda/scaledobject_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (r *ScaledObjectReconciler) finalizeScaledObject(ctx context.Context, logge
return err
}

if _, err := r.updateTriggerAuthenticationStatusOnDelete(ctx, logger, scaledObject); err != nil {
logger.Error(err, "Failed to update TriggerAuthentication Status after removing a finalizer")
}
r.updatePromMetricsOnDelete(namespacedName)
}

Expand Down
Empty file modified controllers/keda/triggerauthentication_controller.go
100644 → 100755
Empty file.
45 changes: 45 additions & 0 deletions controllers/keda/util/string_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,3 +39,44 @@ func Remove(list []string, s string) []string {
}
return list
}

// 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)
}

if !Contains(splitStrings, appendStr) {
splitStrings = append(splitStrings, appendStr)
srcStr = strings.Join(splitStrings, sep)
}
return srcStr
}

// 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
}

splitStrings := []string{}
if srcStr != "" {
splitStrings = strings.Split(srcStr, sep)
}

splitStrings = Remove(splitStrings, str)
srcStr = strings.Join(splitStrings, sep)
return srcStr
}
Loading

0 comments on commit 9632275

Please sign in to comment.