-
Notifications
You must be signed in to change notification settings - Fork 459
/
namespacedcloudprofile.go
87 lines (68 loc) · 3.48 KB
/
namespacedcloudprofile.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
78
79
80
81
82
83
84
85
86
87
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package validation
import (
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/gardener/gardener/pkg/apis/core"
"github.com/gardener/gardener/pkg/utils"
)
// ValidateNamespacedCloudProfile validates a NamespacedCloudProfile object.
func ValidateNamespacedCloudProfile(cloudProfile *core.NamespacedCloudProfile) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&cloudProfile.ObjectMeta, true, ValidateName, field.NewPath("metadata"))...)
allErrs = append(allErrs, validateNamespacedCloudProfileSpec(&cloudProfile.Spec, field.NewPath("spec"))...)
return allErrs
}
// ValidateNamespacedCloudProfileUpdate validates a NamespacedCloudProfile object before an update.
func ValidateNamespacedCloudProfileUpdate(newProfile, oldProfile *core.NamespacedCloudProfile) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateObjectMetaUpdate(&newProfile.ObjectMeta, &oldProfile.ObjectMeta, field.NewPath("metadata"))...)
allErrs = append(allErrs, ValidateNamespacedCloudProfileSpecUpdate(&newProfile.Spec, &oldProfile.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, ValidateNamespacedCloudProfile(newProfile)...)
return allErrs
}
// ValidateNamespacedCloudProfileSpecUpdate validates the spec update of a NamespacedCloudProfile.
func ValidateNamespacedCloudProfileSpecUpdate(oldProfile, newProfile *core.NamespacedCloudProfileSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateImmutableField(oldProfile.Parent, newProfile.Parent, fldPath.Child("parent"))...)
return allErrs
}
// validateNamespacedCloudProfileSpec validates the specification of a NamespacedCloudProfile object.
func validateNamespacedCloudProfileSpec(spec *core.NamespacedCloudProfileSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateParent(spec.Parent, fldPath.Child("parent"))...)
if spec.Kubernetes != nil {
allErrs = append(allErrs, validateKubernetesSettings(*spec.Kubernetes, fldPath.Child("kubernetes"))...)
}
if spec.MachineImages != nil {
allErrs = append(allErrs, validateMachineImages(spec.MachineImages, fldPath.Child("machineImages"))...)
}
if spec.MachineTypes != nil {
allErrs = append(allErrs, validateMachineTypes(spec.MachineTypes, fldPath.Child("machineTypes"))...)
}
if spec.VolumeTypes != nil {
allErrs = append(allErrs, validateVolumeTypes(spec.VolumeTypes, fldPath.Child("volumeTypes"))...)
}
if spec.Regions != nil {
allErrs = append(allErrs, validateRegions(spec.Regions, fldPath.Child("regions"))...)
}
if spec.CABundle != nil {
_, err := utils.DecodeCertificate([]byte(*(spec.CABundle)))
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("caBundle"), *(spec.CABundle), "caBundle is not a valid PEM-encoded certificate"))
}
}
return allErrs
}
func validateParent(parent core.CloudProfileReference, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if parent.Kind != "CloudProfile" {
allErrs = append(allErrs, field.NotSupported(fldPath.Child("kind"), parent.Kind, []string{"CloudProfile"}))
}
if len(parent.Name) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must provide a parent name"))
}
return allErrs
}