func()

in internal/handler/cache.go [171:229]


func (c *CacheHandler) AdjustCache(ctx context.Context) (reconciler.OperationResult, error) {
	var ownedOps v1alpha1.OperationList
	if err := c.client.List(ctx, &ownedOps, client.InNamespace(c.cache.Namespace), client.MatchingFields{v1alpha1.CacheOwnerKey: c.cache.Name}); err != nil {
		return reconciler.RequeueWithError(err)
	}
	availableCaches := []string{}
	for _, op := range ownedOps.Items {
		if c.oputils.IsOperationReady(&op) {
			availableCaches = append(availableCaches, op.Name)
		}
	}
	c.cache.Status.AvailableCaches = availableCaches

	keepAliveCount := int(c.cache.Status.KeepAliveCount)
	cacheBalance := len(availableCaches) - keepAliveCount
	switch {
	case cacheBalance == 0:
		// do nothing: should we remove the not available operations?
	case cacheBalance > 0:
		// remove all the not available operations and cut available operations down to keepAliveCount
		availableCacheNumToRemove := cacheBalance
		opsToRemove := []*v1alpha1.Operation{}
		for _, op := range ownedOps.Items {
			if !c.oputils.IsOperationReady(&op) {
				opsToRemove = append(opsToRemove, &op)
			} else {
				if availableCacheNumToRemove > 0 {
					opsToRemove = append(opsToRemove, &op)
					availableCacheNumToRemove--
				}
			}
		}
		c.logger.Info("removing operations", "operations", opsToRemove)
		if err := c.deleteOperationsAsync(ctx, opsToRemove); err != nil {
			return reconciler.RequeueWithError(err)
		}
	case cacheBalance < 0:
		if len(ownedOps.Items) < keepAliveCount {
			// also count not available operations, create new operations to meet the keepAliveCount
			opsToCreate := []*v1alpha1.Operation{}
			opsNumToCreate := keepAliveCount - len(ownedOps.Items)
			for range opsNumToCreate {
				opName := fmt.Sprintf("cached-operation-%s-%s", c.cache.Status.CacheKey[:8], strings.ToLower(randutils.GenerateRandomString(5)))
				opToCreate := c.initOperationFromCache(opName)
				if err := c.setControllerReferenceFunc(c.cache, opToCreate, c.scheme); err != nil {
					return reconciler.RequeueWithError(err)
				}
				opsToCreate = append(opsToCreate, opToCreate)
			}
			c.logger.Info("creating operations", "operations", opsToCreate)
			if err := c.createOperationsAsync(ctx, opsToCreate); err != nil {
				return reconciler.RequeueWithError(err)
			}
		}
		// else do nothing: we assume that any not ready operations are in progress and will be ready
		// we can bring in stuck operations handling if we consider that's one case for cache controller to solve
	}
	return reconciler.RequeueOnErrorOrContinue(c.updateStatus(ctx))
}