Skip to content

Commit 882f091

Browse files
authored
Merge pull request #2703 from k8s-infra-cherrypick-robot/cherry-pick-2699-to-release-1.4
[release-1.4] Use hash for AzureClusterIdentity finalizer
2 parents a0fc6b8 + 4b3e15b commit 882f091

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

0 commit comments

Comments
 (0)