|
| 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 | + nutanixv1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1" |
| 26 | + credentialTypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials" |
| 27 | + cerrors "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | +) |
| 32 | + |
| 33 | +// ensureNutanixCluster ensures the NutanixCluster cluster object exists. |
| 34 | +func (r *InfraClusterController) ensureNutanixCluster(ctx context.Context, log logr.Logger) (client.Object, error) { |
| 35 | + target := &nutanixv1.NutanixCluster{ObjectMeta: metav1.ObjectMeta{ |
| 36 | + Name: r.Infra.Status.InfrastructureName, |
| 37 | + Namespace: defaultCAPINamespace, |
| 38 | + }} |
| 39 | + |
| 40 | + // Checking whether InfraCluster object exists. If it doesn't, create it. |
| 41 | + if err := r.Get(ctx, client.ObjectKeyFromObject(target), target); err != nil && !cerrors.IsNotFound(err) { |
| 42 | + return nil, fmt.Errorf("failed to get InfraCluster: %w", err) |
| 43 | + } else if err == nil { |
| 44 | + return target, nil |
| 45 | + } |
| 46 | + |
| 47 | + log.Info(fmt.Sprintf("NutanixCluster %s/%s does not exist, creating it", target.Namespace, target.Name)) |
| 48 | + |
| 49 | + apiURL, err := url.Parse(r.Infra.Status.APIServerInternalURL) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to parse apiURL: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + port, err := strconv.ParseInt(apiURL.Port(), 10, 32) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to parse apiURL port: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + if r.Infra.Status.PlatformStatus == nil { |
| 60 | + return nil, fmt.Errorf("infrastructure PlatformStatus should not be nil") |
| 61 | + } |
| 62 | + |
| 63 | + // Build the NutanixCluster spec |
| 64 | + clusterSpec := nutanixv1.NutanixClusterSpec{ |
| 65 | + ControlPlaneEndpoint: clusterv1.APIEndpoint{ |
| 66 | + Host: apiURL.Hostname(), |
| 67 | + Port: int32(port), |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + // Add PrismCentral configuration if available in the Infrastructure spec |
| 72 | + if r.Infra.Spec.PlatformSpec.Nutanix != nil && r.Infra.Spec.PlatformSpec.Nutanix.PrismCentral.Address != "" { |
| 73 | + clusterSpec.PrismCentral = &credentialTypes.NutanixPrismEndpoint{ |
| 74 | + // Address holds the IP address or FQDN of the Nutanix Prism Central |
| 75 | + Address: r.Infra.Spec.PlatformSpec.Nutanix.PrismCentral.Address, |
| 76 | + Port: r.Infra.Spec.PlatformSpec.Nutanix.PrismCentral.Port, |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Add failure domains if available in the Infrastructure spec |
| 81 | + if r.Infra.Spec.PlatformSpec.Nutanix != nil && len(r.Infra.Spec.PlatformSpec.Nutanix.FailureDomains) > 0 { |
| 82 | + failureDomains := make([]nutanixv1.NutanixFailureDomainConfig, 0, len(r.Infra.Spec.PlatformSpec.Nutanix.FailureDomains)) |
| 83 | + for _, fd := range r.Infra.Spec.PlatformSpec.Nutanix.FailureDomains { |
| 84 | + failureDomains = append(failureDomains, nutanixv1.NutanixFailureDomainConfig{ |
| 85 | + Name: fd.Name, |
| 86 | + }) |
| 87 | + } |
| 88 | + clusterSpec.FailureDomains = failureDomains |
| 89 | + } |
| 90 | + |
| 91 | + target = &nutanixv1.NutanixCluster{ |
| 92 | + ObjectMeta: metav1.ObjectMeta{ |
| 93 | + Name: r.Infra.Status.InfrastructureName, |
| 94 | + Namespace: defaultCAPINamespace, |
| 95 | + // The ManagedBy Annotation is set so CAPI infra providers ignore the InfraCluster object, |
| 96 | + // as that's managed externally, in this case by this controller. |
| 97 | + Annotations: map[string]string{ |
| 98 | + clusterv1.ManagedByAnnotation: managedByAnnotationValueClusterCAPIOperatorInfraClusterController, |
| 99 | + }, |
| 100 | + }, |
| 101 | + Spec: clusterSpec, |
| 102 | + } |
| 103 | + |
| 104 | + if err := r.Create(ctx, target); err != nil { |
| 105 | + return nil, fmt.Errorf("failed to create InfraCluster: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + log.Info(fmt.Sprintf("InfraCluster '%s/%s' successfully created", defaultCAPINamespace, r.Infra.Status.InfrastructureName)) |
| 109 | + |
| 110 | + return target, nil |
| 111 | +} |
0 commit comments