github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/infrastructure/v1alpha2/cluster.go (about) 1 package v1alpha2 2 3 import ( 4 corev1 "k8s.io/api/core/v1" 5 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 6 apiv1alpha2 "sigs.k8s.io/cluster-api/api/v1alpha2" 7 8 "github.com/giantswarm/apiextensions/v2/pkg/annotation" 9 "github.com/giantswarm/apiextensions/v2/pkg/id" 10 "github.com/giantswarm/apiextensions/v2/pkg/label" 11 ) 12 13 const ( 14 defaultMasterInstanceType = "m5.xlarge" 15 ) 16 17 // +k8s:deepcopy-gen=false 18 19 type ClusterCRsConfig struct { 20 ClusterID string 21 ControlPlaneID string 22 Credential string 23 Domain string 24 ExternalSNAT bool 25 MasterAZ []string 26 Description string 27 PodsCIDR string 28 Owner string 29 Region string 30 ReleaseComponents map[string]string 31 ReleaseVersion string 32 Labels map[string]string 33 NetworkPool string 34 } 35 36 // +k8s:deepcopy-gen=false 37 38 type ClusterCRs struct { 39 Cluster *apiv1alpha2.Cluster 40 AWSCluster *AWSCluster 41 G8sControlPlane *G8sControlPlane 42 AWSControlPlane *AWSControlPlane 43 } 44 45 func NewClusterCRs(config ClusterCRsConfig) (ClusterCRs, error) { 46 // Default some essentials in case certain information are not given. E.g. 47 // the Tenant Cluster ID may be provided by the user. 48 { 49 if config.ClusterID == "" { 50 config.ClusterID = id.Generate() 51 } 52 if config.ControlPlaneID == "" { 53 config.ControlPlaneID = id.Generate() 54 } 55 } 56 57 awsClusterCR := newAWSClusterCR(config) 58 clusterCR := newClusterCR(awsClusterCR, config) 59 awsControlPlaneCR := newAWSControlPlaneCR(config) 60 g8sControlPlaneCR := newG8sControlPlaneCR(awsControlPlaneCR, config) 61 62 crs := ClusterCRs{ 63 Cluster: clusterCR, 64 AWSCluster: awsClusterCR, 65 G8sControlPlane: g8sControlPlaneCR, 66 AWSControlPlane: awsControlPlaneCR, 67 } 68 69 return crs, nil 70 } 71 72 func newAWSClusterCR(c ClusterCRsConfig) *AWSCluster { 73 awsClusterCR := &AWSCluster{ 74 TypeMeta: metav1.TypeMeta{ 75 Kind: kindAWSCluster, 76 APIVersion: SchemeGroupVersion.String(), 77 }, 78 ObjectMeta: metav1.ObjectMeta{ 79 Name: c.ClusterID, 80 Namespace: metav1.NamespaceDefault, 81 Annotations: map[string]string{ 82 annotation.Docs: "https://docs.giantswarm.io/reference/cp-k8s-api/awsclusters.infrastructure.giantswarm.io", 83 }, 84 Labels: map[string]string{ 85 label.AWSOperatorVersion: c.ReleaseComponents["aws-operator"], 86 label.Cluster: c.ClusterID, 87 label.Organization: c.Owner, 88 label.ReleaseVersion: c.ReleaseVersion, 89 }, 90 }, 91 Spec: AWSClusterSpec{ 92 Cluster: AWSClusterSpecCluster{ 93 Description: c.Description, 94 DNS: AWSClusterSpecClusterDNS{ 95 Domain: c.Domain, 96 }, 97 OIDC: AWSClusterSpecClusterOIDC{}, 98 }, 99 Provider: AWSClusterSpecProvider{ 100 CredentialSecret: AWSClusterSpecProviderCredentialSecret{ 101 Name: c.Credential, 102 Namespace: "giantswarm", 103 }, 104 Pods: AWSClusterSpecProviderPods{ 105 CIDRBlock: c.PodsCIDR, 106 ExternalSNAT: &c.ExternalSNAT, 107 }, 108 Nodes: AWSClusterSpecProviderNodes{ 109 NetworkPool: c.NetworkPool, 110 }, 111 Region: c.Region, 112 }, 113 }, 114 } 115 116 // Single master node 117 if len(c.MasterAZ) == 1 { 118 awsClusterCR.Spec.Provider.Master = AWSClusterSpecProviderMaster{ 119 AvailabilityZone: c.MasterAZ[0], 120 InstanceType: defaultMasterInstanceType, 121 } 122 } 123 124 return awsClusterCR 125 } 126 127 func newAWSControlPlaneCR(c ClusterCRsConfig) *AWSControlPlane { 128 return &AWSControlPlane{ 129 TypeMeta: metav1.TypeMeta{ 130 Kind: kindAWSControlPlane, 131 APIVersion: SchemeGroupVersion.String(), 132 }, 133 ObjectMeta: metav1.ObjectMeta{ 134 Name: c.ControlPlaneID, 135 Namespace: metav1.NamespaceDefault, 136 Annotations: map[string]string{ 137 annotation.Docs: "https://docs.giantswarm.io/reference/cp-k8s-api/awscontrolplanes.infrastructure.giantswarm.io", 138 }, 139 Labels: map[string]string{ 140 label.AWSOperatorVersion: c.ReleaseComponents["aws-operator"], 141 label.Cluster: c.ClusterID, 142 label.ControlPlane: c.ControlPlaneID, 143 label.Organization: c.Owner, 144 label.ReleaseVersion: c.ReleaseVersion, 145 }, 146 }, 147 Spec: AWSControlPlaneSpec{ 148 AvailabilityZones: c.MasterAZ, 149 InstanceType: defaultMasterInstanceType, 150 }, 151 } 152 } 153 154 func newClusterCR(obj *AWSCluster, c ClusterCRsConfig) *apiv1alpha2.Cluster { 155 clusterLabels := map[string]string{} 156 { 157 for key, value := range c.Labels { 158 clusterLabels[key] = value 159 } 160 161 gsLabels := map[string]string{ 162 label.ClusterOperatorVersion: c.ReleaseComponents["cluster-operator"], 163 label.Cluster: c.ClusterID, 164 label.Organization: c.Owner, 165 label.ReleaseVersion: c.ReleaseVersion, 166 } 167 168 for key, value := range gsLabels { 169 clusterLabels[key] = value 170 } 171 } 172 173 clusterCR := &apiv1alpha2.Cluster{ 174 TypeMeta: metav1.TypeMeta{ 175 Kind: "Cluster", 176 APIVersion: "cluster.x-k8s.io/v1alpha2", 177 }, 178 ObjectMeta: metav1.ObjectMeta{ 179 Name: c.ClusterID, 180 Namespace: metav1.NamespaceDefault, 181 Annotations: map[string]string{ 182 annotation.Docs: "https://docs.giantswarm.io/reference/cp-k8s-api/clusters.cluster.x-k8s.io", 183 }, 184 Labels: clusterLabels, 185 }, 186 Spec: apiv1alpha2.ClusterSpec{ 187 InfrastructureRef: &corev1.ObjectReference{ 188 APIVersion: obj.TypeMeta.APIVersion, 189 Kind: obj.TypeMeta.Kind, 190 Name: obj.GetName(), 191 Namespace: obj.GetNamespace(), 192 }, 193 }, 194 } 195 196 return clusterCR 197 } 198 199 func newG8sControlPlaneCR(obj *AWSControlPlane, c ClusterCRsConfig) *G8sControlPlane { 200 return &G8sControlPlane{ 201 TypeMeta: metav1.TypeMeta{ 202 Kind: kindG8sControlPlane, 203 APIVersion: SchemeGroupVersion.String(), 204 }, 205 ObjectMeta: metav1.ObjectMeta{ 206 Name: c.ControlPlaneID, 207 Namespace: metav1.NamespaceDefault, 208 Annotations: map[string]string{ 209 annotation.Docs: "https://docs.giantswarm.io/reference/cp-k8s-api/g8scontrolplanes.infrastructure.giantswarm.io", 210 }, 211 Labels: map[string]string{ 212 label.ClusterOperatorVersion: c.ReleaseComponents["cluster-operator"], 213 label.Cluster: c.ClusterID, 214 label.ControlPlane: c.ControlPlaneID, 215 label.Organization: c.Owner, 216 label.ReleaseVersion: c.ReleaseVersion, 217 }, 218 }, 219 Spec: G8sControlPlaneSpec{ 220 Replicas: len(c.MasterAZ), 221 InfrastructureRef: corev1.ObjectReference{ 222 APIVersion: obj.TypeMeta.APIVersion, 223 Kind: obj.TypeMeta.Kind, 224 Name: obj.GetName(), 225 Namespace: obj.GetNamespace(), 226 }, 227 }, 228 } 229 }