Skip to content

Commit 346b8cf

Browse files
authored
Merge pull request #233 from SovereignCloudStack/ani/issues/232
🌱 Removing the temporary shouldDelete option to kubeClient.Apply that was not used anyway and always set on true
2 parents 3ac74a0 + 594c5e8 commit 346b8cf

File tree

8 files changed

+46
-49
lines changed

8 files changed

+46
-49
lines changed

internal/controller/clusteraddon_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
238238
if clusterAddon.Spec.Version != metadata.Versions.Components.ClusterAddon {
239239
clusterAddon.Status.Ready = false
240240

241-
shouldRequeue, err := r.templateAndApplyClusterAddonHelmChart(ctx, in, true)
241+
shouldRequeue, err := r.templateAndApplyClusterAddonHelmChart(ctx, in)
242242
if err != nil {
243243
conditions.MarkFalse(clusterAddon, csov1alpha1.HelmChartAppliedCondition, csov1alpha1.FailedToApplyObjectsReason, clusterv1.ConditionSeverityError, "failed to apply: %s", err.Error())
244244
return ctrl.Result{}, fmt.Errorf("failed to apply helm chart: %w", err)
@@ -273,7 +273,7 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
273273

274274
// if condition is false we have not yet successfully applied the helm chart
275275
if conditions.IsFalse(clusterAddon, csov1alpha1.HelmChartAppliedCondition) {
276-
shouldRequeue, err := r.templateAndApplyClusterAddonHelmChart(ctx, in, true)
276+
shouldRequeue, err := r.templateAndApplyClusterAddonHelmChart(ctx, in)
277277
if err != nil {
278278
conditions.MarkFalse(clusterAddon, csov1alpha1.HelmChartAppliedCondition, csov1alpha1.FailedToApplyObjectsReason, clusterv1.ConditionSeverityError, "failed to apply: %s", err.Error())
279279
return ctrl.Result{}, fmt.Errorf("failed to apply helm chart: %w", err)
@@ -718,7 +718,7 @@ func (r *ClusterAddonReconciler) getAddonStagesInput(restConfig *rest.Config, cl
718718
return addonStages, nil
719719
}
720720

721-
func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx context.Context, in *templateAndApplyClusterAddonInput, shouldDelete bool) (bool, error) {
721+
func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx context.Context, in *templateAndApplyClusterAddonInput) (bool, error) {
722722
clusterAddonChart := in.clusterAddonChartPath
723723
var shouldRequeue bool
724724

@@ -734,7 +734,7 @@ func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx conte
734734

735735
kubeClient := r.KubeClientFactory.NewClient(clusterAddonNamespace, in.restConfig)
736736

737-
newResources, shouldRequeue, err := kubeClient.Apply(ctx, helmTemplate, in.clusterAddon.Status.Resources, shouldDelete)
737+
newResources, shouldRequeue, err := kubeClient.Apply(ctx, helmTemplate, in.clusterAddon.Status.Resources)
738738
if err != nil {
739739
return false, fmt.Errorf("failed to apply objects from cluster addon Helm chart: %w", err)
740740
}

internal/controller/clusteraddon_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var _ = Describe("ClusterAddonReconciler", func() {
6161
},
6262
}
6363

64-
testEnv.KubeClient.On("Apply", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*csov1alpha1.Resource{}, false, nil)
64+
testEnv.KubeClient.On("Apply", mock.Anything, mock.Anything, mock.Anything).Return([]*csov1alpha1.Resource{}, false, nil)
6565
})
6666

6767
AfterEach(func() {
@@ -240,7 +240,7 @@ var _ = Describe("ClusterAddonReconciler", func() {
240240
}, timeout, interval).Should(BeTrue())
241241

242242
By("checking Update method was called")
243-
Expect(testEnv.KubeClient.AssertCalled(GinkgoT(), "Apply", mock.Anything, mock.Anything, mock.Anything, mock.Anything)).To(BeTrue())
243+
Expect(testEnv.KubeClient.AssertCalled(GinkgoT(), "Apply", mock.Anything, mock.Anything, mock.Anything)).To(BeTrue())
244244
})
245245

246246
It("should not call update if the ClusterAddon version does not change in the ClusterClass update", func() {

internal/controller/clusteraddoncreate_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var _ = Describe("ClusterAddonCreateReconciler", func() {
6969

7070
key = types.NamespacedName{Name: fmt.Sprintf("cluster-addon-%s", cluster.Name), Namespace: testNs.Name}
7171

72-
testEnv.KubeClient.On("Apply", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*csov1alpha1.Resource{}, false, nil)
72+
testEnv.KubeClient.On("Apply", mock.Anything, mock.Anything, mock.Anything).Return([]*csov1alpha1.Resource{}, false, nil)
7373
})
7474

7575
AfterEach(func() {

internal/controller/clusterstackrelease_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (r *ClusterStackReleaseReconciler) templateAndApply(ctx context.Context, re
288288
return false, fmt.Errorf("template is empty")
289289
}
290290

291-
newResources, shouldRequeue, err := kubeClient.Apply(ctx, template, clusterStackRelease.Status.Resources, true)
291+
newResources, shouldRequeue, err := kubeClient.Apply(ctx, template, clusterStackRelease.Status.Resources)
292292
if err != nil {
293293
conditions.MarkFalse(clusterStackRelease, csov1alpha1.HelmChartAppliedCondition, csov1alpha1.FailedToApplyObjectsReason, clusterv1.ConditionSeverityError, "failed to apply")
294294
return false, fmt.Errorf("failed to apply cluster class helm chart: %w", err)

internal/controller/clusterstackrelease_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ var _ = Describe("ClusterStackRelease validation", func() {
311311
},
312312
}
313313

314-
testEnv.KubeClient.On("Apply", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*csov1alpha1.Resource{}, false, nil)
314+
testEnv.KubeClient.On("Apply", mock.Anything, mock.Anything, mock.Anything).Return([]*csov1alpha1.Resource{}, false, nil)
315315
})
316316

317317
AfterEach(func() {

pkg/kube/fake/kube.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewFactory() kubeclient.Factory {
4141
}
4242

4343
// Apply applies the given yaml object.
44-
func (*kube) Apply(_ context.Context, _ []byte, _ []*csov1alpha1.Resource, _ bool) (_ []*csov1alpha1.Resource, _ bool, _ error) {
44+
func (*kube) Apply(_ context.Context, _ []byte, _ []*csov1alpha1.Resource) (_ []*csov1alpha1.Resource, _ bool, _ error) {
4545
return nil, false, nil
4646
}
4747

pkg/kube/kube.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type kube struct {
3737

3838
// Client has all the meathod for helm chart kube operation.
3939
type Client interface {
40-
Apply(ctx context.Context, template []byte, oldResources []*csov1alpha1.Resource, shouldDelete bool) (newResources []*csov1alpha1.Resource, shouldRequeue bool, err error)
40+
Apply(ctx context.Context, template []byte, oldResources []*csov1alpha1.Resource) (newResources []*csov1alpha1.Resource, shouldRequeue bool, err error)
4141
Delete(ctx context.Context, template []byte, oldResources []*csov1alpha1.Resource) (newResources []*csov1alpha1.Resource, shouldRequeue bool, err error)
4242

4343
ApplyNewClusterStack(ctx context.Context, oldTemplate, newTemplate []byte) (newResources []*csov1alpha1.Resource, shouldRequeue bool, err error)
@@ -163,7 +163,7 @@ func (k *kube) DeleteNewClusterStack(ctx context.Context, template []byte) (newR
163163
return newResources, shouldRequeue, nil
164164
}
165165

166-
func (k *kube) Apply(ctx context.Context, template []byte, oldResources []*csov1alpha1.Resource, shouldDelete bool) (newResources []*csov1alpha1.Resource, shouldRequeue bool, err error) {
166+
func (k *kube) Apply(ctx context.Context, template []byte, oldResources []*csov1alpha1.Resource) (newResources []*csov1alpha1.Resource, shouldRequeue bool, err error) {
167167
logger := log.FromContext(ctx)
168168

169169
objs, err := parseK8sYaml(template)
@@ -212,32 +212,29 @@ func (k *kube) Apply(ctx context.Context, template []byte, oldResources []*csov1
212212
newResources = append(newResources, resource)
213213
}
214214

215-
// TODO: cleanup shouldDelete
216-
if shouldDelete {
217-
// make a diff between new objs and oldResources to find out
218-
// a) if an object is in oldResources and synced and not in new objs, then delete should be attempted
219-
// then, all objs should be applied by create or update
220-
// at the end, we should delete objects that are supposed to be deleted
221-
for _, resource := range resourcesToBeDeleted(oldResources, objs) {
222-
// call the function and get dynamic.ResourceInterface
223-
// getDynamicResourceInterface
224-
logger.Info("resource are being deleted", "kind", resource.Kind, "name", resource.Name, "namespace", resource.Namespace)
225-
226-
dr, err := GetDynamicResourceInterface(k.Namespace, k.RestConfig, resource.GroupVersionKind())
227-
if err != nil {
228-
return nil, false, fmt.Errorf("failed to get dynamic resource interface: %w", err)
229-
}
230-
231-
if err := dr.Delete(ctx, resource.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
232-
reterr := fmt.Errorf("failed to delete object: %w", err)
233-
logger.Error(reterr, "failed to delete object", "obj", resource.GroupVersionKind(), "namespacedName", resource.NamespacedName())
234-
235-
// append resource to status and requeue again to be able to retry deletion
236-
resource.Status = csov1alpha1.ResourceStatusNotSynced
237-
resource.Error = reterr.Error()
238-
newResources = append(newResources, resource)
239-
shouldRequeue = true
240-
}
215+
// make a diff between new objs and oldResources to find out
216+
// a) if an object is in oldResources and synced and not in new objs, then delete should be attempted
217+
// then, all objs should be applied by create or update
218+
// at the end, we should delete objects that are supposed to be deleted
219+
for _, resource := range resourcesToBeDeleted(oldResources, objs) {
220+
// call the function and get dynamic.ResourceInterface
221+
// getDynamicResourceInterface
222+
logger.Info("resource are being deleted", "kind", resource.Kind, "name", resource.Name, "namespace", resource.Namespace)
223+
224+
dr, err := GetDynamicResourceInterface(k.Namespace, k.RestConfig, resource.GroupVersionKind())
225+
if err != nil {
226+
return nil, false, fmt.Errorf("failed to get dynamic resource interface: %w", err)
227+
}
228+
229+
if err := dr.Delete(ctx, resource.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
230+
reterr := fmt.Errorf("failed to delete object: %w", err)
231+
logger.Error(reterr, "failed to delete object", "obj", resource.GroupVersionKind(), "namespacedName", resource.NamespacedName())
232+
233+
// append resource to status and requeue again to be able to retry deletion
234+
resource.Status = csov1alpha1.ResourceStatusNotSynced
235+
resource.Error = reterr.Error()
236+
newResources = append(newResources, resource)
237+
shouldRequeue = true
241238
}
242239
}
243240

pkg/kube/mocks/Client.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)