|
| 1 | +// Copyright 2024 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cluster |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + |
| 12 | + v1 "k8s.io/api/admission/v1" |
| 13 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 14 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 16 | + |
| 17 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 18 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables" |
| 19 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils" |
| 20 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/helpers" |
| 21 | +) |
| 22 | + |
| 23 | +type nutanixValidator struct { |
| 24 | + client ctrlclient.Client |
| 25 | + decoder admission.Decoder |
| 26 | +} |
| 27 | + |
| 28 | +func NewNutanixValidator( |
| 29 | + client ctrlclient.Client, decoder admission.Decoder, |
| 30 | +) *nutanixValidator { |
| 31 | + return &nutanixValidator{ |
| 32 | + client: client, |
| 33 | + decoder: decoder, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (a *nutanixValidator) Validator() admission.HandlerFunc { |
| 38 | + return a.validate |
| 39 | +} |
| 40 | + |
| 41 | +func (a *nutanixValidator) validate( |
| 42 | + ctx context.Context, |
| 43 | + req admission.Request, |
| 44 | +) admission.Response { |
| 45 | + if req.Operation == v1.Delete { |
| 46 | + return admission.Allowed("") |
| 47 | + } |
| 48 | + |
| 49 | + cluster := &clusterv1.Cluster{} |
| 50 | + err := a.decoder.Decode(req, cluster) |
| 51 | + if err != nil { |
| 52 | + return admission.Errored(http.StatusBadRequest, err) |
| 53 | + } |
| 54 | + |
| 55 | + if cluster.Spec.Topology == nil { |
| 56 | + return admission.Allowed("") |
| 57 | + } |
| 58 | + |
| 59 | + if utils.GetProvider(cluster) != "nutanix" { |
| 60 | + return admission.Allowed("") |
| 61 | + } |
| 62 | + |
| 63 | + clusterConfig, err := variables.UnmarshalClusterConfigVariable(cluster.Spec.Topology.Variables) |
| 64 | + if err != nil { |
| 65 | + return admission.Denied( |
| 66 | + fmt.Errorf("failed to unmarshal cluster topology variable %q: %w", |
| 67 | + v1alpha1.ClusterConfigVariableName, |
| 68 | + err).Error(), |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + if clusterConfig.Nutanix != nil && |
| 73 | + clusterConfig.Addons != nil { |
| 74 | + // Check if Prism Central IP is in MetalLB Load Balancer IP range. |
| 75 | + if err := checkIfPrismCentralIPInLoadBalancerIPRange( |
| 76 | + clusterConfig.Nutanix.PrismCentralEndpoint, |
| 77 | + clusterConfig.Addons.ServiceLoadBalancer, |
| 78 | + ); err != nil { |
| 79 | + return admission.Denied(err.Error()) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return admission.Allowed("") |
| 84 | +} |
| 85 | + |
| 86 | +// checkIfPrismCentralIPInLoadBalancerIPRange checks if the Prism Central IP is in the MetalLB Load Balancer IP range. |
| 87 | +// Errors out if Prism Central IP is in the Load Balancer IP range. |
| 88 | +func checkIfPrismCentralIPInLoadBalancerIPRange( |
| 89 | + pcEndpoint v1alpha1.NutanixPrismCentralEndpointSpec, |
| 90 | + serviceLoadBalancerConfiguration *v1alpha1.ServiceLoadBalancer, |
| 91 | +) error { |
| 92 | + if serviceLoadBalancerConfiguration == nil || |
| 93 | + serviceLoadBalancerConfiguration.Provider != v1alpha1.ServiceLoadBalancerProviderMetalLB || |
| 94 | + serviceLoadBalancerConfiguration.Configuration == nil { |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + pcHostname, _, err := pcEndpoint.ParseURL() |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + pcIP := net.ParseIP(pcHostname) |
| 104 | + // PC URL can contain IP/FQDN, so compare only if PC is an IP address. |
| 105 | + if pcIP == nil { |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + for _, pool := range serviceLoadBalancerConfiguration.Configuration.AddressRanges { |
| 110 | + isIPInRange, err := helpers.IsIPInRange(pool.Start, pool.End, pcIP.String()) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf( |
| 113 | + "error while checking if Prism Central IP %q is part of MetalLB address range %q-%q: %w", |
| 114 | + pcIP, |
| 115 | + pool.Start, |
| 116 | + pool.End, |
| 117 | + err, |
| 118 | + ) |
| 119 | + } |
| 120 | + if isIPInRange { |
| 121 | + return fmt.Errorf("prism central IP %q must not be part of MetalLB address range %q-%q", |
| 122 | + pcIP, pool.Start, pool.End) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
0 commit comments