Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ type HandOverError struct {
func (r *HandOverError) Error() string {
return r.Message
}

type TemplateInstantiationError struct {
Message string
}

func (r *TemplateInstantiationError) Error() string {
return r.Message
}
15 changes: 15 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions controllers/clustersummary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ func (r *ClusterSummaryReconciler) reconcileDelete(
if errors.As(err, &nonRetriableError) {
return reconcile.Result{Requeue: true, RequeueAfter: deleteHandOverRequeueAfter}, nil
}

var templateError *configv1beta1.TemplateInstantiationError
if errors.As(err, &templateError) {
return reconcile.Result{Requeue: true, RequeueAfter: deleteHandOverRequeueAfter}, nil
}
}

if !r.canRemoveFinalizer(ctx, clusterSummaryScope, logger) {
Expand Down Expand Up @@ -363,9 +368,9 @@ func (r *ClusterSummaryReconciler) reconcileNormal(ctx context.Context,
return reconcile.Result{}, nil
}

err = r.updateMaps(ctx, clusterSummaryScope, logger)
if err != nil {
return reconcile.Result{}, err
updateMapErrs := r.updateMaps(ctx, clusterSummaryScope, logger)
if updateMapErrs != nil {
logger.V(logs.LogInfo).Error(updateMapErrs, "failed to get referenced resources")
}

paused, err := r.isPaused(ctx, clusterSummaryScope.ClusterSummary)
Expand Down
12 changes: 12 additions & 0 deletions controllers/clustersummary_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ func (r *ClusterSummaryReconciler) proceedDeployingFeature(ctx context.Context,
r.updateFeatureStatus(clusterSummaryScope, f.id, &nonRetriableStatus, currentHash, deployerError, logger)
return nil
}
var templateError *configv1beta1.TemplateInstantiationError
if errors.As(deployerError, &templateError) {
nonRetriableStatus := libsveltosv1beta1.FeatureStatusFailedNonRetriable
r.updateFeatureStatus(clusterSummaryScope, f.id, &nonRetriableStatus, currentHash, deployerError, logger)
return nil
}
if r.maxNumberOfConsecutiveFailureReached(clusterSummaryScope, f, logger) {
nonRetriableStatus := libsveltosv1beta1.FeatureStatusFailedNonRetriable
resultError := errors.New("the maximum number of consecutive errors has been reached")
Expand Down Expand Up @@ -412,6 +418,12 @@ func (r *ClusterSummaryReconciler) processUndeployResult(ctx context.Context, cl
r.updateFeatureStatus(clusterSummaryScope, f.id, &removing, nil, result.Err, logger)
return result.Err
}
var templateError *configv1beta1.TemplateInstantiationError
if errors.As(result.Err, &templateError) {
removing := libsveltosv1beta1.FeatureStatusRemoving
r.updateFeatureStatus(clusterSummaryScope, f.id, &removing, nil, result.Err, logger)
return result.Err
}

if *status == libsveltosv1beta1.FeatureStatusRemoved {
if isPullMode {
Expand Down
15 changes: 12 additions & 3 deletions controllers/handlers_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,19 @@ func helmHash(ctx context.Context, c client.Client, clusterSummary *configv1beta
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c,
clusterNamespace, clusterName, sourceRef.Namespace, clusterType)
if err != nil {
return nil, err
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s: %v",
sourceRef.Namespace, err))
// Ignore template instantiation error
continue
}

name, err := libsveltostemplate.GetReferenceResourceName(ctx, c,
clusterNamespace, clusterName, sourceRef.Name, clusterType)
if err != nil {
return nil, err
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s: %v",
sourceRef.Name, err))
// Ignore template instantiation error
continue
}

source, err := getSource(ctx, c, namespace, name, sourceRef.Kind)
Expand Down Expand Up @@ -3989,13 +3995,16 @@ func getInstantiatedChart(ctx context.Context, clusterSummary *configv1beta1.Clu
objects, err := fecthClusterObjects(ctx, getManagementClusterConfig(), getManagementClusterClient(),
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, clusterSummary.Spec.ClusterType, logger)
if err != nil {
logger.V(logs.LogInfo).Error(err, "failed to fetch resources")
return nil, err
}

// Call the new recursive helper function to instantiate all fields.
if err := instantiateStructFields(ctx, getManagementClusterConfig(), getManagementClusterClient(),
instantiatedChart, clusterSummary, objects, mgmtResources, logger); err != nil {
return nil, err
msg := fmt.Sprintf("failed to instantiated template: %v", err)
logger.V(logs.LogInfo).Info(msg)
return nil, &configv1beta1.TemplateInstantiationError{Message: msg}
}

return instantiatedChart, nil
Expand Down
17 changes: 13 additions & 4 deletions controllers/handlers_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ func kustomizationHash(ctx context.Context, c client.Client, clusterSummary *con
if err != nil {
return nil, err
}
if result == nil {
continue
}
config += string(result)

valueFromHash, err := getKustomizeReferenceResourceHash(ctx, c, clusterSummary,
Expand Down Expand Up @@ -430,13 +433,19 @@ func getHashFromKustomizationRef(ctx context.Context, c client.Client, clusterSu
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, clusterSummary.Spec.ClusterNamespace,
clusterSummary.Spec.ClusterName, kustomizationRef.Namespace, clusterSummary.Spec.ClusterType)
if err != nil {
return nil, err
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s: %v",
kustomizationRef.Namespace, err))
// Ignore template instantiation error
return nil, nil
}

name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace,
clusterSummary.Spec.ClusterName, kustomizationRef.Name, clusterSummary.Spec.ClusterType)
if err != nil {
return nil, err
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s: %v",
kustomizationRef.Name, err))
// Ignore template instantiation error
return nil, nil
}

