|
| 1 | +package antiaffinity |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "golang.stackrox.io/kube-linter/internal/check" |
| 7 | + "golang.stackrox.io/kube-linter/internal/diagnostic" |
| 8 | + "golang.stackrox.io/kube-linter/internal/extract" |
| 9 | + "golang.stackrox.io/kube-linter/internal/lintcontext" |
| 10 | + "golang.stackrox.io/kube-linter/internal/objectkinds" |
| 11 | + "golang.stackrox.io/kube-linter/internal/templates" |
| 12 | + "golang.stackrox.io/kube-linter/internal/templates/antiaffinity/internal/params" |
| 13 | + coreV1 "k8s.io/api/core/v1" |
| 14 | + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/labels" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + templates.Register(check.Template{ |
| 20 | + HumanName: "Anti affinity not specified", |
| 21 | + Key: "anti-affinity", |
| 22 | + Description: "Flag objects with multiple replicas but inter-pod anti affinity not specified in the pod template spec", |
| 23 | + SupportedObjectKinds: check.ObjectKindsDesc{ |
| 24 | + ObjectKinds: []string{objectkinds.DeploymentLike}, |
| 25 | + }, |
| 26 | + Parameters: params.ParamDescs, |
| 27 | + ParseAndValidateParams: params.ParseAndValidate, |
| 28 | + Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { |
| 29 | + return func(_ *lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { |
| 30 | + replicas, found := extract.Replicas(object.K8sObject) |
| 31 | + if !found { |
| 32 | + return nil |
| 33 | + } |
| 34 | + if int(replicas) < p.MinReplicas { |
| 35 | + return nil |
| 36 | + } |
| 37 | + podTemplateSpec, hasPods := extract.PodTemplateSpec(object.K8sObject) |
| 38 | + if !hasPods { |
| 39 | + return nil |
| 40 | + } |
| 41 | + if affinity := podTemplateSpec.Spec.Affinity; affinity != nil && affinity.PodAntiAffinity != nil { |
| 42 | + preferredAffinity := affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution |
| 43 | + requiredAffinity := affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution |
| 44 | + for _, preferred := range preferredAffinity { |
| 45 | + if affinityTermMatchesLabelsAgainstNodes(preferred.PodAffinityTerm, podTemplateSpec.Namespace, podTemplateSpec.Labels) { |
| 46 | + return nil |
| 47 | + } |
| 48 | + } |
| 49 | + for _, required := range requiredAffinity { |
| 50 | + if affinityTermMatchesLabelsAgainstNodes(required, podTemplateSpec.Namespace, podTemplateSpec.Labels) { |
| 51 | + return nil |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return []diagnostic.Diagnostic{{Message: fmt.Sprintf("object has %d replicas but does not specify inter pod anti-affinity", replicas)}} |
| 56 | + }, nil |
| 57 | + }), |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func affinityTermMatchesLabelsAgainstNodes(affinityTerm coreV1.PodAffinityTerm, podNamespace string, podLabels map[string]string) bool { |
| 62 | + // If namespaces is not specified in the affinity term, that means the affinity term implicitly applies to the pod's namespace. |
| 63 | + if len(affinityTerm.Namespaces) > 0 { |
| 64 | + var matchingNSFound bool |
| 65 | + for _, ns := range affinityTerm.Namespaces { |
| 66 | + if ns == podNamespace { |
| 67 | + matchingNSFound = true |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + if !matchingNSFound { |
| 72 | + return false |
| 73 | + } |
| 74 | + } |
| 75 | + labelSelector, err := metaV1.LabelSelectorAsSelector(affinityTerm.LabelSelector) |
| 76 | + if err != nil { |
| 77 | + return false |
| 78 | + } |
| 79 | + if affinityTerm.TopologyKey == "kubernetes.io/hostname" && labelSelector.Matches(labels.Set(podLabels)) { |
| 80 | + return true |
| 81 | + } |
| 82 | + return false |
| 83 | +} |
0 commit comments