controller: Make sure there is an available instance manager for a scheduled instance if its related engine image is compatible
Then we can use this instance manager to create new instance for
attching volumes, or clean up instance process for detached volumes.
@@ -179,7 +179,8 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *types.In
}
var im *longhorn.InstanceManager
- if !isCLIAPIVersionOne && status.InstanceManagerName != "" {
+ if !isCLIAPIVersionOne {
+ if status.InstanceManagerName != "" {
im, err = h.ds.GetInstanceManager(status.InstanceManagerName)
if err != nil {
if datastore.ErrorIsNotFound(err) {
@@ -189,6 +190,14 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *types.In
}
}
}
+ // There should be an available instance manager for a scheduled instance when its related engine image is compatible
+ if im == nil && spec.EngineImage != "" && spec.NodeID != "" {
+ im, err = h.ds.GetInstanceManagerByInstance(obj)
+ if err != nil {
+ return errors.Wrapf(err, "failed to get instance manager for instance %v", instanceName)
+ }
+ }
+ }
if spec.LogRequested {
if im == nil {
@@ -208,12 +217,6 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *types.In
if isCLIAPIVersionOne {
return nil
}
- if im == nil {
- im, err = h.ds.GetInstanceManagerByInstance(obj)
- if err != nil {
- return errors.Wrapf(err, "failed to get instance manager")
- }
- }
if i, exists := im.Status.Instances[instanceName]; exists && i.Status.State == types.InstanceStateRunning {
status.Started = true
@@ -244,6 +247,7 @@ func (h *InstanceHandler) ReconcileInstanceState(obj interface{}, spec *types.In
status.Port = 0
return nil
}
+
if im != nil && im.DeletionTimestamp == nil {
// there is a delay between deleteInstance() invocation and state/InstanceManager update,
// deleteInstance() may be called multiple times.
