Skip to content

Commit f020839

Browse files
committed
Patch the Prometheus pod to include the NAD
1 parent a994cc8 commit f020839

File tree

7 files changed

+134
-15
lines changed

7 files changed

+134
-15
lines changed

api/bases/telemetry.openstack.org_metricstorages.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,11 +1368,10 @@ spec:
13681368
type: object
13691369
type: array
13701370
networkAttachments:
1371-
description: Networks in addition to the cluster network, the service
1372-
is attached to
1373-
items:
1371+
additionalProperties:
13741372
type: string
1375-
type: array
1373+
description: NetworkAttachments status of the Prometheus pods
1374+
type: object
13761375
observedGeneration:
13771376
description: |-
13781377
ObservedGeneration - the most recent generation observed for this

api/v1beta1/conditions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ const (
172172
// PrometheusUnableToRemoveTLSMessage
173173
PrometheusUnableToRemoveTLSMessage = "Error occured when trying to remove TLS config: %s"
174174

175+
// PrometheusUnableToRemoveNADMessage
176+
PrometheusUnableToRemoveNADMessage = "Error occured when trying to remove NAD config: %s"
177+
175178
//
176179
// LoggingReady condition messages
177180
//

api/v1beta1/metricstorage_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type MetricStorageSpec struct {
9292

9393
// NetworkAttachments is a list of NetworkAttachment resource names to expose the services to the given network
9494
// +kubebuilder:validation:Optional
95-
NetworkAttachments []string `json:"networkAttachments"`
95+
NetworkAttachments []string `json:"networkAttachments,omitempty"`
9696

9797
// MonitoringStack allows to define a metric storage with
9898
// options supported by Red Hat
@@ -131,8 +131,8 @@ type MetricStorageStatus struct {
131131
// then the controller has not processed the latest changes injected by
132132
// the openstack-operator in the top-level CR (e.g. the ContainerImage)
133133
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
134-
// Networks in addition to the cluster network, the service is attached to
135-
NetworkAttachments []string `json:"networkAttachments,omitempty"`
134+
// NetworkAttachments status of the Prometheus pods
135+
NetworkAttachments map[string]string `json:"networkAttachments,omitempty"`
136136
}
137137

138138
//+kubebuilder:object:root=true

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/telemetry.openstack.org_metricstorages.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,11 +1368,10 @@ spec:
13681368
type: object
13691369
type: array
13701370
networkAttachments:
1371-
description: Networks in addition to the cluster network, the service
1372-
is attached to
1373-
items:
1371+
additionalProperties:
13741372
type: string
1375-
type: array
1373+
description: NetworkAttachments status of the Prometheus pods
1374+
type: object
13761375
observedGeneration:
13771376
description: |-
13781377
ObservedGeneration - the most recent generation observed for this

controllers/metricstorage_controller.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import (
5858
object "github.com/openstack-k8s-operators/lib-common/modules/common/object"
5959
tls "github.com/openstack-k8s-operators/lib-common/modules/common/tls"
6060

61+
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
6162
infranetworkv1 "github.com/openstack-k8s-operators/infra-operator/apis/network/v1beta1"
6263
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
6364
availability "github.com/openstack-k8s-operators/telemetry-operator/pkg/availability"
@@ -506,9 +507,15 @@ func (r *MetricStorageReconciler) reconcileNormal(
506507
// all cert input checks out so report InputReady
507508
instance.Status.Conditions.MarkTrue(condition.TLSInputReadyCondition, condition.InputReadyMessage)
508509

509-
// Networks to attach to
510+
//
511+
// NAD
512+
//
513+
514+
// Get networks to attach to the Prometheus pod
515+
nadList := []networkv1.NetworkAttachmentDefinition{}
516+
510517
for _, netAtt := range instance.Spec.NetworkAttachments {
511-
_, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
518+
nad, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
512519
if err != nil {
513520
if k8s_errors.IsNotFound(err) {
514521
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -527,6 +534,68 @@ func (r *MetricStorageReconciler) reconcileNormal(
527534
err.Error()))
528535
return ctrl.Result{}, err
529536
}
537+
if nad != nil {
538+
nadList = append(nadList, *nad)
539+
}
540+
}
541+
542+
networkAnnotations, err := nad.EnsureNetworksAnnotation(nadList)
543+
544+
if err != nil {
545+
err = fmt.Errorf("failed create network annotation from %s: %w", instance.Spec.NetworkAttachments, err)
546+
instance.Status.Conditions.MarkFalse(
547+
condition.NetworkAttachmentsReadyCondition,
548+
condition.ErrorReason,
549+
condition.SeverityWarning,
550+
condition.NetworkAttachmentsReadyErrorMessage,
551+
err)
552+
return ctrl.Result{}, err
553+
}
554+
555+
// Set NAD annotation to the Prometheus pod
556+
if len(instance.Spec.NetworkAttachments) != 0 {
557+
// Patch Prometheus to add the NAD annotation
558+
prometheusWatchFn := func(_ context.Context, o client.Object) []reconcile.Request {
559+
name := client.ObjectKey{
560+
Namespace: o.GetNamespace(),
561+
Name: o.GetName(),
562+
}
563+
return []reconcile.Request{{NamespacedName: name}}
564+
}
565+
err = r.ensureWatches(ctx, "prometheuses.monitoring.rhobs", &monv1.Prometheus{}, handler.EnqueueRequestsFromMapFunc(prometheusWatchFn))
566+
if err != nil {
567+
instance.Status.Conditions.MarkFalse(telemetryv1.PrometheusReadyCondition,
568+
condition.Reason("Can't watch prometheus resource. The Cluster Observability Operator probably isn't installed"),
569+
condition.SeverityError,
570+
telemetryv1.PrometheusUnableToWatchMessage, err)
571+
Log.Info("Can't watch Prometheus resource. The Cluster Observability Operator probably isn't installed")
572+
return ctrl.Result{RequeueAfter: telemetryv1.PauseBetweenWatchAttempts}, nil
573+
}
574+
prometheusNADPatch := metricstorage.PrometheusNAD(instance)
575+
err = r.Client.Patch(context.Background(), &prometheusNADPatch, client.Apply, client.FieldOwner("telemetry-operator"))
576+
if err != nil {
577+
Log.Error(err, "Can't patch Prometheus resource")
578+
return ctrl.Result{}, err
579+
}
580+
instance.Status.NetworkAttachments = networkAnnotations
581+
} else if len(instance.Spec.NetworkAttachments) == 0 {
582+
// Delete the prometheus CR, so it can be automatically restored without the NAD patch
583+
prometheus := monv1.Prometheus{
584+
ObjectMeta: metav1.ObjectMeta{
585+
Namespace: instance.Namespace,
586+
Name: instance.Name,
587+
},
588+
}
589+
err = r.Client.Delete(context.Background(), &prometheus)
590+
if err != nil && !k8s_errors.IsNotFound(err) {
591+
instance.Status.Conditions.MarkFalse(telemetryv1.PrometheusReadyCondition,
592+
condition.Reason("Can't delete old Prometheus CR to remove NAD configuration"),
593+
condition.SeverityError,
594+
telemetryv1.PrometheusUnableToRemoveNADMessage, err)
595+
Log.Error(err, "Can't delete old Prometheus CR to remove NAD configuration")
596+
return ctrl.Result{}, err
597+
}
598+
instance.Status.NetworkAttachments = nil
530599
}
531600

532601
// when job passed, mark NetworkAttachmentsReadyCondition ready
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2025.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metricstorage
18+
19+
import (
20+
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
21+
monv1 "github.com/rhobs/obo-prometheus-operator/pkg/apis/monitoring/v1"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
)
24+
25+
// PrometheusNAD defines patch for prometheus CR to add NAD annotations
26+
func PrometheusNAD(
27+
instance *telemetryv1.MetricStorage,
28+
) monv1.Prometheus {
29+
prom := monv1.Prometheus{
30+
TypeMeta: metav1.TypeMeta{
31+
Kind: "Prometheus",
32+
APIVersion: "monitoring.rhobs/v1",
33+
},
34+
ObjectMeta: metav1.ObjectMeta{
35+
Name: instance.Name,
36+
Namespace: instance.Namespace,
37+
},
38+
Spec: monv1.PrometheusSpec{
39+
CommonPrometheusFields: monv1.CommonPrometheusFields{
40+
PodMetadata: &monv1.EmbeddedObjectMetadata{
41+
Annotations: instance.Annotations,
42+
},
43+
},
44+
},
45+
}
46+
return prom
47+
}

0 commit comments

Comments
 (0)