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

Share cleanup job #337

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
3 changes: 2 additions & 1 deletion api/v1beta1/manila_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
const (
// DbSyncHash hash
DbSyncHash = "dbsync"

// SvcCleanupHash hash
SvcCleanupHash = "servicecleanup"
// DeploymentHash hash used to detect changes
DeploymentHash = "deployment"
)
Expand Down
56 changes: 49 additions & 7 deletions controllers/manila_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

"github.com/go-logr/logr"
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
Expand Down Expand Up @@ -366,7 +367,15 @@ func (r *ManilaReconciler) reconcileInit(
// run manila db sync
//
dbSyncHash := instance.Status.Hash[manilav1beta1.DbSyncHash]
jobDef := manila.DbSyncJob(instance, serviceLabels, serviceAnnotations)
jobDef := manila.Job(
instance,
serviceLabels,
serviceAnnotations,
nil,
manila.DBSyncJobName,
manila.DBSyncCommand,
0, // no need to delay dbSync
)
dbSyncjob := job.NewJob(
jobDef,
manilav1beta1.DbSyncHash,
Expand Down Expand Up @@ -778,11 +787,36 @@ func (r *ManilaReconciler) reconcileNormal(ctx context.Context, instance *manila
instance.Status.Conditions.MarkTrue(condition.CronJobReadyCondition, condition.CronJobReadyMessage)
// create CronJob - end

err = r.shareCleanup(ctx, instance)
cleanJob, hash, err := r.shareCleanup(ctx, instance)
if err != nil {
return ctrl.Result{}, err
}

if cleanJob {
jobDef := manila.Job(
instance,
serviceLabels,
serviceAnnotations,
ptr.To(manila.TTL),
fmt.Sprintf("%s-%s", manila.SvcCleanupJobName, hash[:manila.TruncateHash]),
manila.SvcCleanupCommand,
manila.ManilaServiceCleanupDelay,
)
shareCleanupJob := job.NewJob(
jobDef,
manilav1beta1.SvcCleanupHash,
false,
manila.ShortDuration,
"",
)
ctrlResult, err := shareCleanupJob.DoJob(
ctx,
helper,
)
if err != nil {
return ctrlResult, err
}
}
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' successfully", instance.Name))
// update the overall status condition if service is ready
if instance.IsReady() {
Expand Down Expand Up @@ -1216,32 +1250,40 @@ func (r *ManilaReconciler) checkManilaShareGeneration(
func (r *ManilaReconciler) shareCleanup(
ctx context.Context,
instance *manilav1beta1.Manila,
) error {
) (bool, string, error) {
cleanJob := false
var deletedShares = []string{}
// Generate a list of share CRs
shares := &manilav1beta1.ManilaShareList{}

listOpts := []client.ListOption{
client.InNamespace(instance.Namespace),
}
if err := r.Client.List(ctx, shares, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve Manila Share CRs %v")
return nil
return cleanJob, "", nil
}
for _, share := range shares.Items {
// Skip shares CRs that we don't own
if manila.GetOwningManilaName(&share) != instance.Name {
continue
}

// Delete the manilaShare if it's no longer in the spec
_, exists := instance.Spec.ManilaShares[share.ShareName()]
if !exists && share.DeletionTimestamp.IsZero() {
err := r.Client.Delete(ctx, &share)
if err != nil && !k8s_errors.IsNotFound(err) {
err = fmt.Errorf("Error cleaning up %s: %w", share.Name, err)
return err
return cleanJob, "", err
}
delete(instance.Status.ManilaSharesReadyCounts, share.ShareName())
deletedShares = append(deletedShares, share.ShareName())
cleanJob = true
}
}
return nil
hash, err := manila.SharesListHash(deletedShares)
if err != nil {
return false, hash, err
}
return cleanJob, hash, nil
}
17 changes: 14 additions & 3 deletions pkg/manila/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ const (
ManilaUserID int64 = 42429
// ManilaGroupID -
ManilaGroupID int64 = 42429

// ManilaPublicPort -
ManilaPublicPort int32 = 8786
// ManilaInternalPort -
ManilaInternalPort int32 = 8786

// ManilaServiceCleanupDelay - Time in seconds that the ServiceCleanupJob
// needs to wait before executing the manila-manage command
ManilaServiceCleanupDelay int32 = 120
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gouthampacha we delay according to this parameter.

// ManilaExtraVolTypeUndefined can be used to label an extraMount which
// is not associated with a specific backend
ManilaExtraVolTypeUndefined storage.ExtraVolType = "Undefined"
Expand Down Expand Up @@ -79,8 +80,18 @@ const (
ShortDuration = time.Duration(5) * time.Second
// NormalDuration -
NormalDuration = time.Duration(10) * time.Second
//DBSyncCommand -
// DBSyncJobName -
DBSyncJobName = "db-sync"
// DBSyncCommand -
DBSyncCommand = "/usr/bin/manila-manage --config-dir /etc/manila/manila.conf.d db sync"
// SvcCleanupJobName -
SvcCleanupJobName = "service-cleanup"
// SvcCleanupCommand -
SvcCleanupCommand = "/usr/bin/manila-manage --config-dir /etc/manila/manila.conf.d service cleanup"
fmount marked this conversation as resolved.
Show resolved Hide resolved
// TruncateHash -
TruncateHash int = 8
// TTL -
TTL int32 = 5 * 60 // 5 minutes
)

// DbsyncPropagation keeps track of the DBSync Service Propagation Type
Expand Down
11 changes: 11 additions & 0 deletions pkg/manila/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manila
import (
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -55,3 +56,13 @@ func GetPodAffinity(componentName string) *corev1.Affinity {
corev1.LabelHostname,
)
}

// SharesListHash - Given a list of share names passed as an array of strings,
// it returns an hash that is currently used to build the job name
func SharesListHash(shareNames []string) (string, error) {
hash, err := util.ObjectHash(shareNames)
if err != nil {
return hash, err
}
return hash, err
}
52 changes: 32 additions & 20 deletions pkg/manila/dbsync.go → pkg/manila/job.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package manila

import (
"fmt"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
manilav1 "github.com/openstack-k8s-operators/manila-operator/api/v1beta1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// DbSyncJob func
func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations map[string]string) *batchv1.Job {
// Job func
func Job(
instance *manilav1.Manila,
labels map[string]string,
annotations map[string]string,
ttl *int32,
jobName string,
jobCommand string,
delay int32,
) *batchv1.Job {
var config0644AccessMode int32 = 0644

// Unlike the individual manila services, the DbSyncJob doesn't need a
// secret that contains all of the config snippets required by every
// service, The two snippet files that it does need (DefaultsConfigFileName
// and CustomConfigFileName) can be extracted from the top-level manila
// config-data secret.
dbSyncVolume := []corev1.Volume{
// Unlike the individual manila services, DbSyncJob or a Job executing a
// manila-manage command doesn't need a secret that contains all of the
// config snippets required by every service, The two snippet files that it
// does need (DefaultsConfigFileName and CustomConfigFileName) can be
// extracted from the top-level manila config-data secret.
manilaJobVolume := []corev1.Volume{
{
Name: "db-sync-config-data",
Name: "job-config-data",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
DefaultMode: &config0644AccessMode,
Expand Down Expand Up @@ -48,9 +56,9 @@ func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations
},
}

dbSyncMounts := []corev1.VolumeMount{
manilaJobMounts := []corev1.VolumeMount{
{
Name: "db-sync-config-data",
Name: "job-config-data",
MountPath: "/etc/manila/manila.conf.d",
ReadOnly: true,
},
Expand All @@ -62,12 +70,13 @@ func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations
},
}

args := []string{"-c", DBSyncCommand}
delayCommand := fmt.Sprintf("sleep %d", delay)
args := []string{"-c", fmt.Sprintf("%s && %s", delayCommand, jobCommand)}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gouthampacha the resulting command is the concatenation between the delay and the jobCommand.


// add CA cert if defined
if instance.Spec.ManilaAPI.TLS.CaBundleSecretName != "" {
dbSyncVolume = append(dbSyncVolume, instance.Spec.ManilaAPI.TLS.CreateVolume())
dbSyncMounts = append(dbSyncMounts, instance.Spec.ManilaAPI.TLS.CreateVolumeMounts(nil)...)
manilaJobVolume = append(manilaJobVolume, instance.Spec.ManilaAPI.TLS.CreateVolume())
manilaJobMounts = append(manilaJobMounts, instance.Spec.ManilaAPI.TLS.CreateVolumeMounts(nil)...)
}

envVars := map[string]env.Setter{}
Expand All @@ -76,7 +85,7 @@ func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name + "-db-sync",
Name: fmt.Sprintf("%s-%s", instance.Name, jobName),
Namespace: instance.Namespace,
Labels: labels,
},
Expand All @@ -90,22 +99,25 @@ func DbSyncJob(instance *manilav1.Manila, labels map[string]string, annotations
ServiceAccountName: instance.RbacResourceName(),
Containers: []corev1.Container{
{
Name: instance.Name + "-db-sync",
Name: fmt.Sprintf("%s-%s", instance.Name, jobName),
Command: []string{
"/bin/bash",
},
Args: args,
Image: instance.Spec.ManilaAPI.ContainerImage,
SecurityContext: manilaDefaultSecurityContext(),
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
VolumeMounts: dbSyncMounts,
VolumeMounts: manilaJobMounts,
},
},
Volumes: dbSyncVolume,
Volumes: manilaJobVolume,
},
},
},
}

if ttl != nil {
// Setting TTL to delete the job after it has completed
job.Spec.TTLSecondsAfterFinished = ttl
}
return job
}
Loading