sigs.k8s.io/cluster-api@v1.7.1/exp/topology/scope/blueprint.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 scope 18 19 import ( 20 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 21 22 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 23 ) 24 25 // ClusterBlueprint holds all the objects required for computing the desired state of a managed Cluster topology, 26 // including the ClusterClass and all the referenced templates. 27 type ClusterBlueprint struct { 28 // Topology holds the topology info from Cluster.Spec. 29 Topology *clusterv1.Topology 30 31 // ClusterClass holds the ClusterClass object referenced from Cluster.Spec.Topology. 32 ClusterClass *clusterv1.ClusterClass 33 34 // InfrastructureClusterTemplate holds the InfrastructureClusterTemplate referenced from ClusterClass. 35 InfrastructureClusterTemplate *unstructured.Unstructured 36 37 // ControlPlane holds the ControlPlaneBlueprint derived from ClusterClass. 38 ControlPlane *ControlPlaneBlueprint 39 40 // MachineDeployments holds the MachineDeploymentBlueprints derived from ClusterClass. 41 MachineDeployments map[string]*MachineDeploymentBlueprint 42 43 // MachinePools holds the MachinePoolBlueprints derived from ClusterClass. 44 MachinePools map[string]*MachinePoolBlueprint 45 } 46 47 // ControlPlaneBlueprint holds the templates required for computing the desired state of a managed control plane. 48 type ControlPlaneBlueprint struct { 49 // Template holds the control plane template referenced from ClusterClass. 50 Template *unstructured.Unstructured 51 52 // InfrastructureMachineTemplate holds the infrastructure machine template for the control plane, if defined in the ClusterClass. 53 InfrastructureMachineTemplate *unstructured.Unstructured 54 55 // MachineHealthCheck holds the MachineHealthCheckClass for this ControlPlane. 56 // +optional 57 MachineHealthCheck *clusterv1.MachineHealthCheckClass 58 } 59 60 // MachineDeploymentBlueprint holds the templates required for computing the desired state of a managed MachineDeployment; 61 // it also holds a copy of the MachineDeployment metadata from Cluster.Topology, thus providing all the required info 62 // in a single place. 63 type MachineDeploymentBlueprint struct { 64 // Metadata holds the metadata for a MachineDeployment. 65 // NOTE: This is a convenience copy of the metadata field from Cluster.Spec.Topology.Workers.MachineDeployments[x]. 66 Metadata clusterv1.ObjectMeta 67 68 // BootstrapTemplate holds the bootstrap template for a MachineDeployment referenced from ClusterClass. 69 BootstrapTemplate *unstructured.Unstructured 70 71 // InfrastructureMachineTemplate holds the infrastructure machine template for a MachineDeployment referenced from ClusterClass. 72 InfrastructureMachineTemplate *unstructured.Unstructured 73 74 // MachineHealthCheck holds the MachineHealthCheckClass for this MachineDeployment. 75 // +optional 76 MachineHealthCheck *clusterv1.MachineHealthCheckClass 77 } 78 79 // MachinePoolBlueprint holds the templates required for computing the desired state of a managed MachinePool; 80 // it also holds a copy of the MachinePool metadata from Cluster.Topology, thus providing all the required info 81 // in a single place. 82 type MachinePoolBlueprint struct { 83 // Metadata holds the metadata for a MachinePool. 84 // NOTE: This is a convenience copy of the metadata field from Cluster.Spec.Topology.Workers.MachinePools[x]. 85 Metadata clusterv1.ObjectMeta 86 87 // BootstrapTemplate holds the bootstrap template for a MachinePool referenced from ClusterClass. 88 BootstrapTemplate *unstructured.Unstructured 89 90 // InfrastructureMachinePoolTemplate holds the infrastructure machine pool template for a MachinePool referenced from ClusterClass. 91 InfrastructureMachinePoolTemplate *unstructured.Unstructured 92 } 93 94 // HasControlPlaneInfrastructureMachine checks whether the clusterClass mandates the controlPlane has infrastructureMachines. 95 func (b *ClusterBlueprint) HasControlPlaneInfrastructureMachine() bool { 96 return b.ClusterClass.Spec.ControlPlane.MachineInfrastructure != nil && b.ClusterClass.Spec.ControlPlane.MachineInfrastructure.Ref != nil 97 } 98 99 // IsControlPlaneMachineHealthCheckEnabled returns true if a MachineHealthCheck should be created for the control plane. 100 // Returns false otherwise. 101 func (b *ClusterBlueprint) IsControlPlaneMachineHealthCheckEnabled() bool { 102 if !b.HasControlPlaneInfrastructureMachine() { 103 return false 104 } 105 // If no MachineHealthCheck is defined in the ClusterClass or in the Cluster Topology then return false. 106 if b.ClusterClass.Spec.ControlPlane.MachineHealthCheck == nil && 107 (b.Topology.ControlPlane.MachineHealthCheck == nil || b.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass.IsZero()) { 108 return false 109 } 110 // If `enable` is not set then consider it as true. A MachineHealthCheck will be created from either ClusterClass or Cluster Topology. 111 if b.Topology.ControlPlane.MachineHealthCheck == nil || b.Topology.ControlPlane.MachineHealthCheck.Enable == nil { 112 return true 113 } 114 // If `enable` is explicitly set, use the value. 115 return *b.Topology.ControlPlane.MachineHealthCheck.Enable 116 } 117 118 // ControlPlaneMachineHealthCheckClass returns the MachineHealthCheckClass that should be used to create the MachineHealthCheck object. 119 func (b *ClusterBlueprint) ControlPlaneMachineHealthCheckClass() *clusterv1.MachineHealthCheckClass { 120 if b.Topology.ControlPlane.MachineHealthCheck != nil && !b.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass.IsZero() { 121 return &b.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass 122 } 123 return b.ControlPlane.MachineHealthCheck 124 } 125 126 // HasControlPlaneMachineHealthCheck returns true if the ControlPlaneClass has both MachineInfrastructure and a MachineHealthCheck defined. 127 func (b *ClusterBlueprint) HasControlPlaneMachineHealthCheck() bool { 128 return b.HasControlPlaneInfrastructureMachine() && b.ClusterClass.Spec.ControlPlane.MachineHealthCheck != nil 129 } 130 131 // IsMachineDeploymentMachineHealthCheckEnabled returns true if a MachineHealthCheck should be created for the MachineDeployment. 132 // Returns false otherwise. 133 func (b *ClusterBlueprint) IsMachineDeploymentMachineHealthCheckEnabled(md *clusterv1.MachineDeploymentTopology) bool { 134 // If no MachineHealthCheck is defined in the ClusterClass or in the Cluster Topology then return false. 135 if b.MachineDeployments[md.Class].MachineHealthCheck == nil && (md.MachineHealthCheck == nil || md.MachineHealthCheck.MachineHealthCheckClass.IsZero()) { 136 return false 137 } 138 // If `enable` is not set then consider it as true. A MachineHealthCheck will be created from either ClusterClass or Cluster Topology. 139 if md.MachineHealthCheck == nil || md.MachineHealthCheck.Enable == nil { 140 return true 141 } 142 // If `enable` is explicitly set, use the value. 143 return *md.MachineHealthCheck.Enable 144 } 145 146 // MachineDeploymentMachineHealthCheckClass return the MachineHealthCheckClass that should be used to create the MachineHealthCheck object. 147 func (b *ClusterBlueprint) MachineDeploymentMachineHealthCheckClass(md *clusterv1.MachineDeploymentTopology) *clusterv1.MachineHealthCheckClass { 148 if md.MachineHealthCheck != nil && !md.MachineHealthCheck.MachineHealthCheckClass.IsZero() { 149 return &md.MachineHealthCheck.MachineHealthCheckClass 150 } 151 return b.MachineDeployments[md.Class].MachineHealthCheck 152 } 153 154 // HasMachineDeployments checks whether the topology has MachineDeployments. 155 func (b *ClusterBlueprint) HasMachineDeployments() bool { 156 return b.Topology.Workers != nil && len(b.Topology.Workers.MachineDeployments) > 0 157 } 158 159 // HasMachinePools checks whether the topology has MachinePools. 160 func (b *ClusterBlueprint) HasMachinePools() bool { 161 return b.Topology.Workers != nil && len(b.Topology.Workers.MachinePools) > 0 162 }