@@ -120,41 +120,174 @@ func (c *Cluster) InstancePoolsMap() (instancePoolsMap map[string][]*clusterv1al
120120 return instancePoolsMap
121121}
122122
123- // validate hub instancePool types
124- func validateHubTypes (poolMap map [string ][]* clusterv1alpha1.InstancePool , clusterType string ) (result error ) {
125- if len (poolMap [clusterv1alpha1 .InstancePoolTypeBastion ]) != 1 {
126- result = multierror .Append (result , fmt .Errorf ("a hub needs to have exactly one '%s' server pool" , clusterv1alpha1 .InstancePoolTypeBastion ))
123+ func (c * Cluster ) validateClusterInstancePoolTypes () error {
124+ errMap := make (map [string ]bool )
125+ poolMap := make (map [string ]bool )
126+
127+ switch c .Type () {
128+ case clusterv1alpha1 .ClusterTypeHub :
129+ poolMap = map [string ]bool {
130+ clusterv1alpha1 .InstancePoolTypeVault : true ,
131+ clusterv1alpha1 .InstancePoolTypeBastion : true ,
132+ }
133+
134+ break
135+
136+ case clusterv1alpha1 .ClusterTypeClusterMulti :
137+ poolMap = map [string ]bool {
138+ clusterv1alpha1 .InstancePoolTypeMaster : true ,
139+ clusterv1alpha1 .InstancePoolTypeWorker : true ,
140+ clusterv1alpha1 .InstancePoolTypeEtcd : true ,
141+ clusterv1alpha1 .InstancePoolTypeMasterEtcd : true ,
142+ clusterv1alpha1 .InstancePoolTypeHybrid : true ,
143+ clusterv1alpha1 .InstancePoolTypeAll : true ,
144+ }
145+
146+ break
147+
148+ case clusterv1alpha1 .ClusterTypeClusterSingle :
149+ poolMap = map [string ]bool {
150+ clusterv1alpha1 .InstancePoolTypeMaster : true ,
151+ clusterv1alpha1 .InstancePoolTypeWorker : true ,
152+ clusterv1alpha1 .InstancePoolTypeEtcd : true ,
153+ clusterv1alpha1 .InstancePoolTypeBastion : true ,
154+ clusterv1alpha1 .InstancePoolTypeJenkins : true ,
155+ clusterv1alpha1 .InstancePoolTypeVault : true ,
156+ clusterv1alpha1 .InstancePoolTypeAll : true ,
157+ clusterv1alpha1 .InstancePoolTypeMasterEtcd : true ,
158+ clusterv1alpha1 .InstancePoolTypeHybrid : true ,
159+ }
160+
161+ break
162+
163+ default :
164+ return fmt .Errorf ("cluster type '%s' not supported" , c .Type ())
127165 }
128166
129- if len (poolMap [clusterv1alpha1 .InstancePoolTypeVault ]) != 1 {
130- result = multierror .Append (result , fmt .Errorf ("a hub needs to have exactly one '%s' server pool" , clusterv1alpha1 .InstancePoolTypeVault ))
167+ for _ , i := range c .Config ().InstancePools {
168+ if _ , ok := poolMap [i .Type ]; ! ok {
169+ errMap [i .Type ] = true
170+ }
131171 }
132172
133- return result
173+ var result * multierror.Error
174+ for t := range errMap {
175+ err := fmt .Errorf ("instance pool type '%s' not valid in cluster type '%s'" , t , c .Type ())
176+ result = multierror .Append (result , err )
177+ }
178+
179+ return result .ErrorOrNil ()
134180}
135181
136- // validate cluster instancePool types
137- func validateClusterTypes (poolMap map [string ][]* clusterv1alpha1.InstancePool , clusterType string ) (result error ) {
138- if len (poolMap [clusterv1alpha1 .InstancePoolTypeEtcd ]) != 1 {
139- result = multierror .Append (result , fmt .Errorf ("a %s needs to have exactly one '%s' server pool" , clusterType , clusterv1alpha1 .InstancePoolTypeEtcd ))
182+ func (c * Cluster ) validateSingleInstancePoolMap (poolMap map [string ][]* clusterv1alpha1.InstancePool , singleList []string ) error {
183+ var result * multierror.Error
184+
185+ for _ , i := range singleList {
186+ if v , ok := poolMap [i ]; ! ok || len (v ) != 1 {
187+ err := fmt .Errorf ("cluster type '%s' requires exactly one '%s' instance pool" , c .Type (), i )
188+ result = multierror .Append (result , err )
189+ }
140190 }
141191
142- if len (poolMap [clusterv1alpha1 .InstancePoolTypeMaster ]) < 1 {
143- result = multierror .Append (result , fmt .Errorf ("a %s needs to have more than one '%s' server pool" , clusterType , clusterv1alpha1 .InstancePoolTypeMaster ))
192+ return result .ErrorOrNil ()
193+ }
194+
195+ func (c * Cluster ) validateMultiInstancePoolMap (poolMap map [string ][]* clusterv1alpha1.InstancePool , instanceType string ) error {
196+ if len (poolMap [instanceType ]) < 1 {
197+ return fmt .Errorf ("cluster type '%s' requires one or more instance pool of type '%s'" , c .Type (), instanceType )
144198 }
145199
146- return result
200+ return nil
201+ }
202+
203+ func (c * Cluster ) validateClusterInstancePoolCount () error {
204+ var result * multierror.Error
205+
206+ poolMap := make (map [string ][]* clusterv1alpha1.InstancePool )
207+ for _ , i := range c .Config ().InstancePools {
208+ poolMap [i .Type ] = append (poolMap [i .Type ], & i )
209+ }
210+
211+ if c .Type () != clusterv1alpha1 .ClusterTypeHub {
212+ err := c .validateMultiInstancePoolMap (poolMap , clusterv1alpha1 .InstancePoolTypeWorker )
213+ if err != nil {
214+ result = multierror .Append (result , err )
215+ }
216+ }
217+
218+ switch c .Type () {
219+ case clusterv1alpha1 .ClusterTypeClusterSingle :
220+ if err := c .validateSingleInstancePoolMap (poolMap , []string {
221+ clusterv1alpha1 .InstancePoolTypeBastion ,
222+ clusterv1alpha1 .InstancePoolTypeVault ,
223+ clusterv1alpha1 .InstancePoolTypeEtcd ,
224+ }); err != nil {
225+ result = multierror .Append (result , err )
226+ }
227+
228+ if err := c .validateMultiInstancePoolMap (poolMap , clusterv1alpha1 .InstancePoolTypeMaster ); err != nil {
229+ result = multierror .Append (result , err )
230+ }
231+
232+ break
233+
234+ case clusterv1alpha1 .ClusterTypeHub :
235+
236+ if err := c .validateSingleInstancePoolMap (poolMap , []string {
237+ clusterv1alpha1 .InstancePoolTypeBastion ,
238+ clusterv1alpha1 .InstancePoolTypeVault ,
239+ }); err != nil {
240+ result = multierror .Append (result , err )
241+ }
242+
243+ break
244+
245+ case clusterv1alpha1 .ClusterTypeClusterMulti :
246+ if err := c .validateSingleInstancePoolMap (poolMap , []string {
247+ clusterv1alpha1 .InstancePoolTypeEtcd ,
248+ }); err != nil {
249+ result = multierror .Append (result , err )
250+ }
251+
252+ if err := c .validateMultiInstancePoolMap (poolMap , clusterv1alpha1 .InstancePoolTypeMaster ); err != nil {
253+ result = multierror .Append (result , err )
254+ }
255+
256+ break
257+
258+ default :
259+ return fmt .Errorf ("cluster type '%s' is not a supported type" , c .Type ())
260+ }
261+
262+ return result .ErrorOrNil ()
147263}
148264
149265// validate server pools
150- func (c * Cluster ) validateInstancePools () (result error ) {
266+ func (c * Cluster ) validateInstancePools () error {
267+ var result * multierror.Error
268+
151269 for _ , instancePool := range c .InstancePools () {
152270 err := instancePool .Validate ()
153271 if err != nil {
154272 result = multierror .Append (result , err )
155273 }
156274 }
157- return result
275+
276+ if result .ErrorOrNil () != nil {
277+ return result .ErrorOrNil ()
278+ }
279+
280+ // validate instance pool types according to cluster type
281+ if err := c .validateClusterInstancePoolTypes (); err != nil {
282+ return err
283+ }
284+
285+ // validate instance pool count according to cluster type
286+ if err := c .validateClusterInstancePoolCount (); err != nil {
287+ return err
288+ }
289+
290+ return nil
158291}
159292
160293// Verify cluster
@@ -179,7 +312,9 @@ func (c *Cluster) VerifyInstancePools() (result error) {
179312 return nil
180313}
181314
182- func (c * Cluster ) Validate () (result error ) {
315+ func (c * Cluster ) Validate () error {
316+ var result * multierror.Error
317+
183318 // validate instance pools
184319 if err := c .validateInstancePools (); err != nil {
185320 result = multierror .Append (result , err )
@@ -216,7 +351,7 @@ func (c *Cluster) Validate() (result error) {
216351 }
217352 }
218353
219- return result
354+ return result . ErrorOrNil ()
220355}
221356
222357// validate network configuration
0 commit comments