Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove token validity metric on delete of CRD #16

Merged
merged 1 commit into from
Jan 4, 2024
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
20 changes: 20 additions & 0 deletions controllers/emergencyaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/appuio/emergency-credentials-controller/controllers/stores"
)

const EmergencyAccountFinalizer = "emergencyaccounts.cluster.appuio.io/finalizer"

type Clock interface {
Now() time.Time
}
Expand Down Expand Up @@ -60,6 +62,24 @@ func (r *EmergencyAccountReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, fmt.Errorf("unable to get EmergencyAccount resource: %w", err)
}

if instance.DeletionTimestamp != nil {
l.Info("EmergencyAccount resource is being deleted")
deleteVerifiedTokensValidUntil(instance.Name)
if controllerutil.RemoveFinalizer(instance, EmergencyAccountFinalizer) {
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{}, fmt.Errorf("unable to remove finalizer: %w", err)
}
}
return ctrl.Result{}, nil
}

if controllerutil.AddFinalizer(instance, EmergencyAccountFinalizer) {
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{}, fmt.Errorf("unable to add finalizer: %w", err)
}
return ctrl.Result{}, nil
}

sa, err := r.reconcileSA(ctx, instance)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to reconcile ServiceAccount: %w", err)
Expand Down
22 changes: 21 additions & 1 deletion controllers/emergencyaccount_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/go-logr/logr/testr"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -16,6 +17,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

emcv1beta1 "github.com/appuio/emergency-credentials-controller/api/v1beta1"
Expand Down Expand Up @@ -55,11 +57,18 @@ func Test_EmergencyAccountReconciler_Reconcile(t *testing.T) {
Clock: clock,
}

// Create token
// Create finalizer
_, err := subject.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(ea)})
require.NoError(t, err)
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(ea), ea))
t.Logf("status %+v", ea.Status)
require.Len(t, ea.Finalizers, 1, "finalizer should be created")

// Create token
_, err = subject.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(ea)})
require.NoError(t, err)
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(ea), ea))
t.Logf("status %+v", ea.Status)
require.Len(t, ea.Status.Tokens, 1, "token should be created")
require.WithinDuration(t, clock.Now(), ea.Status.LastTokenCreationTimestamp.Time, 0, "last token creation timestamp should be set")
lastTimestamp := ea.Status.LastTokenCreationTimestamp.Time
Expand Down Expand Up @@ -97,6 +106,17 @@ func Test_EmergencyAccountReconciler_Reconcile(t *testing.T) {
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(ea), ea))
t.Logf("status %+v", ea.Status)
require.Len(t, ea.Status.Tokens, 3, "should add a new token")

// Finalizer should be removed and no metric left
require.NoError(t, c.Delete(ctx, ea))
deleted := &emcv1beta1.EmergencyAccount{}
_, err = subject.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(ea)})
require.NoError(t, err)
require.Error(t, c.Get(ctx, client.ObjectKeyFromObject(ea), deleted))
require.Len(t, deleted.Finalizers, 0, "finalizer should be removed")
ml, err := testutil.GatherAndCount(metrics.Registry, MetricsNamespace+"_verified_tokens_valid_until_seconds")
require.NoError(t, err)
require.Equal(t, 0, ml, "metric should be removed")
}

type fakeClientControl struct {
Expand Down
4 changes: 4 additions & 0 deletions controllers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var (
)
)

func deleteVerifiedTokensValidUntil(emergencyAccount string) {
verifiedTokensValidUntil.Delete(prometheus.Labels{"emergency_account": emergencyAccount})
}

func init() {
metrics.Registry.MustRegister(verifiedTokensValidUntil)
}
Loading