Skip to content

Commit

Permalink
Improve maintenance code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Saratura committed Jul 10, 2024
1 parent 33f2e7b commit 8d337bb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
29 changes: 16 additions & 13 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 Down Expand Up @@ -73,6 +78,7 @@ func (c *controller) runMaintenance(cmd *cobra.Command, _ []string) error {
return err
}

var m Maintenance
switch serviceName {
case postgresql:

Expand All @@ -81,34 +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:
k := maintenance.NewNextcloud(kubeClient, getHTTPClient())
return k.DoMaintenance(cmd.Context())
m = maintenance.NewNextcloud(kubeClient, getHTTPClient())
default:

panic("service name is mandatory")
}

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

func getHTTPClient() *http.Client {
Expand Down
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
4 changes: 2 additions & 2 deletions pkg/maintenance/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type Minio struct {
}

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

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

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

0 comments on commit 8d337bb

Please sign in to comment.