|
| 1 | +package validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/fairwindsops/polaris/pkg/kube" |
| 9 | + "github.com/qri-io/jsonschema" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + autoscalingv1 "k8s.io/api/autoscaling/v1" |
| 13 | + policyv1 "k8s.io/api/policy/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + registerCustomChecks("pdbMinAvailableGreaterThanHPAMinReplicas", pdbMinAvailableGreaterThanHPAMinReplicas) |
| 20 | +} |
| 21 | + |
| 22 | +func pdbMinAvailableGreaterThanHPAMinReplicas(test schemaTestCase) (bool, []jsonschema.ValError, error) { |
| 23 | + if test.ResourceProvider == nil { |
| 24 | + logrus.Debug("ResourceProvider is nil") |
| 25 | + return true, nil, nil |
| 26 | + } |
| 27 | + |
| 28 | + deployment := &appsv1.Deployment{} |
| 29 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(test.Resource.Resource.Object, deployment) |
| 30 | + if err != nil { |
| 31 | + logrus.Warnf("error converting unstructured to Deployment: %v", err) |
| 32 | + return true, nil, nil |
| 33 | + } |
| 34 | + |
| 35 | + attachedPDB, err := hasPDBAttached(*deployment, test.ResourceProvider.Resources["policy/PodDisruptionBudget"]) |
| 36 | + if err != nil { |
| 37 | + logrus.Warnf("error getting PodDisruptionBudget: %v", err) |
| 38 | + return true, nil, nil |
| 39 | + } |
| 40 | + |
| 41 | + attachedHPA, err := hasHPAAttached(*deployment, test.ResourceProvider.Resources["autoscaling/HorizontalPodAutoscaler"]) |
| 42 | + if err != nil { |
| 43 | + logrus.Warnf("error getting HorizontalPodAutoscaler: %v", err) |
| 44 | + return true, nil, nil |
| 45 | + } |
| 46 | + |
| 47 | + if attachedPDB != nil && attachedHPA != nil { |
| 48 | + logrus.Debugf("both PDB and HPA are attached to deployment %s", deployment.Name) |
| 49 | + |
| 50 | + pdbMinAvailable, isPercent, err := getIntOrPercentValueSafely(attachedPDB.Spec.MinAvailable) |
| 51 | + if err != nil { |
| 52 | + logrus.Warnf("error getting getIntOrPercentValueSafely: %v", err) |
| 53 | + return true, nil, nil |
| 54 | + } |
| 55 | + |
| 56 | + if isPercent { |
| 57 | + // if the value is a percentage, we need to calculate the actual value |
| 58 | + if attachedHPA.Spec.MinReplicas == nil { |
| 59 | + logrus.Debug("attachedHPA.Spec.MinReplicas is nil") |
| 60 | + return true, nil, nil |
| 61 | + } |
| 62 | + |
| 63 | + pdbMinAvailable, err = intstr.GetScaledValueFromIntOrPercent(attachedPDB.Spec.MinAvailable, int(*attachedHPA.Spec.MinReplicas), true) |
| 64 | + if err != nil { |
| 65 | + logrus.Warnf("error getting minAvailable value from PodDisruptionBudget: %v", err) |
| 66 | + return true, nil, nil |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if attachedHPA.Spec.MinReplicas != nil && pdbMinAvailable >= int(*attachedHPA.Spec.MinReplicas) { |
| 71 | + return false, []jsonschema.ValError{ |
| 72 | + { |
| 73 | + PropertyPath: "spec.minAvailable", |
| 74 | + InvalidValue: pdbMinAvailable, |
| 75 | + Message: fmt.Sprintf("The minAvailable value in the PodDisruptionBudget(%s) is %d, which is greater or equal than the minReplicas value in the HorizontalPodAutoscaler(%s) (%d)", attachedPDB.Name, pdbMinAvailable, attachedHPA.Name, *attachedHPA.Spec.MinReplicas), |
| 76 | + }, |
| 77 | + }, nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return true, nil, nil |
| 82 | +} |
| 83 | + |
| 84 | +func hasPDBAttached(deployment appsv1.Deployment, pdbs []kube.GenericResource) (*policyv1.PodDisruptionBudget, error) { |
| 85 | + for _, generic := range pdbs { |
| 86 | + pdb := &policyv1.PodDisruptionBudget{} |
| 87 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(generic.Resource.Object, pdb) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("error converting unstructured to PodDisruptionBudget: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + if pdb.Spec.Selector == nil { |
| 93 | + logrus.Debug("pdb.Spec.Selector is nil") |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if matchesPDBForDeployment(deployment.Spec.Template.Labels, pdb.Spec.Selector.MatchLabels) { |
| 98 | + return pdb, nil |
| 99 | + } |
| 100 | + } |
| 101 | + return nil, nil |
| 102 | +} |
| 103 | + |
| 104 | +// matchesPDBForDeployment checks if the labels of the deployment match the labels of the PDB |
| 105 | +func matchesPDBForDeployment(deploymentLabels, pdbLabels map[string]string) bool { |
| 106 | + for key, value := range pdbLabels { |
| 107 | + if deploymentLabels[key] == value { |
| 108 | + return true |
| 109 | + } |
| 110 | + } |
| 111 | + return false |
| 112 | +} |
| 113 | + |
| 114 | +func hasHPAAttached(deployment appsv1.Deployment, hpas []kube.GenericResource) (*autoscalingv1.HorizontalPodAutoscaler, error) { |
| 115 | + for _, generic := range hpas { |
| 116 | + hpa := &autoscalingv1.HorizontalPodAutoscaler{} |
| 117 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(generic.Resource.Object, hpa) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("error converting unstructured to HorizontalPodAutoscaler: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + if hpa.Spec.ScaleTargetRef.Kind == "Deployment" && hpa.Spec.ScaleTargetRef.Name == deployment.Name { |
| 123 | + return hpa, nil |
| 124 | + } |
| 125 | + } |
| 126 | + return nil, nil |
| 127 | +} |
| 128 | + |
| 129 | +// getIntOrPercentValueSafely is a safer version of getIntOrPercentValue based on private function intstr.getIntOrPercentValueSafely |
| 130 | +func getIntOrPercentValueSafely(intOrStr *intstr.IntOrString) (int, bool, error) { |
| 131 | + switch intOrStr.Type { |
| 132 | + case intstr.Int: |
| 133 | + return intOrStr.IntValue(), false, nil |
| 134 | + case intstr.String: |
| 135 | + isPercent := false |
| 136 | + s := intOrStr.StrVal |
| 137 | + if strings.HasSuffix(s, "%") { |
| 138 | + isPercent = true |
| 139 | + s = strings.TrimSuffix(intOrStr.StrVal, "%") |
| 140 | + } else { |
| 141 | + return 0, false, fmt.Errorf("invalid type: string is not a percentage") |
| 142 | + } |
| 143 | + v, err := strconv.Atoi(s) |
| 144 | + if err != nil { |
| 145 | + return 0, false, fmt.Errorf("invalid value %q: %v", intOrStr.StrVal, err) |
| 146 | + } |
| 147 | + return int(v), isPercent, nil |
| 148 | + } |
| 149 | + return 0, false, fmt.Errorf("invalid type: neither int nor percentage") |
| 150 | +} |
0 commit comments