Skip to content

Commit

Permalink
Include ServerClaim UID in Node ProviderID
Browse files Browse the repository at this point in the history
  • Loading branch information
afritzler committed Oct 10, 2024
1 parent e617156 commit 84c8254
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions pkg/cloudprovider/metal/instances_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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)
}
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions pkg/cloudprovider/metal/instances_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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())
Expand All @@ -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{
Expand All @@ -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())
Expand Down Expand Up @@ -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)
}

0 comments on commit 84c8254

Please sign in to comment.