Skip to content

Commit

Permalink
Fixed review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Tiptur <[email protected]>
  • Loading branch information
Sachin Tiptur committed Jan 16, 2023
1 parent d066d36 commit 6aa9dc1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ package-hosted directory like this:

```bash
cd cloud-controller-manager/cmd/digitalocean-cloud-controller-manager
REGION=fra1 DO_ACCESS_TOKEN=your_access_token DO_IP_ADDR_FAMILIES=ipv4 go run main.go \
REGION=fra1 DO_ACCESS_TOKEN=your_access_token go run main.go \
--kubeconfig <path to your kubeconfig file> \
--leader-elect=false --v=5 --cloud-provider=digitalocean
```
Expand All @@ -79,10 +79,10 @@ You might also need to provide your DigitalOcean access token in
the cloud controller to start, but in that case, you will not be able to
validate integration with DigitalOcean API.

The `DO_IP_ADDR_FAMILIES` is used to configure the required IP familes and the
order in which address should be populated in nodes status. The accepted values
are one of the `{"", "ipv4", "ipv6", "ipv4,ipv6", "ipv6,ipv4"}`.IPv4 is the
default, if not set or empty.
The `DO_IP_ADDR_FAMILIES` is used to configure the required IP families and the
order in which the addresses should be populated in nodes status. The accepted values
are one of the `"ipv4", "ipv6"` or a comma-separated list of multiple IP address
families. IPv4 is the default, if not set or empty.

Please note that if you use a Kubernetes cluster created on DigitalOcean, there
will be a cloud controller manager running in the cluster already, so your local
Expand Down
23 changes: 12 additions & 11 deletions cloud-controller-manager/do/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ const (

var version string

var ipFamilyOpts = []string{"", "ipv4", "ipv6", "ipv4,ipv6", "ipv6,ipv4"}
var ipFamilies string

type tokenSource struct {
AccessToken string
}
Expand Down Expand Up @@ -162,10 +159,8 @@ func newCloud() (cloudprovider.Interface, error) {
addr = fmt.Sprintf("%s:%s", addrHost, addrPort)
}

// var ipFamilies string
ipf, set := os.LookupEnv(doIPAddrFamiliesEnv)
ipFamilies = ipf
if set && !containsString(ipFamilyOpts, ipFamilies) {
if set && !validateAndSetIPFamilies(ipf) {
return nil, fmt.Errorf("invalid value set for environment variable %q", doIPAddrFamiliesEnv)
}

Expand Down Expand Up @@ -292,11 +287,17 @@ func (c *cloud) HasClusterID() bool {
return false
}

func containsString(vals []string, val string) bool {
for _, v := range vals {
if v == val {
return true
func validateAndSetIPFamilies(ipf string) bool {
for _, v := range strings.Split(ipf, ",") {
ipf := strings.TrimSpace(v)
switch ipf {
case "ipv4":
ipFamilies = append(ipFamilies, ipv4Family)
case "ipv6":
ipFamilies = append(ipFamilies, ipv6Family)
default:
return false
}
}
return false
return true
}
35 changes: 26 additions & 9 deletions cloud-controller-manager/do/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/digitalocean/godo"
v1 "k8s.io/api/core/v1"
)

type IPFamily string

var ipFamilies []IPFamily

const (
ipv4Family IPFamily = "ipv4"
ipv6Family IPFamily = "ipv6"
)

// apiResultsPerPage is the maximum page size that DigitalOcean's api supports.
const apiResultsPerPage = 200

Expand Down Expand Up @@ -130,20 +138,31 @@ func nodeAddresses(droplet *godo.Droplet) ([]v1.NodeAddress, error) {
var addresses []v1.NodeAddress
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeHostName, Address: droplet.Name})

for _, i := range strings.Split(ipFamilies, ",") {
addr, err := discoverAddress(droplet, i)
// default case when DO_IP_ADDR_FAMILIES is not set
if ipFamilies == nil {
addr, err := discoverAddress(droplet, ipv4Family)
if err != nil {
return nil, fmt.Errorf("could not get addresses for %s : %v", i, err)
return nil, fmt.Errorf("could not get addresses for %s : %v", ipv4Family, err)
}
addresses = append(addresses, addr...)
} else {
for _, i := range ipFamilies {
addr, err := discoverAddress(droplet, i)
if err != nil {
return nil, fmt.Errorf("could not get addresses for %s : %v", i, err)
}
addresses = append(addresses, addr...)
}
}

return addresses, nil
}

func discoverAddress(droplet *godo.Droplet, family string) ([]v1.NodeAddress, error) {
func discoverAddress(droplet *godo.Droplet, family IPFamily) ([]v1.NodeAddress, error) {
var addresses []v1.NodeAddress
if family == "ipv4" || family == "" {

switch family {
case ipv4Family:
privateIP, err := droplet.PrivateIPv4()
if err != nil || privateIP == "" {
return nil, fmt.Errorf("could not get private ip: %v", err)
Expand All @@ -156,15 +175,13 @@ func discoverAddress(droplet *godo.Droplet, family string) ([]v1.NodeAddress, er
}
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: publicIP})
return addresses, nil
}
if family == "ipv6" {
case ipv6Family:
publicIPv6, err := droplet.PublicIPv6()
if err != nil || publicIPv6 == "" {
return nil, fmt.Errorf("could not get public ipv6: %v", err)
}
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: publicIPv6})
return addresses, nil
}

return addresses, nil
}
10 changes: 3 additions & 7 deletions cloud-controller-manager/do/droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func TestNodeAddresses(t *testing.T) {
Address: "99.99.99.99",
},
{
Type: "public",
Type: v1.NodeExternalIP,
Address: "2a01::10",
},
}

ipFamilies = []IPFamily{ipv4Family, ipv6Family}
addresses, err := instances.NodeAddresses(context.TODO(), "test-droplet")

if !reflect.DeepEqual(addresses, expectedAddresses) {
Expand Down Expand Up @@ -238,12 +238,8 @@ func TestNodeAddressesByProviderID(t *testing.T) {
Type: v1.NodeExternalIP,
Address: "99.99.99.99",
},
{
Type: "public",
Address: "2a01::10",
},
}

ipFamilies = []IPFamily{ipv4Family}
addresses, err := instances.NodeAddressesByProviderID(context.TODO(), "digitalocean://123")

if !reflect.DeepEqual(addresses, expectedAddresses) {
Expand Down

0 comments on commit 6aa9dc1

Please sign in to comment.