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

Handle correct providerID format #36

Merged
merged 1 commit into from
Oct 4, 2017
Merged

Conversation

andrewsykim
Copy link
Contributor

Addresses #15.

In v1.7.x this wasn't important since CCM never set providerID in the first place. In v1.8, it will so we should handle the expected format. The expected format for node's providerID is digitalocean://droplet-id, example:

apiVersion: v1
kind: Node
metadata:
  annotations:
    node.alpha.kubernetes.io/ttl: "0"
    volumes.kubernetes.io/controller-managed-attach-detach: "true"
  creationTimestamp: 2017-09-27T14:30:44Z
  labels:
    beta.kubernetes.io/arch: amd64
    beta.kubernetes.io/instance-type: 512mb
    beta.kubernetes.io/os: linux
    kubernetes.io/hostname: k8s-worker-01
    kubernetes.io/role: node
  name: k8s-worker-01
  resourceVersion: "7123"
  selfLink: /api/v1/nodes/k8s-worker-01
  uid: 720492a5-a390-11e7-8009-6ea0aaddb09e
spec:
  externalID: k8s-worker-01
  providerID: digitalocean://64011171

return "", errors.New("providerID cannot be empty string")
}

split := strings.Split(providerID, "/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings.SplitN(providerID, "/", 3)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

}

// since split[0] is actually "digitalocean:"
if strings.TrimSuffix(split[0], ":") != providerName {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just if split[0] != providerName + ":" {?

// since split[0] is actually "digitalocean:"
if strings.TrimSuffix(split[0], ":") != providerName {
return "", fmt.Errorf("provider name from providerID should be digitalocean: %s", providerID)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return "", fmt.Errorf("provider name from providerID should be %s://%s", providerName, providerID)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

providerID is already in the format providerName://id so it would be digitalocean://digitalocean://droplet-id. I think the confusion here is that providerID is the droplet ID which it is not, it's a separate concept from Kubernetes land :P

// dropletIDFromProviderID returns a droplet's ID extracted from the node's
// providerID spec. The providerID spec should be retrievable from the Kubernetes
// node object. The expected format is: digitalocean://droplet-id
func dropletIDFromProviderID(providerID string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some comments below that are just nitpicks. Take them or not.

Also consider: is there any reason to split on the slashes at all? Why not just

func dropletIDFromProviderID(providerID string) (string, error) {
    const scheme := providerID + "://"

    if !strings.HasPrefix(providerID, scheme) != 0 {
        return "", fmt.Errorf("unexpected providerID format: %s, format should be: digitalocean://12345", providerID)
    }
    return strings.TrimPrefix(providerID, scheme), nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean

const scheme := providerName + "://"

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here I ended up doing strings.Split(providerID, "/") since it makes validation stricter. For example if the provider ID happened to be digitalocean:////droplet-id (which I have seen before on AWS clusters) then trim prefix wouldn't work here. Seems unlikely but possible? The problem mainly is that CCM doesn't have control over what the node provider ID is set to, we can only hope for the best and assume it's a format we expect, otherwise we actually call the *ByNodeName methods instead.

Copy link
Contributor

@bhcleek bhcleek Sep 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great reason for using splitN or HasPrefix, though. Right now on master, instances.NodeAddressesByProviderID will look for the providerID given. All this PR is about is making sure that digitalocean:// is a prefix of providerID. The PR, though, changes behavior where previously instances.dropletById would have been given //droplet-id.

Unless we're prepared to validate other rules about the droplet-id value, not allowing leading /s seems out of scope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said all that, this is all a nitpick; I don't intend it to be a blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants