Skip to content

Commit 78a2024

Browse files
committed
fix: add instance status.starting for instance cleanup
Longhorn 11330 Signed-off-by: Shuo Wu <shuo.wu@suse.com>
1 parent daad9da commit 78a2024

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

controller/engine_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ func (ec *EngineController) CreateInstance(obj interface{}) (*longhorn.InstanceP
494494

495495
instanceManagerStorageIP := ec.ds.GetStorageIPFromPod(instanceManagerPod)
496496

497+
e.Status.Starting = true
498+
if e, err = ec.ds.UpdateEngineStatus(e); err != nil {
499+
return nil, errors.Wrapf(err, "failed to update engine %v status.starting to true before sending instance create request", e.Name)
500+
}
501+
497502
return c.EngineInstanceCreate(&engineapi.EngineInstanceCreateRequest{
498503
Engine: e,
499504
VolumeFrontend: frontend,

controller/instance_handler.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *longhorn
368368

369369
if i, exists := instances[instanceName]; exists && i.Status.State == longhorn.InstanceStateRunning {
370370
status.Started = true
371+
status.Starting = false
371372
break
372373
}
373374

@@ -388,18 +389,24 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *longhorn
388389
}
389390

390391
case longhorn.InstanceStateStopped:
392+
shouldDelete := false
391393
if im != nil && im.DeletionTimestamp == nil {
394+
if _, exists := instances[instanceName]; exists {
395+
shouldDelete = true
396+
}
397+
}
398+
if status.Starting {
399+
shouldDelete = true
400+
}
401+
if shouldDelete {
392402
// there is a delay between deleteInstance() invocation and state/InstanceManager update,
393403
// deleteInstance() may be called multiple times.
394-
if instance, exists := instances[instanceName]; exists {
395-
if shouldDeleteInstance(&instance) {
396-
if err := h.deleteInstance(instanceName, runtimeObj); err != nil {
397-
return err
398-
}
399-
}
404+
if err := h.deleteInstance(instanceName, runtimeObj); err != nil {
405+
return err
400406
}
401407
}
402408
status.Started = false
409+
status.Starting = false
403410
default:
404411
return fmt.Errorf("unknown instance desire state: desire %v", spec.DesireState)
405412
}
@@ -445,17 +452,6 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *longhorn
445452
return nil
446453
}
447454

448-
func shouldDeleteInstance(instance *longhorn.InstanceProcess) bool {
449-
// For a replica of a SPDK volume, a stopped replica means the lvol is not exposed,
450-
// but the lvol is still there. We don't need to delete it.
451-
if types.IsDataEngineV2(instance.Spec.DataEngine) {
452-
if instance.Status.State == longhorn.InstanceStateStopped {
453-
return false
454-
}
455-
}
456-
return true
457-
}
458-
459455
func (h *InstanceHandler) getInstancesFromInstanceManager(obj runtime.Object, instanceManager *longhorn.InstanceManager) (map[string]longhorn.InstanceProcess, error) {
460456
switch obj.(type) {
461457
case *longhorn.Engine:

controller/replica_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ func (rc *ReplicaController) CreateInstance(obj interface{}) (*longhorn.Instance
380380
return nil, err
381381
}
382382

383+
r.Status.Starting = true
384+
if r, err = rc.ds.UpdateReplicaStatus(r); err != nil {
385+
return nil, errors.Wrapf(err, "failed to update replica %v status.starting to true before sending instance create request", r.Name)
386+
}
387+
383388
return c.ReplicaInstanceCreate(&engineapi.ReplicaInstanceCreateRequest{
384389
Replica: r,
385390
DiskName: diskName,

k8s/pkg/apis/longhorn/v1beta2/instancemanager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ type InstanceStatus struct {
102102
// +optional
103103
Port int `json:"port"`
104104
// +optional
105+
Starting bool `json:"starting"`
106+
// +optional
105107
Started bool `json:"started"`
106108
// +optional
107109
LogFetched bool `json:"logFetched"`

0 commit comments

Comments
 (0)