Repository: keikoproj/instance-manager
Pull request #289 (2021-05-03 23:09:12)

fix: add validation for OsFamily annotation value
Signed-off-by: Eytan Avisror <eytan_avisror@intuit.com>

Fixes: #288 

codecov[bot]: # [Codecov](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj) Report
> Merging [#289](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj) (e5b2182) into [master](https://codecov.io/gh/keikoproj/instance-manager/commit/503d90c445366b4bc301420d85731c12332b1a16?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj) (503d90c) will **decrease** coverage by `0.01%`.
> The diff coverage is `75.00%`.

[![Impacted file tree graph](https://codecov.io/gh/keikoproj/instance-manager/pull/289/graphs/tree.svg?width=650&height=150&src=pr&token=IJbjmSBliL&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj)](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj)

```diff
@@            Coverage Diff             @@
##           master     #289      +/-   ##
==========================================
- Coverage   70.00%   69.99%   -0.02%     
==========================================
  Files          19       19              
  Lines        3094     3096       +2     
==========================================
+ Hits         2166     2167       +1     
- Misses        788      789       +1     
  Partials      140      140              
```


| [Impacted Files](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj) | Coverage Δ | |
|---|---|---|
| [controllers/provisioners/eks/eks.go](https://codecov.io/gh/keikoproj/instance-manager/pull/289/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj#diff-Y29udHJvbGxlcnMvcHJvdmlzaW9uZXJzL2Vrcy9la3MuZ28=) | `86.95% <75.00%> (-1.68%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj). Last update [503d90c...e5b2182](https://codecov.io/gh/keikoproj/instance-manager/pull/289?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keikoproj).


Patch:
diff --git a/controllers/provisioners/eks/eks.go b/controllers/provisioners/eks/eks.go
index 79421e2..311ce53 100644
--- a/controllers/provisioners/eks/eks.go
+++ b/controllers/provisioners/eks/eks.go
@@ -53,6 +53,7 @@ var (
 	InstanceMgrLifecycleLabel = "instancemgr.keikoproj.io/lifecycle"
 	InstanceMgrImageLabel     = "instancemgr.keikoproj.io/image"
 
+	AllowedOsFamilies         = []string{OsFamilyWindows, OsFamilyBottleRocket, OsFamilyAmazonLinux2}
 	DefaultManagedPolicies    = []string{"AmazonEKSWorkerNodePolicy", "AmazonEC2ContainerRegistryReadOnly"}
 	CNIManagedPolicy          = "AmazonEKS_CNI_Policy"
 	AutoscalingReadOnlyPolicy = "AutoScalingReadOnlyAccess"
@@ -135,8 +136,12 @@ func (ctx *EksInstanceGroupContext) GetOsFamily() string {
 		instanceGroup = ctx.GetInstanceGroup()
 		annotations   = instanceGroup.GetAnnotations()
 	)
-	if _, exists := annotations[OsFamilyAnnotation]; exists {
-		return annotations[OsFamilyAnnotation]
+
+	if v, exists := annotations[OsFamilyAnnotation]; exists {
+		if common.ContainsEqualFold(AllowedOsFamilies, v) {
+			return annotations[OsFamilyAnnotation]
+		}
+		ctx.Log.Info("used unsupported annotation value '%v=%v', will default to 'amazonlinux2', allowed values: %+v", OsFamilyAnnotation, v, AllowedOsFamilies)
 	}
 
 	return OsFamilyAmazonLinux2


Test patch:
diff --git a/controllers/provisioners/eks/helpers_test.go b/controllers/provisioners/eks/helpers_test.go
index c79b71d..ff2f7d3 100644
--- a/controllers/provisioners/eks/helpers_test.go
+++ b/controllers/provisioners/eks/helpers_test.go
@@ -131,6 +131,10 @@ func TestGetBasicUserDataAmazonLinux2(t *testing.T) {
 			Data:  "bar",
 		},
 	}
+
+	// validate that wrong value still defaults to amazonlinux2
+	ig.Annotations[OsFamilyAnnotation] = "wrong"
+
 	var (
 		args            = ctx.GetBootstrapArgs()
 		kubeletArgs     = ctx.GetKubeletExtraArgs()