Skip to content

Commit

Permalink
refactor parseEndpoint to accept pod names with '.' in it
Browse files Browse the repository at this point in the history
kubernetes allows us to create pod with '.' in between, we should
not limit parsing such endpoints

Signed-off-by: Rewant Soni <resoni@redhat.com>
  • Loading branch information
rewantsoni authored and mergify[bot] committed Sep 24, 2024
1 parent c11db0b commit b0ae317
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
20 changes: 9 additions & 11 deletions internal/controller/csiaddons/csiaddonsnode_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ func (r *CSIAddonsNodeReconciler) resolveEndpoint(ctx context.Context, rawURL st
return rawURL, nil
} else if err != nil {
return "", err
} else if namespace == "" {
return "", fmt.Errorf("failed to get namespace from endpoint %q", rawURL)
} else if podname == "" {
return "", fmt.Errorf("failed to get pod from endpoint %q", rawURL)
}

pod := &corev1.Pod{}
Expand Down Expand Up @@ -250,15 +246,17 @@ func parseEndpoint(rawURL string) (string, string, string, error) {
}

// split hostname -> pod.namespace
parts := strings.Split(endpoint.Hostname(), ".")
podname := parts[0]
namespace := ""
if len(parts) == 2 {
namespace = parts[1]
} else if len(parts) > 2 {
return "", "", "", fmt.Errorf("hostname %q is not in <pod>.<namespace> format", endpoint.Hostname())
hostName := endpoint.Hostname()
lastIndex := strings.LastIndex(hostName, ".")

// check for empty podname and namespace
if lastIndex <= 0 || lastIndex == len(hostName)-1 {
return "", "", "", fmt.Errorf("hostname %q is not in <pod>.<namespace> format", hostName)
}

podname := hostName[0:lastIndex]
namespace := hostName[lastIndex+1:]

return namespace, podname, endpoint.Port(), nil
}

Expand Down
22 changes: 16 additions & 6 deletions internal/controller/csiaddons/csiaddonsnode_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,30 @@ func TestParseEndpoint(t *testing.T) {
_, _, _, err := parseEndpoint("1.2.3.4:5678")
assert.True(t, errors.Is(err, errLegacyEndpoint))

namespace, podname, port, err := parseEndpoint("pod://pod-name:5678")
// test empty namespace
_, _, _, err = parseEndpoint("pod://pod-name:5678")
assert.Error(t, err)

// test empty namespace
_, _, _, err = parseEndpoint("pod://pod-name.:5678")
assert.Error(t, err)

namespace, podname, port, err := parseEndpoint("pod://pod-name.csi-addons:5678")
assert.NoError(t, err)
assert.Equal(t, namespace, "")
assert.Equal(t, namespace, "csi-addons")
assert.Equal(t, podname, "pod-name")
assert.Equal(t, port, "5678")

namespace, podname, port, err = parseEndpoint("pod://pod-name.csi-addons:5678")
namespace, podname, port, err = parseEndpoint("pod://csi.pod.ns-cluster.local:5678")
assert.NoError(t, err)
assert.Equal(t, namespace, "csi-addons")
assert.Equal(t, podname, "pod-name")
assert.Equal(t, namespace, "local")
assert.Equal(t, podname, "csi.pod.ns-cluster")
assert.Equal(t, port, "5678")

_, _, _, err = parseEndpoint("pod://pod.ns.cluster.local:5678")
// test empty podname
_, _, _, err = parseEndpoint("pod://.local:5678")
assert.Error(t, err)

}

func TestParseCapabilities(t *testing.T) {
Expand Down

0 comments on commit b0ae317

Please sign in to comment.