Skip to content

Commit

Permalink
[WIP] clusterbackup refs for postgres were implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
tengu-alt committed Sep 28, 2023
1 parent 88ad6c2 commit 9d62caf
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

.PHONY: docker-build
docker-build: manifests generate test ## Build docker image with the manager.
docker-build: manifests generate ## Build docker image with the manager.
docker build -t ${IMG} .

.PHONY: docker-push
Expand Down
8 changes: 6 additions & 2 deletions apis/clusterresources/v1beta1/clusterbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

// ClusterBackupSpec defines the desired state of ClusterBackup
type ClusterBackupSpec struct {
ClusterID string `json:"clusterId"`
ClusterKind string `json:"clusterKind"`
ClusterID string `json:"clusterId,omitempty"`
ClusterKind string `json:"clusterKind,omitempty"`
}

// ClusterBackupStatus defines the observed state of ClusterBackup
Expand Down Expand Up @@ -71,6 +71,10 @@ func (cbs *ClusterBackupStatus) UpdateStatus(instBackup *models.BackupEvent) {
cbs.Progress = fmt.Sprintf("%f", instBackup.Progress)
}

func (cb *ClusterBackup) DeepCopyObjecte() client.Object {
return cb.DeepCopy()
}

func init() {
SchemeBuilder.Register(&ClusterBackup{}, &ClusterBackupList{})
}
10 changes: 4 additions & 6 deletions apis/clusterresources/v1beta1/clusterbackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/instaclustr/operator/pkg/models"
)

