@@ -30,6 +30,7 @@ import (
30
30
operatorv1alpha1 "github.com/karmada-io/karmada/operator/pkg/apis/operator/v1alpha1"
31
31
"github.com/karmada-io/karmada/operator/pkg/util"
32
32
"github.com/karmada-io/karmada/pkg/util/lifted"
33
+ "k8s.io/apimachinery/pkg/util/intstr"
33
34
)
34
35
35
36
func validateCRDTarball (crdTarball * operatorv1alpha1.CRDTarball , fldPath * field.Path ) (errs field.ErrorList ) {
@@ -97,6 +98,42 @@ func validateETCD(etcd *operatorv1alpha1.Etcd, karmadaName string, fldPath *fiel
97
98
return errs
98
99
}
99
100
101
+ // validateCommonSettings validates the common settings of a component, including PDB configuration
102
+ func validateCommonSettings (commonSettings * operatorv1alpha1.CommonSettings , fldPath * field.Path ) (errs field.ErrorList ) {
103
+ if commonSettings == nil {
104
+ return nil
105
+ }
106
+
107
+ if commonSettings .PodDisruptionBudgetConfig != nil {
108
+ pdbConfig := commonSettings .PodDisruptionBudgetConfig
109
+ pdbPath := fldPath .Child ("podDisruptionBudgetConfig" )
110
+
111
+ // Check if both minAvailable and maxUnavailable are set (mutually exclusive)
112
+ if pdbConfig .MinAvailable != nil && pdbConfig .MaxUnavailable != nil {
113
+ errs = append (errs , field .Invalid (pdbPath , pdbConfig , "minAvailable and maxUnavailable are mutually exclusive, only one can be set" ))
114
+ }
115
+
116
+ // Check if at least one of minAvailable or maxUnavailable is set
117
+ if pdbConfig .MinAvailable == nil && pdbConfig .MaxUnavailable == nil {
118
+ errs = append (errs , field .Invalid (pdbPath , pdbConfig , "either minAvailable or maxUnavailable must be set" ))
119
+ }
120
+
121
+ // Validate minAvailable against replicas if replicas is set
122
+ if pdbConfig .MinAvailable != nil && commonSettings .Replicas != nil {
123
+ replicas := * commonSettings .Replicas
124
+ if pdbConfig .MinAvailable .Type == intstr .Int {
125
+ minAvailable := int32 (pdbConfig .MinAvailable .IntValue ())
126
+ if minAvailable > replicas {
127
+ errs = append (errs , field .Invalid (pdbPath .Child ("minAvailable" ), pdbConfig .MinAvailable ,
128
+ fmt .Sprintf ("minAvailable (%d) cannot be greater than replicas (%d)" , minAvailable , replicas )))
129
+ }
130
+ }
131
+ }
132
+ }
133
+
134
+ return errs
135
+ }
136
+
100
137
func validate (karmada * operatorv1alpha1.Karmada ) error {
101
138
var errs field.ErrorList
102
139
@@ -107,6 +144,32 @@ func validate(karmada *operatorv1alpha1.Karmada) error {
107
144
108
145
errs = append (errs , validateKarmadaAPIServer (components .KarmadaAPIServer , karmada .Spec .HostCluster , fldPath .Child ("karmadaAPIServer" ))... )
109
146
errs = append (errs , validateETCD (components .Etcd , karmada .Name , fldPath .Child ("etcd" ))... )
147
+
148
+ // Validate PDB configurations for all components
149
+ if components .KarmadaAPIServer != nil {
150
+ errs = append (errs , validateCommonSettings (& components .KarmadaAPIServer .CommonSettings , fldPath .Child ("karmadaAPIServer" ))... )
151
+ }
152
+ if components .KarmadaControllerManager != nil {
153
+ errs = append (errs , validateCommonSettings (& components .KarmadaControllerManager .CommonSettings , fldPath .Child ("karmadaControllerManager" ))... )
154
+ }
155
+ if components .KarmadaScheduler != nil {
156
+ errs = append (errs , validateCommonSettings (& components .KarmadaScheduler .CommonSettings , fldPath .Child ("karmadaScheduler" ))... )
157
+ }
158
+ if components .KarmadaWebhook != nil {
159
+ errs = append (errs , validateCommonSettings (& components .KarmadaWebhook .CommonSettings , fldPath .Child ("karmadaWebhook" ))... )
160
+ }
161
+ if components .KarmadaDescheduler != nil {
162
+ errs = append (errs , validateCommonSettings (& components .KarmadaDescheduler .CommonSettings , fldPath .Child ("karmadaDescheduler" ))... )
163
+ }
164
+ if components .KarmadaSearch != nil {
165
+ errs = append (errs , validateCommonSettings (& components .KarmadaSearch .CommonSettings , fldPath .Child ("karmadaSearch" ))... )
166
+ }
167
+ if components .KarmadaMetricsAdapter != nil {
168
+ errs = append (errs , validateCommonSettings (& components .KarmadaMetricsAdapter .CommonSettings , fldPath .Child ("karmadaMetricsAdapter" ))... )
169
+ }
170
+ if components .Etcd != nil && components .Etcd .Local != nil {
171
+ errs = append (errs , validateCommonSettings (& components .Etcd .Local .CommonSettings , fldPath .Child ("etcd" ).Child ("local" ))... )
172
+ }
110
173
}
111
174
112
175
if len (errs ) > 0 {
0 commit comments