Repository: keikoproj/instance-manager (go)

Issue:
Cluster DNS calculation assumes that service cidr range is /16
**Is this a BUG REPORT or FEATURE REQUEST?**:
BUG REPORT 
**What happened**:
Pods running on AL2 nodes started having DNS issues when running the latest version of instance manager. Upon investigation, 
this appears to be because the EKS Cluster CIDR block is a /12 range - which causes this string replacement to fail https://github.com/keikoproj/instance-manager/blob/master/controllers/providers/aws/eks.go#L222. 

**What you expected to happen**:
Pods should have working cluster DNS.
**How to reproduce it (as minimally and precisely as possible)**:
Have an EKS cluster with a ServiceCidr range that is not a /16 and run AL2 nodes.
**Anything else we need to know?**:

**Environment**:

- Kubernetes version:

```bash
kubectl version -o yaml
```

**Other debugging information (if applicable)**:

- InstanceGroup status:

```bash
kubectl describe instancegroup <ig-name>
```

- controller logs:

```bash
kubectl logs <instance-manager pod>
```


Pull request description:
fix: calculate cluster dns correctly for non /16 blocks
Closes #318 
Signed-off-by: Jonah Back <jonah@jonahback.com>

Patch:
diff --git a/controllers/providers/aws/eks.go b/controllers/providers/aws/eks.go
index 8f3922d..3bf2d36 100644
--- a/controllers/providers/aws/eks.go
+++ b/controllers/providers/aws/eks.go
@@ -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, ".")
 }


Test patch:
diff --git a/controllers/providers/aws/eks_test.go b/controllers/providers/aws/eks_test.go
new file mode 100644
index 0000000..6e2c8fe
--- /dev/null
+++ b/controllers/providers/aws/eks_test.go
@@ -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"))
+
+}
\ No newline at end of file