if kustomizationRef.Kind == string(libsveltosv1beta1.ConfigMapReferencedResourceKind) {
Expand Down Expand Up @@ -676,13 +685,13 @@ func prepareFileSystem(ctx context.Context, c client.Client,
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c, clusterSummary.Spec.ClusterNamespace,
clusterSummary.Spec.ClusterName, kustomizationRef.Namespace, clusterSummary.Spec.ClusterType)
if err != nil {
return "", err
return "", &configv1beta1.TemplateInstantiationError{Message: err.Error()}
}

name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace,
clusterSummary.Spec.ClusterName, kustomizationRef.Name, clusterSummary.Spec.ClusterType)
if err != nil {
return "", err
return "", &configv1beta1.TemplateInstantiationError{Message: err.Error()}
}

source, err := getSource(ctx, c, namespace, name, kustomizationRef.Kind)
Expand Down
12 changes: 7 additions & 5 deletions controllers/handlers_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func resourcesHash(ctx context.Context, c client.Client, clusterSummary *configv
var config string
config += string(clusterProfileSpecHash)

referencedObjects := make([]corev1.ObjectReference, len(clusterSummary.Spec.ClusterProfileSpec.PolicyRefs))
referencedObjects := make([]corev1.ObjectReference, 0, len(clusterSummary.Spec.ClusterProfileSpec.PolicyRefs))
for i := range clusterSummary.Spec.ClusterProfileSpec.PolicyRefs {
reference := &clusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i]
namespace, err := libsveltostemplate.GetReferenceResourceNamespace(ctx, c,
Expand All @@ -508,22 +508,24 @@ func resourcesHash(ctx context.Context, c client.Client, clusterSummary *configv
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v",
reference.Kind, reference.Namespace, reference.Name, err))
return nil, err
// Ignore template instantiation error
continue
}

name, err := libsveltostemplate.GetReferenceResourceName(ctx, c, clusterSummary.Spec.ClusterNamespace,
clusterSummary.Spec.ClusterName, reference.Name, clusterSummary.Spec.ClusterType)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s %s/%s: %v",
reference.Kind, reference.Namespace, reference.Name, err))
return nil, err
// Ignore template instantiation error
continue
}

referencedObjects[i] = corev1.ObjectReference{
referencedObjects = append(referencedObjects, corev1.ObjectReference{
Kind: clusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Kind,
Namespace: namespace,
Name: name,
}
})
}

sort.Sort(dependencymanager.SortedCorev1ObjectReference(referencedObjects))
Expand Down
14 changes: 8 additions & 6 deletions controllers/handlers_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,18 +676,20 @@ func collectReferencedObjects(ctx context.Context, controlClusterClient client.C
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, references[i].Namespace,
clusterSummary.Spec.ClusterType)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v",
reference.Kind, reference.Namespace, reference.Name, err))
return nil, nil, err
msg := fmt.Sprintf("failed to instantiate namespace for %s %s/%s: %v",
reference.Kind, reference.Namespace, reference.Name, err)
logger.V(logs.LogInfo).Info(msg)
return nil, nil, &configv1beta1.TemplateInstantiationError{Message: msg}
}

name, err := libsveltostemplate.GetReferenceResourceName(ctx, getManagementClusterClient(),
clusterSummary.Spec.ClusterNamespace, clusterSummary.Spec.ClusterName, references[i].Name,
clusterSummary.Spec.ClusterType)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to instantiate name for %s %s/%s: %v",
reference.Kind, reference.Namespace, reference.Name, err))
return nil, nil, err
msg := fmt.Sprintf("failed to instantiate name for %s %s/%s: %v",
reference.Kind, reference.Namespace, reference.Name, err)
logger.V(logs.LogInfo).Info(msg)
return nil, nil, &configv1beta1.TemplateInstantiationError{Message: msg}
}

if reference.Kind == string(libsveltosv1beta1.ConfigMapReferencedResourceKind) {
Expand Down
Loading