Skip to content

Commit

Permalink
Raise events on scaling activity (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dee-kryvenko authored Feb 13, 2025
1 parent b9150b7 commit 31af81f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
APIReader: mgr.GetAPIReader(),
Recorder: mgr.GetEventRecorderFor("argocd-autoscaler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ReplicaSetScaler")
os.Exit(1)
Expand Down
7 changes: 7 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ kind: Role
metadata:
name: argocd-autoscaler
rules:
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- ""
resources:
Expand Down
17 changes: 17 additions & 0 deletions internal/controller/autoscaler/replicasetscaler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -66,6 +67,7 @@ type ReplicaSetScalerReconciler struct {
client.Client
Scheme *runtime.Scheme
APIReader client.Reader
Recorder record.EventRecorder

lastReconciled sync.Map
}
Expand Down Expand Up @@ -97,6 +99,7 @@ func init() {
// +kubebuilder:rbac:namespace=argocd-autoscaler,groups=autoscaler.argoproj.io,resources=replicasetscalers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:namespace=argocd-autoscaler,groups=autoscaler.argoproj.io,resources=replicasetscalers/status,verbs=get;update;patch
// +kubebuilder:rbac:namespace=argocd-autoscaler,groups=autoscaler.argoproj.io,resources=replicasetscalers/finalizers,verbs=update
// +kubebuilder:rbac:namespace=argocd-autoscaler,groups=core,resources=events,verbs=create;patch

// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.0/pkg/reconcile
Expand Down Expand Up @@ -342,6 +345,13 @@ func (r *ReplicaSetScalerReconciler) Reconcile(ctx context.Context, req ctrl.Req
}
log.Info("RS scaled to 0",
"ref", scaler.Spec.ReplicaSetControllerRef)
if r.Recorder != nil {
r.Recorder.Event(
r.GetRSObject(&replicaSetController),
corev1.EventTypeNormal,
"ScaledToZero", "Scaled to 0 replicas",
)
}
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
}

Expand Down Expand Up @@ -448,6 +458,13 @@ func (r *ReplicaSetScalerReconciler) Reconcile(ctx context.Context, req ctrl.Req
"desiredReplicas", desiredReplicas,
"actualReplicas", actualReplicas,
"restart", restart)
if r.Recorder != nil {
r.Recorder.Event(
r.GetRSObject(&replicaSetController),
corev1.EventTypeNormal,
"Scaled", fmt.Sprintf("Scaled from %d to %d replicas", actualReplicas, desiredReplicas),
)
}
replicaSetScalerChangesTotalCounter.WithLabelValues(
req.NamespacedName.String(),
scaler.Spec.ReplicaSetControllerRef.Kind,
Expand Down

0 comments on commit 31af81f

Please sign in to comment.