-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
284 additions
and
0 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
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
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,93 @@ | ||
package novaconductor | ||
|
||
import ( | ||
"fmt" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/ptr" | ||
|
||
"github.com/openstack-k8s-operators/lib-common/modules/common/env" | ||
novav1 "github.com/openstack-k8s-operators/nova-operator/api/v1beta1" | ||
"github.com/openstack-k8s-operators/nova-operator/pkg/nova" | ||
) | ||
|
||
func DBPurgeCronJob( | ||
instance *novav1.NovaConductor, | ||
labels map[string]string, | ||
annotations map[string]string, | ||
) *batchv1.CronJob { | ||
args := []string{"-c", nova.KollaServiceCommand} | ||
|
||
envVars := map[string]env.Setter{} | ||
envVars["KOLLA_CONFIG_STRATEGY"] = env.SetValue("COPY_ALWAYS") | ||
envVars["KOLLA_BOOTSTRAP"] = env.SetValue("true") | ||
|
||
envVars["ARCHIVE_AGE"] = env.SetValue(fmt.Sprintf("%d", *instance.Spec.DBPurge.ArchiveAge)) | ||
envVars["PURGE_AGE"] = env.SetValue(fmt.Sprintf("%d", *instance.Spec.DBPurge.PurgeAge)) | ||
|
||
env := env.MergeEnvs([]corev1.EnvVar{}, envVars) | ||
|
||
volumes := []corev1.Volume{ | ||
nova.GetConfigVolume(nova.GetServiceConfigSecretName(instance.Name)), | ||
nova.GetScriptVolume(nova.GetScriptSecretName(instance.Name)), | ||
} | ||
volumeMounts := []corev1.VolumeMount{ | ||
nova.GetConfigVolumeMount(), | ||
nova.GetScriptVolumeMount(), | ||
nova.GetKollaConfigVolumeMount("nova-conductor-dbpurge"), | ||
} | ||
|
||
// add CA cert if defined | ||
if instance.Spec.TLS.CaBundleSecretName != "" { | ||
volumes = append(volumes, instance.Spec.TLS.CreateVolume()) | ||
volumeMounts = append(volumeMounts, instance.Spec.TLS.CreateVolumeMounts(nil)...) | ||
} | ||
|
||
cron := &batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: instance.Name + "-db-purge", | ||
Namespace: instance.Namespace, | ||
Labels: labels, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: *instance.Spec.DBPurge.Schedule, | ||
ConcurrencyPolicy: batchv1.ForbidConcurrent, | ||
JobTemplate: batchv1.JobTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: labels, | ||
Annotations: annotations, | ||
}, | ||
Spec: batchv1.JobSpec{ | ||
Parallelism: ptr.To[int32](1), | ||
Completions: ptr.To[int32](1), | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
RestartPolicy: corev1.RestartPolicyOnFailure, | ||
ServiceAccountName: instance.Spec.ServiceAccount, | ||
Volumes: volumes, | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "nova-manage", | ||
Command: []string{ | ||
"/bin/bash", | ||
}, | ||
Args: args, | ||
Image: instance.Spec.ContainerImage, | ||
SecurityContext: &corev1.SecurityContext{ | ||
RunAsUser: ptr.To(nova.NovaUserID), | ||
}, | ||
Env: env, | ||
VolumeMounts: volumeMounts, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return cron | ||
} |
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,38 @@ | ||
#!/bin/bash | ||
# 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. | ||
|
||
set -x | ||
export ARCHIVE_AGE=${ARCHIVE_AGE:?"Please specify ARCHIVE_AGE variable."} | ||
export PURGE_AGE=${PURGE_AGE:?"Please specify PURGE_AGE variable."} | ||
archive_before=$(date --date="${ARCHIVE_AGE} day ago" +%Y-%m-%d) | ||
purge_before=$(date --date="${PURGE_AGE} day ago" +%Y-%m-%d) | ||
|
||
nova-manage db archive_deleted_rows --verbose --until-complete --task-log --before "${archive_before}" | ||
ret=$? | ||
# 0 means no error and nothing is archived | ||
# 1 means no error and someting is archived | ||
if [ $ret -gt "1" ]; then | ||
exit $ret | ||
fi | ||
|
||
nova-manage db purge --verbose --before "${purge_before}" | ||
ret=$? | ||
# 0 means no error and something is deleted | ||
# 3 means no error and nothing is deleted | ||
if [[ $ret -eq 1 || $ret -eq 2 || $ret -gt 3 ]]; then | ||
exit $ret | ||
fi | ||
|
||
exit 0 |
37 changes: 37 additions & 0 deletions
37
templates/novaconductor/config/nova-conductor-dbpurge-config.json
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,37 @@ | ||
{ | ||
"command": "/bin/dbpurge.sh", | ||
"config_files": [ | ||
{ | ||
"source": "/var/lib/openstack/config/nova-blank.conf", | ||
"dest": "/etc/nova/nova.conf", | ||
"owner": "nova", | ||
"perm": "0600" | ||
}, | ||
{ | ||
"source": "/var/lib/openstack/config/01-nova.conf", | ||
"dest": "/etc/nova/nova.conf.d/01-nova.conf", | ||
"owner": "nova", | ||
"perm": "0600" | ||
}, | ||
{ | ||
"source": "/var/lib/openstack/config/02-nova-override.conf", | ||
"dest": "/etc/nova/nova.conf.d/02-nova-override.conf", | ||
"owner": "nova", | ||
"perm": "0600", | ||
"optional": true | ||
}, | ||
{ | ||
"source": "/var/lib/openstack/bin/dbpurge.sh", | ||
"dest": "/bin/", | ||
"owner": "nova", | ||
"perm": "0700" | ||
} | ||
], | ||
"permissions": [ | ||
{ | ||
"path": "/var/log/nova", | ||
"owner": "nova:nova", | ||
"recurse": true | ||
} | ||
] | ||
} |
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
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
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