@@ -12,8 +12,11 @@ import (
1212 "crypto/sha256"
1313
1414 "github.com/go-logr/logr"
15+ networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
16+ "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
1517 "github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
1618 "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
19+ nad "github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment"
1720 "github.com/openstack-k8s-operators/lib-common/modules/common/pvc"
1821 "github.com/openstack-k8s-operators/lib-common/modules/common/util"
1922 "gopkg.in/yaml.v3"
@@ -548,6 +551,101 @@ func GetCommonRbacRules(privileged bool) []rbacv1.PolicyRule {
548551 return []rbacv1.PolicyRule {rbacPolicyRule }
549552}
550553
554+ // EnsureNetworkAttachments fetches NetworkAttachmentDefinitions and creates annotations
555+ func (r * Reconciler ) EnsureNetworkAttachments (
556+ ctx context.Context ,
557+ log logr.Logger ,
558+ helper * helper.Helper ,
559+ networkAttachments []string ,
560+ namespace string ,
561+ conditions * condition.Conditions ,
562+ ) (map [string ]string , ctrl.Result , error ) {
563+ nadList := []networkv1.NetworkAttachmentDefinition {}
564+ for _ , netAtt := range networkAttachments {
565+ netAttachDef , err := nad .GetNADWithName (ctx , helper , netAtt , namespace )
566+ if err != nil {
567+ if k8s_errors .IsNotFound (err ) {
568+ // Since the net-attach-def CR should have been manually created by the user and referenced in the spec,
569+ // we treat this as a warning because it means that the service will not be able to start.
570+ log .Info (fmt .Sprintf ("network-attachment-definition %s not found" , netAtt ))
571+ conditions .Set (condition .FalseCondition (
572+ condition .NetworkAttachmentsReadyCondition ,
573+ condition .ErrorReason ,
574+ condition .SeverityWarning ,
575+ condition .NetworkAttachmentsReadyWaitingMessage ,
576+ netAtt ))
577+ return nil , ctrl.Result {RequeueAfter : time .Second * 10 }, nil
578+ }
579+ conditions .Set (condition .FalseCondition (
580+ condition .NetworkAttachmentsReadyCondition ,
581+ condition .ErrorReason ,
582+ condition .SeverityWarning ,
583+ condition .NetworkAttachmentsReadyErrorMessage ,
584+ err .Error ()))
585+ return nil , ctrl.Result {}, err
586+ }
587+
588+ if netAttachDef != nil {
589+ nadList = append (nadList , * netAttachDef )
590+ }
591+ }
592+
593+ serviceAnnotations , err := nad .EnsureNetworksAnnotation (nadList )
594+ if err != nil {
595+ return nil , ctrl.Result {}, fmt .Errorf ("failed create network annotation from %s: %w" ,
596+ networkAttachments , err )
597+ }
598+
599+ return serviceAnnotations , ctrl.Result {}, nil
600+ }
601+
602+ // VerifyNetworkAttachments verifies network status on the pod and updates conditions
603+ func (r * Reconciler ) VerifyNetworkAttachments (
604+ ctx context.Context ,
605+ helper * helper.Helper ,
606+ instance client.Object ,
607+ networkAttachments []string ,
608+ serviceLabels map [string ]string ,
609+ nextWorkflowStep int ,
610+ conditions * condition.Conditions ,
611+ networkAttachmentStatus * map [string ][]string ,
612+ ) (ctrl.Result , error ) {
613+ if ! r .PodExists (ctx , instance , nextWorkflowStep ) {
614+ return ctrl.Result {}, nil
615+ }
616+
617+ networkReady , status , err := nad .VerifyNetworkStatusFromAnnotation (
618+ ctx ,
619+ helper ,
620+ networkAttachments ,
621+ serviceLabels ,
622+ 1 ,
623+ )
624+ if err != nil {
625+ return ctrl.Result {}, err
626+ }
627+
628+ * networkAttachmentStatus = status
629+
630+ if networkReady {
631+ conditions .MarkTrue (
632+ condition .NetworkAttachmentsReadyCondition ,
633+ condition .NetworkAttachmentsReadyMessage )
634+ } else {
635+ err := fmt .Errorf ("%w: %s" , ErrNetworkAttachmentsMismatch , networkAttachments )
636+ conditions .Set (condition .FalseCondition (
637+ condition .NetworkAttachmentsReadyCondition ,
638+ condition .ErrorReason ,
639+ condition .SeverityWarning ,
640+ condition .NetworkAttachmentsReadyErrorMessage ,
641+ err .Error ()))
642+
643+ return ctrl.Result {}, err
644+ }
645+
646+ return ctrl.Result {}, nil
647+ }
648+
551649// EnsureCloudsConfigMapExists ensures that frameworks like Tobiko and Horizon have password values
552650// present in clouds.yaml. This code ensures that we set a default value of
553651// 12345678 when password value is missing in the clouds.yaml
0 commit comments