diff --git a/.reuse/dep5 b/.reuse/dep5 index 02af697..4c14e41 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -9,7 +9,7 @@ Source: https://github.com/ironcore-dev/cloud-provider-metal Files: .github/* .gitignore - .golangci.yaml + .golangci.yml .dockerignore CODE_OF_CONDUCT.md CODEOWNERS diff --git a/pkg/cloudprovider/metal/instances_v2.go b/pkg/cloudprovider/metal/instances_v2.go index 9cfaa5c..4410635 100644 --- a/pkg/cloudprovider/metal/instances_v2.go +++ b/pkg/cloudprovider/metal/instances_v2.go @@ -8,6 +8,8 @@ import ( "fmt" "strings" + "k8s.io/apimachinery/pkg/types" + metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -120,7 +122,7 @@ func (o *metalInstancesV2) InstanceMetadata(ctx context.Context, node *corev1.No providerID := node.Spec.ProviderID if providerID == "" { - providerID = fmt.Sprintf("%s://%s/%s", ProviderName, o.metalNamespace, serverClaim.Name) + providerID = o.getProviderIDForSeverClaim(serverClaim) } // TODO: use constants here @@ -148,6 +150,10 @@ func (o *metalInstancesV2) InstanceMetadata(ctx context.Context, node *corev1.No }, nil } +func (o *metalInstancesV2) getProviderIDForSeverClaim(serverClaim *metalv1alpha1.ServerClaim) string { + return fmt.Sprintf("%s://%s/%s/%s", ProviderName, o.metalNamespace, serverClaim.Name, serverClaim.UID) +} + func (o *metalInstancesV2) getServerClaimForNode(ctx context.Context, node *corev1.Node) (*metalv1alpha1.ServerClaim, error) { var serverClaim *metalv1alpha1.ServerClaim if node.Spec.ProviderID != "" { @@ -174,7 +180,7 @@ func (o *metalInstancesV2) getServerClaimForNode(ctx context.Context, node *core } func (o *metalInstancesV2) getServerClaimFromProviderID(ctx context.Context, providerID string) (*metalv1alpha1.ServerClaim, error) { - objKey, err := getObjectKeyFromProviderID(providerID) + objKey, objUID, err := getObjectKeyFromProviderID(providerID) if err != nil { return nil, fmt.Errorf("failed to get object key for ProviderID %s: %w", providerID, err) } @@ -185,16 +191,22 @@ func (o *metalInstancesV2) getServerClaimFromProviderID(ctx context.Context, pro } return nil, fmt.Errorf("failed to get server claim object for ProviderID %s: %w", providerID, err) } + + // Check for UID equality. For backwards compatibility we only compare if the UID is part of the ProviderID. + if len(objUID) > 0 && serverClaim.UID != objUID { + return nil, cloudprovider.InstanceNotFound + } + return serverClaim, nil } -func getObjectKeyFromProviderID(providerID string) (client.ObjectKey, error) { +func getObjectKeyFromProviderID(providerID string) (client.ObjectKey, types.UID, error) { parts := strings.Split(strings.TrimPrefix(providerID, fmt.Sprintf("%s://", ProviderName)), "/") - if len(parts) != 2 { - return client.ObjectKey{}, fmt.Errorf("invalid format of ProviderID %s", providerID) + if len(parts) != 3 { + return client.ObjectKey{}, "", fmt.Errorf("invalid format of ProviderID %s", providerID) } return client.ObjectKey{ Namespace: parts[0], Name: parts[1], - }, nil + }, types.UID(parts[2]), nil } diff --git a/pkg/cloudprovider/metal/instances_v2_test.go b/pkg/cloudprovider/metal/instances_v2_test.go index 7a350a7..5e4e2b9 100644 --- a/pkg/cloudprovider/metal/instances_v2_test.go +++ b/pkg/cloudprovider/metal/instances_v2_test.go @@ -98,7 +98,7 @@ var _ = Describe("InstancesV2", func() { instanceMetadata, err := instancesProvider.InstanceMetadata(ctx, node) Expect(err).NotTo(HaveOccurred()) Eventually(instanceMetadata).Should(SatisfyAll( - HaveField("ProviderID", getProviderID(serverClaim.Namespace, serverClaim.Name)), + HaveField("ProviderID", getProviderIDForServerClaim(serverClaim)), HaveField("InstanceType", "foo"), HaveField("NodeAddresses", ContainElements( corev1.NodeAddress{ @@ -163,7 +163,7 @@ var _ = Describe("InstancesV2", func() { GenerateName: "test-", }, Spec: corev1.NodeSpec{ - ProviderID: getProviderID(serverClaim.Namespace, serverClaim.Name), + ProviderID: getProviderIDForServerClaim(serverClaim), }, } Expect(k8sClient.Create(ctx, node)).To(Succeed()) @@ -183,7 +183,7 @@ var _ = Describe("InstancesV2", func() { instanceMetadata, err := instancesProvider.InstanceMetadata(ctx, node) Expect(err).NotTo(HaveOccurred()) Eventually(instanceMetadata).Should(SatisfyAll( - HaveField("ProviderID", getProviderID(serverClaim.Namespace, serverClaim.Name)), + HaveField("ProviderID", getProviderIDForServerClaim(serverClaim)), HaveField("InstanceType", "foo"), HaveField("NodeAddresses", ContainElements( corev1.NodeAddress{ @@ -207,7 +207,7 @@ var _ = Describe("InstancesV2", func() { Name: "foo", }, Spec: corev1.NodeSpec{ - ProviderID: getProviderID(ns.Name, "bar"), + ProviderID: fmt.Sprintf("%s://%s/%s/%s", ProviderName, ns.Name, "foo", "bar"), }, } Expect(k8sClient.Create(ctx, node)).To(Succeed()) @@ -252,6 +252,6 @@ var _ = Describe("InstancesV2", func() { }) }) -func getProviderID(namespace, serverClaimName string) string { - return fmt.Sprintf("%s://%s/%s", ProviderName, namespace, serverClaimName) +func getProviderIDForServerClaim(serverClaim *metalv1alpha1.ServerClaim) string { + return fmt.Sprintf("%s://%s/%s/%s", ProviderName, serverClaim.Namespace, serverClaim.Name, serverClaim.UID) }