func()

in pkg/pool/pool.go [308:387]


func (p *pool) ReconcilePool() *worker.WarmPoolJob {
	p.lock.Lock()
	defer p.lock.Unlock()

	// Total created resources includes all the resources for the instance that are not yet deleted
	totalCreatedResources := len(p.warmResources) + len(p.usedResources) + len(p.coolDownQueue) +
		p.pendingCreate + p.pendingDelete

	log := p.log.WithValues("resync", p.reSyncRequired, "warm", len(p.warmResources), "used",
		len(p.usedResources), "pending create", p.pendingCreate, "pending delete", &p.pendingDelete,
		"cool down queue", len(p.coolDownQueue), "total resources", totalCreatedResources,
		"max capacity", p.capacity, "desired size", p.warmPoolConfig.DesiredSize)

	if p.reSyncRequired {
		// If Pending operations are present then we can't re-sync as the upstream
		// and pool could change during re-sync
		if p.pendingCreate != 0 || p.pendingDelete != 0 {
			p.log.Info("cannot re-sync as there are pending add/delete request")
			return &worker.WarmPoolJob{
				Operations: worker.OperationReconcileNotRequired,
			}
		}
		p.log.Info("submitting request re-sync the pool")
		return worker.NewWarmPoolReSyncJob(p.nodeName)
	}

	if len(p.usedResources)+p.pendingCreate+p.pendingDelete+len(p.coolDownQueue) == p.capacity {
		log.V(1).Info("cannot reconcile, at max capacity")
		return &worker.WarmPoolJob{Operations: worker.OperationReconcileNotRequired}
	}

	// Consider pending create as well so we don't create multiple subsequent create request
	deviation := p.warmPoolConfig.DesiredSize - (len(p.warmResources) + p.pendingCreate)

	// Need to create more resources for warm pool
	if deviation > p.warmPoolConfig.MaxDeviation {
		// The maximum number of resources that can be created
		canCreateUpto := p.capacity - totalCreatedResources
		if canCreateUpto == 0 {
			return &worker.WarmPoolJob{Operations: worker.OperationReconcileNotRequired}
		}

		// Need to add to warm pool
		if deviation > canCreateUpto {
			log.V(1).Info("can only create limited resources", "can create", canCreateUpto,
				"requested", deviation, "desired", deviation)
			deviation = canCreateUpto
		}

		// Increment the pending to the size of deviation, once we get async response on creation success we can decrement
		// pending
		p.pendingCreate += deviation

		log.Info("created job to add resources to warm pool", "requested count", deviation)

		return worker.NewWarmPoolCreateJob(p.nodeName, deviation)

	} else if -deviation > p.warmPoolConfig.MaxDeviation {
		// Need to delete from warm pool
		deviation = -deviation
		var resourceToDelete []string
		for i := len(p.warmResources) - 1; i >= len(p.warmResources)-deviation; i-- {
			resourceToDelete = append(resourceToDelete, p.warmResources[i])
		}

		// Remove resources to be deleted form the warm pool
		p.warmResources = p.warmResources[:len(p.warmResources)-deviation]
		// Increment pending to the number of resource being deleted, once successfully deleted the count can be decremented
		p.pendingDelete += deviation
		// Submit the job to delete resources

		log.Info("created job to delete resources from warm pool", "resources to delete", resourceToDelete)

		return worker.NewWarmPoolDeleteJob(p.nodeName, resourceToDelete)
	}

	log.V(1).Info("no need for reconciliation")

	return &worker.WarmPoolJob{Operations: worker.OperationReconcileNotRequired}
}