sigs.k8s.io/cluster-api@v1.6.3/internal/test/builder/builders.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package builder 18 19 import ( 20 "fmt" 21 "strings" 22 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 "k8s.io/apimachinery/pkg/util/intstr" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 29 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 30 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" 31 ) 32 33 // ClusterBuilder holds the variables and objects required to build a clusterv1.Cluster. 34 type ClusterBuilder struct { 35 namespace string 36 name string 37 labels map[string]string 38 annotations map[string]string 39 topology *clusterv1.Topology 40 infrastructureCluster *unstructured.Unstructured 41 controlPlane *unstructured.Unstructured 42 network *clusterv1.ClusterNetwork 43 } 44 45 // Cluster returns a ClusterBuilder with the given name and namespace. 46 func Cluster(namespace, name string) *ClusterBuilder { 47 return &ClusterBuilder{ 48 namespace: namespace, 49 name: name, 50 } 51 } 52 53 // WithClusterNetwork sets the ClusterNetwork for the ClusterBuilder. 54 func (c *ClusterBuilder) WithClusterNetwork(clusterNetwork *clusterv1.ClusterNetwork) *ClusterBuilder { 55 c.network = clusterNetwork 56 return c 57 } 58 59 // WithLabels sets the labels for the ClusterBuilder. 60 func (c *ClusterBuilder) WithLabels(labels map[string]string) *ClusterBuilder { 61 c.labels = labels 62 return c 63 } 64 65 // WithAnnotations sets the annotations for the ClusterBuilder. 66 func (c *ClusterBuilder) WithAnnotations(annotations map[string]string) *ClusterBuilder { 67 c.annotations = annotations 68 return c 69 } 70 71 // WithInfrastructureCluster adds the passed InfrastructureCluster to the ClusterBuilder. 72 func (c *ClusterBuilder) WithInfrastructureCluster(t *unstructured.Unstructured) *ClusterBuilder { 73 c.infrastructureCluster = t 74 return c 75 } 76 77 // WithControlPlane adds the passed ControlPlane to the ClusterBuilder. 78 func (c *ClusterBuilder) WithControlPlane(t *unstructured.Unstructured) *ClusterBuilder { 79 c.controlPlane = t 80 return c 81 } 82 83 // WithTopology adds the passed Topology object to the ClusterBuilder. 84 func (c *ClusterBuilder) WithTopology(topology *clusterv1.Topology) *ClusterBuilder { 85 c.topology = topology 86 return c 87 } 88 89 // Build returns a Cluster with the attributes added to the ClusterBuilder. 90 func (c *ClusterBuilder) Build() *clusterv1.Cluster { 91 obj := &clusterv1.Cluster{ 92 TypeMeta: metav1.TypeMeta{ 93 Kind: "Cluster", 94 APIVersion: clusterv1.GroupVersion.String(), 95 }, 96 ObjectMeta: metav1.ObjectMeta{ 97 Name: c.name, 98 Namespace: c.namespace, 99 Labels: c.labels, 100 Annotations: c.annotations, 101 }, 102 Spec: clusterv1.ClusterSpec{ 103 Topology: c.topology, 104 ClusterNetwork: c.network, 105 }, 106 } 107 if c.infrastructureCluster != nil { 108 obj.Spec.InfrastructureRef = objToRef(c.infrastructureCluster) 109 } 110 if c.controlPlane != nil { 111 obj.Spec.ControlPlaneRef = objToRef(c.controlPlane) 112 } 113 return obj 114 } 115 116 // ClusterTopologyBuilder contains the fields needed to build a testable ClusterTopology. 117 type ClusterTopologyBuilder struct { 118 class string 119 workers *clusterv1.WorkersTopology 120 version string 121 controlPlaneReplicas int32 122 controlPlaneMHC *clusterv1.MachineHealthCheckTopology 123 variables []clusterv1.ClusterVariable 124 } 125 126 // ClusterTopology returns a ClusterTopologyBuilder. 127 func ClusterTopology() *ClusterTopologyBuilder { 128 return &ClusterTopologyBuilder{ 129 workers: &clusterv1.WorkersTopology{}, 130 } 131 } 132 133 // WithClass adds the passed ClusterClass name to the ClusterTopologyBuilder. 134 func (c *ClusterTopologyBuilder) WithClass(class string) *ClusterTopologyBuilder { 135 c.class = class 136 return c 137 } 138 139 // WithVersion adds the passed version to the ClusterTopologyBuilder. 140 func (c *ClusterTopologyBuilder) WithVersion(version string) *ClusterTopologyBuilder { 141 c.version = version 142 return c 143 } 144 145 // WithControlPlaneReplicas adds the passed replicas value to the ClusterTopologyBuilder. 146 func (c *ClusterTopologyBuilder) WithControlPlaneReplicas(replicas int32) *ClusterTopologyBuilder { 147 c.controlPlaneReplicas = replicas 148 return c 149 } 150 151 // WithControlPlaneMachineHealthCheck adds MachineHealthCheckTopology used as the MachineHealthCheck value. 152 func (c *ClusterTopologyBuilder) WithControlPlaneMachineHealthCheck(mhc *clusterv1.MachineHealthCheckTopology) *ClusterTopologyBuilder { 153 c.controlPlaneMHC = mhc 154 return c 155 } 156 157 // WithMachineDeployment passes the full MachineDeploymentTopology and adds it to an existing list in the ClusterTopologyBuilder. 158 func (c *ClusterTopologyBuilder) WithMachineDeployment(mdc clusterv1.MachineDeploymentTopology) *ClusterTopologyBuilder { 159 c.workers.MachineDeployments = append(c.workers.MachineDeployments, mdc) 160 return c 161 } 162 163 // WithMachinePool passes the full MachinePoolTopology and adds it to an existing list in the ClusterTopologyBuilder. 164 func (c *ClusterTopologyBuilder) WithMachinePool(mpc clusterv1.MachinePoolTopology) *ClusterTopologyBuilder { 165 c.workers.MachinePools = append(c.workers.MachinePools, mpc) 166 return c 167 } 168 169 // WithVariables adds the passed variables to the ClusterTopologyBuilder. 170 func (c *ClusterTopologyBuilder) WithVariables(vars ...clusterv1.ClusterVariable) *ClusterTopologyBuilder { 171 c.variables = vars 172 return c 173 } 174 175 // Build returns a testable cluster Topology object with any values passed to the builder. 176 func (c *ClusterTopologyBuilder) Build() *clusterv1.Topology { 177 return &clusterv1.Topology{ 178 Class: c.class, 179 Workers: c.workers, 180 Version: c.version, 181 ControlPlane: clusterv1.ControlPlaneTopology{ 182 Replicas: &c.controlPlaneReplicas, 183 MachineHealthCheck: c.controlPlaneMHC, 184 }, 185 Variables: c.variables, 186 } 187 } 188 189 // MachineDeploymentTopologyBuilder holds the values needed to create a testable MachineDeploymentTopology. 190 type MachineDeploymentTopologyBuilder struct { 191 class string 192 name string 193 replicas *int32 194 mhc *clusterv1.MachineHealthCheckTopology 195 variables []clusterv1.ClusterVariable 196 } 197 198 // MachineDeploymentTopology returns a builder used to create a testable MachineDeploymentTopology. 199 func MachineDeploymentTopology(name string) *MachineDeploymentTopologyBuilder { 200 return &MachineDeploymentTopologyBuilder{ 201 name: name, 202 } 203 } 204 205 // WithClass adds a class string used as the MachineDeploymentTopology class. 206 func (m *MachineDeploymentTopologyBuilder) WithClass(class string) *MachineDeploymentTopologyBuilder { 207 m.class = class 208 return m 209 } 210 211 // WithReplicas adds a replicas value used as the MachineDeploymentTopology replicas value. 212 func (m *MachineDeploymentTopologyBuilder) WithReplicas(replicas int32) *MachineDeploymentTopologyBuilder { 213 m.replicas = &replicas 214 return m 215 } 216 217 // WithVariables adds variables used as the MachineDeploymentTopology variables value. 218 func (m *MachineDeploymentTopologyBuilder) WithVariables(variables ...clusterv1.ClusterVariable) *MachineDeploymentTopologyBuilder { 219 m.variables = variables 220 return m 221 } 222 223 // WithMachineHealthCheck adds MachineHealthCheckTopology used as the MachineHealthCheck value. 224 func (m *MachineDeploymentTopologyBuilder) WithMachineHealthCheck(mhc *clusterv1.MachineHealthCheckTopology) *MachineDeploymentTopologyBuilder { 225 m.mhc = mhc 226 return m 227 } 228 229 // Build returns a testable MachineDeploymentTopology with any values passed to the builder. 230 func (m *MachineDeploymentTopologyBuilder) Build() clusterv1.MachineDeploymentTopology { 231 md := clusterv1.MachineDeploymentTopology{ 232 Class: m.class, 233 Name: m.name, 234 Replicas: m.replicas, 235 MachineHealthCheck: m.mhc, 236 } 237 238 if len(m.variables) > 0 { 239 md.Variables = &clusterv1.MachineDeploymentVariables{ 240 Overrides: m.variables, 241 } 242 } 243 244 return md 245 } 246 247 // MachinePoolTopologyBuilder holds the values needed to create a testable MachinePoolTopology. 248 type MachinePoolTopologyBuilder struct { 249 class string 250 name string 251 replicas *int32 252 failureDomains []string 253 variables []clusterv1.ClusterVariable 254 } 255 256 // MachinePoolTopology returns a builder used to create a testable MachinePoolTopology. 257 func MachinePoolTopology(name string) *MachinePoolTopologyBuilder { 258 return &MachinePoolTopologyBuilder{ 259 name: name, 260 } 261 } 262 263 // WithClass adds a class string used as the MachinePoolTopology class. 264 func (m *MachinePoolTopologyBuilder) WithClass(class string) *MachinePoolTopologyBuilder { 265 m.class = class 266 return m 267 } 268 269 // WithReplicas adds a replicas value used as the MachinePoolTopology replicas value. 270 func (m *MachinePoolTopologyBuilder) WithReplicas(replicas int32) *MachinePoolTopologyBuilder { 271 m.replicas = &replicas 272 return m 273 } 274 275 // WithFailureDomains adds a failureDomains value used as the MachinePoolTopology failureDomains value. 276 func (m *MachinePoolTopologyBuilder) WithFailureDomains(failureDomains ...string) *MachinePoolTopologyBuilder { 277 m.failureDomains = failureDomains 278 return m 279 } 280 281 // WithVariables adds variables used as the MachinePoolTopology variables value. 282 func (m *MachinePoolTopologyBuilder) WithVariables(variables ...clusterv1.ClusterVariable) *MachinePoolTopologyBuilder { 283 m.variables = variables 284 return m 285 } 286 287 // Build returns a testable MachinePoolTopology with any values passed to the builder. 288 func (m *MachinePoolTopologyBuilder) Build() clusterv1.MachinePoolTopology { 289 mp := clusterv1.MachinePoolTopology{ 290 Class: m.class, 291 Name: m.name, 292 Replicas: m.replicas, 293 FailureDomains: m.failureDomains, 294 } 295 296 if len(m.variables) > 0 { 297 mp.Variables = &clusterv1.MachinePoolVariables{ 298 Overrides: m.variables, 299 } 300 } 301 302 return mp 303 } 304 305 // ClusterClassBuilder holds the variables and objects required to build a clusterv1.ClusterClass. 306 type ClusterClassBuilder struct { 307 namespace string 308 name string 309 infrastructureClusterTemplate *unstructured.Unstructured 310 controlPlaneMetadata *clusterv1.ObjectMeta 311 controlPlaneTemplate *unstructured.Unstructured 312 controlPlaneInfrastructureMachineTemplate *unstructured.Unstructured 313 controlPlaneMHC *clusterv1.MachineHealthCheckClass 314 controlPlaneNodeDrainTimeout *metav1.Duration 315 controlPlaneNodeVolumeDetachTimeout *metav1.Duration 316 controlPlaneNodeDeletionTimeout *metav1.Duration 317 controlPlaneNamingStrategy *clusterv1.ControlPlaneClassNamingStrategy 318 machineDeploymentClasses []clusterv1.MachineDeploymentClass 319 machinePoolClasses []clusterv1.MachinePoolClass 320 variables []clusterv1.ClusterClassVariable 321 statusVariables []clusterv1.ClusterClassStatusVariable 322 patches []clusterv1.ClusterClassPatch 323 } 324 325 // ClusterClass returns a ClusterClassBuilder with the given name and namespace. 326 func ClusterClass(namespace, name string) *ClusterClassBuilder { 327 return &ClusterClassBuilder{ 328 namespace: namespace, 329 name: name, 330 } 331 } 332 333 // WithInfrastructureClusterTemplate adds the passed InfrastructureClusterTemplate to the ClusterClassBuilder. 334 func (c *ClusterClassBuilder) WithInfrastructureClusterTemplate(t *unstructured.Unstructured) *ClusterClassBuilder { 335 c.infrastructureClusterTemplate = t 336 return c 337 } 338 339 // WithControlPlaneTemplate adds the passed ControlPlaneTemplate to the ClusterClassBuilder. 340 func (c *ClusterClassBuilder) WithControlPlaneTemplate(t *unstructured.Unstructured) *ClusterClassBuilder { 341 c.controlPlaneTemplate = t 342 return c 343 } 344 345 // WithControlPlaneMetadata adds the given labels and annotations for use with the ControlPlane to the ClusterClassBuilder. 346 func (c *ClusterClassBuilder) WithControlPlaneMetadata(labels, annotations map[string]string) *ClusterClassBuilder { 347 c.controlPlaneMetadata = &clusterv1.ObjectMeta{ 348 Labels: labels, 349 Annotations: annotations, 350 } 351 return c 352 } 353 354 // WithControlPlaneInfrastructureMachineTemplate adds the ControlPlane's InfrastructureMachineTemplate to the ClusterClassBuilder. 355 func (c *ClusterClassBuilder) WithControlPlaneInfrastructureMachineTemplate(t *unstructured.Unstructured) *ClusterClassBuilder { 356 c.controlPlaneInfrastructureMachineTemplate = t 357 return c 358 } 359 360 // WithControlPlaneMachineHealthCheck adds a MachineHealthCheck for the ControlPlane to the ClusterClassBuilder. 361 func (c *ClusterClassBuilder) WithControlPlaneMachineHealthCheck(mhc *clusterv1.MachineHealthCheckClass) *ClusterClassBuilder { 362 c.controlPlaneMHC = mhc 363 return c 364 } 365 366 // WithControlPlaneNodeDrainTimeout adds a NodeDrainTimeout for the ControlPlane to the ClusterClassBuilder. 367 func (c *ClusterClassBuilder) WithControlPlaneNodeDrainTimeout(t *metav1.Duration) *ClusterClassBuilder { 368 c.controlPlaneNodeDrainTimeout = t 369 return c 370 } 371 372 // WithControlPlaneNodeVolumeDetachTimeout adds a NodeVolumeDetachTimeout for the ControlPlane to the ClusterClassBuilder. 373 func (c *ClusterClassBuilder) WithControlPlaneNodeVolumeDetachTimeout(t *metav1.Duration) *ClusterClassBuilder { 374 c.controlPlaneNodeVolumeDetachTimeout = t 375 return c 376 } 377 378 // WithControlPlaneNodeDeletionTimeout adds a NodeDeletionTimeout for the ControlPlane to the ClusterClassBuilder. 379 func (c *ClusterClassBuilder) WithControlPlaneNodeDeletionTimeout(t *metav1.Duration) *ClusterClassBuilder { 380 c.controlPlaneNodeDeletionTimeout = t 381 return c 382 } 383 384 // WithControlPlaneNamingStrategy sets the NamingStrategy for the ControlPlane to the ClusterClassBuilder. 385 func (c *ClusterClassBuilder) WithControlPlaneNamingStrategy(n *clusterv1.ControlPlaneClassNamingStrategy) *ClusterClassBuilder { 386 c.controlPlaneNamingStrategy = n 387 return c 388 } 389 390 // WithVariables adds the Variables to the ClusterClassBuilder. 391 func (c *ClusterClassBuilder) WithVariables(vars ...clusterv1.ClusterClassVariable) *ClusterClassBuilder { 392 c.variables = vars 393 return c 394 } 395 396 // WithStatusVariables adds the ClusterClassStatusVariables to the ClusterClassBuilder. 397 func (c *ClusterClassBuilder) WithStatusVariables(vars ...clusterv1.ClusterClassStatusVariable) *ClusterClassBuilder { 398 c.statusVariables = vars 399 return c 400 } 401 402 // WithPatches adds the patches to the ClusterClassBuilder. 403 func (c *ClusterClassBuilder) WithPatches(patches []clusterv1.ClusterClassPatch) *ClusterClassBuilder { 404 c.patches = patches 405 return c 406 } 407 408 // WithWorkerMachineDeploymentClasses adds the variables and objects needed to create MachineDeploymentTemplates for a ClusterClassBuilder. 409 func (c *ClusterClassBuilder) WithWorkerMachineDeploymentClasses(mdcs ...clusterv1.MachineDeploymentClass) *ClusterClassBuilder { 410 if c.machineDeploymentClasses == nil { 411 c.machineDeploymentClasses = make([]clusterv1.MachineDeploymentClass, 0) 412 } 413 c.machineDeploymentClasses = append(c.machineDeploymentClasses, mdcs...) 414 return c 415 } 416 417 // WithWorkerMachinePoolClasses adds the variables and objects needed to create MachinePoolTemplates for a ClusterClassBuilder. 418 func (c *ClusterClassBuilder) WithWorkerMachinePoolClasses(mpcs ...clusterv1.MachinePoolClass) *ClusterClassBuilder { 419 if c.machinePoolClasses == nil { 420 c.machinePoolClasses = make([]clusterv1.MachinePoolClass, 0) 421 } 422 c.machinePoolClasses = append(c.machinePoolClasses, mpcs...) 423 return c 424 } 425 426 // Build takes the objects and variables in the ClusterClass builder and uses them to create a ClusterClass object. 427 func (c *ClusterClassBuilder) Build() *clusterv1.ClusterClass { 428 obj := &clusterv1.ClusterClass{ 429 TypeMeta: metav1.TypeMeta{ 430 Kind: "ClusterClass", 431 APIVersion: clusterv1.GroupVersion.String(), 432 }, 433 ObjectMeta: metav1.ObjectMeta{ 434 Name: c.name, 435 Namespace: c.namespace, 436 }, 437 Spec: clusterv1.ClusterClassSpec{ 438 Variables: c.variables, 439 Patches: c.patches, 440 }, 441 Status: clusterv1.ClusterClassStatus{ 442 Variables: c.statusVariables, 443 }, 444 } 445 if c.infrastructureClusterTemplate != nil { 446 obj.Spec.Infrastructure = clusterv1.LocalObjectTemplate{ 447 Ref: objToRef(c.infrastructureClusterTemplate), 448 } 449 } 450 if c.controlPlaneMetadata != nil { 451 obj.Spec.ControlPlane.Metadata = *c.controlPlaneMetadata 452 } 453 if c.controlPlaneTemplate != nil { 454 obj.Spec.ControlPlane.LocalObjectTemplate = clusterv1.LocalObjectTemplate{ 455 Ref: objToRef(c.controlPlaneTemplate), 456 } 457 } 458 if c.controlPlaneMHC != nil { 459 obj.Spec.ControlPlane.MachineHealthCheck = c.controlPlaneMHC 460 } 461 if c.controlPlaneNodeDrainTimeout != nil { 462 obj.Spec.ControlPlane.NodeDrainTimeout = c.controlPlaneNodeDrainTimeout 463 } 464 if c.controlPlaneNodeVolumeDetachTimeout != nil { 465 obj.Spec.ControlPlane.NodeVolumeDetachTimeout = c.controlPlaneNodeVolumeDetachTimeout 466 } 467 if c.controlPlaneNodeDeletionTimeout != nil { 468 obj.Spec.ControlPlane.NodeDeletionTimeout = c.controlPlaneNodeDeletionTimeout 469 } 470 if c.controlPlaneInfrastructureMachineTemplate != nil { 471 obj.Spec.ControlPlane.MachineInfrastructure = &clusterv1.LocalObjectTemplate{ 472 Ref: objToRef(c.controlPlaneInfrastructureMachineTemplate), 473 } 474 } 475 if c.controlPlaneNamingStrategy != nil { 476 obj.Spec.ControlPlane.NamingStrategy = c.controlPlaneNamingStrategy 477 } 478 479 obj.Spec.Workers.MachineDeployments = c.machineDeploymentClasses 480 obj.Spec.Workers.MachinePools = c.machinePoolClasses 481 return obj 482 } 483 484 // MachineDeploymentClassBuilder holds the variables and objects required to build a clusterv1.MachineDeploymentClass. 485 type MachineDeploymentClassBuilder struct { 486 class string 487 infrastructureMachineTemplate *unstructured.Unstructured 488 bootstrapTemplate *unstructured.Unstructured 489 labels map[string]string 490 annotations map[string]string 491 machineHealthCheckClass *clusterv1.MachineHealthCheckClass 492 failureDomain *string 493 nodeDrainTimeout *metav1.Duration 494 nodeVolumeDetachTimeout *metav1.Duration 495 nodeDeletionTimeout *metav1.Duration 496 minReadySeconds *int32 497 strategy *clusterv1.MachineDeploymentStrategy 498 namingStrategy *clusterv1.MachineDeploymentClassNamingStrategy 499 } 500 501 // MachineDeploymentClass returns a MachineDeploymentClassBuilder with the given name and namespace. 502 func MachineDeploymentClass(class string) *MachineDeploymentClassBuilder { 503 return &MachineDeploymentClassBuilder{ 504 class: class, 505 } 506 } 507 508 // WithInfrastructureTemplate registers the passed Unstructured object as the InfrastructureMachineTemplate for the MachineDeploymentClassBuilder. 509 func (m *MachineDeploymentClassBuilder) WithInfrastructureTemplate(t *unstructured.Unstructured) *MachineDeploymentClassBuilder { 510 m.infrastructureMachineTemplate = t 511 return m 512 } 513 514 // WithBootstrapTemplate registers the passed Unstructured object as the BootstrapTemplate for the MachineDeploymentClassBuilder. 515 func (m *MachineDeploymentClassBuilder) WithBootstrapTemplate(t *unstructured.Unstructured) *MachineDeploymentClassBuilder { 516 m.bootstrapTemplate = t 517 return m 518 } 519 520 // WithLabels sets the labels for the MachineDeploymentClassBuilder. 521 func (m *MachineDeploymentClassBuilder) WithLabels(labels map[string]string) *MachineDeploymentClassBuilder { 522 m.labels = labels 523 return m 524 } 525 526 // WithAnnotations sets the annotations for the MachineDeploymentClassBuilder. 527 func (m *MachineDeploymentClassBuilder) WithAnnotations(annotations map[string]string) *MachineDeploymentClassBuilder { 528 m.annotations = annotations 529 return m 530 } 531 532 // WithMachineHealthCheckClass sets the MachineHealthCheckClass for the MachineDeploymentClassBuilder. 533 func (m *MachineDeploymentClassBuilder) WithMachineHealthCheckClass(mhc *clusterv1.MachineHealthCheckClass) *MachineDeploymentClassBuilder { 534 m.machineHealthCheckClass = mhc 535 return m 536 } 537 538 // WithFailureDomain sets the FailureDomain for the MachineDeploymentClassBuilder. 539 func (m *MachineDeploymentClassBuilder) WithFailureDomain(f *string) *MachineDeploymentClassBuilder { 540 m.failureDomain = f 541 return m 542 } 543 544 // WithNodeDrainTimeout sets the NodeDrainTimeout for the MachineDeploymentClassBuilder. 545 func (m *MachineDeploymentClassBuilder) WithNodeDrainTimeout(t *metav1.Duration) *MachineDeploymentClassBuilder { 546 m.nodeDrainTimeout = t 547 return m 548 } 549 550 // WithNodeVolumeDetachTimeout sets the NodeVolumeDetachTimeout for the MachineDeploymentClassBuilder. 551 func (m *MachineDeploymentClassBuilder) WithNodeVolumeDetachTimeout(t *metav1.Duration) *MachineDeploymentClassBuilder { 552 m.nodeVolumeDetachTimeout = t 553 return m 554 } 555 556 // WithNodeDeletionTimeout sets the NodeDeletionTimeout for the MachineDeploymentClassBuilder. 557 func (m *MachineDeploymentClassBuilder) WithNodeDeletionTimeout(t *metav1.Duration) *MachineDeploymentClassBuilder { 558 m.nodeDeletionTimeout = t 559 return m 560 } 561 562 // WithMinReadySeconds sets the MinReadySeconds for the MachineDeploymentClassBuilder. 563 func (m *MachineDeploymentClassBuilder) WithMinReadySeconds(t *int32) *MachineDeploymentClassBuilder { 564 m.minReadySeconds = t 565 return m 566 } 567 568 // WithStrategy sets the Strategy for the MachineDeploymentClassBuilder. 569 func (m *MachineDeploymentClassBuilder) WithStrategy(s *clusterv1.MachineDeploymentStrategy) *MachineDeploymentClassBuilder { 570 m.strategy = s 571 return m 572 } 573 574 // WithNamingStrategy sets the NamingStrategy for the MachineDeploymentClassBuilder. 575 func (m *MachineDeploymentClassBuilder) WithNamingStrategy(n *clusterv1.MachineDeploymentClassNamingStrategy) *MachineDeploymentClassBuilder { 576 m.namingStrategy = n 577 return m 578 } 579 580 // Build creates a full MachineDeploymentClass object with the variables passed to the MachineDeploymentClassBuilder. 581 func (m *MachineDeploymentClassBuilder) Build() *clusterv1.MachineDeploymentClass { 582 obj := &clusterv1.MachineDeploymentClass{ 583 Class: m.class, 584 Template: clusterv1.MachineDeploymentClassTemplate{ 585 Metadata: clusterv1.ObjectMeta{ 586 Labels: m.labels, 587 Annotations: m.annotations, 588 }, 589 }, 590 } 591 if m.bootstrapTemplate != nil { 592 obj.Template.Bootstrap.Ref = objToRef(m.bootstrapTemplate) 593 } 594 if m.infrastructureMachineTemplate != nil { 595 obj.Template.Infrastructure.Ref = objToRef(m.infrastructureMachineTemplate) 596 } 597 if m.machineHealthCheckClass != nil { 598 obj.MachineHealthCheck = m.machineHealthCheckClass 599 } 600 if m.failureDomain != nil { 601 obj.FailureDomain = m.failureDomain 602 } 603 if m.nodeDrainTimeout != nil { 604 obj.NodeDrainTimeout = m.nodeDrainTimeout 605 } 606 if m.nodeVolumeDetachTimeout != nil { 607 obj.NodeVolumeDetachTimeout = m.nodeVolumeDetachTimeout 608 } 609 if m.nodeDeletionTimeout != nil { 610 obj.NodeDeletionTimeout = m.nodeDeletionTimeout 611 } 612 if m.minReadySeconds != nil { 613 obj.MinReadySeconds = m.minReadySeconds 614 } 615 if m.strategy != nil { 616 obj.Strategy = m.strategy 617 } 618 if m.namingStrategy != nil { 619 obj.NamingStrategy = m.namingStrategy 620 } 621 return obj 622 } 623 624 // MachinePoolClassBuilder holds the variables and objects required to build a clusterv1.MachinePoolClass. 625 type MachinePoolClassBuilder struct { 626 class string 627 infrastructureMachinePoolTemplate *unstructured.Unstructured 628 bootstrapTemplate *unstructured.Unstructured 629 labels map[string]string 630 annotations map[string]string 631 failureDomains []string 632 nodeDrainTimeout *metav1.Duration 633 nodeVolumeDetachTimeout *metav1.Duration 634 nodeDeletionTimeout *metav1.Duration 635 minReadySeconds *int32 636 namingStrategy *clusterv1.MachinePoolClassNamingStrategy 637 } 638 639 // MachinePoolClass returns a MachinePoolClassBuilder with the given name and namespace. 640 func MachinePoolClass(class string) *MachinePoolClassBuilder { 641 return &MachinePoolClassBuilder{ 642 class: class, 643 } 644 } 645 646 // WithInfrastructureTemplate registers the passed Unstructured object as the InfrastructureMachinePoolTemplate for the MachinePoolClassBuilder. 647 func (m *MachinePoolClassBuilder) WithInfrastructureTemplate(t *unstructured.Unstructured) *MachinePoolClassBuilder { 648 m.infrastructureMachinePoolTemplate = t 649 return m 650 } 651 652 // WithBootstrapTemplate registers the passed Unstructured object as the BootstrapTemplate for the MachinePoolClassBuilder. 653 func (m *MachinePoolClassBuilder) WithBootstrapTemplate(t *unstructured.Unstructured) *MachinePoolClassBuilder { 654 m.bootstrapTemplate = t 655 return m 656 } 657 658 // WithLabels sets the labels for the MachinePoolClassBuilder. 659 func (m *MachinePoolClassBuilder) WithLabels(labels map[string]string) *MachinePoolClassBuilder { 660 m.labels = labels 661 return m 662 } 663 664 // WithAnnotations sets the annotations for the MachinePoolClassBuilder. 665 func (m *MachinePoolClassBuilder) WithAnnotations(annotations map[string]string) *MachinePoolClassBuilder { 666 m.annotations = annotations 667 return m 668 } 669 670 // WithFailureDomains sets the FailureDomains for the MachinePoolClassBuilder. 671 func (m *MachinePoolClassBuilder) WithFailureDomains(failureDomains ...string) *MachinePoolClassBuilder { 672 m.failureDomains = failureDomains 673 return m 674 } 675 676 // WithNodeDrainTimeout sets the NodeDrainTimeout for the MachinePoolClassBuilder. 677 func (m *MachinePoolClassBuilder) WithNodeDrainTimeout(t *metav1.Duration) *MachinePoolClassBuilder { 678 m.nodeDrainTimeout = t 679 return m 680 } 681 682 // WithNodeVolumeDetachTimeout sets the NodeVolumeDetachTimeout for the MachinePoolClassBuilder. 683 func (m *MachinePoolClassBuilder) WithNodeVolumeDetachTimeout(t *metav1.Duration) *MachinePoolClassBuilder { 684 m.nodeVolumeDetachTimeout = t 685 return m 686 } 687 688 // WithNodeDeletionTimeout sets the NodeDeletionTimeout for the MachinePoolClassBuilder. 689 func (m *MachinePoolClassBuilder) WithNodeDeletionTimeout(t *metav1.Duration) *MachinePoolClassBuilder { 690 m.nodeDeletionTimeout = t 691 return m 692 } 693 694 // WithMinReadySeconds sets the MinReadySeconds for the MachinePoolClassBuilder. 695 func (m *MachinePoolClassBuilder) WithMinReadySeconds(t *int32) *MachinePoolClassBuilder { 696 m.minReadySeconds = t 697 return m 698 } 699 700 // WithNamingStrategy sets the NamingStrategy for the MachinePoolClassBuilder. 701 func (m *MachinePoolClassBuilder) WithNamingStrategy(n *clusterv1.MachinePoolClassNamingStrategy) *MachinePoolClassBuilder { 702 m.namingStrategy = n 703 return m 704 } 705 706 // Build creates a full MachinePoolClass object with the variables passed to the MachinePoolClassBuilder. 707 func (m *MachinePoolClassBuilder) Build() *clusterv1.MachinePoolClass { 708 obj := &clusterv1.MachinePoolClass{ 709 Class: m.class, 710 Template: clusterv1.MachinePoolClassTemplate{ 711 Metadata: clusterv1.ObjectMeta{ 712 Labels: m.labels, 713 Annotations: m.annotations, 714 }, 715 }, 716 } 717 if m.bootstrapTemplate != nil { 718 obj.Template.Bootstrap.Ref = objToRef(m.bootstrapTemplate) 719 } 720 if m.infrastructureMachinePoolTemplate != nil { 721 obj.Template.Infrastructure.Ref = objToRef(m.infrastructureMachinePoolTemplate) 722 } 723 if m.failureDomains != nil { 724 obj.FailureDomains = m.failureDomains 725 } 726 if m.nodeDrainTimeout != nil { 727 obj.NodeDrainTimeout = m.nodeDrainTimeout 728 } 729 if m.nodeVolumeDetachTimeout != nil { 730 obj.NodeVolumeDetachTimeout = m.nodeVolumeDetachTimeout 731 } 732 if m.nodeDeletionTimeout != nil { 733 obj.NodeDeletionTimeout = m.nodeDeletionTimeout 734 } 735 if m.minReadySeconds != nil { 736 obj.MinReadySeconds = m.minReadySeconds 737 } 738 if m.namingStrategy != nil { 739 obj.NamingStrategy = m.namingStrategy 740 } 741 return obj 742 } 743 744 // InfrastructureMachineTemplateBuilder holds the variables and objects needed to build an InfrastructureMachineTemplate. 745 type InfrastructureMachineTemplateBuilder struct { 746 obj *unstructured.Unstructured 747 } 748 749 // InfrastructureMachineTemplate creates an InfrastructureMachineTemplateBuilder with the given name and namespace. 750 func InfrastructureMachineTemplate(namespace, name string) *InfrastructureMachineTemplateBuilder { 751 obj := &unstructured.Unstructured{} 752 obj.SetName(name) 753 obj.SetNamespace(namespace) 754 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 755 obj.SetKind(GenericInfrastructureMachineTemplateKind) 756 // Set the mandatory spec fields for the object. 757 setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}}) 758 return &InfrastructureMachineTemplateBuilder{ 759 obj, 760 } 761 } 762 763 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 764 // to the value of the spec field. 765 // 766 // Note: all the paths should start with "spec." 767 // 768 // Example map: map[string]interface{}{ 769 // "spec.version": "v1.2.3", 770 // }. 771 func (i *InfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureMachineTemplateBuilder { 772 setSpecFields(i.obj, fields) 773 return i 774 } 775 776 // Build takes the objects and variables in the InfrastructureMachineTemplateBuilder and generates an unstructured object. 777 func (i *InfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructured { 778 return i.obj 779 } 780 781 // TestInfrastructureMachineTemplateBuilder holds the variables and objects needed to build an TestInfrastructureMachineTemplate. 782 type TestInfrastructureMachineTemplateBuilder struct { 783 obj *unstructured.Unstructured 784 } 785 786 // TestInfrastructureMachineTemplate creates an TestInfrastructureMachineTemplateBuilder with the given name and namespace. 787 func TestInfrastructureMachineTemplate(namespace, name string) *TestInfrastructureMachineTemplateBuilder { 788 obj := &unstructured.Unstructured{} 789 obj.SetName(name) 790 obj.SetNamespace(namespace) 791 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 792 obj.SetKind(TestInfrastructureMachineTemplateKind) 793 // Set the mandatory spec fields for the object. 794 if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil { 795 panic(err) 796 } 797 return &TestInfrastructureMachineTemplateBuilder{ 798 obj, 799 } 800 } 801 802 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 803 // to the value of the spec field. 804 // 805 // Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD. 806 // 807 // Example map: map[string]interface{}{ 808 // "spec.version": "v1.2.3", 809 // }. 810 func (i *TestInfrastructureMachineTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureMachineTemplateBuilder { 811 setSpecFields(i.obj, fields) 812 return i 813 } 814 815 // Build takes the objects and variables in the InfrastructureMachineTemplateBuilder and generates an unstructured object. 816 func (i *TestInfrastructureMachineTemplateBuilder) Build() *unstructured.Unstructured { 817 return i.obj 818 } 819 820 // InfrastructureMachinePoolTemplateBuilder holds the variables and objects needed to build an InfrastructureMachinePoolTemplate. 821 type InfrastructureMachinePoolTemplateBuilder struct { 822 obj *unstructured.Unstructured 823 } 824 825 // InfrastructureMachinePoolTemplate creates an InfrastructureMachinePoolTemplateBuilder with the given name and namespace. 826 func InfrastructureMachinePoolTemplate(namespace, name string) *InfrastructureMachinePoolTemplateBuilder { 827 obj := &unstructured.Unstructured{} 828 obj.SetName(name) 829 obj.SetNamespace(namespace) 830 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 831 obj.SetKind(GenericInfrastructureMachinePoolTemplateKind) 832 // Set the mandatory spec fields for the object. 833 setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}}) 834 return &InfrastructureMachinePoolTemplateBuilder{ 835 obj, 836 } 837 } 838 839 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 840 // to the value of the spec field. 841 // 842 // Note: all the paths should start with "spec." 843 // 844 // Example map: map[string]interface{}{ 845 // "spec.version": "v1.2.3", 846 // }. 847 func (i *InfrastructureMachinePoolTemplateBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureMachinePoolTemplateBuilder { 848 setSpecFields(i.obj, fields) 849 return i 850 } 851 852 // Build takes the objects and variables in the InfrastructureMachineTemplateBuilder and generates an unstructured object. 853 func (i *InfrastructureMachinePoolTemplateBuilder) Build() *unstructured.Unstructured { 854 return i.obj 855 } 856 857 // TestInfrastructureMachinePoolTemplateBuilder holds the variables and objects needed to build an TestInfrastructureMachinePoolTemplate. 858 type TestInfrastructureMachinePoolTemplateBuilder struct { 859 obj *unstructured.Unstructured 860 } 861 862 // TestInfrastructureMachinePoolTemplate creates an TestInfrastructureMachinePoolTemplateBuilder with the given name and namespace. 863 func TestInfrastructureMachinePoolTemplate(namespace, name string) *TestInfrastructureMachinePoolTemplateBuilder { 864 obj := &unstructured.Unstructured{} 865 obj.SetName(name) 866 obj.SetNamespace(namespace) 867 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 868 obj.SetKind(TestInfrastructureMachinePoolTemplateKind) 869 // Set the mandatory spec fields for the object. 870 if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil { 871 panic(err) 872 } 873 return &TestInfrastructureMachinePoolTemplateBuilder{ 874 obj, 875 } 876 } 877 878 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 879 // to the value of the spec field. 880 // 881 // Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD. 882 // 883 // Example map: map[string]interface{}{ 884 // "spec.version": "v1.2.3", 885 // }. 886 func (i *TestInfrastructureMachinePoolTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureMachinePoolTemplateBuilder { 887 setSpecFields(i.obj, fields) 888 return i 889 } 890 891 // Build takes the objects and variables in the TestInfrastructureMachineTemplateBuilder and generates an unstructured object. 892 func (i *TestInfrastructureMachinePoolTemplateBuilder) Build() *unstructured.Unstructured { 893 return i.obj 894 } 895 896 // InfrastructureMachinePoolBuilder holds the variables and objects needed to build an InfrastructureMachinePool. 897 type InfrastructureMachinePoolBuilder struct { 898 obj *unstructured.Unstructured 899 } 900 901 // InfrastructureMachinePool creates an InfrastructureMachinePoolBuilder with the given name and namespace. 902 func InfrastructureMachinePool(namespace, name string) *InfrastructureMachinePoolBuilder { 903 obj := &unstructured.Unstructured{} 904 obj.SetName(name) 905 obj.SetNamespace(namespace) 906 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 907 obj.SetKind(GenericInfrastructureMachinePoolKind) 908 // Set the mandatory spec fields for the object. 909 setSpecFields(obj, map[string]interface{}{"spec": map[string]interface{}{}}) 910 return &InfrastructureMachinePoolBuilder{ 911 obj, 912 } 913 } 914 915 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 916 // to the value of the spec field. 917 // 918 // Note: all the paths should start with "spec." 919 // 920 // Example map: map[string]interface{}{ 921 // "spec.version": "v1.2.3", 922 // }. 923 func (i *InfrastructureMachinePoolBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureMachinePoolBuilder { 924 setSpecFields(i.obj, fields) 925 return i 926 } 927 928 // Build takes the objects and variables in the InfrastructureMachinePoolBuilder and generates an unstructured object. 929 func (i *InfrastructureMachinePoolBuilder) Build() *unstructured.Unstructured { 930 return i.obj 931 } 932 933 // TestInfrastructureMachinePoolBuilder holds the variables and objects needed to build an TestInfrastructureMachinePool. 934 type TestInfrastructureMachinePoolBuilder struct { 935 obj *unstructured.Unstructured 936 } 937 938 // TestInfrastructureMachinePool creates an TestInfrastructureMachinePoolBuilder with the given name and namespace. 939 func TestInfrastructureMachinePool(namespace, name string) *TestInfrastructureMachinePoolBuilder { 940 obj := &unstructured.Unstructured{} 941 obj.SetName(name) 942 obj.SetNamespace(namespace) 943 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 944 obj.SetKind(TestInfrastructureMachinePoolKind) 945 // Set the mandatory spec fields for the object. 946 if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec"); err != nil { 947 panic(err) 948 } 949 return &TestInfrastructureMachinePoolBuilder{ 950 obj, 951 } 952 } 953 954 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 955 // to the value of the spec field. 956 // 957 // Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD. 958 // 959 // Example map: map[string]interface{}{ 960 // "spec.version": "v1.2.3", 961 // }. 962 func (i *TestInfrastructureMachinePoolBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureMachinePoolBuilder { 963 setSpecFields(i.obj, fields) 964 return i 965 } 966 967 // Build takes the objects and variables in the TestInfrastructureMachinePoolBuilder and generates an unstructured object. 968 func (i *TestInfrastructureMachinePoolBuilder) Build() *unstructured.Unstructured { 969 return i.obj 970 } 971 972 // BootstrapTemplateBuilder holds the variables needed to build a generic BootstrapTemplate. 973 type BootstrapTemplateBuilder struct { 974 obj *unstructured.Unstructured 975 } 976 977 // BootstrapTemplate creates a BootstrapTemplateBuilder with the given name and namespace. 978 func BootstrapTemplate(namespace, name string) *BootstrapTemplateBuilder { 979 obj := &unstructured.Unstructured{} 980 obj.SetAPIVersion(BootstrapGroupVersion.String()) 981 obj.SetKind(GenericBootstrapConfigTemplateKind) 982 obj.SetNamespace(namespace) 983 obj.SetName(name) 984 setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}}) 985 986 return &BootstrapTemplateBuilder{obj: obj} 987 } 988 989 // WithSpecFields will add fields of any type to the object spec. It takes an argument, fields, which is of the form path: object. 990 func (b *BootstrapTemplateBuilder) WithSpecFields(fields map[string]interface{}) *BootstrapTemplateBuilder { 991 setSpecFields(b.obj, fields) 992 return b 993 } 994 995 // Build creates a new Unstructured object with the information passed to the BootstrapTemplateBuilder. 996 func (b *BootstrapTemplateBuilder) Build() *unstructured.Unstructured { 997 return b.obj 998 } 999 1000 // TestBootstrapTemplateBuilder holds the variables needed to build a generic TestBootstrapTemplate. 1001 type TestBootstrapTemplateBuilder struct { 1002 obj *unstructured.Unstructured 1003 } 1004 1005 // TestBootstrapTemplate creates a TestBootstrapTemplateBuilder with the given name and namespace. 1006 func TestBootstrapTemplate(namespace, name string) *TestBootstrapTemplateBuilder { 1007 obj := &unstructured.Unstructured{} 1008 obj.SetAPIVersion(BootstrapGroupVersion.String()) 1009 obj.SetKind(TestBootstrapConfigTemplateKind) 1010 obj.SetNamespace(namespace) 1011 obj.SetName(name) 1012 setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}}) 1013 1014 return &TestBootstrapTemplateBuilder{ 1015 obj: obj, 1016 } 1017 } 1018 1019 // WithSpecFields will add fields of any type to the object spec. It takes an argument, fields, which is of the form path: object. 1020 // NOTE: The path should correspond to a field defined in the CRD. 1021 func (b *TestBootstrapTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestBootstrapTemplateBuilder { 1022 setSpecFields(b.obj, fields) 1023 return b 1024 } 1025 1026 // Build creates a new Unstructured object with the information passed to the BootstrapTemplateBuilder. 1027 func (b *TestBootstrapTemplateBuilder) Build() *unstructured.Unstructured { 1028 return b.obj 1029 } 1030 1031 // BootstrapConfigBuilder holds the variables needed to build a generic BootstrapConfig. 1032 type BootstrapConfigBuilder struct { 1033 obj *unstructured.Unstructured 1034 } 1035 1036 // BootstrapConfig creates a BootstrapConfigBuilder with the given name and namespace. 1037 func BootstrapConfig(namespace, name string) *BootstrapConfigBuilder { 1038 obj := &unstructured.Unstructured{} 1039 obj.SetAPIVersion(BootstrapGroupVersion.String()) 1040 obj.SetKind(GenericBootstrapConfigKind) 1041 obj.SetNamespace(namespace) 1042 obj.SetName(name) 1043 setSpecFields(obj, map[string]interface{}{"spec": map[string]interface{}{}}) 1044 1045 return &BootstrapConfigBuilder{obj: obj} 1046 } 1047 1048 // WithSpecFields will add fields of any type to the object spec. It takes an argument, fields, which is of the form path: object. 1049 func (b *BootstrapConfigBuilder) WithSpecFields(fields map[string]interface{}) *BootstrapConfigBuilder { 1050 setSpecFields(b.obj, fields) 1051 return b 1052 } 1053 1054 // Build creates a new Unstructured object with the information passed to the BootstrapConfigBuilder. 1055 func (b *BootstrapConfigBuilder) Build() *unstructured.Unstructured { 1056 return b.obj 1057 } 1058 1059 // TestBootstrapConfigBuilder holds the variables needed to build a generic TestBootstrapConfig. 1060 type TestBootstrapConfigBuilder struct { 1061 obj *unstructured.Unstructured 1062 } 1063 1064 // TestBootstrapConfig creates a TestBootstrapConfigBuilder with the given name and namespace. 1065 func TestBootstrapConfig(namespace, name string) *TestBootstrapConfigBuilder { 1066 obj := &unstructured.Unstructured{} 1067 obj.SetAPIVersion(BootstrapGroupVersion.String()) 1068 obj.SetKind(TestBootstrapConfigKind) 1069 obj.SetNamespace(namespace) 1070 obj.SetName(name) 1071 setSpecFields(obj, map[string]interface{}{"spec": map[string]interface{}{}}) 1072 1073 return &TestBootstrapConfigBuilder{ 1074 obj: obj, 1075 } 1076 } 1077 1078 // WithSpecFields will add fields of any type to the object spec. It takes an argument, fields, which is of the form path: object. 1079 // NOTE: The path should correspond to a field defined in the CRD. 1080 func (b *TestBootstrapConfigBuilder) WithSpecFields(fields map[string]interface{}) *TestBootstrapConfigBuilder { 1081 setSpecFields(b.obj, fields) 1082 return b 1083 } 1084 1085 // Build creates a new Unstructured object with the information passed to the BootstrapConfigBuilder. 1086 func (b *TestBootstrapConfigBuilder) Build() *unstructured.Unstructured { 1087 return b.obj 1088 } 1089 1090 // InfrastructureClusterTemplateBuilder holds the variables needed to build a generic InfrastructureClusterTemplate. 1091 type InfrastructureClusterTemplateBuilder struct { 1092 obj *unstructured.Unstructured 1093 } 1094 1095 // InfrastructureClusterTemplate returns an InfrastructureClusterTemplateBuilder with the given name and namespace. 1096 func InfrastructureClusterTemplate(namespace, name string) *InfrastructureClusterTemplateBuilder { 1097 obj := &unstructured.Unstructured{} 1098 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 1099 obj.SetKind(GenericInfrastructureClusterTemplateKind) 1100 obj.SetNamespace(namespace) 1101 obj.SetName(name) 1102 if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil { 1103 panic(err) 1104 } 1105 return &InfrastructureClusterTemplateBuilder{ 1106 obj: obj, 1107 } 1108 } 1109 1110 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1111 // to the value of the spec field. 1112 // 1113 // Note: all the paths should start with "spec." 1114 // 1115 // Example map: map[string]interface{}{ 1116 // "spec.version": "v1.2.3", 1117 // }. 1118 func (i *InfrastructureClusterTemplateBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureClusterTemplateBuilder { 1119 setSpecFields(i.obj, fields) 1120 return i 1121 } 1122 1123 // Build creates a new Unstructured object with the variables passed to the InfrastructureClusterTemplateBuilder. 1124 func (i *InfrastructureClusterTemplateBuilder) Build() *unstructured.Unstructured { 1125 return i.obj 1126 } 1127 1128 // TestInfrastructureClusterTemplateBuilder holds the variables needed to build a generic TestInfrastructureClusterTemplate. 1129 type TestInfrastructureClusterTemplateBuilder struct { 1130 obj *unstructured.Unstructured 1131 } 1132 1133 // TestInfrastructureClusterTemplate returns an TestInfrastructureClusterTemplateBuilder with the given name and namespace. 1134 func TestInfrastructureClusterTemplate(namespace, name string) *TestInfrastructureClusterTemplateBuilder { 1135 obj := &unstructured.Unstructured{} 1136 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 1137 obj.SetKind(TestInfrastructureClusterTemplateKind) 1138 obj.SetNamespace(namespace) 1139 obj.SetName(name) 1140 if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil { 1141 panic(err) 1142 } 1143 return &TestInfrastructureClusterTemplateBuilder{ 1144 obj: obj, 1145 } 1146 } 1147 1148 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1149 // to the value of the spec field. 1150 // 1151 // Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD. 1152 // 1153 // Example map: map[string]interface{}{ 1154 // "spec.version": "v1.2.3", 1155 // }. 1156 func (i *TestInfrastructureClusterTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureClusterTemplateBuilder { 1157 setSpecFields(i.obj, fields) 1158 return i 1159 } 1160 1161 // Build creates a new Unstructured object with the variables passed to the InfrastructureClusterTemplateBuilder. 1162 func (i *TestInfrastructureClusterTemplateBuilder) Build() *unstructured.Unstructured { 1163 return i.obj 1164 } 1165 1166 // ControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template. 1167 type ControlPlaneTemplateBuilder struct { 1168 obj *unstructured.Unstructured 1169 } 1170 1171 // ControlPlaneTemplate creates a NewControlPlaneTemplate builder with the given name and namespace. 1172 func ControlPlaneTemplate(namespace, name string) *ControlPlaneTemplateBuilder { 1173 obj := &unstructured.Unstructured{} 1174 obj.SetAPIVersion(ControlPlaneGroupVersion.String()) 1175 obj.SetKind(GenericControlPlaneTemplateKind) 1176 obj.SetNamespace(namespace) 1177 obj.SetName(name) 1178 1179 // Initialize the spec.template.spec to make the object valid in reconciliation. 1180 setSpecFields(obj, map[string]interface{}{"spec.template.spec": map[string]interface{}{}}) 1181 return &ControlPlaneTemplateBuilder{obj: obj} 1182 } 1183 1184 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1185 // to the value of the spec field. 1186 // 1187 // Note: all the paths should start with "spec." 1188 // 1189 // Example map: map[string]interface{}{ 1190 // "spec.version": "v1.2.3", 1191 // }. 1192 func (c *ControlPlaneTemplateBuilder) WithSpecFields(fields map[string]interface{}) *ControlPlaneTemplateBuilder { 1193 setSpecFields(c.obj, fields) 1194 return c 1195 } 1196 1197 // WithInfrastructureMachineTemplate adds the given Unstructured object to the ControlPlaneTemplateBuilder as its InfrastructureMachineTemplate. 1198 func (c *ControlPlaneTemplateBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *ControlPlaneTemplateBuilder { 1199 if err := setNestedRef(c.obj, t, "spec", "template", "spec", "machineTemplate", "infrastructureRef"); err != nil { 1200 panic(err) 1201 } 1202 return c 1203 } 1204 1205 // Build creates an Unstructured object from the variables passed to the ControlPlaneTemplateBuilder. 1206 func (c *ControlPlaneTemplateBuilder) Build() *unstructured.Unstructured { 1207 return c.obj 1208 } 1209 1210 // TestControlPlaneTemplateBuilder holds the variables and objects needed to build a generic ControlPlane template. 1211 type TestControlPlaneTemplateBuilder struct { 1212 obj *unstructured.Unstructured 1213 } 1214 1215 // TestControlPlaneTemplate creates a NewControlPlaneTemplate builder with the given name and namespace. 1216 func TestControlPlaneTemplate(namespace, name string) *TestControlPlaneTemplateBuilder { 1217 obj := &unstructured.Unstructured{} 1218 obj.SetName(name) 1219 obj.SetNamespace(namespace) 1220 obj.SetAPIVersion(ControlPlaneGroupVersion.String()) 1221 obj.SetKind(TestControlPlaneTemplateKind) 1222 // Set the mandatory spec field for the object. 1223 if err := unstructured.SetNestedField(obj.Object, map[string]interface{}{}, "spec", "template", "spec"); err != nil { 1224 panic(err) 1225 } 1226 return &TestControlPlaneTemplateBuilder{ 1227 obj, 1228 } 1229 } 1230 1231 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1232 // to the value of the spec field. 1233 // 1234 // Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD. 1235 // 1236 // Example map: map[string]interface{}{ 1237 // "spec.version": "v1.2.3", 1238 // }. 1239 func (c *TestControlPlaneTemplateBuilder) WithSpecFields(fields map[string]interface{}) *TestControlPlaneTemplateBuilder { 1240 setSpecFields(c.obj, fields) 1241 return c 1242 } 1243 1244 // WithInfrastructureMachineTemplate adds the given Unstructured object to the ControlPlaneTemplateBuilder as its InfrastructureMachineTemplate. 1245 func (c *TestControlPlaneTemplateBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *TestControlPlaneTemplateBuilder { 1246 if err := setNestedRef(c.obj, t, "spec", "template", "spec", "machineTemplate", "infrastructureRef"); err != nil { 1247 panic(err) 1248 } 1249 return c 1250 } 1251 1252 // Build creates an Unstructured object from the variables passed to the ControlPlaneTemplateBuilder. 1253 func (c *TestControlPlaneTemplateBuilder) Build() *unstructured.Unstructured { 1254 return c.obj 1255 } 1256 1257 // InfrastructureClusterBuilder holds the variables and objects needed to build a generic InfrastructureCluster. 1258 type InfrastructureClusterBuilder struct { 1259 obj *unstructured.Unstructured 1260 } 1261 1262 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1263 // to the value of the spec field. 1264 // 1265 // Note: all the paths should start with "spec." 1266 // 1267 // Example map: map[string]interface{}{ 1268 // "spec.version": "v1.2.3", 1269 // }. 1270 func (i *InfrastructureClusterBuilder) WithSpecFields(fields map[string]interface{}) *InfrastructureClusterBuilder { 1271 setSpecFields(i.obj, fields) 1272 return i 1273 } 1274 1275 // InfrastructureCluster returns and InfrastructureClusterBuilder with the given name and namespace. 1276 func InfrastructureCluster(namespace, name string) *InfrastructureClusterBuilder { 1277 obj := &unstructured.Unstructured{} 1278 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 1279 obj.SetKind(GenericInfrastructureClusterKind) 1280 obj.SetNamespace(namespace) 1281 obj.SetName(name) 1282 return &InfrastructureClusterBuilder{obj: obj} 1283 } 1284 1285 // Build returns an Unstructured object with the information passed to the InfrastructureClusterBuilder. 1286 func (i *InfrastructureClusterBuilder) Build() *unstructured.Unstructured { 1287 return i.obj 1288 } 1289 1290 // TestInfrastructureClusterBuilder holds the variables and objects needed to build a generic TestInfrastructureCluster. 1291 type TestInfrastructureClusterBuilder struct { 1292 obj *unstructured.Unstructured 1293 } 1294 1295 // TestInfrastructureCluster returns and TestInfrastructureClusterBuilder with the given name and namespace. 1296 func TestInfrastructureCluster(namespace, name string) *TestInfrastructureClusterBuilder { 1297 obj := &unstructured.Unstructured{} 1298 obj.SetAPIVersion(InfrastructureGroupVersion.String()) 1299 obj.SetKind(TestInfrastructureClusterKind) 1300 obj.SetNamespace(namespace) 1301 obj.SetName(name) 1302 return &TestInfrastructureClusterBuilder{ 1303 obj: obj, 1304 } 1305 } 1306 1307 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1308 // to the value of the spec field. 1309 // 1310 // Note: all the paths should start with "spec."; the path should correspond to a field defined in the CRD. 1311 // 1312 // Example map: map[string]interface{}{ 1313 // "spec.version": "v1.2.3", 1314 // }. 1315 func (i *TestInfrastructureClusterBuilder) WithSpecFields(fields map[string]interface{}) *TestInfrastructureClusterBuilder { 1316 setSpecFields(i.obj, fields) 1317 return i 1318 } 1319 1320 // Build returns an Unstructured object with the information passed to the InfrastructureClusterBuilder. 1321 func (i *TestInfrastructureClusterBuilder) Build() *unstructured.Unstructured { 1322 return i.obj 1323 } 1324 1325 // ControlPlaneBuilder holds the variables and objects needed to build a generic object for cluster.spec.controlPlaneRef. 1326 type ControlPlaneBuilder struct { 1327 obj *unstructured.Unstructured 1328 } 1329 1330 // ControlPlane returns a ControlPlaneBuilder with the given name and Namespace. 1331 func ControlPlane(namespace, name string) *ControlPlaneBuilder { 1332 obj := &unstructured.Unstructured{} 1333 obj.SetAPIVersion(ControlPlaneGroupVersion.String()) 1334 obj.SetKind(GenericControlPlaneKind) 1335 obj.SetNamespace(namespace) 1336 obj.SetName(name) 1337 return &ControlPlaneBuilder{ 1338 obj: obj, 1339 } 1340 } 1341 1342 // WithInfrastructureMachineTemplate adds the given unstructured object to the ControlPlaneBuilder as its InfrastructureMachineTemplate. 1343 func (c *ControlPlaneBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *ControlPlaneBuilder { 1344 // TODO(killianmuldoon): Update to use the internal/contract package, when it is importable from here 1345 if err := setNestedRef(c.obj, t, "spec", "machineTemplate", "infrastructureRef"); err != nil { 1346 panic(err) 1347 } 1348 return c 1349 } 1350 1351 // WithReplicas sets the number of replicas for the ControlPlaneBuilder. 1352 func (c *ControlPlaneBuilder) WithReplicas(replicas int64) *ControlPlaneBuilder { 1353 if err := unstructured.SetNestedField(c.obj.Object, replicas, "spec", "replicas"); err != nil { 1354 panic(err) 1355 } 1356 return c 1357 } 1358 1359 // WithVersion adds the passed version to the ControlPlaneBuilder. 1360 func (c *ControlPlaneBuilder) WithVersion(version string) *ControlPlaneBuilder { 1361 if err := unstructured.SetNestedField(c.obj.Object, version, "spec", "version"); err != nil { 1362 panic(err) 1363 } 1364 return c 1365 } 1366 1367 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1368 // to the value of the spec field. 1369 // 1370 // Note: all the paths should start with "spec." 1371 // 1372 // Example map: map[string]interface{}{ 1373 // "spec.version": "v1.2.3", 1374 // }. 1375 func (c *ControlPlaneBuilder) WithSpecFields(fields map[string]interface{}) *ControlPlaneBuilder { 1376 setSpecFields(c.obj, fields) 1377 return c 1378 } 1379 1380 // WithStatusFields sets a map of status fields on the unstructured object. The keys in the map represent the path and the value corresponds 1381 // to the value of the status field. 1382 // 1383 // Note: all the paths should start with "status." 1384 // 1385 // Example map: map[string]interface{}{ 1386 // "status.version": "v1.2.3", 1387 // }. 1388 func (c *ControlPlaneBuilder) WithStatusFields(fields map[string]interface{}) *ControlPlaneBuilder { 1389 setStatusFields(c.obj, fields) 1390 return c 1391 } 1392 1393 // Build generates an Unstructured object from the information passed to the ControlPlaneBuilder. 1394 func (c *ControlPlaneBuilder) Build() *unstructured.Unstructured { 1395 return c.obj 1396 } 1397 1398 // TestControlPlaneBuilder holds the variables and objects needed to build a generic object for cluster.spec.controlPlaneRef. 1399 type TestControlPlaneBuilder struct { 1400 obj *unstructured.Unstructured 1401 } 1402 1403 // TestControlPlane returns a TestControlPlaneBuilder with the given name and Namespace. 1404 func TestControlPlane(namespace, name string) *ControlPlaneBuilder { 1405 obj := &unstructured.Unstructured{} 1406 obj.SetAPIVersion(ControlPlaneGroupVersion.String()) 1407 obj.SetKind(TestControlPlaneKind) 1408 obj.SetNamespace(namespace) 1409 obj.SetName(name) 1410 return &ControlPlaneBuilder{ 1411 obj: obj, 1412 } 1413 } 1414 1415 // WithInfrastructureMachineTemplate adds the given unstructured object to the TestControlPlaneBuilder as its InfrastructureMachineTemplate. 1416 func (c *TestControlPlaneBuilder) WithInfrastructureMachineTemplate(t *unstructured.Unstructured) *TestControlPlaneBuilder { 1417 // TODO(killianmuldoon): Update to use the internal/contract package, when it is importable from here 1418 if err := setNestedRef(c.obj, t, "spec", "machineTemplate", "infrastructureRef"); err != nil { 1419 panic(err) 1420 } 1421 return c 1422 } 1423 1424 // WithReplicas sets the number of replicas for the TestControlPlaneBuilder. 1425 func (c *TestControlPlaneBuilder) WithReplicas(replicas int64) *TestControlPlaneBuilder { 1426 if err := unstructured.SetNestedField(c.obj.Object, replicas, "spec", "replicas"); err != nil { 1427 panic(err) 1428 } 1429 return c 1430 } 1431 1432 // WithVersion adds the passed version to the TestControlPlaneBuilder. 1433 func (c *TestControlPlaneBuilder) WithVersion(version string) *TestControlPlaneBuilder { 1434 if err := unstructured.SetNestedField(c.obj.Object, version, "spec", "version"); err != nil { 1435 panic(err) 1436 } 1437 return c 1438 } 1439 1440 // WithSpecFields sets a map of spec fields on the unstructured object. The keys in the map represent the path and the value corresponds 1441 // to the value of the spec field. 1442 // 1443 // Note: all the paths should start with "spec." 1444 // 1445 // Example map: map[string]interface{}{ 1446 // "spec.version": "v1.2.3", 1447 // }. 1448 func (c *TestControlPlaneBuilder) WithSpecFields(fields map[string]interface{}) *TestControlPlaneBuilder { 1449 setSpecFields(c.obj, fields) 1450 return c 1451 } 1452 1453 // WithStatusFields sets a map of status fields on the unstructured object. The keys in the map represent the path and the value corresponds 1454 // to the value of the status field. 1455 // 1456 // Note: all the paths should start with "status." 1457 // 1458 // Example map: map[string]interface{}{ 1459 // "status.version": "v1.2.3", 1460 // }. 1461 func (c *TestControlPlaneBuilder) WithStatusFields(fields map[string]interface{}) *TestControlPlaneBuilder { 1462 setStatusFields(c.obj, fields) 1463 return c 1464 } 1465 1466 // Build generates an Unstructured object from the information passed to the TestControlPlaneBuilder. 1467 func (c *TestControlPlaneBuilder) Build() *unstructured.Unstructured { 1468 return c.obj 1469 } 1470 1471 // MachinePoolBuilder holds the variables and objects needed to build a generic MachinePool. 1472 type MachinePoolBuilder struct { 1473 namespace string 1474 name string 1475 bootstrap *unstructured.Unstructured 1476 infrastructure *unstructured.Unstructured 1477 version *string 1478 clusterName string 1479 replicas *int32 1480 labels map[string]string 1481 status *expv1.MachinePoolStatus 1482 minReadySeconds *int32 1483 } 1484 1485 // MachinePool creates a MachinePoolBuilder with the given name and namespace. 1486 func MachinePool(namespace, name string) *MachinePoolBuilder { 1487 return &MachinePoolBuilder{ 1488 name: name, 1489 namespace: namespace, 1490 } 1491 } 1492 1493 // WithBootstrap adds the passed Unstructured object to the MachinePoolBuilder as a bootstrap. 1494 func (m *MachinePoolBuilder) WithBootstrap(ref *unstructured.Unstructured) *MachinePoolBuilder { 1495 m.bootstrap = ref 1496 return m 1497 } 1498 1499 // WithInfrastructure adds the passed Unstructured object to the MachinePool builder as an InfrastructureMachinePool. 1500 func (m *MachinePoolBuilder) WithInfrastructure(ref *unstructured.Unstructured) *MachinePoolBuilder { 1501 m.infrastructure = ref 1502 return m 1503 } 1504 1505 // WithLabels adds the given labels to the MachinePoolBuilder. 1506 func (m *MachinePoolBuilder) WithLabels(labels map[string]string) *MachinePoolBuilder { 1507 m.labels = labels 1508 return m 1509 } 1510 1511 // WithVersion sets the passed version on the MachinePool spec. 1512 func (m *MachinePoolBuilder) WithVersion(version string) *MachinePoolBuilder { 1513 m.version = &version 1514 return m 1515 } 1516 1517 // WithClusterName sets the passed clusterName on the MachinePool spec. 1518 func (m *MachinePoolBuilder) WithClusterName(clusterName string) *MachinePoolBuilder { 1519 m.clusterName = clusterName 1520 return m 1521 } 1522 1523 // WithReplicas sets the number of replicas for the MachinePoolBuilder. 1524 func (m *MachinePoolBuilder) WithReplicas(replicas int32) *MachinePoolBuilder { 1525 m.replicas = &replicas 1526 return m 1527 } 1528 1529 // WithStatus sets the passed status object as the status of the MachinePool object. 1530 func (m *MachinePoolBuilder) WithStatus(status expv1.MachinePoolStatus) *MachinePoolBuilder { 1531 m.status = &status 1532 return m 1533 } 1534 1535 // WithMinReadySeconds sets the passed value on the machine pool spec. 1536 func (m *MachinePoolBuilder) WithMinReadySeconds(minReadySeconds int32) *MachinePoolBuilder { 1537 m.minReadySeconds = &minReadySeconds 1538 return m 1539 } 1540 1541 // Build creates a new MachinePool with the variables and objects passed to the MachinePoolBuilder. 1542 func (m *MachinePoolBuilder) Build() *expv1.MachinePool { 1543 obj := &expv1.MachinePool{ 1544 TypeMeta: metav1.TypeMeta{ 1545 Kind: "MachinePool", 1546 APIVersion: expv1.GroupVersion.String(), 1547 }, 1548 ObjectMeta: metav1.ObjectMeta{ 1549 Name: m.name, 1550 Namespace: m.namespace, 1551 Labels: m.labels, 1552 }, 1553 Spec: expv1.MachinePoolSpec{ 1554 ClusterName: m.clusterName, 1555 Replicas: m.replicas, 1556 MinReadySeconds: m.minReadySeconds, 1557 Template: clusterv1.MachineTemplateSpec{ 1558 Spec: clusterv1.MachineSpec{ 1559 Version: m.version, 1560 ClusterName: m.clusterName, 1561 }, 1562 }, 1563 }, 1564 } 1565 if m.bootstrap != nil { 1566 obj.Spec.Template.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrap) 1567 } 1568 if m.infrastructure != nil { 1569 obj.Spec.Template.Spec.InfrastructureRef = *objToRef(m.infrastructure) 1570 } 1571 if m.status != nil { 1572 obj.Status = *m.status 1573 } 1574 return obj 1575 } 1576 1577 // MachineDeploymentBuilder holds the variables and objects needed to build a generic MachineDeployment. 1578 type MachineDeploymentBuilder struct { 1579 namespace string 1580 name string 1581 clusterName string 1582 bootstrapTemplate *unstructured.Unstructured 1583 infrastructureTemplate *unstructured.Unstructured 1584 selector *metav1.LabelSelector 1585 version *string 1586 replicas *int32 1587 generation *int64 1588 labels map[string]string 1589 status *clusterv1.MachineDeploymentStatus 1590 minReadySeconds *int32 1591 } 1592 1593 // MachineDeployment creates a MachineDeploymentBuilder with the given name and namespace. 1594 func MachineDeployment(namespace, name string) *MachineDeploymentBuilder { 1595 return &MachineDeploymentBuilder{ 1596 name: name, 1597 namespace: namespace, 1598 } 1599 } 1600 1601 // WithBootstrapTemplate adds the passed Unstructured object to the MachineDeploymentBuilder as a bootstrapTemplate. 1602 func (m *MachineDeploymentBuilder) WithBootstrapTemplate(ref *unstructured.Unstructured) *MachineDeploymentBuilder { 1603 m.bootstrapTemplate = ref 1604 return m 1605 } 1606 1607 // WithInfrastructureTemplate adds the passed unstructured object to the MachineDeployment builder as an infrastructureMachineTemplate. 1608 func (m *MachineDeploymentBuilder) WithInfrastructureTemplate(ref *unstructured.Unstructured) *MachineDeploymentBuilder { 1609 m.infrastructureTemplate = ref 1610 return m 1611 } 1612 1613 // WithSelector adds the passed selector to the MachineDeployment as the selector. 1614 func (m *MachineDeploymentBuilder) WithSelector(selector metav1.LabelSelector) *MachineDeploymentBuilder { 1615 m.selector = &selector 1616 return m 1617 } 1618 1619 // WithClusterName adds the clusterName to the MachineDeploymentBuilder. 1620 func (m *MachineDeploymentBuilder) WithClusterName(name string) *MachineDeploymentBuilder { 1621 m.clusterName = name 1622 return m 1623 } 1624 1625 // WithLabels adds the given labels to the MachineDeploymentBuilder. 1626 func (m *MachineDeploymentBuilder) WithLabels(labels map[string]string) *MachineDeploymentBuilder { 1627 m.labels = labels 1628 return m 1629 } 1630 1631 // WithVersion sets the passed version on the machine deployment spec. 1632 func (m *MachineDeploymentBuilder) WithVersion(version string) *MachineDeploymentBuilder { 1633 m.version = &version 1634 return m 1635 } 1636 1637 // WithReplicas sets the number of replicas for the MachineDeploymentClassBuilder. 1638 func (m *MachineDeploymentBuilder) WithReplicas(replicas int32) *MachineDeploymentBuilder { 1639 m.replicas = &replicas 1640 return m 1641 } 1642 1643 // WithGeneration sets the passed value on the machine deployments object metadata. 1644 func (m *MachineDeploymentBuilder) WithGeneration(generation int64) *MachineDeploymentBuilder { 1645 m.generation = &generation 1646 return m 1647 } 1648 1649 // WithStatus sets the passed status object as the status of the machine deployment object. 1650 func (m *MachineDeploymentBuilder) WithStatus(status clusterv1.MachineDeploymentStatus) *MachineDeploymentBuilder { 1651 m.status = &status 1652 return m 1653 } 1654 1655 // WithMinReadySeconds sets the passed value on the machine deployment spec. 1656 func (m *MachineDeploymentBuilder) WithMinReadySeconds(minReadySeconds int32) *MachineDeploymentBuilder { 1657 m.minReadySeconds = &minReadySeconds 1658 return m 1659 } 1660 1661 // Build creates a new MachineDeployment with the variables and objects passed to the MachineDeploymentBuilder. 1662 func (m *MachineDeploymentBuilder) Build() *clusterv1.MachineDeployment { 1663 obj := &clusterv1.MachineDeployment{ 1664 TypeMeta: metav1.TypeMeta{ 1665 Kind: "MachineDeployment", 1666 APIVersion: clusterv1.GroupVersion.String(), 1667 }, 1668 ObjectMeta: metav1.ObjectMeta{ 1669 Name: m.name, 1670 Namespace: m.namespace, 1671 Labels: m.labels, 1672 }, 1673 } 1674 if m.generation != nil { 1675 obj.Generation = *m.generation 1676 } 1677 if m.version != nil { 1678 obj.Spec.Template.Spec.Version = m.version 1679 } 1680 obj.Spec.Replicas = m.replicas 1681 if m.bootstrapTemplate != nil { 1682 obj.Spec.Template.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrapTemplate) 1683 } 1684 if m.infrastructureTemplate != nil { 1685 obj.Spec.Template.Spec.InfrastructureRef = *objToRef(m.infrastructureTemplate) 1686 } 1687 if m.selector != nil { 1688 obj.Spec.Selector = *m.selector 1689 } 1690 if m.status != nil { 1691 obj.Status = *m.status 1692 } 1693 if m.clusterName != "" { 1694 obj.Spec.Template.Spec.ClusterName = m.clusterName 1695 obj.Spec.ClusterName = m.clusterName 1696 if obj.Spec.Selector.MatchLabels == nil { 1697 obj.Spec.Selector.MatchLabels = map[string]string{} 1698 } 1699 obj.Spec.Selector.MatchLabels[clusterv1.ClusterNameLabel] = m.clusterName 1700 obj.Spec.Template.Labels = map[string]string{ 1701 clusterv1.ClusterNameLabel: m.clusterName, 1702 } 1703 } 1704 obj.Spec.MinReadySeconds = m.minReadySeconds 1705 1706 return obj 1707 } 1708 1709 // MachineSetBuilder holds the variables and objects needed to build a MachineSet. 1710 type MachineSetBuilder struct { 1711 namespace string 1712 name string 1713 bootstrapTemplate *unstructured.Unstructured 1714 infrastructureTemplate *unstructured.Unstructured 1715 replicas *int32 1716 labels map[string]string 1717 clusterName string 1718 ownerRefs []metav1.OwnerReference 1719 } 1720 1721 // MachineSet creates a MachineSetBuilder with the given name and namespace. 1722 func MachineSet(namespace, name string) *MachineSetBuilder { 1723 return &MachineSetBuilder{ 1724 name: name, 1725 namespace: namespace, 1726 } 1727 } 1728 1729 // WithBootstrapTemplate adds the passed Unstructured object to the MachineSetBuilder as a bootstrapTemplate. 1730 func (m *MachineSetBuilder) WithBootstrapTemplate(ref *unstructured.Unstructured) *MachineSetBuilder { 1731 m.bootstrapTemplate = ref 1732 return m 1733 } 1734 1735 // WithInfrastructureTemplate adds the passed unstructured object to the MachineSetBuilder as an infrastructureMachineTemplate. 1736 func (m *MachineSetBuilder) WithInfrastructureTemplate(ref *unstructured.Unstructured) *MachineSetBuilder { 1737 m.infrastructureTemplate = ref 1738 return m 1739 } 1740 1741 // WithLabels adds the given labels to the MachineSetBuilder. 1742 func (m *MachineSetBuilder) WithLabels(labels map[string]string) *MachineSetBuilder { 1743 m.labels = labels 1744 return m 1745 } 1746 1747 // WithReplicas sets the number of replicas for the MachineSetBuilder. 1748 func (m *MachineSetBuilder) WithReplicas(replicas *int32) *MachineSetBuilder { 1749 m.replicas = replicas 1750 return m 1751 } 1752 1753 // WithClusterName sets the number of replicas for the MachineSetBuilder. 1754 func (m *MachineSetBuilder) WithClusterName(name string) *MachineSetBuilder { 1755 m.clusterName = name 1756 return m 1757 } 1758 1759 // WithOwnerReferences adds ownerReferences for the MachineSetBuilder. 1760 func (m *MachineSetBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) *MachineSetBuilder { 1761 m.ownerRefs = ownerRefs 1762 return m 1763 } 1764 1765 // Build creates a new MachineSet with the variables and objects passed to the MachineSetBuilder. 1766 func (m *MachineSetBuilder) Build() *clusterv1.MachineSet { 1767 obj := &clusterv1.MachineSet{ 1768 TypeMeta: metav1.TypeMeta{ 1769 Kind: "MachineSet", 1770 APIVersion: clusterv1.GroupVersion.String(), 1771 }, 1772 ObjectMeta: metav1.ObjectMeta{ 1773 Name: m.name, 1774 Namespace: m.namespace, 1775 Labels: m.labels, 1776 OwnerReferences: m.ownerRefs, 1777 }, 1778 } 1779 obj.Spec.ClusterName = m.clusterName 1780 obj.Spec.Replicas = m.replicas 1781 if m.bootstrapTemplate != nil { 1782 obj.Spec.Template.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrapTemplate) 1783 } 1784 if m.infrastructureTemplate != nil { 1785 obj.Spec.Template.Spec.InfrastructureRef = *objToRef(m.infrastructureTemplate) 1786 } 1787 return obj 1788 } 1789 1790 // MachineBuilder holds the variables required to build a Machine. 1791 type MachineBuilder struct { 1792 name string 1793 namespace string 1794 version *string 1795 clusterName string 1796 bootstrap *unstructured.Unstructured 1797 labels map[string]string 1798 } 1799 1800 // Machine returns a MachineBuilder. 1801 func Machine(namespace, name string) *MachineBuilder { 1802 return &MachineBuilder{ 1803 name: name, 1804 namespace: namespace, 1805 } 1806 } 1807 1808 // WithVersion adds a version to the MachineBuilder. 1809 func (m *MachineBuilder) WithVersion(version string) *MachineBuilder { 1810 m.version = &version 1811 return m 1812 } 1813 1814 // WithBootstrapTemplate adds a bootstrap template to the MachineBuilder. 1815 func (m *MachineBuilder) WithBootstrapTemplate(bootstrap *unstructured.Unstructured) *MachineBuilder { 1816 m.bootstrap = bootstrap 1817 return m 1818 } 1819 1820 // WithClusterName adds a clusterName to the MachineBuilder. 1821 func (m *MachineBuilder) WithClusterName(clusterName string) *MachineBuilder { 1822 m.clusterName = clusterName 1823 return m 1824 } 1825 1826 // WithLabels adds the given labels to the MachineSetBuilder. 1827 func (m *MachineBuilder) WithLabels(labels map[string]string) *MachineBuilder { 1828 m.labels = labels 1829 return m 1830 } 1831 1832 // Build produces a Machine object from the information passed to the MachineBuilder. 1833 func (m *MachineBuilder) Build() *clusterv1.Machine { 1834 machine := &clusterv1.Machine{ 1835 TypeMeta: metav1.TypeMeta{ 1836 Kind: "Machine", 1837 APIVersion: clusterv1.GroupVersion.String(), 1838 }, 1839 ObjectMeta: metav1.ObjectMeta{ 1840 Namespace: m.namespace, 1841 Name: m.name, 1842 Labels: m.labels, 1843 }, 1844 Spec: clusterv1.MachineSpec{ 1845 Version: m.version, 1846 ClusterName: m.clusterName, 1847 }, 1848 } 1849 if m.bootstrap != nil { 1850 machine.Spec.Bootstrap.ConfigRef = objToRef(m.bootstrap) 1851 } 1852 if m.clusterName != "" { 1853 if len(m.labels) == 0 { 1854 machine.Labels = map[string]string{} 1855 } 1856 machine.ObjectMeta.Labels[clusterv1.ClusterNameLabel] = m.clusterName 1857 } 1858 return machine 1859 } 1860 1861 // objToRef returns a reference to the given object. 1862 func objToRef(obj client.Object) *corev1.ObjectReference { 1863 gvk := obj.GetObjectKind().GroupVersionKind() 1864 return &corev1.ObjectReference{ 1865 Kind: gvk.Kind, 1866 APIVersion: gvk.GroupVersion().String(), 1867 Namespace: obj.GetNamespace(), 1868 Name: obj.GetName(), 1869 } 1870 } 1871 1872 // setNestedRef sets the value of a nested field to a reference to the refObj provided. 1873 func setNestedRef(obj, refObj *unstructured.Unstructured, fields ...string) error { 1874 ref := map[string]interface{}{ 1875 "kind": refObj.GetKind(), 1876 "namespace": refObj.GetNamespace(), 1877 "name": refObj.GetName(), 1878 "apiVersion": refObj.GetAPIVersion(), 1879 } 1880 return unstructured.SetNestedField(obj.UnstructuredContent(), ref, fields...) 1881 } 1882 1883 // setSpecFields sets fields in an unstructured object from a map. 1884 func setSpecFields(obj *unstructured.Unstructured, fields map[string]interface{}) { 1885 for k, v := range fields { 1886 fieldParts := strings.Split(k, ".") 1887 if len(fieldParts) == 0 { 1888 panic(fmt.Errorf("fieldParts invalid")) 1889 } 1890 if fieldParts[0] != "spec" { 1891 panic(fmt.Errorf("can not set fields outside spec")) 1892 } 1893 if err := unstructured.SetNestedField(obj.UnstructuredContent(), v, strings.Split(k, ".")...); err != nil { 1894 panic(err) 1895 } 1896 } 1897 } 1898 1899 // setStatusFields sets fields in an unstructured object from a map. 1900 func setStatusFields(obj *unstructured.Unstructured, fields map[string]interface{}) { 1901 for k, v := range fields { 1902 fieldParts := strings.Split(k, ".") 1903 if len(fieldParts) == 0 { 1904 panic(fmt.Errorf("fieldParts invalid")) 1905 } 1906 if fieldParts[0] != "status" { 1907 panic(fmt.Errorf("can not set fields outside status")) 1908 } 1909 if err := unstructured.SetNestedField(obj.UnstructuredContent(), v, strings.Split(k, ".")...); err != nil { 1910 panic(err) 1911 } 1912 } 1913 } 1914 1915 // MachineHealthCheckBuilder holds fields for creating a MachineHealthCheck. 1916 type MachineHealthCheckBuilder struct { 1917 name string 1918 namespace string 1919 ownerRefs []metav1.OwnerReference 1920 selector metav1.LabelSelector 1921 clusterName string 1922 conditions []clusterv1.UnhealthyCondition 1923 maxUnhealthy *intstr.IntOrString 1924 } 1925 1926 // MachineHealthCheck returns a MachineHealthCheckBuilder with the given name and namespace. 1927 func MachineHealthCheck(namespace, name string) *MachineHealthCheckBuilder { 1928 return &MachineHealthCheckBuilder{ 1929 name: name, 1930 namespace: namespace, 1931 } 1932 } 1933 1934 // WithSelector adds the selector used to target machines for the MachineHealthCheck. 1935 func (m *MachineHealthCheckBuilder) WithSelector(selector metav1.LabelSelector) *MachineHealthCheckBuilder { 1936 m.selector = selector 1937 return m 1938 } 1939 1940 // WithClusterName adds a cluster name for the MachineHealthCheck. 1941 func (m *MachineHealthCheckBuilder) WithClusterName(clusterName string) *MachineHealthCheckBuilder { 1942 m.clusterName = clusterName 1943 return m 1944 } 1945 1946 // WithUnhealthyConditions adds the spec used to build the parameters of the MachineHealthCheck. 1947 func (m *MachineHealthCheckBuilder) WithUnhealthyConditions(conditions []clusterv1.UnhealthyCondition) *MachineHealthCheckBuilder { 1948 m.conditions = conditions 1949 return m 1950 } 1951 1952 // WithOwnerReferences adds ownerreferences for the MachineHealthCheck. 1953 func (m *MachineHealthCheckBuilder) WithOwnerReferences(ownerRefs []metav1.OwnerReference) *MachineHealthCheckBuilder { 1954 m.ownerRefs = ownerRefs 1955 return m 1956 } 1957 1958 // WithMaxUnhealthy adds a MaxUnhealthyValue for the MachineHealthCheck. 1959 func (m *MachineHealthCheckBuilder) WithMaxUnhealthy(maxUnhealthy *intstr.IntOrString) *MachineHealthCheckBuilder { 1960 m.maxUnhealthy = maxUnhealthy 1961 return m 1962 } 1963 1964 // Build returns a MachineHealthCheck with the supplied details. 1965 func (m *MachineHealthCheckBuilder) Build() *clusterv1.MachineHealthCheck { 1966 // create a MachineHealthCheck with the spec given in the ClusterClass 1967 mhc := &clusterv1.MachineHealthCheck{ 1968 TypeMeta: metav1.TypeMeta{ 1969 Kind: "MachineHealthCheck", 1970 APIVersion: clusterv1.GroupVersion.String(), 1971 }, 1972 ObjectMeta: metav1.ObjectMeta{ 1973 Name: m.name, 1974 Namespace: m.namespace, 1975 OwnerReferences: m.ownerRefs, 1976 }, 1977 Spec: clusterv1.MachineHealthCheckSpec{ 1978 ClusterName: m.clusterName, 1979 Selector: m.selector, 1980 UnhealthyConditions: m.conditions, 1981 MaxUnhealthy: m.maxUnhealthy, 1982 }, 1983 } 1984 if m.clusterName != "" { 1985 mhc.Labels = map[string]string{clusterv1.ClusterNameLabel: m.clusterName} 1986 } 1987 1988 return mhc 1989 }