Skip to content

Commit

Permalink
fix: calculate cluster dns correctly for non /16 blocks
Browse files Browse the repository at this point in the history
Signed-off-by: Jonah Back <jonah@jonahback.com>
  • Loading branch information
backjo committed Jun 29, 2021
1 parent a5c9617 commit b93e8b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions controllers/providers/aws/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func (w *AwsWorker) GetDNSClusterIP(cluster *eks.Cluster) string {
return ""
}
serviceCidr := aws.StringValue(cluster.KubernetesNetworkConfig.ServiceIpv4Cidr)
// addresses assigned from either the 10.100.0.0/16 or 172.20.0.0/16 CIDR blocks
return strings.ReplaceAll(serviceCidr, "0/16", "10")
// addresses are by default assigned from either the 10.100.0.0/16 or 172.20.0.0/16 CIDR blocks.
// custom ranges could be blocks that are not /16 size.
ip := strings.Split(serviceCidr, "/")[0] // remove block size
blocks := strings.Split(ip, ".")
blocks[3] = "10" // replace last byte in IP address with "10" as is the convention
return strings.Join(blocks, ".")
}
19 changes: 19 additions & 0 deletions controllers/providers/aws/eks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package aws

import (
"github.com/aws/aws-sdk-go/service/eks"
"github.com/onsi/gomega"
"testing"
)

func TestClusterDns(t *testing.T) {
var (
g = gomega.NewGomegaWithT(t)
)

awsWorker := AwsWorker{}
cidr := "172.16.0.0/12"
ip := awsWorker.GetDNSClusterIP(&eks.Cluster{KubernetesNetworkConfig: &eks.KubernetesNetworkConfigResponse{ServiceIpv4Cidr: &cidr}})
g.Expect(ip).To(gomega.Equal("172.16.0.10"))

}

0 comments on commit b93e8b9

Please sign in to comment.