Skip to content

Commit

Permalink
Log information about StatefulSets as they are created, updated and d…
Browse files Browse the repository at this point in the history
…eleted (#182)

* Update test app StatefulSet to match expected configuration of StatefulSets used with rollout-operator

* Log information about StatefulSets as they are created, updated and deleted

* Add changelog entry
  • Loading branch information
charleskorn authored Nov 29, 2024
1 parent e74c10f commit 499f385
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## main / unreleased

* [ENHANCEMENT] Log debug information about StatefulSets as they are created, updated and deleted. #182

## v0.20.1

* [BUGFIX] Improved handling of URL ports in `createPrepareDownscaleEndpoints` function. The function now correctly preserves the port when replacing the host in the URL. #176
Expand Down
3 changes: 3 additions & 0 deletions development/test-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ metadata:
name: test-app
labels:
grafana.com/prepare-downscale: "true"
rollout-group: test-app
annotations:
grafana.com/prepare-downscale-http-path: "/"
grafana.com/prepare-downscale-http-port: "80"
Expand All @@ -36,3 +37,5 @@ spec:
containers:
- name: app
image: nginx:latest
updateStrategy:
type: OnDelete
56 changes: 50 additions & 6 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,9 @@ func (c *RolloutController) Init() error {
// We enqueue a reconcile request each time any of the observed StatefulSets are updated. The UpdateFunc
// is also called every sync period even if no changes occurred.
_, err := c.statefulSetsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c.enqueueReconcile()
},
UpdateFunc: func(old, new interface{}) {
c.enqueueReconcile()
},
AddFunc: c.onAdded,
UpdateFunc: c.onUpdated,
DeleteFunc: c.onDeleted,
})
if err != nil {
return err
Expand Down Expand Up @@ -172,6 +169,53 @@ func (c *RolloutController) Init() error {
return nil
}

func (c *RolloutController) onAdded(obj interface{}) {
sts, isStatefulSet := obj.(*v1.StatefulSet)
if isStatefulSet {
level.Debug(c.logger).Log(
"msg", "observed StatefulSet added",
"name", sts.Name,
"namespace", sts.Namespace,
"replicas", sts.Spec.Replicas,
"generation", sts.Generation,
"creation_timestamp", sts.CreationTimestamp,
)
}

c.enqueueReconcile()
}

func (c *RolloutController) onUpdated(old, new interface{}) {
oldSts, oldIsStatefulSet := old.(*v1.StatefulSet)
newSts, newIsStatefulSet := new.(*v1.StatefulSet)
if oldIsStatefulSet && newIsStatefulSet && oldSts.Generation != newSts.Generation {
level.Debug(c.logger).Log(
"msg", "observed StatefulSet updated",
"name", oldSts.Name,
"namespace", oldSts.Namespace,
"old_replicas", oldSts.Spec.Replicas,
"new_replicas", newSts.Spec.Replicas,
"old_generation", oldSts.Generation,
"new_generation", newSts.Generation,
)
}

c.enqueueReconcile()
}

func (c *RolloutController) onDeleted(obj interface{}) {
sts, isStatefulSet := obj.(*v1.StatefulSet)
if isStatefulSet {
level.Debug(c.logger).Log(
"msg", "observed StatefulSet deleted",
"name", sts.Name,
"namespace", sts.Namespace,
"replicas", sts.Spec.Replicas,
"generation", sts.Generation,
)
}
}

// Run runs the controller and blocks until Stop() is called.
func (c *RolloutController) Run() {
ctx := context.Background()
Expand Down

0 comments on commit 499f385

Please sign in to comment.