Skip to content

Commit 876fbdd

Browse files
CecileRobertMichonk8s-infra-cherrypick-robot
authored andcommitted
Use hash for AzureClusterIdentity finalizer
1 parent cb18268 commit 876fbdd

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

controllers/helpers.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package controllers
1818

1919
import (
2020
"context"
21+
"crypto/sha256"
22+
"encoding/hex"
2123
"encoding/json"
2224
"fmt"
2325

@@ -601,10 +603,21 @@ func GetClusterIdentityFromRef(ctx context.Context, c client.Client, azureCluste
601603
return nil, nil
602604
}
603605

604-
func clusterIdentityFinalizer(prefix, clusterNamespace, clusterName string) string {
606+
// deprecatedClusterIdentityFinalizer is was briefly used to compute a finalizer without a hash in releases v1.5.1 and v1.4.4.
607+
// It is kept here to ensure that we can remove it from existing clusters for backwards compatibility.
608+
// This function should be removed in a future release.
609+
func deprecatedClusterIdentityFinalizer(prefix, clusterNamespace, clusterName string) string {
605610
return fmt.Sprintf("%s/%s-%s", prefix, clusterNamespace, clusterName)
606611
}
607612

613+
// clusterIdentityFinalizer generates a finalizer key.
614+
// The finalizer key is a combination of the prefix and a hash of the cluster name and namespace.
615+
// We use a hash to ensure that the finalizer key name is not longer than 63 characters.
616+
func clusterIdentityFinalizer(prefix, clusterNamespace, clusterName string) string {
617+
hash := sha256.Sum224([]byte(fmt.Sprintf("%s-%s", clusterNamespace, clusterName)))
618+
return fmt.Sprintf("%s/%s", prefix, hex.EncodeToString(hash[:]))
619+
}
620+
608621
// EnsureClusterIdentity ensures that the identity ref is allowed in the namespace and sets a finalizer.
609622
func EnsureClusterIdentity(ctx context.Context, c client.Client, object conditions.Setter, identityRef *corev1.ObjectReference, finalizerPrefix string) error {
610623
name := object.GetName()
@@ -621,6 +634,8 @@ func EnsureClusterIdentity(ctx context.Context, c client.Client, object conditio
621634
if err != nil {
622635
return errors.Wrap(err, "failed to init patch helper")
623636
}
637+
// Remove deprecated finalizer if it exists.
638+
controllerutil.RemoveFinalizer(identity, deprecatedClusterIdentityFinalizer(finalizerPrefix, namespace, name))
624639
// If the AzureClusterIdentity doesn't have our finalizer, add it.
625640
controllerutil.AddFinalizer(identity, clusterIdentityFinalizer(finalizerPrefix, namespace, name))
626641
// Register the finalizer immediately to avoid orphaning Azure resources on delete.

controllers/helpers_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"strings"
2324
"testing"
2425

2526
"github.com/Azure/go-autorest/autorest"
@@ -677,3 +678,86 @@ const (
677678
"cloudProviderBackoffJitter": 1.2000000000000002
678679
}`
679680
)
681+
682+
func Test_clusterIdentityFinalizer(t *testing.T) {
683+
type args struct {
684+
prefix string
685+
clusterNamespace string
686+
clusterName string
687+
}
688+
tests := []struct {
689+
name string
690+
args args
691+
want string
692+
}{
693+
{
694+
name: "cluster identity finalizer should be deterministic",
695+
args: args{
696+
prefix: infrav1.ClusterFinalizer,
697+
clusterNamespace: "foo",
698+
clusterName: "bar",
699+
},
700+
want: "azurecluster.infrastructure.cluster.x-k8s.io/48998dbcd8fb929369c78981cbfb6f26145ea0412e6e05a1423941a6",
701+
},
702+
{
703+
name: "long cluster name and namespace",
704+
args: args{
705+
prefix: infrav1.ClusterFinalizer,
706+
clusterNamespace: "this-is-a-very-very-very-very-very-very-very-very-very-long-namespace-name",
707+
clusterName: "this-is-a-very-very-very-very-very-very-very-very-very-long-cluster-name",
708+
},
709+
want: "azurecluster.infrastructure.cluster.x-k8s.io/557d064144d2b495db694dedc53c9a1e9bd8575bdf06b5b151972614",
710+
},
711+
}
712+
for _, tt := range tests {
713+
t.Run(tt.name, func(t *testing.T) {
714+
got := clusterIdentityFinalizer(tt.args.prefix, tt.args.clusterNamespace, tt.args.clusterName)
715+
if got != tt.want {
716+
t.Errorf("clusterIdentityFinalizer() = %v, want %v", got, tt.want)
717+
}
718+
key := strings.Split(got, "/")[1]
719+
if len(key) > 63 {
720+
t.Errorf("clusterIdentityFinalizer() name %v length = %v should be less than 63 characters", key, len(key))
721+
}
722+
})
723+
}
724+
}
725+
726+
func Test_deprecatedClusterIdentityFinalizer(t *testing.T) {
727+
type args struct {
728+
prefix string
729+
clusterNamespace string
730+
clusterName string
731+
}
732+
tests := []struct {
733+
name string
734+
args args
735+
want string
736+
}{
737+
{
738+
name: "cluster identity finalizer should be deterministic",
739+
args: args{
740+
prefix: infrav1.ClusterFinalizer,
741+
clusterNamespace: "foo",
742+
clusterName: "bar",
743+
},
744+
want: "azurecluster.infrastructure.cluster.x-k8s.io/foo-bar",
745+
},
746+
{
747+
name: "long cluster name and namespace",
748+
args: args{
749+
prefix: infrav1.ClusterFinalizer,
750+
clusterNamespace: "this-is-a-very-very-very-very-very-very-very-very-very-long-namespace-name",
751+
clusterName: "this-is-a-very-very-very-very-very-very-very-very-very-long-cluster-name",
752+
},
753+
want: "azurecluster.infrastructure.cluster.x-k8s.io/this-is-a-very-very-very-very-very-very-very-very-very-long-namespace-name-this-is-a-very-very-very-very-very-very-very-very-very-long-cluster-name",
754+
},
755+
}
756+
for _, tt := range tests {
757+
t.Run(tt.name, func(t *testing.T) {
758+
if got := deprecatedClusterIdentityFinalizer(tt.args.prefix, tt.args.clusterNamespace, tt.args.clusterName); got != tt.want {
759+
t.Errorf("deprecatedClusterIdentityFinalizer() = %v, want %v", got, tt.want)
760+
}
761+
})
762+
}
763+
}

0 commit comments

Comments
 (0)