Skip to content

Commit

Permalink
Merge pull request #192 from vshn/nextcloud-maint
Browse files Browse the repository at this point in the history
Add Nextcloud maintenance
  • Loading branch information
zugao authored Jul 11, 2024
2 parents 14c794e + 8d337bb commit 02dd941
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 44 deletions.
10 changes: 5 additions & 5 deletions apis/vshn/v1/vshn_nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type VSHNNextcloudParameters struct {
Instances int `json:"instances,omitempty"`
}

// VSHNNextcloudserviceSpec contains nextcloud DBaaS specific properties
// VSHNNextcloudServiceSpec contains nextcloud DBaaS specific properties
type VSHNNextcloudServiceSpec struct {
// +kubebuilder:validation:Required

Expand Down Expand Up @@ -118,11 +118,11 @@ type VSHNNextcloudServiceSpec struct {
PostgreSQLParameters *VSHNPostgreSQLParameters `json:"postgreSQLParameters,omitempty"`
}

// VSHNNextcloudsettings contains Nextcloud specific settings.
type VSHNNextcloudsettings struct{}
// VSHNNextcloudSettings contains Nextcloud specific settings.
type VSHNNextcloudSettings struct{}

// VSHNNextcloudsizeSpec contains settings to control the sizing of a service.
type VSHNNextcloudsizeSpec struct {
// VSHNNextcloudSizeSpec contains settings to control the sizing of a service.
type VSHNNextcloudSizeSpec struct {

// CPURequests defines the requests amount of Kubernetes CPUs for an instance.
CPURequests string `json:"cpuRequests,omitempty"`
Expand Down
38 changes: 19 additions & 19 deletions apis/vshn/v1/zz_generated.deepcopy.go

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

31 changes: 20 additions & 11 deletions cmd/maintenance.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"net/http"
"time"
Expand All @@ -20,6 +21,10 @@ var (
MaintenanceCMD = newMaintenanceCMD()
)

type Maintenance interface {
DoMaintenance(ctx context.Context) error
}

type service enumflag.Flag

const (
Expand All @@ -29,6 +34,7 @@ const (
minio
mariadb
keycloak
nextcloud
)

var maintenanceServices = map[service][]string{
Expand All @@ -37,6 +43,7 @@ var maintenanceServices = map[service][]string{
minio: {"minio"},
mariadb: {"mariadb"},
keycloak: {"keycloak"},
nextcloud: {"nextcloud"},
}

var serviceName service
Expand Down Expand Up @@ -71,6 +78,7 @@ func (c *controller) runMaintenance(cmd *cobra.Command, _ []string) error {
return err
}

var m Maintenance
switch serviceName {
case postgresql:

Expand All @@ -79,30 +87,31 @@ func (c *controller) runMaintenance(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("missing environment variable: %s", "SG_NAMESPACE")
}

pg := maintenance.PostgreSQL{
m = &maintenance.PostgreSQL{
Client: kubeClient,
SgURL: "https://stackgres-restapi." + sgNamespace + ".svc",
MaintTimeout: time.Hour,
}
return pg.DoMaintenance(cmd.Context())
case redis:
r := maintenance.NewRedis(kubeClient, getHTTPClient())
return r.DoMaintenance(cmd.Context())
m = maintenance.NewRedis(kubeClient, getHTTPClient())

case minio:
m := maintenance.NewMinio(kubeClient, getHTTPClient())
return m.DoMaintenance(cmd.Context())
m = maintenance.NewMinio(kubeClient, getHTTPClient())

case mariadb:
m := maintenance.NewMariaDB(kubeClient, getHTTPClient())
return m.DoMaintenance(cmd.Context())
m = maintenance.NewMariaDB(kubeClient, getHTTPClient())

case keycloak:
k := maintenance.NewKeycloak(kubeClient, getHTTPClient())
return k.DoMaintenance(cmd.Context())
m = maintenance.NewKeycloak(kubeClient, getHTTPClient())

case nextcloud:
m = maintenance.NewNextcloud(kubeClient, getHTTPClient())
default:

panic("service name is mandatory")
}

return nil
return m.DoMaintenance(cmd.Context())
}

func getHTTPClient() *http.Client {
Expand Down
26 changes: 25 additions & 1 deletion pkg/comp-functions/functions/vshnnextcloud/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"time"

"dario.cat/mergo"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -46,6 +48,9 @@ var nextcloudConfig string
//go:embed files/nextcloud-post-installation.sh
var nextcloudPostInstallation string

//go:embed files/nextcloud-post-upgrade.sh
var nextcloudPostUpgrade string

// DeployNextcloud deploys a nexctloud instance via the codecentric Helm Chart.
func DeployNextcloud(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {

Expand Down Expand Up @@ -272,6 +277,7 @@ func newValues(ctx context.Context, svc *runtime.ServiceRuntime, comp *vshnv1.VS
}
}

updatedNextcloudConfig := setBackgroundJobMaintenance(comp.Spec.Parameters.Maintenance.GetMaintenanceTimeOfDay(), nextcloudConfig)
values = map[string]any{
"nextcloud": map[string]any{
"host": comp.Spec.Parameters.Service.FQDN,
Expand All @@ -282,7 +288,7 @@ func newValues(ctx context.Context, svc *runtime.ServiceRuntime, comp *vshnv1.VS
"passwordKey": adminPWSecretField,
},
"configs": map[string]string{
"vshn-nextcloud.config.php": nextcloudConfig,
"vshn-nextcloud.config.php": updatedNextcloudConfig,
},
"containerPort": 8080,
"podSecurityContext": securityContext,
Expand Down Expand Up @@ -317,6 +323,11 @@ func newValues(ctx context.Context, svc *runtime.ServiceRuntime, comp *vshnv1.VS
"mountPath": "/docker-entrypoint-hooks.d/post-installation/vshn-post-installation.sh",
"subPath": "vshn-post-installation.sh",
},
{
"name": "nextcloud-hooks",
"mountPath": "/docker-entrypoint-hooks.d/post-upgrade/vshn-post-upgrade.sh",
"subPath": "vshn-post-upgrade.sh",
},
},
},
"securityContext": securityContext,
Expand Down Expand Up @@ -408,6 +419,7 @@ func addNextcloudHooks(svc *runtime.ServiceRuntime, comp *vshnv1.VSHNNextcloud)
},
Data: map[string]string{
"vshn-post-installation.sh": nextcloudPostInstallation,
"vshn-post-upgrade.sh": nextcloudPostUpgrade,
},
}

Expand All @@ -417,3 +429,15 @@ func addNextcloudHooks(svc *runtime.ServiceRuntime, comp *vshnv1.VSHNNextcloud)
}
return nil
}

func setBackgroundJobMaintenance(timeOfDay, nextcloudConfig string) string {
parsedTime, err := time.Parse(time.TimeOnly, timeOfDay)
if err != nil {
// set the default value at 1 am
return "1"
}
// Start Background Job Maintenance no earlier than 20 min after the regular Maintenance
// and no later than 1 hour and 19 min after the regular Maintenance
backgroundJobHour := parsedTime.Add(20 * time.Minute).Add(time.Hour).Hour()
return strings.Replace(nextcloudConfig, "%maintenance_value%", strconv.Itoa(backgroundJobHour), 1)
}
38 changes: 38 additions & 0 deletions pkg/comp-functions/functions/vshnnextcloud/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package vshnnextcloud

import (
"context"
_ "embed"
"encoding/json"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -86,6 +88,42 @@ func Test_addReleaseInternalDB(t *testing.T) {
assert.Equal(t, true, intDb["enabled"])
}

//go:embed files/vshn-nextcloud.config.php
var testNextcloudConfig string

func Test_setBackgroundJobMaintenance(t *testing.T) {

tests := []struct {
name string
timeOfDay string
want string
}{
{
name: "10MinEarlierThan20Expect21",
timeOfDay: "19:50:01",
want: "21",
},
{
name: "21MinEarlierThan20Expect20",
timeOfDay: "19:39:01",
want: "20",
},
{
name: "1MinAfterThan0Expect1",
timeOfDay: "00:01:01",
want: "1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
updatedNextcloudConfig := setBackgroundJobMaintenance(tt.timeOfDay, testNextcloudConfig)
splitted := strings.Split(updatedNextcloudConfig, "'maintenance_window_start' => ")
actual := strings.Trim(splitted[1][:2], ",\n")
assert.Equal(t, tt.want, actual)
})
}
}

func getNextcloudComp(t *testing.T, file string) (*runtime.ServiceRuntime, *vshnv1.VSHNNextcloud) {
svc := commontest.LoadRuntimeFromFile(t, file)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

/var/www/html/occ db:add-missing-indices
/var/www/html/occ app:update --all
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
$CONFIG = array (
'check_data_directory_permissions' => false,
'trusted_proxies' => ['10.0.0.0/8'],
'maintenance_window_start' => %maintenance_value%,
);
31 changes: 31 additions & 0 deletions pkg/comp-functions/functions/vshnnextcloud/maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package vshnnextcloud

import (
"context"
"fmt"

xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common/maintenance"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddMaintenanceJob will add a job to do the maintenance for the instance
func AddMaintenanceJob(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {

comp := &vshnv1.VSHNNextcloud{}
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.SetRandomSchedules(comp, comp)

instanceNamespace := comp.GetInstanceNamespace()
schedule := comp.GetFullMaintenanceSchedule()

return maintenance.New(comp, svc, schedule, instanceNamespace, comp.GetServiceName()).
WithHelmBasedService().
Run(ctx)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnnextcloud/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func init() {
Name: "ingress",
Execute: AddIngress,
},
{
Name: "maintenance",
Execute: AddMaintenanceJob,
},
},
})
}
4 changes: 2 additions & 2 deletions pkg/maintenance/keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Keycloak struct {
}

// NewKeycloak returns a new Keycloak object
func NewKeycloak(c client.Client, hc *http.Client) Keycloak {
return Keycloak{
func NewKeycloak(c client.Client, hc *http.Client) *Keycloak {
return &Keycloak{
k8sClient: c,
httpClient: hc,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/maintenance/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type MariaDB struct {
}

// NewMariaDB returns a new Redis maintenance job runner
func NewMariaDB(c client.Client, hc *http.Client) MariaDB {
return MariaDB{
func NewMariaDB(c client.Client, hc *http.Client) *MariaDB {
return &MariaDB{
k8sClient: c,
httpClient: hc,
}
Expand Down
Loading

0 comments on commit 02dd941

Please sign in to comment.