Skip to content

Commit

Permalink
Expose DBPurge Age and Schedule
Browse files Browse the repository at this point in the history
This patch updates the current manila CR to expose both dbPurgeAge
and dbPurgeSchedule as customizable parameters. They were previously
hardcoded as constants in the code, and it makes sense removing that
part as long as the human operator has the ability to customize them.

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Nov 11, 2023
1 parent b6df633 commit b7e20ad
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 37 deletions.
9 changes: 9 additions & 0 deletions api/bases/manila.openstack.org_manilas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ spec:
databaseUser:
default: manila
type: string
dbPurge:
properties:
age:
default: 30
type: integer
schedule:
default: 1 0 * * *
type: string
type: object
debug:
properties:
dbPurge:
Expand Down
31 changes: 5 additions & 26 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1beta1

import (
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -30,6 +29,11 @@ const (
ManilaSchedulerContainerImage = "quay.io/podified-antelope-centos9/openstack-manila-scheduler:current-podified"
// ManilaShareContainerImage is the fall-back container image for ManilaShare
ManilaShareContainerImage = "quay.io/podified-antelope-centos9/openstack-manila-share:current-podified"
//DBPurgeDefaultAge indicates the number of days of purging DB records
DBPurgeDefaultAge = 30
//DBPurgeDefaultSchedule is in crontab format, and the default runs the job once every day
DBPurgeDefaultSchedule = "1 0 * * *"

)

// ManilaTemplate defines common input parameters used by all Manila services
Expand Down Expand Up @@ -114,19 +118,6 @@ type PasswordSelector struct {
Service string `json:"service,omitempty"`
}

// ManilaDebug indicates whether certain stages of Manila deployment should
// pause in debug mode
type ManilaDebug struct {
// +kubebuilder:validation:Optional
// +kubebuilder:default=false
// dbSync enable debug
DBSync bool `json:"dbSync,omitempty"`
// +kubebuilder:validation:Optional
// +kubebuilder:default=false
// dbPurge enable debug on the DBPurge CronJob
DBPurge bool `json:"dbPurge,omitempty"`
}

// ManilaServiceDebug indicates whether certain stages of Manila service
// deployment should pause in debug mode
type ManilaServiceDebug struct {
Expand All @@ -135,15 +126,3 @@ type ManilaServiceDebug struct {
// service enable debug
Service bool `json:"service,omitempty"`
}

// SetupDefaults - initializes any CRD field defaults based on environment variables (the defaulting mechanism itself is implemented via webhooks)
func SetupDefaults() {
// Acquire environmental defaults and initialize Manila defaults with them
manilaDefaults := ManilaDefaults{
APIContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_API_IMAGE_URL_DEFAULT", ManilaAPIContainerImage),
SchedulerContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_SCHEDULER_IMAGE_URL_DEFAULT", ManilaSchedulerContainerImage),
ShareContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_SHARE_IMAGE_URL_DEFAULT", ManilaShareContainerImage),
}

SetupManilaDefaults(manilaDefaults)
}
29 changes: 29 additions & 0 deletions api/v1beta1/manila_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ type ManilaSpec struct {
// NodeSelector here acts as a default value and can be overridden by service
// specific NodeSelector Settings.
NodeSelector map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// DBPurge parameters -
DBPurge DBPurge `json:"dbPurge,omitempty"`
}

// ManilaStatus defines the observed state of Manila
Expand Down Expand Up @@ -143,6 +147,31 @@ func init() {
SchemeBuilder.Register(&Manila{}, &ManilaList{})
}

// DBPurge struct is used to model the parameters exposed to the Manila API CronJob
type DBPurge struct {
// +kubebuilder:validation:Optional
// +kubebuilder:default=30
// Age -
Age int `json:"age"`
// +kubebuilder:validation:Optional
// +kubebuilder:default="1 0 * * *"
//Schedule -
Schedule string `json:"schedule"`
}

// ManilaDebug indicates whether certain stages of Manila deployment should
// pause in debug mode
type ManilaDebug struct {
// +kubebuilder:validation:Optional
// +kubebuilder:default=false
// dbSync enable debug
DBSync bool `json:"dbSync,omitempty"`
// +kubebuilder:validation:Optional
// +kubebuilder:default=false
// DBPurge enables debug mode on the DBPurge CronJob
DBPurge bool `json:"dbPurge,omitempty"`
}

// IsReady - returns true if Manila is reconciled successfully
func (instance Manila) IsReady() bool {
return instance.Status.Conditions.IsTrue(condition.ReadyCondition)
Expand Down
25 changes: 21 additions & 4 deletions api/v1beta1/manila_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,36 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
)

// ManilaDefaults -
type ManilaDefaults struct {
APIContainerImageURL string
SchedulerContainerImageURL string
ShareContainerImageURL string
DBPurgeAge int
DBPurgeSchedule string
}

var manilaDefaults ManilaDefaults

// log is for logging in this package.
var manilalog = logf.Log.WithName("manila-resource")

// SetupManilaDefaults - initialize Manila spec defaults for use with either internal or external webhooks
func SetupManilaDefaults(defaults ManilaDefaults) {
manilaDefaults = defaults
manilalog.Info("Manila defaults initialized", "defaults", defaults)
// SetupDefaults - initializes any CRD field defaults based on environment variables
// (the defaulting mechanism itself is implemented via webhooks)
func SetupDefaults() {
// Acquire environmental defaults and initialize Manila defaults with them
manilaDefaults := ManilaDefaults{
APIContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_API_IMAGE_URL_DEFAULT", ManilaAPIContainerImage),
SchedulerContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_SCHEDULER_IMAGE_URL_DEFAULT", ManilaSchedulerContainerImage),
ShareContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_SHARE_IMAGE_URL_DEFAULT", ManilaShareContainerImage),
DBPurgeAge: DBPurgeDefaultAge,
DBPurgeSchedule: DBPurgeDefaultSchedule,
}

manilalog.Info("Manila defaults initialized", "defaults", manilaDefaults)
}

// SetupWebhookWithManager sets up the webhook with the Manager
Expand Down Expand Up @@ -82,6 +94,11 @@ func (spec *ManilaSpec) Default() {
spec.ManilaShares[key] = manilaShare
}
}

if (spec.DBPurge.Age == 0) && (spec.DBPurge.Schedule == "") {
spec.DBPurge.Age = manilaDefaults.DBPurgeAge
spec.DBPurge.Schedule = manilaDefaults.DBPurgeSchedule
}
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
Expand Down
16 changes: 16 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions config/crd/bases/manila.openstack.org_manilas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ spec:
databaseUser:
default: manila
type: string
dbPurge:
properties:
age:
default: 30
type: integer
schedule:
default: 1 0 * * *
type: string
type: object
debug:
properties:
dbPurge:
Expand Down
5 changes: 0 additions & 5 deletions pkg/manila/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ const (
// CustomServiceConfigSecretsFileName - Snippet generated by Secrets passed
// to the sub CR
CustomServiceConfigSecretsFileName = "04-config.conf"

//DBPurgeAge -
DBPurgeAge = 30
//DBPurgeDefaultSchedule -
DBPurgeDefaultSchedule = "1 0 * * *"
)

// DbsyncPropagation keeps track of the DBSync Service Propagation Type
Expand Down
4 changes: 2 additions & 2 deletions pkg/manila/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CronJob(
DBPurgeCommandString := strings.Join(DBPurgeCommand, " ")

// Extend the resulting command with the DBPurgeAge int
args = append(args, DBPurgeCommandString+strconv.Itoa(DBPurgeAge))
args = append(args, DBPurgeCommandString+strconv.Itoa(instance.Spec.DBPurge.Age))

parallelism := int32(1)
completions := int32(1)
Expand Down Expand Up @@ -83,7 +83,7 @@ func CronJob(
Namespace: instance.Namespace,
},
Spec: batchv1.CronJobSpec{
Schedule: DBPurgeDefaultSchedule,
Schedule: instance.Spec.DBPurge.Schedule,
ConcurrencyPolicy: batchv1.ForbidConcurrent,
JobTemplate: batchv1.JobTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit b7e20ad

Please sign in to comment.