func()

in internal/handler/requirement.go [141:183]


func (r *RequirementHandler) EnsureCacheExisted(ctx context.Context) (reconciler.OperationResult, error) {
	if !r.phaseIn(v1alpha1.RequirementPhaseCacheChecking) {
		return reconciler.ContinueProcessing()
	}

	r.logger.V(1).Info("operation: EnsureCacheExisted")

	// candidate operation id exists, go to next step to acquire the operation
	if len(r.requirement.Status.OperationName) != 0 {
		return reconciler.ContinueProcessing()
	}

	if len(r.requirement.Status.CacheKey) == 0 {
		r.logger.Error(fmt.Errorf("empty cache key"), "Cache key is empty, cannot proceed with cache creation")
		return reconciler.RequeueWithError(fmt.Errorf("empty cache key"))
	}
	cache := &v1alpha1.Cache{}
	// Try to get the Cache CR
	if err := r.client.Get(ctx, types.NamespacedName{Name: r.defaultCacheName(), Namespace: r.requirement.Namespace}, cache); err != nil {
		if client.IgnoreNotFound(err) != nil {
			// If the error is not a NotFound error, return it
			return reconciler.RequeueWithError(err)
		}
		// cache cr not found, create it
		cache.Name = r.defaultCacheName()
		cache.Namespace = r.requirement.Namespace
		cache.Spec = v1alpha1.CacheSpec{
			OperationTemplate: r.requirement.Spec.Template,
			ExpireTime:        r.cacheutils.DefaultCacheExpireTime(),
		}
		err = r.client.Create(ctx, cache)
		if err != nil {
			return reconciler.RequeueWithError(err)
		}
		r.setCacheNotExistedStatus()
		return reconciler.RequeueOnErrorOrContinue(r.client.Status().Update(ctx, r.requirement))
	}
	// extend cache expire time every time when cache is checked
	cache.Spec.ExpireTime = r.cacheutils.DefaultCacheExpireTime()
	_ = r.client.Update(ctx, cache)
	r.requirement.Status.OperationName = r.cacheutils.RandomSelectCachedOperation(cache)
	return reconciler.RequeueOnErrorOrContinue(r.client.Status().Update(ctx, r.requirement))
}