/
validation.go
77 lines (63 loc) · 3.64 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package validation
import (
"sort"
unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/core/v1"
"k8s.io/kubernetes/pkg/apis/core/validation"
quotav1 "github.com/openshift/api/quota/v1"
)
// convertClusterResourceQuotaToAppliedClusterQuota returns back a converted AppliedClusterResourceQuota which is NOT a deep copy.
func convertAppliedClusterResourceQuotaToClusterResourceQuota(in *quotav1.AppliedClusterResourceQuota) *quotav1.ClusterResourceQuota {
return "av1.ClusterResourceQuota{
ObjectMeta: in.ObjectMeta,
Spec: in.Spec,
Status: in.Status,
}
}
func ValidateClusterResourceQuota(clusterquota *quotav1.ClusterResourceQuota) field.ErrorList {
allErrs := validation.ValidateObjectMeta(&clusterquota.ObjectMeta, false, validation.ValidateResourceQuotaName, field.NewPath("metadata"))
hasSelectionCriteria := (clusterquota.Spec.Selector.LabelSelector != nil && len(clusterquota.Spec.Selector.LabelSelector.MatchLabels)+len(clusterquota.Spec.Selector.LabelSelector.MatchExpressions) > 0) ||
(len(clusterquota.Spec.Selector.AnnotationSelector) > 0)
if !hasSelectionCriteria {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "selector"), "must restrict the selected projects"))
}
if clusterquota.Spec.Selector.LabelSelector != nil {
allErrs = append(allErrs, unversionedvalidation.ValidateLabelSelector(clusterquota.Spec.Selector.LabelSelector, field.NewPath("spec", "selector", "labels"))...)
if len(clusterquota.Spec.Selector.LabelSelector.MatchLabels)+len(clusterquota.Spec.Selector.LabelSelector.MatchExpressions) == 0 {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "selector", "labels"), clusterquota.Spec.Selector.LabelSelector, "must restrict the selected projects"))
}
}
if clusterquota.Spec.Selector.AnnotationSelector != nil {
allErrs = append(allErrs, validation.ValidateAnnotations(clusterquota.Spec.Selector.AnnotationSelector, field.NewPath("spec", "selector", "annotations"))...)
}
internalQuota := &core.ResourceQuotaSpec{}
if err := v1.Convert_v1_ResourceQuotaSpec_To_core_ResourceQuotaSpec(&clusterquota.Spec.Quota, internalQuota, nil); err != nil {
panic(err)
}
internalStatus := &core.ResourceQuotaStatus{}
if err := v1.Convert_v1_ResourceQuotaStatus_To_core_ResourceQuotaStatus(&clusterquota.Status.Total, internalStatus, nil); err != nil {
panic(err)
}
allErrs = append(allErrs, validation.ValidateResourceQuotaSpec(internalQuota, field.NewPath("spec", "quota"))...)
allErrs = append(allErrs, validation.ValidateResourceQuotaStatus(internalStatus, field.NewPath("status", "overall"))...)
orderedNamespaces := clusterquota.Status.Namespaces.DeepCopy()
sort.Slice(orderedNamespaces, func(i, j int) bool {
return orderedNamespaces[i].Namespace < orderedNamespaces[j].Namespace
})
for _, namespace := range orderedNamespaces {
fldPath := field.NewPath("status", "namespaces").Key(namespace.Namespace)
for k, v := range namespace.Status.Used {
resPath := fldPath.Key(string(k))
allErrs = append(allErrs, validation.ValidateResourceQuotaResourceName(string(k), resPath)...)
allErrs = append(allErrs, validation.ValidateResourceQuantityValue(string(k), v, resPath)...)
}
}
return allErrs
}
func ValidateClusterResourceQuotaUpdate(clusterquota, oldClusterResourceQuota *quotav1.ClusterResourceQuota) field.ErrorList {
allErrs := validation.ValidateObjectMetaUpdate(&clusterquota.ObjectMeta, &oldClusterResourceQuota.ObjectMeta, field.NewPath("metadata"))
allErrs = append(allErrs, ValidateClusterResourceQuota(clusterquota)...)
return allErrs
}