From 1061841e0a5a6ebd3b774b5e5bfa105aa5e7f60d Mon Sep 17 00:00:00 2001 From: Daniel Reuter Date: Tue, 16 Jan 2024 09:39:34 +0100 Subject: [PATCH] feat: use deployment strategy check from kubescore --- extadvice/advice.go | 5 ++-- extadvice/deployment_strategy/instructions.md | 3 ++ extcommon/kubescore.go | 7 +++-- extdeployment/deployment_discovery.go | 1 - extdeployment/deployment_discovery_test.go | 29 ++++++++++++++++++- go.mod | 6 ++-- go.sum | 12 ++++---- 7 files changed, 46 insertions(+), 17 deletions(-) diff --git a/extadvice/advice.go b/extadvice/advice.go index 67caf29..88dd1c9 100644 --- a/extadvice/advice.go +++ b/extadvice/advice.go @@ -99,17 +99,16 @@ func GetAdviceDescriptionImagePullPolicy() advice_kit_api.AdviceDefinition { } func GetAdviceDescriptionDeploymentStrategy() advice_kit_api.AdviceDefinition { - return advice_kit_api.AdviceDefinition{ Id: DeploymentStrategyID, Label: "Rolling Update Deployments", Version: extbuild.GetSemverVersionStringOrUnknown(), Icon: "data:image/svg+xml,%3Csvg%20width%3D%2224%22%20height%3D%2224%22%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0A%3Cpath%20d%3D%22M10.4478%202.65625C11.2739%202.24209%2012.2447%202.23174%2013.0794%202.62821L19.2871%205.57666C20.3333%206.07356%2021%207.12832%2021%208.28652V15.7134C21%2016.8717%2020.3333%2017.9264%2019.2871%2018.4233L13.0794%2021.3718C12.2447%2021.7682%2011.2739%2021.7579%2010.4478%2021.3437L4.65545%2018.4397L5.55182%2016.6518L11.3441%2019.5558C11.6195%2019.6939%2011.9431%2019.6973%2012.2214%2019.5652L18.429%2016.6167C18.7778%2016.4511%2019%2016.0995%2019%2015.7134V8.28652C19%207.90045%2018.7778%207.54887%2018.429%207.38323L12.2214%204.43479C11.9431%204.30263%2011.6195%204.30608%2011.3441%204.44413L5.55182%207.34814C5.21357%207.51773%205%207.8637%205%208.24208V15.7579C5%2016.1363%205.21357%2016.4822%205.55182%2016.6518L4.65545%2018.4397C3.6407%2017.931%203%2016.893%203%2015.7579V8.24208C3%207.10694%203.6407%206.06901%204.65545%205.56026L10.4478%202.65625Z%22%20fill%3D%22%231D2632%22%2F%3E%0A%3Cpath%20d%3D%22M11.1377%207.16465C11.5966%206.95033%2012.1359%206.94497%2012.5997%207.15014L16.0484%208.67595C16.6296%208.9331%2017%209.47893%2017%2010.0783V13.9217C17%2014.5211%2016.6296%2015.0669%2016.0484%2015.324L12.5997%2016.8499C12.1359%2017.055%2011.5966%2017.0497%2011.1377%2016.8353L7.9197%2015.3325C7.35594%2015.0693%207%2014.5321%207%2013.9447V10.0553C7%209.46787%207.35594%208.93074%207.9197%208.66747L11.1377%207.16465Z%22%20fill%3D%22%231D2632%22%2F%3E%0A%3C%2Fsvg%3E%0A", Tags: &[]string{"kubernetes", "deployment", "strategy"}, - AssessmentQueryApplicable: "target.type=\"" + extdeployment.DeploymentTargetType + "\"", + AssessmentQueryApplicable: "target.type=\"" + extdeployment.DeploymentTargetType + "\" AND k8s.specification.has-rolling-update-strategy IS PRESENT", Status: advice_kit_api.AdviceDefinitionStatus{ ActionNeeded: advice_kit_api.AdviceDefinitionStatusActionNeeded{ - AssessmentQuery: "k8s.deployment.strategy IS PRESENT AND k8s.deployment.strategy!=\"RollingUpdate\" ", + AssessmentQuery: "k8s.specification.has-rolling-update-strategy=\"false\"", Description: advice_kit_api.AdviceDefinitionStatusActionNeededDescription{ Instruction: ReadAdviceFile(DeploymentStrategyContent, "deployment_strategy/instructions.md"), Motivation: ReadAdviceFile(DeploymentStrategyContent, "deployment_strategy/motivation.md"), diff --git a/extadvice/deployment_strategy/instructions.md b/extadvice/deployment_strategy/instructions.md index 6332581..e1950e4 100644 --- a/extadvice/deployment_strategy/instructions.md +++ b/extadvice/deployment_strategy/instructions.md @@ -19,3 +19,6 @@ spec: ### Read More [Kubernetes Documentation - Deployment Strategy](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy) + +--- +This advice is powered by [kube-score](https://kube-score.com/). diff --git a/extcommon/kubescore.go b/extcommon/kubescore.go index ecb79ec..8a848b7 100644 --- a/extcommon/kubescore.go +++ b/extcommon/kubescore.go @@ -57,6 +57,7 @@ func GetKubeScoreForDeployment(deployment *appsv1.Deployment, services []*corev1 addContainerBasedScore(scores, attributes, "container-image-tag", "k8s.container.image.with-latest-tag") addContainerBasedScore(scores, attributes, "container-image-pull-policy", "k8s.container.image.without-image-pull-policy-always") addSimpleScore(scores, attributes, "deployment-has-host-podantiaffinity", "k8s.specification.has-host-podantiaffinity") + addSimpleScore(scores, attributes, "deployment-strategy", "k8s.specification.has-rolling-update-strategy") return attributes } @@ -180,9 +181,9 @@ func addProbesScores(scores []scorecard.TestScore, attributes map[string][]strin return } } - } - if (check.Comments == nil || len(check.Comments) == 0) && check.Grade == scorecard.GradeAllOK { - attributes["k8s.specification.probes.summary"] = []string{"OK"} + if (check.Comments == nil || len(check.Comments) == 0) && check.Grade == scorecard.GradeAllOK { + attributes["k8s.specification.probes.summary"] = []string{"OK"} + } } } diff --git a/extdeployment/deployment_discovery.go b/extdeployment/deployment_discovery.go index 7d37b1c..ba61b30 100644 --- a/extdeployment/deployment_discovery.go +++ b/extdeployment/deployment_discovery.go @@ -96,7 +96,6 @@ func (d *deploymentDiscovery) DiscoverTargets(_ context.Context) ([]discovery_ki "k8s.deployment": {deployment.Name}, "k8s.cluster-name": {extconfig.Config.ClusterName}, "k8s.distribution": {d.k8s.Distribution}, - "k8s.deployment.strategy": {string(deployment.Spec.Strategy.Type)}, "k8s.deployment.min-ready-seconds": {fmt.Sprintf("%d", deployment.Spec.MinReadySeconds)}, } if deployment.Spec.Replicas != nil { diff --git a/extdeployment/deployment_discovery_test.go b/extdeployment/deployment_discovery_test.go index a094bc9..91bb227 100644 --- a/extdeployment/deployment_discovery_test.go +++ b/extdeployment/deployment_discovery_test.go @@ -49,7 +49,6 @@ func Test_deploymentDiscovery(t *testing.T) { "k8s.deployment.label.best-city": {"Kevelaer"}, "k8s.label.best-city": {"Kevelaer"}, "k8s.deployment.min-ready-seconds": {"10"}, - "k8s.deployment.strategy": {"RollingUpdate"}, "k8s.specification.replicas": {"3"}, "k8s.cluster-name": {"development"}, "k8s.pod.name": {"shop-pod-aaaaa", "shop-pod-bbbbb"}, @@ -244,6 +243,34 @@ func Test_deploymentDiscovery(t *testing.T) { "k8s.container.image.without-image-pull-policy-always": {"nginx", "shop"}, }, }, + { + name: "should report wrong rollout strategy", + pods: []*v1.Pod{testPod("aaaaa", nil)}, + deployment: testDeployment(func(deployment *appsv1.Deployment) { + deployment.Spec.Strategy.Type = appsv1.RecreateDeploymentStrategyType + }), + service: testService(nil), + expectedAttributes: map[string][]string{ + "k8s.specification.has-rolling-update-strategy": {"false"}, + }, + }, + { + name: "should not report wrong rollout strategy if no service is defined", + pods: []*v1.Pod{testPod("aaaaa", nil)}, + deployment: testDeployment(func(deployment *appsv1.Deployment) { + deployment.Spec.Strategy.Type = appsv1.RecreateDeploymentStrategyType + }), + expectedAttributesAbsence: []string{"k8s.specification.has-rolling-update-strategy"}, + }, + { + name: "should report good rollout strategy", + pods: []*v1.Pod{testPod("aaaaa", nil)}, + deployment: testDeployment(nil), + service: testService(nil), + expectedAttributes: map[string][]string{ + "k8s.specification.has-rolling-update-strategy": {"true"}, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/go.mod b/go.mod index 2218218..d16ccea 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/steadybit/discovery-kit/go/discovery_kit_test v1.1.2 github.com/steadybit/extension-kit v1.8.11 github.com/stretchr/testify v1.8.4 - github.com/zegl/kube-score v1.17.1-0.20231219134923-2460a44c6705 + github.com/zegl/kube-score v1.17.1-0.20240105120143-9c453bc76d1e golang.org/x/exp v0.0.0-20231127185646-65229373498e golang.org/x/text v0.14.0 k8s.io/api v0.29.0 @@ -121,8 +121,8 @@ require ( golang.org/x/crypto v0.17.0 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/oauth2 v0.15.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/term v0.15.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/term v0.16.0 // indirect golang.org/x/time v0.5.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/protobuf v1.31.0 // indirect diff --git a/go.sum b/go.sum index 0349770..b560690 100644 --- a/go.sum +++ b/go.sum @@ -311,8 +311,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/zegl/kube-score v1.17.1-0.20231219134923-2460a44c6705 h1:NwWjSeapOClYi2N0iJVaZy62uDhiWU9ItS3UrYI0bao= -github.com/zegl/kube-score v1.17.1-0.20231219134923-2460a44c6705/go.mod h1:hcOWuN06r3ht4ajdb746v4pJuqQb1Oe6j7KbVpXBSDw= +github.com/zegl/kube-score v1.17.1-0.20240105120143-9c453bc76d1e h1:ynwtKYwifKDaiIl9BgXYPBlwn7MuIZqUoAJF181i5AI= +github.com/zegl/kube-score v1.17.1-0.20240105120143-9c453bc76d1e/go.mod h1:WdtBud0STZcmhHUT65+tg9DkLuYM6HVBjiLBxJmki1k= github.com/zmwangx/debounce v1.0.0 h1:Dyf+WfLESjc2bqFKHgI1dZTW9oh6CJm8SBDkhXrwLB4= github.com/zmwangx/debounce v1.0.0/go.mod h1:U+/QHt+bSMdUh8XKOb6U+MQV5Ew4eS8M3ua5WJ7Ns6I= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= @@ -370,15 +370,15 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= -golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= -golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=