|
| 1 | +/* |
| 2 | +Copyright 2024 Red Hat, Inc. |
| 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 | +package infracluster |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "net/url" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + mapiv1beta1 "github.com/openshift/api/machine/v1beta1" |
| 26 | + cerrors "k8s.io/apimachinery/pkg/api/errors" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + gcpv1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/yaml" |
| 32 | +) |
| 33 | + |
| 34 | +// ensureGCPCluster ensures the GCPCluster cluster object exists. |
| 35 | +// |
| 36 | +//nolint:funlen |
| 37 | +func (r *InfraClusterController) ensureGCPCluster(ctx context.Context, log logr.Logger) (client.Object, error) { |
| 38 | + target := &gcpv1.GCPCluster{ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: r.Infra.Status.InfrastructureName, |
| 40 | + Namespace: defaultCAPINamespace, |
| 41 | + }} |
| 42 | + |
| 43 | + // Checking whether InfraCluster object exists. If it doesn't, create it. |
| 44 | + if err := r.Get(ctx, client.ObjectKeyFromObject(target), target); err != nil && !cerrors.IsNotFound(err) { |
| 45 | + return nil, fmt.Errorf("failed to get InfraCluster: %w", err) |
| 46 | + } else if err == nil { |
| 47 | + return target, nil |
| 48 | + } |
| 49 | + |
| 50 | + log.Info(fmt.Sprintf("GCPCluster %s/%s does not exist, creating it", target.Namespace, target.Name)) |
| 51 | + |
| 52 | + apiURL, err := url.Parse(r.Infra.Status.APIServerInternalURL) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to parse apiUrl: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + port, err := strconv.ParseInt(apiURL.Port(), 10, 32) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed to parse apiUrl port: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + if r.Infra.Status.PlatformStatus == nil { |
| 63 | + return nil, fmt.Errorf("infrastructure PlatformStatus should not be nil: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + gcpProjectID, err := r.getGCPProjectID(ctx) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("error obtaining GCP Project ID: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + providerSpec, err := getGCPMAPIProviderSpec(ctx, r.Client) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("error obtaining GCP Provider Spec: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + target = &gcpv1.GCPCluster{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Name: r.Infra.Status.InfrastructureName, |
| 79 | + Namespace: defaultCAPINamespace, |
| 80 | + // The ManagedBy Annotation is set so CAPI infra providers ignore the InfraCluster object, |
| 81 | + // as that's managed externally, in this case by this controller. |
| 82 | + Annotations: map[string]string{ |
| 83 | + clusterv1.ManagedByAnnotation: managedByAnnotationValueClusterCAPIOperatorInfraClusterController, |
| 84 | + }, |
| 85 | + }, |
| 86 | + Spec: gcpv1.GCPClusterSpec{ |
| 87 | + Network: gcpv1.NetworkSpec{ |
| 88 | + Name: &providerSpec.NetworkInterfaces[0].Network, |
| 89 | + }, |
| 90 | + Region: r.Infra.Status.PlatformStatus.GCP.Region, |
| 91 | + Project: gcpProjectID, |
| 92 | + ControlPlaneEndpoint: clusterv1.APIEndpoint{ |
| 93 | + Host: apiURL.Hostname(), |
| 94 | + Port: int32(port), |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + if err := r.Create(ctx, target); err != nil { |
| 100 | + return nil, fmt.Errorf("failed to create InfraCluster: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + log.Info(fmt.Sprintf("InfraCluster '%s/%s' successfully created", defaultCAPINamespace, r.Infra.Status.InfrastructureName)) |
| 104 | + |
| 105 | + return target, nil |
| 106 | +} |
| 107 | + |
| 108 | +// getGCPMAPIProviderSpec returns a GCP Machine ProviderSpec from the the cluster. |
| 109 | +func getGCPMAPIProviderSpec(ctx context.Context, cl client.Client) (*mapiv1beta1.GCPMachineProviderSpec, error) { |
| 110 | + rawProviderSpec, err := getRawMAPIProviderSpec(ctx, cl) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("unable to obtain MAPI ProviderSpec: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + providerSpec := &mapiv1beta1.GCPMachineProviderSpec{} |
| 116 | + if err := yaml.Unmarshal(rawProviderSpec, providerSpec); err != nil { |
| 117 | + return nil, fmt.Errorf("unable to unmarshal MAPI ProviderSpec: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + return providerSpec, nil |
| 121 | +} |
| 122 | + |
| 123 | +// getGCPProjectID obtains the GCP Project ID. |
| 124 | +func (r *InfraClusterController) getGCPProjectID(ctx context.Context) (string, error) { |
| 125 | + if r.Infra.Spec.PlatformSpec.GCP == nil || len(r.Infra.Status.PlatformStatus.GCP.ProjectID) == 0 { |
| 126 | + // Devise GCP Project ID via MAPI providerSpec. |
| 127 | + machineSpec, err := getGCPMAPIProviderSpec(ctx, r.Client) |
| 128 | + if err != nil || machineSpec == nil { |
| 129 | + return "", fmt.Errorf("unable to get GCP MAPI ProviderSpec: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + return machineSpec.ProjectID, nil |
| 133 | + } |
| 134 | + |
| 135 | + projectID := r.Infra.Status.PlatformStatus.GCP.ProjectID |
| 136 | + |
| 137 | + return projectID, nil |
| 138 | +} |
0 commit comments