|
| 1 | +/* |
| 2 | +Copyright 2024 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 openstack |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "k8s.io/cloud-provider-openstack/pkg/util" |
| 22 | + "regexp" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/gophercloud/gophercloud" |
| 26 | + |
| 27 | + "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners" |
| 28 | + "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers" |
| 29 | + "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors" |
| 30 | + "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools" |
| 31 | + openstackutil "k8s.io/cloud-provider-openstack/pkg/util/openstack" |
| 32 | +) |
| 33 | + |
| 34 | +// lbHasOldClusterName checks if the OCCM LB prefix is present and if so, validates the cluster-name |
| 35 | +// component value. Returns true if the cluster-name component of the loadbalancer's name doesn't match |
| 36 | +// clusterName. |
| 37 | +func lbHasOldClusterName(loadbalancer *loadbalancers.LoadBalancer, clusterName string) bool { |
| 38 | + if !strings.HasPrefix(loadbalancer.Name, servicePrefix) { |
| 39 | + // This one was probably not created by OCCM, let's leave it as is. |
| 40 | + return false |
| 41 | + } |
| 42 | + existingClusterName := getClusterName("", loadbalancer.Name) |
| 43 | + |
| 44 | + return existingClusterName != clusterName |
| 45 | +} |
| 46 | + |
| 47 | +// decomposeLBName returns clusterName based on object name |
| 48 | +func getClusterName(resourcePrefix, objectName string) string { |
| 49 | + // This is not 100% bulletproof when string was cut because full name would exceed 255 characters, but honestly |
| 50 | + // it's highly unlikely, because it would mean cluster name, namespace and name would together need to exceed 200 |
| 51 | + // characters. As a precaution the _<name> part is treated as optional in the regexp, assuming the longest trim |
| 52 | + // that can happen will reach namespace, but never the clusterName. This fails if there's _ in clusterName, but |
| 53 | + // we can't do nothing about it. |
| 54 | + lbNameRegex := regexp.MustCompile(fmt.Sprintf("%s%s(.+?)_[^_]+(_[^_]+)?$", resourcePrefix, servicePrefix)) // this is static |
| 55 | + |
| 56 | + matches := lbNameRegex.FindAllStringSubmatch(objectName, -1) |
| 57 | + if matches == nil { |
| 58 | + return "" |
| 59 | + } |
| 60 | + return matches[0][1] |
| 61 | +} |
| 62 | + |
| 63 | +// replaceClusterName tries to cut servicePrefix, replaces clusterName and puts back the prefix if it was there |
| 64 | +func replaceClusterName(oldClusterName, clusterName, objectName string) string { |
| 65 | + // Remove the prefix first |
| 66 | + var found bool |
| 67 | + objectName, found = strings.CutPrefix(objectName, servicePrefix) |
| 68 | + objectName = strings.Replace(objectName, oldClusterName, clusterName, 1) |
| 69 | + if found { |
| 70 | + // This should always happen because we check that in lbHasOldClusterName, but just for safety. |
| 71 | + objectName = servicePrefix + objectName |
| 72 | + } |
| 73 | + // We need to cut the name or tag to 255 characters, just as regular LB creation does. |
| 74 | + return util.CutString255(objectName) |
| 75 | +} |
| 76 | + |
| 77 | +// renameLoadBalancer renames all the children and then the LB itself to match new lbName. |
| 78 | +// The purpose is handling a change of clusterName. |
| 79 | +func renameLoadBalancer(client *gophercloud.ServiceClient, loadbalancer *loadbalancers.LoadBalancer, lbName, clusterName string) (*loadbalancers.LoadBalancer, error) { |
| 80 | + lbListeners, err := openstackutil.GetListenersByLoadBalancerID(client, loadbalancer.ID) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + for _, listener := range lbListeners { |
| 85 | + if !strings.HasPrefix(listener.Name, listenerPrefix) { |
| 86 | + // It doesn't seem to be ours, let's not touch it. |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + oldClusterName := getClusterName(fmt.Sprintf("%s[0-9]+_", listenerPrefix), listener.Name) |
| 91 | + |
| 92 | + if oldClusterName != clusterName { |
| 93 | + // First let's handle pool which we assume is a child of the listener. Only one pool per one listener. |
| 94 | + lbPool, err := openstackutil.GetPoolByListener(client, loadbalancer.ID, listener.ID) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + oldClusterName = getClusterName(fmt.Sprintf("%s[0-9]+_", poolPrefix), lbPool.Name) |
| 99 | + if oldClusterName != clusterName { |
| 100 | + if lbPool.MonitorID != "" { |
| 101 | + // If monitor exists, let's handle it first, as we treat it as child of the pool. |
| 102 | + monitor, err := openstackutil.GetHealthMonitor(client, lbPool.MonitorID) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + oldClusterName := getClusterName(fmt.Sprintf("%s[0-9]+_", monitorPrefix), monitor.Name) |
| 107 | + if oldClusterName != clusterName { |
| 108 | + monitor.Name = replaceClusterName(oldClusterName, clusterName, monitor.Name) |
| 109 | + err = openstackutil.UpdateHealthMonitor(client, monitor.ID, monitors.UpdateOpts{Name: &monitor.Name}, loadbalancer.ID) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Monitor is handled, let's rename the pool. |
| 117 | + lbPool.Name = replaceClusterName(oldClusterName, clusterName, lbPool.Name) |
| 118 | + err = openstackutil.UpdatePool(client, loadbalancer.ID, lbPool.ID, pools.UpdateOpts{Name: &lbPool.Name}) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for i, tag := range listener.Tags { |
| 125 | + // There might be tags for shared listeners, that's why we analyze each tag on its own. |
| 126 | + oldClusterNameTag := getClusterName("", tag) |
| 127 | + if oldClusterNameTag != "" && oldClusterNameTag != clusterName { |
| 128 | + listener.Tags[i] = replaceClusterName(oldClusterNameTag, clusterName, tag) |
| 129 | + } |
| 130 | + } |
| 131 | + listener.Name = replaceClusterName(oldClusterName, clusterName, listener.Name) |
| 132 | + err = openstackutil.UpdateListener(client, loadbalancer.ID, listener.ID, listeners.UpdateOpts{Name: &listener.Name, Tags: &listener.Tags}) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // At last we rename the LB. This is to make sure we only stop retrying to rename the LB once all of the children |
| 140 | + // are handled. |
| 141 | + for i, tag := range loadbalancer.Tags { |
| 142 | + // There might be tags for shared lbs, that's why we analyze each tag on its own. |
| 143 | + oldClusterNameTag := getClusterName("", tag) |
| 144 | + if oldClusterNameTag != "" && oldClusterNameTag != clusterName { |
| 145 | + loadbalancer.Tags[i] = replaceClusterName(oldClusterNameTag, clusterName, tag) |
| 146 | + } |
| 147 | + } |
| 148 | + return openstackutil.UpdateLoadBalancer(client, loadbalancer.ID, loadbalancers.UpdateOpts{Name: &lbName, Tags: &loadbalancer.Tags}) |
| 149 | +} |
0 commit comments