Skip to content

Commit

Permalink
Support non-DR restore.
Browse files Browse the repository at this point in the history
Previously, the only restore supported was in a DR scenario and required
usage of a separate python package ("kubedrctl").

With this change, one can create a resource "MetadataRestore" and etcd
snapshot (and optionally certificates) will be restored to the given
PVC.
  • Loading branch information
draghuram committed Feb 21, 2020
1 parent 871f2e7 commit 1a415a5
Show file tree
Hide file tree
Showing 21 changed files with 760 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DOCKER_KUBEDR_IMAGE_TAG ?= latest
DOCKER_KUBEDR_IMAGE_NAME_SHORT ?= kubedr
DOCKER_KUBEDR_IMAGE_NAME_LONG ?= ${DOCKER_PREFIX}${DOCKER_KUBEDR_IMAGE_NAME_SHORT}

DOCKER_KUBEDRUTIL_IMAGE_TAG ?= 0.2.10
DOCKER_KUBEDRUTIL_IMAGE_TAG ?= 0.2.11
DOCKER_KUBEDRUTIL_IMAGE_NAME_SHORT ?= kubedrutil
DOCKER_KUBEDRUTIL_IMAGE_NAME_LONG ?= ${DOCKER_PREFIX}${DOCKER_KUBEDRUTIL_IMAGE_NAME_SHORT}

Expand Down
5 changes: 5 additions & 0 deletions docs/userguide/source/monitoring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,15 @@ An example::
dataAdded: 1573023
filesChanged: 1
filesNew: 0
mbrName': mbr-4c1223d6
snapshotId: b0f347ef
totalBytesProcessed: 15736864
totalDurationSecs: "0.318463127"

Apart from the stats regarding the backup, the status also contains
the name of the ``MetadataBackupRecord`` resource that is required to
perform restores.

Events
======

Expand Down
3 changes: 3 additions & 0 deletions kubedr/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ resources:
- group: kubedr
version: v1alpha1
kind: MetadataBackupRecord
- group: kubedr
version: v1alpha1
kind: MetadataRestore
3 changes: 3 additions & 0 deletions kubedr/api/v1alpha1/metadatabackuppolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type MetadataBackupPolicyStatus struct {
// Name of the pod that performed the backup.
// +kubebuilder:validation:Optional
BackupPod string `json:"backupPod"`

// +kubebuilder:validation:Optional
MBRName string `json:"mbrName"`
}

// +kubebuilder:object:root=true
Expand Down
83 changes: 83 additions & 0 deletions kubedr/api/v1alpha1/metadatarestore_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2020 Catalogic Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// MetadataRestoreSpec defines the desired state of MetadataRestore
type MetadataRestoreSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// kubebuilder:validation:MinLength:=1
MBRName string `json:"mbrName"`

// kubebuilder:validation:MinLength:=1
PVCName string `json:"pvcName"`
}

// MetadataRestoreStatus defines the observed state of MetadataRestore
type MetadataRestoreStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// +kubebuilder:validation:Optional
ObservedGeneration int64 `json:"observedGeneration"`

RestoreStatus string `json:"restoreStatus"`

// +kubebuilder:validation:Optional
RestoreErrorMessage string `json:"restoreErrorMessage"`

RestoreTime string `json:"restoreTime"`
}

// The creation of this resource triggers full restore of the data
// (etcd snapshot and certificates (if they were part of the backup).
// It would have been ideal to use a custom subresource (such as
// "/restore" but custom subresources are not yet supported for
// custom resources.

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

// MetadataRestore is the Schema for the metadatarestores API
type MetadataRestore struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec MetadataRestoreSpec `json:"spec,omitempty"`
Status MetadataRestoreStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// MetadataRestoreList contains a list of MetadataRestore
type MetadataRestoreList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MetadataRestore `json:"items"`
}

