|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 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 common //nolint:revive // common is the established package name for multi-group shared code |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 25 | + util "github.com/openstack-k8s-operators/lib-common/modules/common/util" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | + ctrl "sigs.k8s.io/controller-runtime" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | +) |
| 33 | + |
| 34 | +// EnsureSecret ensures that the Secret object exists and the expected fields |
| 35 | +// are in the Secret. It returns a hash of the values of the expected fields. |
| 36 | +func EnsureSecret( |
| 37 | + ctx context.Context, |
| 38 | + secretName types.NamespacedName, |
| 39 | + expectedFields []string, |
| 40 | + reader client.Reader, |
| 41 | + conditionUpdater ConditionUpdater, |
| 42 | + requeueTimeout time.Duration, |
| 43 | +) (string, ctrl.Result, corev1.Secret, error) { |
| 44 | + secret := &corev1.Secret{} |
| 45 | + err := reader.Get(ctx, secretName, secret) |
| 46 | + if err != nil { |
| 47 | + if k8s_errors.IsNotFound(err) { |
| 48 | + // Since the service secret should have been manually created by the user and referenced in the spec, |
| 49 | + // we treat this as a warning because it means that the service will not be able to start. |
| 50 | + log.FromContext(ctx).Info(fmt.Sprintf("secret %s not found", secretName)) |
| 51 | + conditionUpdater.Set(condition.FalseCondition( |
| 52 | + condition.InputReadyCondition, |
| 53 | + condition.ErrorReason, |
| 54 | + condition.SeverityWarning, |
| 55 | + "Input data resources missing: %s", "secret/"+secretName.Name)) |
| 56 | + return "", |
| 57 | + ctrl.Result{RequeueAfter: requeueTimeout}, |
| 58 | + *secret, |
| 59 | + nil |
| 60 | + } |
| 61 | + conditionUpdater.Set(condition.FalseCondition( |
| 62 | + condition.InputReadyCondition, |
| 63 | + condition.ErrorReason, |
| 64 | + condition.SeverityWarning, |
| 65 | + condition.InputReadyErrorMessage, |
| 66 | + err.Error())) |
| 67 | + return "", ctrl.Result{}, *secret, err |
| 68 | + } |
| 69 | + |
| 70 | + // collect the secret values the caller expects to exist |
| 71 | + values := [][]byte{} |
| 72 | + for _, field := range expectedFields { |
| 73 | + val, ok := secret.Data[field] |
| 74 | + if !ok { |
| 75 | + err := fmt.Errorf("%w: '%s' not found in secret/%s", util.ErrFieldNotFound, field, secretName.Name) |
| 76 | + conditionUpdater.Set(condition.FalseCondition( |
| 77 | + condition.InputReadyCondition, |
| 78 | + condition.ErrorReason, |
| 79 | + condition.SeverityWarning, |
| 80 | + condition.InputReadyErrorMessage, |
| 81 | + err.Error())) |
| 82 | + return "", ctrl.Result{}, *secret, err |
| 83 | + } |
| 84 | + values = append(values, val) |
| 85 | + } |
| 86 | + |
| 87 | + // TODO(gibi): Do we need to watch the Secret for changes? |
| 88 | + |
| 89 | + hash, err := util.ObjectHash(values) |
| 90 | + if err != nil { |
| 91 | + conditionUpdater.Set(condition.FalseCondition( |
| 92 | + condition.InputReadyCondition, |
| 93 | + condition.ErrorReason, |
| 94 | + condition.SeverityWarning, |
| 95 | + condition.InputReadyErrorMessage, |
| 96 | + err.Error())) |
| 97 | + return "", ctrl.Result{}, *secret, err |
| 98 | + } |
| 99 | + |
| 100 | + return hash, ctrl.Result{}, *secret, nil |
| 101 | +} |
0 commit comments