Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to VerifySecret when checking the ctlplane secret, and other code consolidation #437

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions controllers/cinder_common.go
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
}
49 changes: 23 additions & 26 deletions controllers/cinder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -396,7 +397,7 @@ func (r *CinderReconciler) reconcileInit(
jobDef,
cinderv1beta1.DbSyncHash,
instance.Spec.PreserveJobs,
time.Duration(5)*time.Second,
cinder.ShortDuration,
dbSyncHash,
)
ctrlResult, err := dbSyncjob.DoJob(
Expand Down Expand Up @@ -492,7 +493,7 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
condition.RequestedReason,
condition.SeverityInfo,
condition.RabbitMqTransportURLReadyRunningMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
return cinder.ResultRequeue, nil
}

instance.Status.Conditions.MarkTrue(condition.RabbitMqTransportURLReadyCondition, condition.RabbitMqTransportURLReadyMessage)
Expand All @@ -504,14 +505,15 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
//
memcached, err := memcachedv1.GetMemcachedByName(ctx, helper, instance.Spec.MemcachedInstance, instance.Namespace)
if err != nil {
Log.Info(fmt.Sprintf("%s... requeueing", condition.MemcachedReadyWaitingMessage))
if k8s_errors.IsNotFound(err) {
Log.Info(fmt.Sprintf("memcached %s not found", instance.Spec.MemcachedInstance))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.MemcachedReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.MemcachedReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
return cinder.ResultRequeue, nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.MemcachedReadyCondition,
Expand All @@ -523,13 +525,13 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
}

if !memcached.IsReady() {
Log.Info(fmt.Sprintf("memcached %s is not ready", memcached.Name))
Log.Info(fmt.Sprintf("%s... requeueing", condition.MemcachedReadyWaitingMessage))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.MemcachedReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.MemcachedReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
return cinder.ResultRequeue, nil
}
// Mark the Memcached Service as Ready if we get to this point with no errors
instance.Status.Conditions.MarkTrue(
Expand All @@ -539,28 +541,23 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
//
// check for required OpenStack secret holding passwords for service/admin user and add hash to the vars map
//
ospSecret, hash, err := secret.GetSecret(ctx, helper, instance.Spec.Secret, instance.Namespace)

result, err := verifyServiceSecret(
ctx,
types.NamespacedName{Namespace: instance.Namespace, Name: instance.Spec.Secret},
[]string{
instance.Spec.PasswordSelectors.Service,
},
helper.GetClient(),
&instance.Status.Conditions,
cinder.NormalDuration,
&configVars,
)
if err != nil {
if k8s_errors.IsNotFound(err) {
Log.Info(fmt.Sprintf("OpenStack secret %s not found", instance.Spec.Secret))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.InputReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
return result, err
} else if (result != ctrl.Result{}) {
return result, nil
}
// Add a prefix to the var name to avoid accidental collision with other non-secret vars.
configVars["secret-"+ospSecret.Name] = env.SetValue(hash)

instance.Status.Conditions.MarkTrue(condition.InputReadyCondition, condition.InputReadyMessage)
// run check OpenStack secret - end

Expand Down Expand Up @@ -629,7 +626,7 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
return cinder.ResultRequeue, fmt.Errorf(condition.NetworkAttachmentsReadyWaitingMessage, netAtt)
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
Expand Down
Loading
Loading