// log is for logging in this package.
Expand Down Expand Up @@ -54,10 +52,10 @@ var _ webhook.Validator = &ClusterBackup{}
func (r *ClusterBackup) ValidateCreate() error {
clusterbackuplog.Info("validate create", "name", r.Name)

_, ok := models.ClusterKindsMap[r.Spec.ClusterKind]
if !ok {
return models.ErrUnsupportedBackupClusterKind
}
//_, ok := models.ClusterKindsMap[r.Spec.ClusterKind]
//if !ok {
// return models.ErrUnsupportedBackupClusterKind
//}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion apis/clusterresources/v1beta1/maintenanceevents_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// MaintenanceEventsSpec defines the desired state of MaintenanceEvents
type MaintenanceEventsSpec struct {
ClusterID string `json:"clusterId"`
ClusterID string `json:"clusterId,omitempty"`
MaintenanceEventsReschedules []*MaintenanceEventReschedule `json:"maintenanceEventsReschedule"`
}

Expand Down
15 changes: 8 additions & 7 deletions apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ type PgRestoreFrom struct {
type PgSpec struct {
PgRestoreFrom *PgRestoreFrom `json:"pgRestoreFrom,omitempty"`
Cluster `json:",inline"`
DataCentres []*PgDataCentre `json:"dataCentres,omitempty"`
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
Description string `json:"description,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
DataCentres []*PgDataCentre `json:"dataCentres,omitempty"`
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
Description string `json:"description,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
ClusterResourceRefs map[string]*UserReference `json:"clusterResourceRefs,omitempty"`
//+kubebuilder:validate:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down Expand Up @@ -155,12 +156,12 @@ func (pg *PostgreSQL) NewBackupSpec(startTimestamp int) *clusterresourcesv1beta1
ObjectMeta: ctrl.ObjectMeta{
Name: models.PgBackupPrefix + pg.Status.ID + "-" + strconv.Itoa(startTimestamp),
Namespace: pg.Namespace,
Annotations: map[string]string{models.StartTimestampAnnotation: strconv.Itoa(startTimestamp)},
Annotations: map[string]string{models.StartTimestampAnnotation: strconv.Itoa(startTimestamp), "clusterID": pg.Status.ID},
Labels: map[string]string{models.ClusterIDLabel: pg.Status.ID},
Finalizers: []string{models.DeletionFinalizer},
},
Spec: clusterresourcesv1beta1.ClusterBackupSpec{
ClusterID: pg.Status.ID,
//ClusterID: pg.Status.ID,
ClusterKind: models.PgClusterKind,
},
}
Expand Down
15 changes: 15 additions & 0 deletions apis/clusters/v1beta1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ spec:
type: string
clusterKind:
type: string
required:
- clusterId
- clusterKind
type: object
status:
description: ClusterBackupStatus defines the observed state of ClusterBackup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ spec:
type: object
type: array
required:
- clusterId
- maintenanceEventsReschedule
type: object
status:
Expand Down
12 changes: 12 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_postgresqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ spec:
additionalProperties:
type: string
type: object
clusterResourceRefs:
additionalProperties:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: object
dataCentres:
items:
properties:
Expand Down
2 changes: 1 addition & 1 deletion config/samples/clusterresources_v1beta1_clusterbackup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ kind: ClusterBackup
metadata:
name: clusterbackup-sample
spec:
clusterId: 2ae611cf-ac91-4325-941c-a35c043f9c34
# clusterId: 2ae611cf-ac91-4325-941c-a35c043f9c34
clusterKind: PostgreSQL
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ kind: MaintenanceEvents
metadata:
name: maintenanceevents-sample
spec:
clusterId: "9cf09a53-a09e-450a-ba7d-e98b3c724911"
# clusterId: "f78c42f9-2b31-4922-8c2f-ab03f8caed5e"
maintenanceEventsReschedule:
- scheduledStartTime: "2023-11-09T04:30:00Z"
maintenanceEventId: "0d25b466-bc22-44a8-b15d-8f92e815cb6e"
- scheduledStartTime: "2023-11-15T06:00:00Z"
maintenanceEventId: "d4806381-cd1e-48df-b9ba-70f9b0829c72"
6 changes: 5 additions & 1 deletion config/samples/clusters_v1beta1_postgresql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
# annotations:
# testAnnotation: test
spec:
name: "username-test"
name: "oleksandr-pg"
version: "15.4.0"
dataCentres:
- region: "US_WEST_2"
Expand Down Expand Up @@ -45,6 +45,10 @@ spec:
# userRef:
# - namespace: default
# name: postgresqluser-sample
clusterResourceRefs:
# ClusterBackup:
# namespace: default
# name: clusterbackup-sample
privateNetworkCluster: false
synchronousModeStrict: false
# resizeSettings:
Expand Down
34 changes: 23 additions & 11 deletions controllers/clusterresources/clusterbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package clusterresources

import (
"context"
"fmt"
"strconv"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -77,11 +78,15 @@ func (r *ClusterBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reques

patch := backup.NewPatch()

if backup.Labels[models.ClusterIDLabel] != backup.Spec.ClusterID {
if backup.Annotations["clusterID"] == "" {
return models.ExitReconcile, nil
}

if backup.Labels[models.ClusterIDLabel] != backup.Annotations["clusterID"] {
if backup.Labels == nil {
backup.Labels = map[string]string{models.ClusterIDLabel: backup.Spec.ClusterID}
backup.Labels = map[string]string{models.ClusterIDLabel: backup.Annotations["clusterID"]}
} else {
backup.Labels[models.ClusterIDLabel] = backup.Spec.ClusterID
backup.Labels[models.ClusterIDLabel] = backup.Annotations["clusterID"]
}
err = r.Patch(ctx, backup, patch)
if err != nil {
Expand All @@ -98,11 +103,11 @@ func (r *ClusterBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}

backupsList, err := r.listClusterBackups(ctx, backup.Spec.ClusterID, backup.Namespace)
backupsList, err := r.listClusterBackups(ctx, backup.Annotations["clusterID"], backup.Namespace)
if err != nil {
logger.Error(err, "Cannot get cluster backups",
"backup name", backup.Name,
"cluster ID", backup.Spec.ClusterID,
"cluster ID", backup.Annotations["clusterID"],
)

r.EventRecorder.Eventf(
Expand All @@ -118,11 +123,12 @@ func (r *ClusterBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
clusterKind = models.PgAppKind
}

iBackup, err := r.API.GetClusterBackups(backup.Spec.ClusterID, clusterKind)
iBackup, err := r.API.GetClusterBackups(backup.Annotations["clusterID"], clusterKind)
if err != nil {
logger.Error(err, "Cannot get cluster backups from Instaclustr",
"backup name", backup.Name,
"cluster ID", backup.Spec.ClusterID,
"cluster ID", backup.Annotations["clusterID"],
"LENGTH OF ANNOTS", len(backup.Annotations),
)

r.EventRecorder.Eventf(
Expand All @@ -136,11 +142,11 @@ func (r *ClusterBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
iBackupEvents := iBackup.GetBackupEvents(backup.Spec.ClusterKind)

if len(iBackupEvents) < len(backupsList.Items) {
err = r.API.TriggerClusterBackup(backup.Spec.ClusterID, models.ClusterKindsMap[backup.Spec.ClusterKind])
err = r.API.TriggerClusterBackup(backup.Annotations["clusterID"], models.ClusterKindsMap[backup.Spec.ClusterKind])
if err != nil {
logger.Error(err, "Cannot trigger cluster backup",
"backup name", backup.Name,
"cluster ID", backup.Spec.ClusterID,
"cluster ID", backup.Annotations["clusterID"],
)

r.EventRecorder.Eventf(
Expand All @@ -156,7 +162,7 @@ func (r *ClusterBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
"Resource creation request is sent",
)
logger.Info("New cluster backup request was sent",
"cluster ID", backup.Spec.ClusterID,
"cluster ID", backup.Annotations["clusterID"],
)
}

Expand Down Expand Up @@ -214,7 +220,7 @@ func (r *ClusterBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reques

logger.Info("Cluster backup resource was reconciled",
"backup name", backup.Name,
"cluster ID", backup.Spec.ClusterID,
"cluster ID", backup.Annotations["clusterID"],
)

return models.ExitReconcile, nil
Expand All @@ -239,7 +245,13 @@ func (r *ClusterBackupReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1beta1.ClusterBackup{}, builder.WithPredicates(predicate.Funcs{
UpdateFunc: func(event event.UpdateEvent) bool {
//newObj := event.ObjectNew.(*v1beta1.ClusterBackup)
//oldObj := event.ObjectOld.(*v1beta1.ClusterBackup)
if fmt.Sprint(event.ObjectOld.(*v1beta1.ClusterBackup).Annotations) != fmt.Sprint(event.ObjectNew.(*v1beta1.ClusterBackup).Annotations) {

Check failure on line 250 in controllers/clusterresources/clusterbackup_controller.go

View workflow job for this annotation

GitHub Actions / PRE-MAIN Build and push the Instaclustr Operator

S1008: should use 'return fmt.Sprint(event.ObjectOld.(*v1beta1.ClusterBackup).Annotations) != fmt.Sprint(event.ObjectNew.(*v1beta1.ClusterBackup).Annotations)' instead of 'if fmt.Sprint(event.ObjectOld.(*v1beta1.ClusterBackup).Annotations) != fmt.Sprint(event.ObjectNew.(*v1beta1.ClusterBackup).Annotations) { return true }; return false' (gosimple)
return true
}
return false
//return true
},
})).
Complete(r)
Expand Down
11 changes: 11 additions & 0 deletions controllers/clusterresources/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package clusterresources

import (
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"

k8sCore "k8s.io/api/core/v1"
Expand Down Expand Up @@ -87,3 +88,13 @@ func getUserCreds(secret *k8sCore.Secret) (username, password string, err error)

return username, password, nil
}

type Object interface {
client.Object
NewPatch() client.Patch
}

//func NewObjectPatch(obj Object) client.Patch {
// //old := obj.DeepCopyObjecte()
// return client.StrategicMergeFrom(obj.DeepCopyObjecte())
//}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (r *MaintenanceEventsReconciler) Reconcile(ctx context.Context, req ctrl.Re
MaintenanceEventID: me.Spec.MaintenanceEventsReschedules[len(me.Spec.MaintenanceEventsReschedules)-1].MaintenanceEventID,
ScheduledStartTime: me.Spec.MaintenanceEventsReschedules[len(me.Spec.MaintenanceEventsReschedules)-1].ScheduledStartTime,
}
//

err = r.API.RescheduleMaintenanceEvent(meReschedule)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions controllers/clusters/kafka_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ func (r *KafkaReconciler) handleCreateUser(
u.Status.ClustersEvents = make(map[string]string)
}

//

u.Status.ClustersEvents[kafka.Status.ID] = models.CreatingEvent
err = r.Status().Patch(ctx, u, patch)
if err != nil {
Expand Down
Loading

0 comments on commit 9d62caf

Please sign in to comment.