@@ -124,35 +124,48 @@ func (c *Controller) enableNetworkFS(networkFS *networkfsv1.NetworkFilesystem) (
124124 if err := c .updateLHVolumeAttachment (networkFS , true ); err != nil {
125125 return nil , err
126126 }
127- networkFSCpy := networkFS .DeepCopy ()
128- networkFSCpy .Status .State = networkfsv1 .NetworkFSStateEnabling
129- networkFSCpy .Status .Status = networkfsv1 .EndpointStatusNotReady
130- networkFSCpy .Status .Type = networkfsv1 .NetworkFSTypeNFS
131- if ! reflect .DeepEqual (networkFS , networkFSCpy ) {
132- return c .NetworkFilsystems .UpdateStatus (networkFSCpy )
127+ networkFSNew := setNetworkFSToEnabling (networkFS , networkfsv1 .NetworkFSTypeNFS )
128+ if ! reflect .DeepEqual (networkFS , networkFSNew ) {
129+ return c .NetworkFilsystems .UpdateStatus (networkFSNew )
133130 }
134131 }
135132
136133 if lhShareMgr .Status .State != longhornv2 .ShareManagerStateRunning {
134+ // check the LHVA again, we encounter the corner case that the lhva is cleaned up
135+ // when we disable/enable the network filesystem very soon.
136+ lhva , err := c .lhClient .LonghornV1beta2 ().VolumeAttachments (utils .LHNameSpace ).Get (context .Background (), networkFS .Name , metav1.GetOptions {})
137+ if err != nil {
138+ logrus .Errorf ("Failed to get Longhorn volume attachment %s: %v" , networkFS .Name , err )
139+ return nil , err
140+ }
141+ if len (lhva .Spec .AttachmentTickets ) == 0 {
142+ // this will reset the network filesystem status to disabled
143+ // after reconcile, we will retry to enable the network filesystem
144+ logrus .Infof ("The LHVA %s has no attachment tickets, reset the network filesystem status for retry" , networkFS .Name )
145+ networkfsNew := setNetworkFSToDefault (networkFS )
146+ if ! reflect .DeepEqual (networkFS , networkfsNew ) {
147+ return c .NetworkFilsystems .UpdateStatus (networkfsNew )
148+ }
149+ }
137150 logrus .Infof ("Wait the share manager %s to be running" , networkFS .Name )
138151 return nil , fmt .Errorf ("wait the share manager %s to be running" , networkFS .Name )
139152 }
140153
141154 // LH RWX volume endpoint should only have one address and one port
155+ netFSEndpoint := ""
142156 service , err := c .coreClient .Service ().Get (utils .LHNameSpace , networkFS .Name , metav1.GetOptions {})
143157 if err != nil {
144158 logrus .Errorf ("Failed to get service %s: %v" , networkFS .Name , err )
145159 return nil , err
146160 }
147- networkFSCpy := networkFS .DeepCopy ()
148161 if service .Spec .ClusterIP != corev1 .ClusterIPNone {
149162 // means we depends on the service
150163 if service .Spec .ClusterIP == "" {
151164 // first init, service controller will update
152165 logrus .Infof ("Skip update on networkfs controller with the first time service init" )
153166 return nil , nil
154167 }
155- networkFSCpy . Status . Endpoint = service .Spec .ClusterIP
168+ netFSEndpoint = service .Spec .ClusterIP
156169 } else {
157170 endpoint , err := c .endpointsClient .Get (utils .LHNameSpace , networkFS .Name , metav1.GetOptions {})
158171 if err != nil && ! errors .IsNotFound (err ) {
@@ -168,7 +181,7 @@ func (c *Controller) enableNetworkFS(networkFS *networkfsv1.NetworkFilesystem) (
168181 if endpoint .Subsets [0 ].Ports [0 ].Name != "nfs" {
169182 return nil , fmt .Errorf ("endpoint %s has no nfs port" , networkFS .Name )
170183 }
171- networkFSCpy . Status . Endpoint = endpoint .Subsets [0 ].Addresses [0 ].IP
184+ netFSEndpoint = endpoint .Subsets [0 ].Addresses [0 ].IP
172185 }
173186
174187 pv , err := c .coreClient .PersistentVolume ().Get (networkFS .Name , metav1.GetOptions {})
@@ -180,20 +193,9 @@ func (c *Controller) enableNetworkFS(networkFS *networkfsv1.NetworkFilesystem) (
180193 if _ , found := pv .Spec .CSI .VolumeAttributes ["nfsOptions" ]; found {
181194 opts = pv .Spec .CSI .VolumeAttributes ["nfsOptions" ]
182195 }
196+ networkFSNew := setNetworkFSToEnabled (networkFS , networkfsv1 .NetworkFSTypeNFS , netFSEndpoint , opts )
183197 // update network filesystem status
184- networkFSCpy .Status .State = networkfsv1 .NetworkFSStateEnabled
185- networkFSCpy .Status .Type = networkfsv1 .NetworkFSTypeNFS
186- networkFSCpy .Status .Status = networkfsv1 .EndpointStatusReady
187- networkFSCpy .Status .MountOpts = opts
188- conds := networkfsv1.NetworkFSCondition {
189- Type : networkfsv1 .ConditionTypeReady ,
190- Status : corev1 .ConditionTrue ,
191- LastTransitionTime : metav1 .Now (),
192- Reason : "Endpoint is ready" ,
193- Message : "Endpoint contains the corresponding address" ,
194- }
195- networkFSCpy .Status .NetworkFSConds = utils .UpdateNetworkFSConds (networkFSCpy .Status .NetworkFSConds , conds )
196- return c .NetworkFilsystems .UpdateStatus (networkFSCpy )
198+ return c .NetworkFilsystems .UpdateStatus (networkFSNew )
197199}
198200
199201func (c * Controller ) updateLHVolumeAttachment (networkFS * networkfsv1.NetworkFilesystem , attach bool ) error {
@@ -279,3 +281,52 @@ func isEnabling(networkFS *networkfsv1.NetworkFilesystem) bool {
279281func isDisabling (networkFS * networkfsv1.NetworkFilesystem ) bool {
280282 return networkFS .Status .State == networkfsv1 .NetworkFSStateDisabling
281283}
284+
285+ func setNetworkFSToDefault (networkFS * networkfsv1.NetworkFilesystem ) * networkfsv1.NetworkFilesystem {
286+ networkFSCpy := networkFS .DeepCopy ()
287+ setNetworkFSStatus (networkFSCpy , networkfsv1 .NetworkFSStateDisabled , networkfsv1 .EndpointStatusNotReady )
288+ return networkFSCpy
289+ }
290+
291+ func setNetworkFSToEnabling (networkFS * networkfsv1.NetworkFilesystem , targetNetFS string ) * networkfsv1.NetworkFilesystem {
292+ networkFSCpy := networkFS .DeepCopy ()
293+ setNetworkFSStatus (networkFSCpy , networkfsv1 .NetworkFSStateEnabling , networkfsv1 .EndpointStatusNotReady )
294+ networkFSCpy .Status .Type = targetNetFS
295+ return networkFSCpy
296+ }
297+
298+ func setNetworkFSToEnabled (networkFS * networkfsv1.NetworkFilesystem , targetNetFS , endpoint , opts string ) * networkfsv1.NetworkFilesystem {
299+ networkFSCpy := networkFS .DeepCopy ()
300+ setNetworkFSStatus (networkFSCpy , networkfsv1 .NetworkFSStateEnabled , networkfsv1 .EndpointStatusReady )
301+ setNetworkFSType (networkFSCpy , targetNetFS )
302+ setNetworkFSEndpoint (networkFSCpy , endpoint )
303+ setNetworkFSOpts (networkFSCpy , opts )
304+ conds := networkfsv1.NetworkFSCondition {
305+ Type : networkfsv1 .ConditionTypeEndpointChanged ,
306+ Status : corev1 .ConditionTrue ,
307+ LastTransitionTime : metav1 .Now (),
308+ Reason : "Endpoint is changed" ,
309+ Message : fmt .Sprintf ("Endpoint is changed to %s" , endpoint ),
310+ }
311+ networkFSCpy .Status .NetworkFSConds = utils .UpdateNetworkFSConds (networkFSCpy .Status .NetworkFSConds , conds )
312+ return networkFSCpy
313+ }
314+
315+ func setNetworkFSStatus (networkFS * networkfsv1.NetworkFilesystem ,
316+ state networkfsv1.NetworkFSState ,
317+ status networkfsv1.EndpointStatus ) {
318+ networkFS .Status .State = state
319+ networkFS .Status .Status = status
320+ }
321+
322+ func setNetworkFSType (networkFS * networkfsv1.NetworkFilesystem , targetNetFS string ) {
323+ networkFS .Status .Type = targetNetFS
324+ }
325+
326+ func setNetworkFSEndpoint (networkFS * networkfsv1.NetworkFilesystem , endpoint string ) {
327+ networkFS .Status .Endpoint = endpoint
328+ }
329+
330+ func setNetworkFSOpts (networkFS * networkfsv1.NetworkFilesystem , opts string ) {
331+ networkFS .Status .MountOpts = opts
332+ }
0 commit comments