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

Add ensureNAD common function #340

Merged
merged 1 commit into from
Oct 1, 2024
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
4 changes: 2 additions & 2 deletions api/v1beta1/manila_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ func (spec *ManilaSpecCore) SetDefaultRouteAnnotations(annotations map[string]st
valHAProxy, okHAProxy := annotations[haProxyAnno]

// Human operator set the HAProxy timeout manually
if (!okManila && okHAProxy) {
if !okManila && okHAProxy {
return
}

// Human operator modified the HAProxy timeout manually without removing the Manila flag
if (okManila && okHAProxy && valManila != valHAProxy) {
if okManila && okHAProxy && valManila != valHAProxy {
delete(annotations, manilaAnno)
return
}
Expand Down
51 changes: 51 additions & 0 deletions controllers/manila_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
nad "github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment"
"github.com/openstack-k8s-operators/manila-operator/pkg/manila"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -38,6 +39,56 @@ type conditionUpdater interface {
MarkTrue(t condition.Type, messageFormat string, messageArgs ...interface{})
}

// ensureNAD - common function called in the Manila controllers that GetNAD based
// on the string[] passed as input and produces the required Annotation for each
// Manila component
func ensureNAD(
ctx context.Context,
conditionUpdater conditionUpdater,
nAttach []string,
helper *helper.Helper,
) (map[string]string, ctrl.Result, error) {

var serviceAnnotations map[string]string
var err error
// Iterate over the []networkattachment, get the corresponding NAD and create
// the required annotation
for _, netAtt := range nAttach {
_, err = nad.GetNADWithName(ctx, helper, netAtt, helper.GetBeforeObject().GetNamespace())
if err != nil {
if k8s_errors.IsNotFound(err) {
log.FromContext(ctx).Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
conditionUpdater.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return serviceAnnotations, manila.ResultRequeue, nil
}
conditionUpdater.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return serviceAnnotations, manila.ResultRequeue, nil
}
}
// Create NetworkAnnotations
serviceAnnotations, err = nad.CreateNetworksAnnotation(helper.GetBeforeObject().GetNamespace(), nAttach)
if err != nil {
err := fmt.Errorf("failed create network annotation from %s: %w", nAttach, err)
conditionUpdater.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
}
return serviceAnnotations, ctrl.Result{}, err
}

// 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.
Expand Down
54 changes: 8 additions & 46 deletions controllers/manila_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/job"
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
nad "github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment"
common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
Expand Down Expand Up @@ -511,7 +510,7 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
// check for required OpenStack secret holding passwords for service/admin user and add hash to the vars map
//

result, err := verifyServiceSecret(
ctrlResult, err := verifyServiceSecret(
ctx,
types.NamespacedName{Namespace: instance.Namespace, Name: instance.Spec.Secret},
[]string{
Expand All @@ -522,10 +521,8 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
manila.NormalDuration,
&configVars,
)
if err != nil {
return result, err
} else if (result != ctrl.Result{}) {
return result, nil
if (err != nil || ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
instance.Status.Conditions.MarkTrue(condition.InputReadyCondition, condition.InputReadyMessage)
// run check OpenStack secret - end
Expand Down Expand Up @@ -592,51 +589,16 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila

instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)

//
// TODO check when/if Init, Update, or Upgrade should/could be skipped
//

// Check networks that the DBSync job will use in reconcileInit. The ones from the API service are always enough,
// it doesn't need the storage specific ones that manila-share may have.
for _, netAtt := range instance.Spec.ManilaAPI.NetworkAttachments {
_, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
r.Log.Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return manila.ResultRequeue, nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
}

serviceAnnotations, err := nad.CreateNetworksAnnotation(instance.Namespace, instance.Spec.ManilaAPI.NetworkAttachments)
var serviceAnnotations map[string]string
// Ensure NAD annotations are generated
serviceAnnotations, ctrlResult, err = ensureNAD(ctx, &instance.Status.Conditions, instance.Spec.ManilaAPI.NetworkAttachments, helper)
if err != nil {
err := fmt.Errorf("failed create network annotation from %s: %w",
instance.Spec.ManilaAPI.NetworkAttachments, err)
instance.Status.Conditions.MarkFalse(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error())
return ctrl.Result{}, err
return ctrlResult, err
}
instance.Status.Conditions.MarkTrue(condition.NetworkAttachmentsReadyCondition, condition.NetworkAttachmentsReadyMessage)

// Handle service init
ctrlResult, err := r.reconcileInit(ctx, instance, helper, serviceLabels, serviceAnnotations)
ctrlResult, err = r.reconcileInit(ctx, instance, helper, serviceLabels, serviceAnnotations)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
Expand Down
41 changes: 4 additions & 37 deletions controllers/manilaapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,44 +786,11 @@ func (r *ManilaAPIReconciler) reconcileNormal(ctx context.Context, instance *man
}
instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)

//
// TODO check when/if Init, Update, or Upgrade should/could be skipped
//

// networks to attach to
for _, netAtt := range instance.Spec.NetworkAttachments {
_, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
r.Log.Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return manila.ResultRequeue, nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
}

serviceAnnotations, err := nad.CreateNetworksAnnotation(instance.Namespace, instance.Spec.NetworkAttachments)
var serviceAnnotations map[string]string
// Ensure NAD annotations are generated
serviceAnnotations, ctrlResult, err = ensureNAD(ctx, &instance.Status.Conditions, instance.Spec.NetworkAttachments, helper)
if err != nil {
err := fmt.Errorf("failed create network annotation from %s: %w", instance.Spec.NetworkAttachments, err)
instance.Status.Conditions.MarkFalse(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err)
return ctrl.Result{}, err
return ctrlResult, err
}

// Handle service init
Expand Down
45 changes: 5 additions & 40 deletions controllers/manilascheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,8 @@ func (r *ManilaSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
manila.NormalDuration,
&configVars,
)
if err != nil {
if (err != nil || ctrlResult != ctrl.Result{}) {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}

//
Expand Down Expand Up @@ -453,44 +451,11 @@ func (r *ManilaSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
}
instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)

//
// TODO check when/if Init, Update, or Upgrade should/could be skipped
//

// networks to attach to
for _, netAtt := range instance.Spec.NetworkAttachments {
_, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
r.Log.Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return manila.ResultRequeue, nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
}

serviceAnnotations, err := nad.CreateNetworksAnnotation(instance.Namespace, instance.Spec.NetworkAttachments)
var serviceAnnotations map[string]string
// Ensure NAD annotations are generated
serviceAnnotations, ctrlResult, err = ensureNAD(ctx, &instance.Status.Conditions, instance.Spec.NetworkAttachments, helper)
if err != nil {
err := fmt.Errorf("failed create network annotation from %s: %w", instance.Spec.NetworkAttachments, err)
instance.Status.Conditions.MarkFalse(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err)
return ctrl.Result{}, err
return ctrlResult, err
}

//
Expand Down
45 changes: 5 additions & 40 deletions controllers/manilashare_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,8 @@ func (r *ManilaShareReconciler) reconcileNormal(ctx context.Context, instance *m
manila.NormalDuration,
&configVars,
)
if err != nil {
if (err != nil || ctrlResult != ctrl.Result{}) {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}

//
Expand Down Expand Up @@ -450,44 +448,11 @@ func (r *ManilaShareReconciler) reconcileNormal(ctx context.Context, instance *m
}
instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)

//
// TODO check when/if Init, Update, or Upgrade should/could be skipped
//

// networks to attach to
for _, netAtt := range instance.Spec.NetworkAttachments {
_, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
r.Log.Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return manila.ResultRequeue, nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
}

serviceAnnotations, err := nad.CreateNetworksAnnotation(instance.Namespace, instance.Spec.NetworkAttachments)
var serviceAnnotations map[string]string
// Ensure NAD annotations are generated
serviceAnnotations, ctrlResult, err = ensureNAD(ctx, &instance.Status.Conditions, instance.Spec.NetworkAttachments, helper)
if err != nil {
err := fmt.Errorf("failed create network annotation from %s: %w", instance.Spec.NetworkAttachments, err)
instance.Status.Conditions.MarkFalse(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err)
return ctrl.Result{}, err
return ctrlResult, err
}

//
Expand Down
Loading