|
| 1 | +package gatewaypodmonitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + "github.com/openshift/cluster-ingress-operator/pkg/manifests" |
| 10 | + operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" |
| 11 | + |
| 12 | + "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 15 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 16 | + "k8s.io/apimachinery/pkg/types" |
| 17 | + gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 18 | +) |
| 19 | + |
| 20 | +func (r *reconciler) ensureGatewayPodMonitor(ctx context.Context, gateway *gatewayapiv1.Gateway) (bool, *unstructured.Unstructured, error) { |
| 21 | + name := operatorcontroller.GatewayPodMonitorName(gateway) |
| 22 | + have, current, err := r.currentGatewayPodMonitor(ctx, name) |
| 23 | + if err != nil { |
| 24 | + return false, nil, err |
| 25 | + } |
| 26 | + |
| 27 | + ownerRef := metav1.OwnerReference{ |
| 28 | + APIVersion: gatewayapiv1.GroupVersion.String(), |
| 29 | + Kind: "Gateway", |
| 30 | + Name: gateway.Name, |
| 31 | + UID: gateway.UID, |
| 32 | + } |
| 33 | + desired := desiredGatewayPodMonitor(name, ownerRef) |
| 34 | + |
| 35 | + switch { |
| 36 | + case !have: |
| 37 | + if err := r.client.Create(ctx, desired); err != nil { |
| 38 | + if errors.IsAlreadyExists(err) { |
| 39 | + return r.currentGatewayPodMonitor(ctx, name) |
| 40 | + } |
| 41 | + return false, nil, fmt.Errorf("failed to create gateway podmonitor: %w", err) |
| 42 | + } |
| 43 | + log.Info("Created gateway podmonitor", "namespace", desired.GetNamespace(), "name", desired.GetName()) |
| 44 | + return r.currentGatewayPodMonitor(ctx, name) |
| 45 | + default: |
| 46 | + if updated, err := r.updateGatewayPodMonitor(ctx, current, desired); err != nil { |
| 47 | + return true, current, fmt.Errorf("failed to update gateway podmonitor: %w", err) |
| 48 | + } else if updated { |
| 49 | + return r.currentGatewayPodMonitor(ctx, name) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return true, current, err |
| 54 | +} |
| 55 | + |
| 56 | +func desiredGatewayPodMonitor(name types.NamespacedName, ownerRef metav1.OwnerReference) *unstructured.Unstructured { |
| 57 | + // Use []interface{} for all slice fields to avoid DeepCopy failures |
| 58 | + // and comparison issues with API-returned objects. |
| 59 | + pm := &unstructured.Unstructured{ |
| 60 | + Object: map[string]interface{}{ |
| 61 | + "metadata": map[string]interface{}{ |
| 62 | + "namespace": name.Namespace, |
| 63 | + "name": name.Name, |
| 64 | + "labels": map[string]interface{}{ |
| 65 | + manifests.OwningGatewayLabel: ownerRef.Name, |
| 66 | + }, |
| 67 | + }, |
| 68 | + "spec": map[string]interface{}{ |
| 69 | + "selector": map[string]interface{}{ |
| 70 | + "matchLabels": map[string]interface{}{ |
| 71 | + operatorcontroller.GatewayNameLabelKey: ownerRef.Name, |
| 72 | + }, |
| 73 | + }, |
| 74 | + "podMetricsEndpoints": []interface{}{ |
| 75 | + map[string]interface{}{ |
| 76 | + "path": "/stats/prometheus", |
| 77 | + "interval": "60s", |
| 78 | + "port": "http-envoy-prom", |
| 79 | + "metricRelabelings": []interface{}{ |
| 80 | + map[string]interface{}{ |
| 81 | + "action": "keep", |
| 82 | + "sourceLabels": []interface{}{"__name__"}, |
| 83 | + "regex": "istio_.*|envoy_cluster_upstream_cx_active|envoy_cluster_upstream_cx_total|envoy_cluster_upstream_rq_total|envoy_listener_downstream_cx_active|envoy_listener_http_downstream_rq|envoy_server_memory_allocated|envoy_server_memory_heap_size|envoy_server_uptime", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + pm.SetGroupVersionKind(schema.GroupVersionKind{ |
| 92 | + Group: "monitoring.coreos.com", |
| 93 | + Kind: "PodMonitor", |
| 94 | + Version: "v1", |
| 95 | + }) |
| 96 | + pm.SetOwnerReferences([]metav1.OwnerReference{ownerRef}) |
| 97 | + return pm |
| 98 | +} |
| 99 | + |
| 100 | +func (r *reconciler) currentGatewayPodMonitor(ctx context.Context, name types.NamespacedName) (bool, *unstructured.Unstructured, error) { |
| 101 | + pm := &unstructured.Unstructured{} |
| 102 | + pm.SetGroupVersionKind(schema.GroupVersionKind{ |
| 103 | + Group: "monitoring.coreos.com", |
| 104 | + Kind: "PodMonitor", |
| 105 | + Version: "v1", |
| 106 | + }) |
| 107 | + if err := r.client.Get(ctx, name, pm); err != nil { |
| 108 | + if errors.IsNotFound(err) { |
| 109 | + return false, nil, nil |
| 110 | + } |
| 111 | + return false, nil, err |
| 112 | + } |
| 113 | + return true, pm, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (r *reconciler) updateGatewayPodMonitor(ctx context.Context, current, desired *unstructured.Unstructured) (bool, error) { |
| 117 | + changed, updated := gatewayPodMonitorChanged(current, desired) |
| 118 | + if !changed { |
| 119 | + return false, nil |
| 120 | + } |
| 121 | + diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) |
| 122 | + if err := r.client.Update(ctx, updated); err != nil { |
| 123 | + return false, err |
| 124 | + } |
| 125 | + log.Info("Updated gateway podmonitor", "namespace", updated.GetNamespace(), "name", updated.GetName(), "diff", diff) |
| 126 | + return true, nil |
| 127 | +} |
| 128 | + |
| 129 | +func gatewayPodMonitorChanged(current, desired *unstructured.Unstructured) (bool, *unstructured.Unstructured) { |
| 130 | + changed := false |
| 131 | + updated := current.DeepCopy() |
| 132 | + |
| 133 | + if !cmp.Equal(current.Object["spec"], desired.Object["spec"], cmpopts.EquateEmpty()) { |
| 134 | + changed = true |
| 135 | + updated.Object["spec"] = desired.Object["spec"] |
| 136 | + } |
| 137 | + if !cmp.Equal(current.GetLabels(), desired.GetLabels(), cmpopts.EquateEmpty()) { |
| 138 | + changed = true |
| 139 | + updated.SetLabels(desired.GetLabels()) |
| 140 | + } |
| 141 | + if !cmp.Equal(current.GetOwnerReferences(), desired.GetOwnerReferences(), cmpopts.EquateEmpty()) { |
| 142 | + changed = true |
| 143 | + updated.SetOwnerReferences(desired.GetOwnerReferences()) |
| 144 | + } |
| 145 | + |
| 146 | + if !changed { |
| 147 | + return false, nil |
| 148 | + } |
| 149 | + return true, updated |
| 150 | +} |
0 commit comments