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

Allow multiple addresses in k0s cloud provider #4856

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/cloud-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Adding a static IP address to a node using `kubectl`:
```shell
kubectl annotate \
node <node> \
k0sproject.io/node-ip-external=<external IP>
k0sproject.io/node-ip-external=<external IP>[,<external IP 2>][,<external IP 3>]
```

Both IPv4 and IPv6 addresses are supported.
Both IPv4 and IPv6 addresses and multiple comma-separated values are supported.

### Defaults

Expand Down
22 changes: 13 additions & 9 deletions inttest/k0scloudprovider/k0scloudprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package k0scloudprovider
import (
"context"
"fmt"
"strings"
"testing"

"github.com/k0sproject/k0s/inttest/common"
Expand Down Expand Up @@ -59,38 +60,41 @@ func (s *K0sCloudProviderSuite) TestK0sGetsUp() {
s.testAddAddress(ctx, kc, nodeName, "1.2.3.4")
s.testAddAddress(ctx, kc, nodeName, "2041:0000:140F::875B:131B")
s.testAddAddress(ctx, kc, nodeName, "GIGO")
s.testAddAddress(ctx, kc, nodeName, "1.2.3.4,GIGO")
}

// testAddAddress adds the provided address to a node and ensures that the
// cloud-provider will set it on the node.
func (s *K0sCloudProviderSuite) testAddAddress(ctx context.Context, client kubernetes.Interface, nodeName string, ip string) {
func (s *K0sCloudProviderSuite) testAddAddress(ctx context.Context, client kubernetes.Interface, nodeName string, addresses string) {
// Adds or sets the special ExternalIPAnnotation with an IP address to the worker
// node, and after a few seconds the IP address should be listed as a NodeExternalIP.
patch := fmt.Sprintf(
`[{"op":"add", "path":"/metadata/annotations/%s", "value":"%s"}]`,
jsonpointer.Escape(k0scloudprovider.ExternalIPAnnotation), jsonpointer.Escape(ip),
jsonpointer.Escape(k0scloudprovider.ExternalIPAnnotation), jsonpointer.Escape(addresses),
)
_, err := client.CoreV1().Nodes().Patch(ctx, nodeName, types.JSONPatchType, []byte(patch), metav1.PatchOptions{})
s.Require().NoError(err, "Failed to add or set the annotation for the external IP address")

// Need to ensure that a matching 'ExternalIP' address has been added,
// indicating that k0s-cloud-provider properly processed the annotation.
s.T().Logf("Waiting for k0s-cloud-provider to update the external IP on node %s to %s", nodeName, ip)
s.T().Logf("Waiting for k0s-cloud-provider to update the external IP on node %s to %s", nodeName, addresses)
s.Require().NoError(watch.Nodes(client.CoreV1().Nodes()).
WithObjectName(nodeName).
WithErrorCallback(common.RetryWatchErrors(s.T().Logf)).
Until(ctx, func(node *corev1.Node) (bool, error) {
for _, nodeAddr := range node.Status.Addresses {
if nodeAddr.Type == corev1.NodeExternalIP {
if nodeAddr.Address == ip {
return true, nil
for _, addr := range strings.Split(addresses, ",") {
for _, nodeAddr := range node.Status.Addresses {
if nodeAddr.Type == corev1.NodeExternalIP {
if nodeAddr.Address == addr {
return true, nil
}
break
}
break
}
}

return false, nil
}), "While waiting for k0s-cloud-provider to update the external IP on node %s to %s", nodeName, ip)
}), "While waiting for k0s-cloud-provider to update the external IP on node %s to %s", nodeName, addresses)
}

func TestK0sCloudProviderSuite(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/k0scloudprovider/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package k0scloudprovider

import (
"strings"

v1 "k8s.io/api/core/v1"
cloudproviderapi "k8s.io/cloud-provider/api"
)
Expand Down Expand Up @@ -82,6 +84,8 @@ func populateExternalAddress(addrs *[]v1.NodeAddress, node *v1.Node) {

// Search the nodes annotations for any external IP address definitions.
if externalIP, ok := node.Annotations[ExternalIPAnnotation]; ok {
*addrs = append(*addrs, v1.NodeAddress{Type: v1.NodeExternalIP, Address: externalIP})
for _, addr := range strings.Split(externalIP, ",") {
*addrs = append(*addrs, v1.NodeAddress{Type: v1.NodeExternalIP, Address: addr})
}
}
}
19 changes: 18 additions & 1 deletion pkg/k0scloudprovider/addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestPopulateInternalAddress(t *testing.T) {

var testDataPopulateExternalAddress = []populateAddressTestData{
{
name: "Equality",
name: "Equality single address",
input: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
Expand All @@ -136,6 +136,23 @@ var testDataPopulateExternalAddress = []populateAddressTestData{
{Type: v1.NodeExternalIP, Address: "1.2.3.4"},
},
},
{
name: "Equality multiple addresses",
input: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
ExternalIPAnnotation: "1.2.3.4,2041:0000:140F::875B:131B",
},
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{},
},
},
output: []v1.NodeAddress{
{Type: v1.NodeExternalIP, Address: "1.2.3.4"},
{Type: v1.NodeExternalIP, Address: "2041:0000:140F::875B:131B"},
},
},
{
name: "Missing",
input: &v1.Node{
Expand Down
Loading