Skip to content

Commit f6f5707

Browse files
Merge pull request #1024 from austincunningham/THREESCALE-11344-2.15
Threescale 11344 2.15 create an generic pod monitor mutator to apply changes to the update
2 parents c003b31 + 9262791 commit f6f5707

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

pkg/3scale/amp/operator/system_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (r *SystemReconciler) Reconcile() (reconcile.Result, error) {
288288
return reconcile.Result{}, err
289289
}
290290

291-
err = r.ReconcilePodMonitor(system.SystemAppPodMonitor(), reconcilers.CreateOnlyMutator)
291+
err = r.ReconcilePodMonitor(system.SystemAppPodMonitor(), reconcilers.GenericPodMonitorMutator)
292292
if err != nil {
293293
return reconcile.Result{}, err
294294
}

pkg/reconcilers/pod_monitors.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package reconcilers
2+
3+
import (
4+
"fmt"
5+
"github.com/3scale/3scale-operator/pkg/common"
6+
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
7+
"reflect"
8+
)
9+
10+
func GenericPodMonitorMutator(existingObj, desiredObj common.KubernetesObject) (bool, error) {
11+
existing, ok := existingObj.(*monitoringv1.PodMonitor)
12+
if !ok {
13+
return false, fmt.Errorf("%T is not a *monitoringv1.PodMonitor", existingObj)
14+
}
15+
desired, ok := desiredObj.(*monitoringv1.PodMonitor)
16+
if !ok {
17+
return false, fmt.Errorf("%T is not a *monitoringv1.PodMonitor", desiredObj)
18+
}
19+
20+
updated := false
21+
if !reflect.DeepEqual(desired.Spec, existing.Spec) {
22+
existing.Spec = desired.Spec
23+
updated = true
24+
}
25+
26+
return updated, nil
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package reconcilers
2+
3+
import (
4+
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
"testing"
7+
)
8+
9+
func podMonitoringTestFactory(port string) *monitoringv1.PodMonitor {
10+
return &monitoringv1.PodMonitor{
11+
ObjectMeta: metav1.ObjectMeta{
12+
Name: "test-app",
13+
},
14+
Spec: monitoringv1.PodMonitorSpec{
15+
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
16+
{
17+
Port: port,
18+
Path: "/metrics",
19+
Scheme: "http",
20+
},
21+
},
22+
},
23+
}
24+
}
25+
func TestGenericPodMonitorMutator(t *testing.T) {
26+
var existingPort string = "8080"
27+
var desiredPort string = "9090"
28+
29+
existing := podMonitoringTestFactory(existingPort)
30+
desired := podMonitoringTestFactory(desiredPort)
31+
32+
update, err := GenericPodMonitorMutator(existing, desired)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
if !update {
37+
t.Fatal("when defaults can be applied, reconciler reported no update needed")
38+
}
39+
40+
if existing.Spec.PodMetricsEndpoints[0].Port != desiredPort {
41+
t.Fatalf("PodMonitor not reconciled. Expected: %s, got: %s", desiredPort, existing.Spec.PodMetricsEndpoints[0].Port)
42+
}
43+
}

0 commit comments

Comments
 (0)