-
Notifications
You must be signed in to change notification settings - Fork 229
NE-1113: Implement podMonitor provisioning with Gateway API #1425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rikatz
wants to merge
1
commit into
openshift:master
Choose a base branch
from
rikatz:ne-1113
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package gatewaypodmonitor | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| logf "github.com/openshift/cluster-ingress-operator/pkg/log" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/manifests" | ||
| operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
| gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| controllerName = "gateway_podmonitor_controller" | ||
| ) | ||
|
|
||
| var log = logf.Logger.WithName(controllerName) | ||
|
|
||
| func NewUnmanaged(mgr manager.Manager) (controller.Controller, error) { | ||
| operatorCache := mgr.GetCache() | ||
| reconciler := &reconciler{ | ||
| client: mgr.GetClient(), | ||
| cache: operatorCache, | ||
| } | ||
| c, err := controller.NewUnmanaged(controllerName, controller.Options{Reconciler: reconciler}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| isOperandNamespace := predicate.NewPredicateFuncs(func(o client.Object) bool { | ||
| return o.GetNamespace() == operatorcontroller.DefaultOperandNamespace | ||
| }) | ||
|
|
||
| if err := c.Watch(source.Kind[client.Object](operatorCache, &gatewayapiv1.Gateway{}, &handler.EnqueueRequestForObject{}, isOperandNamespace)); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| podMonitor := &unstructured.Unstructured{} | ||
| podMonitor.SetGroupVersionKind(schema.GroupVersionKind{ | ||
| Group: "monitoring.coreos.com", | ||
| Kind: "PodMonitor", | ||
| Version: "v1", | ||
| }) | ||
| if err := c.Watch(source.Kind[client.Object](operatorCache, podMonitor, enqueueRequestForOwningGateway(), isOperandNamespace)); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| func enqueueRequestForOwningGateway() handler.EventHandler { | ||
| return handler.EnqueueRequestsFromMapFunc( | ||
| func(ctx context.Context, a client.Object) []reconcile.Request { | ||
| labels := a.GetLabels() | ||
| if gatewayName, ok := labels[manifests.OwningGatewayLabel]; ok { | ||
| log.Info("Queueing gateway", "related object", a.GetNamespace()+"/"+a.GetName()) | ||
| return []reconcile.Request{{NamespacedName: types.NamespacedName{ | ||
| Name: gatewayName, | ||
| Namespace: operatorcontroller.DefaultOperandNamespace, | ||
| }}} | ||
| } | ||
| return []reconcile.Request{} | ||
| }) | ||
| } | ||
|
|
||
| type reconciler struct { | ||
| client client.Client | ||
| cache cache.Cache | ||
| } | ||
|
|
||
| func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
| log.Info("Reconciling gateway", "request", request) | ||
|
|
||
| gateway := gatewayapiv1.Gateway{} | ||
| if err := r.cache.Get(ctx, request.NamespacedName, &gateway); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return reconcile.Result{}, nil | ||
| } | ||
| return reconcile.Result{}, err | ||
| } | ||
|
|
||
| if _, _, err := r.ensureGatewayPodMonitor(ctx, &gateway); err != nil { | ||
| return reconcile.Result{}, err | ||
| } | ||
|
|
||
| return reconcile.Result{}, nil | ||
| } | ||
150 changes: 150 additions & 0 deletions
150
pkg/operator/controller/gateway-podmonitor/podmonitor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package gatewaypodmonitor | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/manifests" | ||
| operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
| ) | ||
|
|
||
| func (r *reconciler) ensureGatewayPodMonitor(ctx context.Context, gateway *gatewayapiv1.Gateway) (bool, *unstructured.Unstructured, error) { | ||
| name := operatorcontroller.GatewayPodMonitorName(gateway) | ||
| have, current, err := r.currentGatewayPodMonitor(ctx, name) | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
|
|
||
| ownerRef := metav1.OwnerReference{ | ||
| APIVersion: gatewayapiv1.GroupVersion.String(), | ||
| Kind: "Gateway", | ||
| Name: gateway.Name, | ||
| UID: gateway.UID, | ||
| } | ||
| desired := desiredGatewayPodMonitor(name, ownerRef) | ||
|
|
||
| switch { | ||
| case !have: | ||
| if err := r.client.Create(ctx, desired); err != nil { | ||
| if errors.IsAlreadyExists(err) { | ||
| return r.currentGatewayPodMonitor(ctx, name) | ||
| } | ||
| return false, nil, fmt.Errorf("failed to create gateway podmonitor: %w", err) | ||
| } | ||
| log.Info("Created gateway podmonitor", "namespace", desired.GetNamespace(), "name", desired.GetName()) | ||
| return r.currentGatewayPodMonitor(ctx, name) | ||
| default: | ||
| if updated, err := r.updateGatewayPodMonitor(ctx, current, desired); err != nil { | ||
| return true, current, fmt.Errorf("failed to update gateway podmonitor: %w", err) | ||
| } else if updated { | ||
| return r.currentGatewayPodMonitor(ctx, name) | ||
| } | ||
| } | ||
|
|
||
| return true, current, err | ||
| } | ||
|
|
||
| func desiredGatewayPodMonitor(name types.NamespacedName, ownerRef metav1.OwnerReference) *unstructured.Unstructured { | ||
| // Use []interface{} for all slice fields to avoid DeepCopy failures | ||
| // and comparison issues with API-returned objects. | ||
| pm := &unstructured.Unstructured{ | ||
| Object: map[string]interface{}{ | ||
| "metadata": map[string]interface{}{ | ||
| "namespace": name.Namespace, | ||
| "name": name.Name, | ||
| "labels": map[string]interface{}{ | ||
| manifests.OwningGatewayLabel: ownerRef.Name, | ||
| }, | ||
| }, | ||
| "spec": map[string]interface{}{ | ||
| "selector": map[string]interface{}{ | ||
| "matchLabels": map[string]interface{}{ | ||
| operatorcontroller.GatewayNameLabelKey: ownerRef.Name, | ||
| }, | ||
| }, | ||
| "podMetricsEndpoints": []interface{}{ | ||
| map[string]interface{}{ | ||
| "path": "/stats/prometheus", | ||
| "interval": "60s", | ||
| "port": "http-envoy-prom", | ||
| "metricRelabelings": []interface{}{ | ||
| map[string]interface{}{ | ||
| "action": "keep", | ||
| "sourceLabels": []interface{}{"__name__"}, | ||
| "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", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| pm.SetGroupVersionKind(schema.GroupVersionKind{ | ||
| Group: "monitoring.coreos.com", | ||
| Kind: "PodMonitor", | ||
| Version: "v1", | ||
| }) | ||
| pm.SetOwnerReferences([]metav1.OwnerReference{ownerRef}) | ||
| return pm | ||
| } | ||
|
|
||
| func (r *reconciler) currentGatewayPodMonitor(ctx context.Context, name types.NamespacedName) (bool, *unstructured.Unstructured, error) { | ||
| pm := &unstructured.Unstructured{} | ||
| pm.SetGroupVersionKind(schema.GroupVersionKind{ | ||
| Group: "monitoring.coreos.com", | ||
| Kind: "PodMonitor", | ||
| Version: "v1", | ||
| }) | ||
| if err := r.client.Get(ctx, name, pm); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return false, nil, nil | ||
| } | ||
| return false, nil, err | ||
| } | ||
| return true, pm, nil | ||
| } | ||
|
|
||
| func (r *reconciler) updateGatewayPodMonitor(ctx context.Context, current, desired *unstructured.Unstructured) (bool, error) { | ||
| changed, updated := gatewayPodMonitorChanged(current, desired) | ||
| if !changed { | ||
| return false, nil | ||
| } | ||
| diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) | ||
| if err := r.client.Update(ctx, updated); err != nil { | ||
| return false, err | ||
| } | ||
| log.Info("Updated gateway podmonitor", "namespace", updated.GetNamespace(), "name", updated.GetName(), "diff", diff) | ||
| return true, nil | ||
| } | ||
|
|
||
| func gatewayPodMonitorChanged(current, desired *unstructured.Unstructured) (bool, *unstructured.Unstructured) { | ||
| changed := false | ||
| updated := current.DeepCopy() | ||
|
|
||
| if !cmp.Equal(current.Object["spec"], desired.Object["spec"], cmpopts.EquateEmpty()) { | ||
| changed = true | ||
| updated.Object["spec"] = desired.Object["spec"] | ||
| } | ||
| if !cmp.Equal(current.GetLabels(), desired.GetLabels(), cmpopts.EquateEmpty()) { | ||
| changed = true | ||
| updated.SetLabels(desired.GetLabels()) | ||
| } | ||
| if !cmp.Equal(current.GetOwnerReferences(), desired.GetOwnerReferences(), cmpopts.EquateEmpty()) { | ||
| changed = true | ||
| updated.SetOwnerReferences(desired.GetOwnerReferences()) | ||
| } | ||
|
|
||
| if !changed { | ||
| return false, nil | ||
| } | ||
| return true, updated | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.