|
| 1 | +package applier |
| 2 | + |
| 3 | +type Applier interface { |
| 4 | + Apply() (err error) |
| 5 | +} |
| 6 | + |
| 7 | +type Implementer struct{} |
| 8 | + |
| 9 | +// Applies resources to the target clusters. |
| 10 | +func (i *Implementer) Apply() (err error) { |
| 11 | + |
| 12 | + return err |
| 13 | +} |
| 14 | + |
| 15 | +// Helpers: |
| 16 | + |
| 17 | +// getClusterList |
| 18 | + |
| 19 | +// func (r *Reconciler) applyNeeded(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, parsedResources []*unstructured.Unstructured, logger logr.Logger) (clusterList clusterv1.ClusterList, err error) { |
| 20 | +// _, list, err := r.verifyResourcesOnClusters(ctx, cdk8sAppProxy, parsedResources, logger) |
| 21 | +// if err != nil { |
| 22 | +// return clusterList, err |
| 23 | +// } |
| 24 | +// clusterList = list |
| 25 | + |
| 26 | +// return clusterList, nil |
| 27 | +// } |
| 28 | + |
| 29 | +// func (r *Reconciler) verifyResourcesOnClusters(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, parsedResources []*unstructured.Unstructured, logger logr.Logger) (foundMissingResourcesOnAnyCluster bool, clusterList clusterv1.ClusterList, err error) { |
| 30 | +// foundMissingResourcesOnAnyCluster = false |
| 31 | + |
| 32 | +// if len(parsedResources) == 0 { |
| 33 | +// logger.Info("No parsed resources to verify. Skipping resource verification.") |
| 34 | + |
| 35 | +// return foundMissingResourcesOnAnyCluster, clusterList, err |
| 36 | +// } |
| 37 | + |
| 38 | +// selector, err := metav1.LabelSelectorAsSelector(&cdk8sAppProxy.Spec.ClusterSelector) |
| 39 | +// if err != nil { |
| 40 | +// logger.Error(err, "Failed to parse ClusterSelector for verification, assuming resources might be missing.", "selector", cdk8sAppProxy.Spec.ClusterSelector) |
| 41 | +// foundMissingResourcesOnAnyCluster = true |
| 42 | + |
| 43 | +// return foundMissingResourcesOnAnyCluster, clusterList, r.updateStatusWithError(ctx, cdk8sAppProxy, addonsv1alpha1.ClusterSelectorParseFailedReason, "Failed to parse ClusterSelector for verification", err, false) |
| 44 | +// } |
| 45 | + |
| 46 | +// logger.Info("Listing clusters for resource verification", "selector", selector.String(), "namespace", cdk8sAppProxy.Namespace) |
| 47 | +// if err := r.List(ctx, &clusterList, client.MatchingLabelsSelector{Selector: selector}); err != nil { |
| 48 | +// logger.Error(err, "Failed to list clusters for verification, assuming resources might be missing.") |
| 49 | +// foundMissingResourcesOnAnyCluster = true |
| 50 | + |
| 51 | +// return foundMissingResourcesOnAnyCluster, clusterList, r.updateStatusWithError(ctx, cdk8sAppProxy, addonsv1alpha1.ListClustersFailedReason, "Failed to list clusters for verification", err, false) |
| 52 | +// } |
| 53 | + |
| 54 | +// if len(clusterList.Items) == 0 { |
| 55 | +// logger.Info("No clusters found matching selector for verification. Skipping resource verification on clusters.") |
| 56 | + |
| 57 | +// return foundMissingResourcesOnAnyCluster, clusterList, err |
| 58 | +// } |
| 59 | + |
| 60 | +// logger.Info("Successfully listed clusters for verification", "count", len(clusterList.Items)) |
| 61 | +// for _, cluster := range clusterList.Items { |
| 62 | +// clusterLogger := logger.WithValues("targetCluster", cluster.Name) |
| 63 | +// clusterLogger.Info("Verifying resources on cluster") |
| 64 | +// dynamicClient, err := r.getDynamicClientForCluster(ctx, cluster.Namespace, cluster.Name) |
| 65 | +// if err != nil { |
| 66 | +// clusterLogger.Error(err, "Failed to get dynamic client for verification. Assuming resources missing on this cluster.") |
| 67 | +// foundMissingResourcesOnAnyCluster = true |
| 68 | + |
| 69 | +// break |
| 70 | +// } |
| 71 | +// for _, resource := range parsedResources { |
| 72 | +// gvr := resource.GroupVersionKind().GroupVersion().WithResource(getPluralFromKind(resource.GetKind())) |
| 73 | +// exists, checkErr := r.checkIfResourceExists(ctx, dynamicClient, gvr, resource.GetNamespace(), resource.GetName()) |
| 74 | +// if checkErr != nil { |
| 75 | +// clusterLogger.Error(checkErr, "Error checking resource existence. Assuming resource missing.", "resourceName", resource.GetName(), "GVK", gvr) |
| 76 | +// foundMissingResourcesOnAnyCluster = true |
| 77 | + |
| 78 | +// break |
| 79 | +// } |
| 80 | +// if !exists { |
| 81 | +// clusterLogger.Info("Resource missing on target cluster.", "resourceName", resource.GetName(), "GVK", gvr, "namespace", resource.GetNamespace()) |
| 82 | +// foundMissingResourcesOnAnyCluster = true |
| 83 | + |
| 84 | +// break |
| 85 | +// } |
| 86 | +// } |
| 87 | +// if foundMissingResourcesOnAnyCluster { |
| 88 | +// break |
| 89 | +// } |
| 90 | +// } |
| 91 | + |
| 92 | +// return foundMissingResourcesOnAnyCluster, clusterList, err |
| 93 | +// } |
| 94 | + |
| 95 | +//nolint:unparam // ctrl.Result is required for controller-runtime reconciler pattern |
| 96 | +// func (r *Reconciler) applyResourcesToClusters(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, parsedResources []*unstructured.Unstructured, clusterList clusterv1.ClusterList, logger logr.Logger) (ctrl.Result, error) { |
| 97 | +// logger.Info("Proceeding with application of resources to target clusters.") |
| 98 | + |
| 99 | +// // Ensure clusterList is populated if needed |
| 100 | +// if len(clusterList.Items) == 0 && len(parsedResources) > 0 { |
| 101 | +// logger.Info("Cluster list for apply phase is empty, re-listing.") |
| 102 | +// selector, err := metav1.LabelSelectorAsSelector(&cdk8sAppProxy.Spec.ClusterSelector) |
| 103 | +// if err != nil { |
| 104 | +// logger.Error(err, "Failed to parse ClusterSelector for application phase", "selector", cdk8sAppProxy.Spec.ClusterSelector) |
| 105 | + |
| 106 | +// return ctrl.Result{}, r.updateStatusWithError(ctx, cdk8sAppProxy, addonsv1alpha1.ClusterSelectorParseFailedReason, "Failed to parse ClusterSelector for application", err, false) |
| 107 | +// } |
| 108 | +// logger.Info("Listing clusters for application phase", "selector", selector.String(), "namespace", cdk8sAppProxy.Namespace) |
| 109 | +// if err := r.List(ctx, &clusterList, client.MatchingLabelsSelector{Selector: selector}); err != nil { |
| 110 | +// logger.Error(err, "Failed to list clusters for application phase") |
| 111 | + |
| 112 | +// return ctrl.Result{}, r.updateStatusWithError(ctx, cdk8sAppProxy, addonsv1alpha1.ListClustersFailedReason, "Failed to list clusters for application", err, false) |
| 113 | +// } |
| 114 | +// if len(clusterList.Items) == 0 { |
| 115 | +// logger.Info("No clusters found matching the selector for application phase.") |
| 116 | +// conditions.MarkFalse(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition, addonsv1alpha1.NoMatchingClustersReason, clusterv1.ConditionSeverityInfo, "No clusters found matching selector for application") |
| 117 | +// if errStatusUpdate := r.Status().Update(ctx, cdk8sAppProxy); errStatusUpdate != nil { |
| 118 | +// logger.Error(errStatusUpdate, "Failed to update status when no matching clusters found for application") |
| 119 | +// } |
| 120 | + |
| 121 | +// return ctrl.Result{}, nil |
| 122 | +// } |
| 123 | +// logger.Info("Successfully listed clusters for application phase", "count", len(clusterList.Items)) |
| 124 | +// } else if len(parsedResources) == 0 { |
| 125 | +// logger.Info("No parsed resources to apply, skipping application to clusters.") |
| 126 | +// cdk8sAppProxy.Status.ObservedGeneration = cdk8sAppProxy.Generation |
| 127 | +// conditions.MarkTrue(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition) |
| 128 | +// if cdk8sAppProxy.Spec.GitRepository != nil && cdk8sAppProxy.Spec.GitRepository.URL != "" { |
| 129 | +// } |
| 130 | +// if err := r.Status().Update(ctx, cdk8sAppProxy); err != nil { |
| 131 | +// logger.Error(err, "Failed to update status when no resources to apply") |
| 132 | + |
| 133 | +// return ctrl.Result{}, err |
| 134 | +// } |
| 135 | + |
| 136 | +// return ctrl.Result{}, nil |
| 137 | +// } |
| 138 | + |
| 139 | +// var overallSuccess = true |
| 140 | +// var firstErrorEncountered error |
| 141 | + |
| 142 | +// for _, cluster := range clusterList.Items { |
| 143 | +// clusterLogger := logger.WithValues("targetCluster", cluster.Name) |
| 144 | +// clusterLogger.Info("Processing cluster for resource application") |
| 145 | +// dynamicClient, err := r.getDynamicClientForCluster(ctx, cluster.Namespace, cluster.Name) |
| 146 | +// if err != nil { |
| 147 | +// clusterLogger.Error(err, "Failed to get dynamic client for cluster application") |
| 148 | +// overallSuccess = false |
| 149 | +// if firstErrorEncountered == nil { |
| 150 | +// firstErrorEncountered = err |
| 151 | +// } |
| 152 | +// conditions.MarkFalse(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition, addonsv1alpha1.KubeconfigUnavailableReason, clusterv1.ConditionSeverityError, "Failed to get dynamic client for cluster %s: %v", cluster.Name, err) |
| 153 | + |
| 154 | +// continue |
| 155 | +// } |
| 156 | +// clusterLogger.Info("Successfully created dynamic client for cluster application") |
| 157 | +// for _, resource := range parsedResources { |
| 158 | +// resourceCopy := resource.DeepCopy() |
| 159 | +// gvk := resourceCopy.GroupVersionKind() |
| 160 | +// gvr := gvk.GroupVersion().WithResource(getPluralFromKind(gvk.Kind)) |
| 161 | +// applyOpts := metav1.ApplyOptions{FieldManager: "cdk8sappproxy-controller", Force: true} |
| 162 | + |
| 163 | +// clusterLogger.Info("Applying resource", "GVK", gvk.String(), "Name", resourceCopy.GetName(), "Namespace", resourceCopy.GetNamespace()) |
| 164 | +// _, applyErr := dynamicClient.Resource(gvr).Namespace(resourceCopy.GetNamespace()).Apply(ctx, resourceCopy.GetName(), resourceCopy, applyOpts) |
| 165 | +// if applyErr != nil { |
| 166 | +// clusterLogger.Error(applyErr, "Failed to apply resource to cluster", "resourceName", resourceCopy.GetName()) |
| 167 | +// overallSuccess = false |
| 168 | +// if firstErrorEncountered == nil { |
| 169 | +// firstErrorEncountered = applyErr |
| 170 | +// } |
| 171 | +// conditions.MarkFalse(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition, addonsv1alpha1.ResourceApplyFailedReason, clusterv1.ConditionSeverityError, "Failed to apply %s %s to cluster %s: %v", gvk.Kind, resourceCopy.GetName(), cluster.Name, applyErr) |
| 172 | +// } else { |
| 173 | +// clusterLogger.Info("Successfully applied resource to cluster", "resourceName", resourceCopy.GetName()) |
| 174 | +// } |
| 175 | +// } |
| 176 | +// } |
| 177 | + |
| 178 | +// if !overallSuccess { |
| 179 | +// logger.Error(firstErrorEncountered, "One or more errors occurred during resource application to clusters") |
| 180 | + |
| 181 | +// return ctrl.Result{}, firstErrorEncountered |
| 182 | +// } |
| 183 | + |
| 184 | +// // If we reach here, the overallSuccess is true. |
| 185 | +// if cdk8sAppProxy.Spec.GitRepository != nil && cdk8sAppProxy.Spec.GitRepository.URL != "" { |
| 186 | +// } |
| 187 | + |
| 188 | +// cdk8sAppProxy.Status.ObservedGeneration = cdk8sAppProxy.Generation |
| 189 | +// conditions.MarkTrue(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition) |
| 190 | +// if err := r.Status().Update(ctx, cdk8sAppProxy); err != nil { |
| 191 | +// logger.Error(err, "Failed to update status after successful reconciliation and application") |
| 192 | + |
| 193 | +// return ctrl.Result{}, err |
| 194 | +// } |
| 195 | +// logger.Info("Successfully reconciled Cdk8sAppProxy and applied/verified resources.") |
| 196 | + |
| 197 | +// return ctrl.Result{}, nil |
| 198 | +// } |
0 commit comments