-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from ASBishop/OSPRH-8192
Move to VerifySecret when checking the ctlplane secret, and other code consolidation
- Loading branch information
Showing
7 changed files
with
299 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/openstack-k8s-operators/lib-common/modules/common/condition" | ||
"github.com/openstack-k8s-operators/lib-common/modules/common/secret" | ||
"k8s.io/apimachinery/pkg/types" | ||
"time" | ||
|
||
"github.com/openstack-k8s-operators/cinder-operator/pkg/cinder" | ||
"github.com/openstack-k8s-operators/lib-common/modules/common/env" | ||
"github.com/openstack-k8s-operators/lib-common/modules/common/helper" | ||
k8s_errors "k8s.io/apimachinery/pkg/api/errors" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
type conditionUpdater interface { | ||
Set(c *condition.Condition) | ||
MarkTrue(t condition.Type, messageFormat string, messageArgs ...interface{}) | ||
} | ||
|
||
// verifyServiceSecret - ensures that the Secret object exists and the expected | ||
// fields are in the Secret. It also sets a hash of the values of the expected | ||
// fields passed as input. | ||
func verifyServiceSecret( | ||
ctx context.Context, | ||
secretName types.NamespacedName, | ||
expectedFields []string, | ||
reader client.Reader, | ||
conditionUpdater conditionUpdater, | ||
requeueTimeout time.Duration, | ||
envVars *map[string]env.Setter, | ||
) (ctrl.Result, error) { | ||
|
||
hash, res, err := secret.VerifySecret(ctx, secretName, expectedFields, reader, requeueTimeout) | ||
if err != nil { | ||
conditionUpdater.Set(condition.FalseCondition( | ||
condition.InputReadyCondition, | ||
condition.ErrorReason, | ||
condition.SeverityWarning, | ||
condition.InputReadyErrorMessage, | ||
err.Error())) | ||
return res, err | ||
} else if (res != ctrl.Result{}) { | ||
log.FromContext(ctx).Info(fmt.Sprintf("OpenStack secret %s not found", secretName)) | ||
conditionUpdater.Set(condition.FalseCondition( | ||
condition.InputReadyCondition, | ||
condition.RequestedReason, | ||
condition.SeverityInfo, | ||
condition.InputReadyWaitingMessage)) | ||
return res, nil | ||
} | ||
(*envVars)[secretName.Name] = env.SetValue(hash) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// verifyConfigSecrets - It iterates over the secretNames passed as input and | ||
// sets the hash of values in the envVars map. | ||
func verifyConfigSecrets( | ||
ctx context.Context, | ||
h *helper.Helper, | ||
conditionUpdater conditionUpdater, | ||
secretNames []string, | ||
namespace string, | ||
envVars *map[string]env.Setter, | ||
) (ctrl.Result, error) { | ||
var hash string | ||
var err error | ||
for _, secretName := range secretNames { | ||
_, hash, err = secret.GetSecret(ctx, h, secretName, namespace) | ||
if err != nil { | ||
if k8s_errors.IsNotFound(err) { | ||
log.FromContext(ctx).Info(fmt.Sprintf("Secret %s not found", secretName)) | ||
conditionUpdater.Set(condition.FalseCondition( | ||
condition.InputReadyCondition, | ||
condition.RequestedReason, | ||
condition.SeverityInfo, | ||
condition.InputReadyWaitingMessage)) | ||
return cinder.ResultRequeue, nil | ||
} | ||
conditionUpdater.Set(condition.FalseCondition( | ||
condition.InputReadyCondition, | ||
condition.ErrorReason, | ||
condition.SeverityWarning, | ||
condition.InputReadyErrorMessage, | ||
err.Error())) | ||
return ctrl.Result{}, err | ||
} | ||
// Add a prefix to the var name to avoid accidental collision with other non-secret | ||
// vars. The secret names themselves will be unique. | ||
(*envVars)["secret-"+secretName] = env.SetValue(hash) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.