@@ -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
0 commit comments