Skip to content

Commit

Permalink
Enable status subresource for policies.
Browse files Browse the repository at this point in the history
After every backup, policy status is updated with details of
the just finished backup.
  • Loading branch information
draghuram committed Jan 29, 2020
1 parent e2c385a commit ca9cc8e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ DOCKER_KUBEDR_IMAGE_NAME_SHORT = kubedr
DOCKER_KUBEDR_IMAGE_NAME_LONG = ${DOCKER_REGISTRY}${DOCKER_KUBEDR_IMAGE_NAME_SHORT}
DOCKER_KUBEDR_IMAGE_NAME_LONG_DOCKERHUB = ${DOCKER_PREFIX}${DOCKER_KUBEDR_IMAGE_NAME_SHORT}

DOCKER_KUBEDRUTIL_IMAGE_TAG ?= 0.2.2
DOCKER_KUBEDRUTIL_IMAGE_TAG ?= 0.2.3
DOCKER_KUBEDRUTIL_IMAGE_NAME_SHORT = kubedrutil
DOCKER_KUBEDRUTIL_IMAGE_NAME_LONG = ${DOCKER_REGISTRY}${DOCKER_KUBEDRUTIL_IMAGE_NAME_SHORT}
DOCKER_KUBEDRUTIL_IMAGE_NAME_LONG_DOCKERHUB = ${DOCKER_PREFIX}${DOCKER_KUBEDRUTIL_IMAGE_NAME_SHORT}

build: docker_build go_build
build: manifests docker_build go_build

manifests:
cd ${DOCKER_DIR_BASE} && make manifests

docker_build:
cd ${DOCKER_DIR_BASE} && \
Expand Down
6 changes: 3 additions & 3 deletions kubedr/api/v1alpha1/backuplocation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ type BackupLocationSpec struct {
type BackupLocationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
InitStatus string `json:"initStatus,omitempty"`
InitStatus string `json:"initStatus"`

// +kubebuilder:validation:Optional
InitErrorMessage string `json:"initErrorMessage,omitempty"`
InitErrorMessage string `json:"initErrorMessage"`

InitTime string `json:"initTime,omitempty"`
InitTime string `json:"initTime"`
}

// +kubebuilder:object:root=true
Expand Down
27 changes: 25 additions & 2 deletions kubedr/api/v1alpha1/metadatabackuppolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -66,11 +67,33 @@ type MetadataBackupPolicySpec struct {

// MetadataBackupPolicyStatus defines the observed state of MetadataBackupPolicy
type MetadataBackupPolicyStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
BackupTime string `json:"backupTime"`
BackupStatus string `json:"backupStatus"`

// +kubebuilder:validation:Optional
BackupErrorMessage string `json:"backupErrorMessage"`

// +kubebuilder:validation:Optional
FilesNew uint `json:"filesNew"`

// +kubebuilder:validation:Optional
FilesChanged uint `json:"filesChanged"`

// +kubebuilder:validation:Optional
DataAdded uint64 `json:"dataAdded"`

// +kubebuilder:validation:Optional
TotalBytesProcessed uint64 `json:"totalBytesProcessed"`

// +kubebuilder:validation:Optional
TotalDurationSecs resource.Quantity `json:"totalDurationSecs"`

// +kubebuilder:validation:Optional
SnapshotID string `json:"snapshotId"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// MetadataBackupPolicy is the Schema for the metadatabackuppolicies API
type MetadataBackupPolicy struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ spec:
of cluster Important: Run "make" to regenerate code after modifying
this file'
type: string
initTime:
type: string
required:
- initErrorMessage
- initStatus
- initTime
type: object
type: object
version: v1alpha1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ spec:
plural: metadatabackuppolicies
singular: metadatabackuppolicy
scope: ""
subresources:
status: {}
validation:
openAPIV3Schema:
description: MetadataBackupPolicy is the Schema for the metadatabackuppolicies
Expand Down Expand Up @@ -72,6 +74,30 @@ spec:
type: object
status:
description: MetadataBackupPolicyStatus defines the observed state of MetadataBackupPolicy
properties:
backupErrorMessage:
type: string
backupStatus:
type: string
backupTime:
type: string
dataAdded:
format: int64
type: integer
filesChanged:
type: integer
filesNew:
type: integer
snapshotId:
type: string
totalBytesProcessed:
format: int64
type: integer
totalDurationSecs:
type: string
required:
- backupStatus
- backupTime
type: object
type: object
version: v1alpha1
Expand Down
12 changes: 12 additions & 0 deletions kubedr/controllers/metadatabackuppolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type MetadataBackupPolicyReconciler struct {
func (r *MetadataBackupPolicyReconciler) newPolicy(policy *kubedrv1alpha1.MetadataBackupPolicy,
namespace string, cronJobName string) (ctrl.Result, error) {

r.setStatus(policy)

backupCronjob, err := r.buildBackupCronjob(policy, namespace, cronJobName)
if err != nil {
r.Log.Error(err, "Error in creating backup cronjob")
Expand Down Expand Up @@ -132,6 +134,16 @@ func (r *MetadataBackupPolicyReconciler) processSpec(policy *kubedrv1alpha1.Meta
return r.processUpdate(policy, &cronJob)
}

func (r *MetadataBackupPolicyReconciler) setStatus(policy *kubedrv1alpha1.MetadataBackupPolicy) {
policy.Status.BackupStatus = "Initializing"
policy.Status.BackupTime = metav1.Now().String()

r.Log.Info("Updating status...")
if err := r.Status().Update(context.Background(), policy); err != nil {
r.Log.Error(err, "unable to update policy status")
}
}

// +kubebuilder:rbac:groups=kubedr.catalogicsoftware.com,resources=metadatabackuppolicies,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kubedr.catalogicsoftware.com,resources=metadatabackuppolicies/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=create;get;list;update;patch;delete;watch
Expand Down

0 comments on commit ca9cc8e

Please sign in to comment.