Skip to content

Commit

Permalink
Update status even if reconciliation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sarataha committed Oct 26, 2023
1 parent 2e4ddbd commit 4e77f8e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/controller/automatedclusterdiscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (r *AutomatedClusterDiscoveryReconciler) Reconcile(ctx context.Context, req
// Set the value of the reconciliation request in status.
if v, ok := meta.ReconcileAnnotationValue(clusterDiscovery.GetAnnotations()); ok {
clusterDiscovery.Status.LastHandledReconcileAt = v
if err := r.Status().Update(ctx, clusterDiscovery); err != nil {
return ctrl.Result{}, err
}
}

if clusterDiscovery.Spec.Type == "aks" {
Expand Down
92 changes: 89 additions & 3 deletions internal/controller/automatedclusterdiscovery_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"errors"
"path/filepath"
"sort"
"testing"
Expand Down Expand Up @@ -565,13 +566,98 @@ func TestReconcilingWithAnnotationChange(t *testing.T) {
assert.Equal(t, aksCluster.Status.LastHandledReconcileAt, "testing")
}

func TestReconcilingWithAnnotationChangeWithError(t *testing.T) {
ctx := context.TODO()
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "config", "crd", "bases"),
"testdata/crds"},
}
cfg, err := testEnv.Start()
if err != nil {
t.Fatalf("Failed to start test environment: %v", err)
}
defer func() {
if err := testEnv.Stop(); err != nil {
t.Fatalf("Failed to stop test environment: %v", err)
}
}()

scheme := runtime.NewScheme()
assert.NoError(t, clustersv1alpha1.AddToScheme(scheme))
assert.NoError(t, gitopsv1alpha1.AddToScheme(scheme))
assert.NoError(t, clientgoscheme.AddToScheme(scheme))

k8sClient, err := client.New(cfg, client.Options{Scheme: scheme})
assert.NoError(t, err)

mgr, err := manager.New(cfg, manager.Options{
Scheme: scheme,
})
assert.NoError(t, err)

aksCluster := &clustersv1alpha1.AutomatedClusterDiscovery{
ObjectMeta: metav1.ObjectMeta{
Name: "test-aks",
Namespace: "default",
},
Spec: clustersv1alpha1.AutomatedClusterDiscoverySpec{
Type: "aks",
AKS: &clustersv1alpha1.AKS{
SubscriptionID: "subscription-123",
},
Interval: metav1.Duration{Duration: time.Minute},
},
}

err = k8sClient.Create(ctx, aksCluster)
assert.NoError(t, err)
defer deleteClusterDiscoveryAndInventory(t, k8sClient, aksCluster)

reconciler := &AutomatedClusterDiscoveryReconciler{
Client: k8sClient,
Scheme: scheme,
AKSProvider: func(providerID string) providers.Provider {
return &stubProvider{
responseErr: errors.New("test error"),
}
},
}
assert.NoError(t, reconciler.SetupWithManager(mgr))

key := types.NamespacedName{Name: aksCluster.Name, Namespace: aksCluster.Namespace}
_, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: key})
assert.Error(t, err)

err = k8sClient.Get(ctx, client.ObjectKeyFromObject(aksCluster), aksCluster)
assert.NoError(t, err)

assert.Equal(t, aksCluster.Status.LastHandledReconcileAt, "")

// add an annotation
aksCluster.Annotations = map[string]string{
meta.ReconcileRequestAnnotation: "testing",
}
err = k8sClient.Update(ctx, aksCluster)
assert.NoError(t, err)

_, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: key})
assert.Error(t, err)

err = k8sClient.Get(ctx, client.ObjectKeyFromObject(aksCluster), aksCluster)
assert.NoError(t, err)
assert.Equal(t, "testing", aksCluster.Annotations[meta.ReconcileRequestAnnotation])
assert.Equal(t, "testing", aksCluster.Status.LastHandledReconcileAt)
}

type stubProvider struct {
response []*providers.ProviderCluster
clusterID string
response []*providers.ProviderCluster
clusterID string
responseErr error
}

func (s *stubProvider) ListClusters(ctx context.Context) ([]*providers.ProviderCluster, error) {
return s.response, nil
return s.response, s.responseErr
}

func (s *stubProvider) ClusterID(ctx context.Context, kubeClient client.Reader) (string, error) {
Expand Down

0 comments on commit 4e77f8e

Please sign in to comment.