|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package v1beta1 |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/openstack-k8s-operators/lib-common/modules/common/helper" |
| 23 | + "github.com/openstack-k8s-operators/lib-common/modules/common/object" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 28 | +) |
| 29 | + |
| 30 | +// ManageTransportSecretFinalizer ensures consumerFinalizer is present on the |
| 31 | +// transport secret identified by secretName. It never removes the finalizer |
| 32 | +// from a previous secret — that is the consumer's responsibility after it has |
| 33 | +// confirmed its deployment is running with the new credentials (typically via |
| 34 | +// RemoveTransportSecretConsumerFinalizer). This ensures the TransportURL |
| 35 | +// controller waits for all consumers before releasing the old RabbitMQ user. |
| 36 | +func ManageTransportSecretFinalizer( |
| 37 | + ctx context.Context, |
| 38 | + h *helper.Helper, |
| 39 | + namespace string, |
| 40 | + secretName string, |
| 41 | + consumerFinalizer string, |
| 42 | +) error { |
| 43 | + if secretName == "" { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + secret := &corev1.Secret{} |
| 48 | + key := types.NamespacedName{Name: secretName, Namespace: namespace} |
| 49 | + if err := h.GetClient().Get(ctx, key, secret); err != nil { |
| 50 | + return fmt.Errorf("failed to get transport secret %s: %w", secretName, err) |
| 51 | + } |
| 52 | + |
| 53 | + return object.AddConsumerFinalizer(ctx, h, secret, consumerFinalizer) |
| 54 | +} |
| 55 | + |
| 56 | +// RemoveTransportSecretConsumerFinalizer removes consumerFinalizer from the |
| 57 | +// transport secret identified by secretName. It is a no-op when secretName |
| 58 | +// is empty or the secret no longer exists. |
| 59 | +func RemoveTransportSecretConsumerFinalizer( |
| 60 | + ctx context.Context, |
| 61 | + h *helper.Helper, |
| 62 | + namespace string, |
| 63 | + secretName string, |
| 64 | + consumerFinalizer string, |
| 65 | +) error { |
| 66 | + if secretName == "" { |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + secret := &corev1.Secret{} |
| 71 | + key := types.NamespacedName{Name: secretName, Namespace: namespace} |
| 72 | + if err := h.GetClient().Get(ctx, key, secret); err != nil { |
| 73 | + if k8s_errors.IsNotFound(err) { |
| 74 | + return nil |
| 75 | + } |
| 76 | + return err |
| 77 | + } |
| 78 | + return object.RemoveConsumerFinalizer(ctx, h, secret, consumerFinalizer) |
| 79 | +} |
| 80 | + |
| 81 | +// HasTransportConsumerFinalizer returns true if the secret has any finalizer |
| 82 | +// matching the transport consumer pattern (openstack.org/*-transport-consumer). |
| 83 | +func HasTransportConsumerFinalizer(secret *corev1.Secret) bool { |
| 84 | + for _, f := range secret.Finalizers { |
| 85 | + if strings.HasSuffix(f, TransportSecretConsumerSuffix) && |
| 86 | + strings.HasPrefix(f, "openstack.org/") { |
| 87 | + return true |
| 88 | + } |
| 89 | + } |
| 90 | + return false |
| 91 | +} |
| 92 | + |
| 93 | +// HasSpecificTransportConsumerFinalizer returns true if the secret has the |
| 94 | +// given consumer finalizer. |
| 95 | +func HasSpecificTransportConsumerFinalizer(secret *corev1.Secret, consumerFinalizer string) bool { |
| 96 | + return controllerutil.ContainsFinalizer(secret, consumerFinalizer) |
| 97 | +} |
0 commit comments