|
| 1 | +package podtopologyconstraints |
| 2 | + |
| 3 | +import ( |
| 4 | + ks "github.com/zegl/kube-score/domain" |
| 5 | + "github.com/zegl/kube-score/score/checks" |
| 6 | + "github.com/zegl/kube-score/scorecard" |
| 7 | +) |
| 8 | + |
| 9 | +func Register(allChecks *checks.Checks) { |
| 10 | + allChecks.RegisterPodCheck("Pod Topology Spread Constraints", "Pod Topology Spread Constraints", podTopologySpreadConstraints) |
| 11 | +} |
| 12 | + |
| 13 | +func podTopologySpreadConstraints(pod ks.PodSpecer) (score scorecard.TestScore, err error) { |
| 14 | + spreads := pod.GetPodTemplateSpec().Spec.TopologySpreadConstraints |
| 15 | + |
| 16 | + if spreads == nil { |
| 17 | + score.Grade = scorecard.GradeAllOK |
| 18 | + score.AddComment("", "Pod Topology Spread Constraints", "No Pod Topology Spread Constraints set, kube-scheduler defaults assumed") |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + for _, spread := range spreads { |
| 23 | + if spread.LabelSelector == nil { |
| 24 | + score.Grade = scorecard.GradeCritical |
| 25 | + score.AddComment("", "Pod Topology Spread Constraints", "No labelSelector detected. A label selector is needed determine the number of pods in a topology domain") |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + if spread.MaxSkew == 0 { |
| 30 | + score.Grade = scorecard.GradeCritical |
| 31 | + score.AddComment("", "Pod Topology Spread Constraints", "MaxSkew is set to zero. This is not allowed.") |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if spread.MinDomains != nil && *spread.MinDomains == 0 { |
| 36 | + score.Grade = scorecard.GradeCritical |
| 37 | + score.AddComment("", "Pod Topology Spread Constraints", "MaxDomain set to zero. This is not allowed. Constraint behaves if minDomains is set to 1 if nil") |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if spread.TopologyKey == "" { |
| 42 | + score.Grade = scorecard.GradeCritical |
| 43 | + score.AddComment("", "Pod Topology Spread Constraints", "TopologyKey is not set. This is the key of node labels used to bucket nodes into a domain") |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + if spread.WhenUnsatisfiable != "DoNotSchedule" && spread.WhenUnsatisfiable != "ScheduleAnyway" { |
| 48 | + score.Grade = scorecard.GradeCritical |
| 49 | + score.AddComment("", "Pod Topology Spread Constraints", "Invalid WhenUnsatisfiable setting detected") |
| 50 | + return |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + score.Grade = scorecard.GradeAllOK |
| 55 | + score.AddComment("", "Pod Topology Spread Constraints", "Pod Topology Spread Constraints") |
| 56 | + return |
| 57 | +} |
0 commit comments