func init() {
SchemeBuilder.Register(&MetadataRestore{}, &MetadataRestoreList{})
}
89 changes: 89 additions & 0 deletions kubedr/api/v1alpha1/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 @@ -91,6 +91,8 @@ spec:
type: integer
filesNew:
type: integer
mbrName:
type: string
snapshotId:
type: string
totalBytesProcessed:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.2.2
creationTimestamp: null
name: metadatarestores.kubedr.catalogicsoftware.com
spec:
group: kubedr.catalogicsoftware.com
names:
kind: MetadataRestore
listKind: MetadataRestoreList
plural: metadatarestores
singular: metadatarestore
scope: ""
subresources:
status: {}
validation:
openAPIV3Schema:
description: MetadataRestore is the Schema for the metadatarestores API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: MetadataRestoreSpec defines the desired state of MetadataRestore
properties:
mbrName:
description: kubebuilder:validation:MinLength:=1
type: string
pvcName:
description: kubebuilder:validation:MinLength:=1
type: string
required:
- mbrName
- pvcName
type: object
status:
description: MetadataRestoreStatus defines the observed state of MetadataRestore
properties:
observedGeneration:
format: int64
type: integer
restoreErrorMessage:
type: string
restoreStatus:
type: string
restoreTime:
type: string
required:
- restoreStatus
- restoreTime
type: object
type: object
version: v1alpha1
versions:
- name: v1alpha1
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
3 changes: 3 additions & 0 deletions kubedr/config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ resources:
- bases/kubedr.catalogicsoftware.com_backuplocations.yaml
- bases/kubedr.catalogicsoftware.com_metadatabackuppolicies.yaml
- bases/kubedr.catalogicsoftware.com_metadatabackuprecords.yaml
- bases/kubedr.catalogicsoftware.com_metadatarestores.yaml
# +kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
Expand All @@ -13,13 +14,15 @@ patchesStrategicMerge:
# - patches/webhook_in_backuplocations.yaml
# - patches/webhook_in_metadatabackuppolicies.yaml
# - patches/webhook_in_metadatabackuprecords.yaml
#- patches/webhook_in_metadatarestores.yaml
# +kubebuilder:scaffold:crdkustomizewebhookpatch

# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
# patches here are for enabling the CA injection for each CRD
# - patches/cainjection_in_backuplocations.yaml
# - patches/cainjection_in_metadatabackuppolicies.yaml
# - patches/cainjection_in_metadatabackuprecords.yaml
#- patches/cainjection_in_metadatarestores.yaml
# +kubebuilder:scaffold:crdkustomizecainjectionpatch

# the following config is for teaching kustomize how to do kustomization for CRDs.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
name: metadatarestores.kubedr.catalogicsoftware.com
17 changes: 17 additions & 0 deletions kubedr/config/crd/patches/webhook_in_metadatarestores.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# The following patch enables conversion webhook for CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: metadatarestores.kubedr.catalogicsoftware.com
spec:
conversion:
strategy: Webhook
webhookClientConfig:
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
caBundle: Cg==
service:
namespace: system
name: webhook-service
path: /convert
7 changes: 7 additions & 0 deletions kubedr/config/samples/kubedr_v1alpha1_metadatarestore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kubedr.catalogicsoftware.com/v1alpha1
kind: MetadataRestore
metadata:
name: metadatarestore-sample
spec:
# Add fields here
foo: bar
3 changes: 2 additions & 1 deletion kubedr/controllers/backuplocation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ func (r *BackupLocationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
return ctrl.Result{}, err
}

log.Info("Creating a new Pod", "Pod.Namespace", initPod.Namespace, "Pod.Name", initPod.Name)
log.Info("Starting a new Pod", "Pod.Namespace", initPod.Namespace, "Pod.Name", initPod.Name)
err = r.Create(ctx, initPod)
if err != nil {
r.Log.Error(err, "Error in starting init pod")
r.setStatus(&backupLoc, "Failed", err.Error())
return ctrl.Result{}, err
}
Expand Down
1 change: 1 addition & 0 deletions kubedr/controllers/metadatabackuppolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (r *MetadataBackupPolicyReconciler) processSpecAndStatus(policy *kubedrv1al
}

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

Expand Down
Loading

0 comments on commit 1a415a5

Please sign in to comment.