Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize tenant backup and restore #83

Merged
merged 11 commits into from
Oct 18, 2023
174 changes: 2 additions & 172 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include make/*

VERSION ?= 2.0.0
# Image URL to use all building/pushing image targets
IMG ?= oceanbasedev/ob-operator:${VERSION}
Expand Down Expand Up @@ -44,175 +46,3 @@ all: build
.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -timeout 60m -v ./... -coverprofile cover.out

##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
sudo docker build -t ${IMG} --build-arg GOPROXY=${GOPROXY} --build-arg GOSUMDB=${GOSUMDB} --build-arg RACE=${RACE} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
sudo docker push ${IMG}

# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
# To properly provided solutions that supports more than one platform you should use this option.
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- docker buildx create --name project-v3-builder
docker buildx use project-v3-builder
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- docker buildx rm project-v3-builder
rm Dockerfile.cross

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: redeploy
redeploy: undeploy uninstall export-crd export-operator install deploy ## redeploy crd and controller to the K8s cluster specified in ~/.kube/config.

.PHONY: export-crd
export-crd: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd > deploy/crd.yaml

.PHONY: export-operator
export-operator: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default > deploy/operator.yaml


##@ Build Dependencies

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
YQ ?= $(LOCALBIN)/yq
SEMVER ?= $(LOCALBIN)/semver

$(YQ):
curl -o $(LOCALBIN)/yq ${YQ_DOWNLOAD_URL}
chmod +x $(LOCALBIN)/yq

$(SEMVER):
curl -o $(LOCALBIN)/semver ${SEMVER_DOWNLOAD_URL}
chmod +x $(LOCALBIN)/semver

## Tool Versions
KUSTOMIZE_VERSION ?= v5.0.0
CONTROLLER_TOOLS_VERSION ?= v0.13.0 # v0.11.3 can not support webhook manifests' generation, update to v0.13.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
$(KUSTOMIZE): $(LOCALBIN)
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/kustomize; \
fi
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) --output install_kustomize.sh && bash install_kustomize.sh $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); rm install_kustomize.sh; }

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: tools
tools: $(YQ) $(SEMVER)

.PHONY: GOLANGCI_LINT
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
$(GOLANGCI_LINT):
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANG_CI_VERSION}

.PHONY: lint
lint: $(GOLANGCI_LINT) ## Run linting.
$(GOLANGCI_LINT) run -v --timeout=10m

.PHONY: commit-hook
commit-hook: $(GOLANGCI_LINT) ## Install commit hook.
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo "#!/bin/sh" > .git/hooks/pre-commit
echo "make lint" >> .git/hooks/pre-commit

.PHONY: connect
connect:
ifdef TENANT
$(eval nodeHost = $(shell kubectl get pods -o jsonpath='{.items[1].status.podIP}'))
$(eval pwd = $(shell kubectl get secret $(shell kubectl get obtenant ${TENANT} -o jsonpath='{.status.credentials.root}') -o jsonpath='{.data.password}' | base64 -d))
$(if $(strip $(pwd)), mysql -h$(nodeHost) -P2881 -A -uroot@${TENANT} -p$(pwd) -Doceanbase, mysql -h$(nodeHost) -P2881 -A -uroot@${TENANT} -Doceanbase)
else
mysql -h$(shell kubectl get pods -o jsonpath='{.items[1].status.podIP}') -P2881 -A -uroot -p -Doceanbase
endif
6 changes: 4 additions & 2 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ type BackupPolicyStatusType string
type BackupDestType string
type LogArchiveDestState string
type ArchiveBinding string

type BackupDestination struct {
Type BackupDestType `json:"type,omitempty"`
Path string `json:"path,omitempty"`
Type BackupDestType `json:"type,omitempty"`
Path string `json:"path,omitempty"`
OSSAccessSecret string `json:"ossAccessSecret,omitempty"`
}

type RestoreJobStatus string
Expand Down
5 changes: 4 additions & 1 deletion api/v1alpha1/obtenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ type TenantSourceSpec struct {
}

type RestoreSourceSpec struct {
SourceUri string `json:"sourceUri"`
ArchiveSource *apitypes.BackupDestination `json:"archiveSource,omitempty"`
BakDataSource *apitypes.BackupDestination `json:"bakDataSource,omitempty"`

SourceUri string `json:"sourceUri,omitempty"` // Deprecated
Until RestoreUntilConfig `json:"until"`
Description *string `json:"description,omitempty"`
ReplayLogUntil *RestoreUntilConfig `json:"replayLogUntil,omitempty"`
Expand Down
63 changes: 62 additions & 1 deletion api/v1alpha1/obtenant_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"context"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -58,7 +59,6 @@ func (r *OBTenant) Default() {
if err != nil {
tenantlog.Error(err, "Failed to get cluster")
} else {
tenantlog.Info("Get cluster", "cluster", cluster)
r.SetOwnerReferences([]metav1.OwnerReference{{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Expand Down Expand Up @@ -91,6 +91,10 @@ func (r *OBTenant) ValidateUpdate(old runtime.Object) (admission.Warnings, error
}

func (r *OBTenant) validateMutation() error {
// Ignore deleted object
if r.GetDeletionTimestamp() != nil {
return nil
}
var allErrs field.ErrorList

// 1. Standby tenant must have a source
Expand All @@ -110,6 +114,63 @@ func (r *OBTenant) validateMutation() error {
}
}

// 3. Tenant restoring from OSS type Backup Data must have a OSSAccessSecret
if r.Spec.Source != nil && r.Spec.Source.Restore != nil {
res := r.Spec.Source.Restore

if res.ArchiveSource == nil && res.BakDataSource == nil && res.SourceUri == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore"), res, "Restore must have a source option, but both archiveSource, bakDataSource and sourceUri are nil now"))
}

if res.ArchiveSource != nil && res.ArchiveSource.Type == constants.BackupDestTypeOSS {
if res.ArchiveSource.OSSAccessSecret == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("archiveSource").Child("ossAccessSecret"), res.ArchiveSource.OSSAccessSecret, "Tenant restoring from OSS type backup data must have a OSSAccessSecret"))
}
secret := &v1.Secret{}
err := tenantClt.Get(context.Background(), types.NamespacedName{
Namespace: r.GetNamespace(),
Name: res.ArchiveSource.OSSAccessSecret,
}, secret)
if err != nil {
if apierrors.IsNotFound(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("archiveSource").Child("ossAccessSecret"), res.ArchiveSource.OSSAccessSecret, "Given OSSAccessSecret not found"))
}
allErrs = append(allErrs, field.InternalError(field.NewPath("spec").Child("source").Child("restore").Child("archiveSource").Child("ossAccessSecret"), err))
}

if _, ok := secret.Data["accessId"]; !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("archiveSource").Child("ossAccessSecret"), res.ArchiveSource.OSSAccessSecret, "accessId field not found in given OSSAccessSecret"))
}
if _, ok := secret.Data["accessKey"]; !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("archiveSource").Child("ossAccessSecret"), res.ArchiveSource.OSSAccessSecret, "accessKey field not found in given OSSAccessSecret"))
}
}

if res.BakDataSource != nil && res.BakDataSource.Type == constants.BackupDestTypeOSS {
if res.BakDataSource.OSSAccessSecret == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("bakDataSource").Child("ossAccessSecret"), res.BakDataSource.OSSAccessSecret, "Tenant restoring from OSS type backup data must have a OSSAccessSecret"))
}
secret := &v1.Secret{}
err := tenantClt.Get(context.Background(), types.NamespacedName{
Namespace: r.GetNamespace(),
Name: res.BakDataSource.OSSAccessSecret,
}, secret)
if err != nil {
if apierrors.IsNotFound(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("bakDataSource").Child("ossAccessSecret"), res.BakDataSource.OSSAccessSecret, "Given OSSAccessSecret not found"))
}
allErrs = append(allErrs, field.InternalError(field.NewPath("spec").Child("source").Child("restore").Child("bakDataSource").Child("ossAccessSecret"), err))
}

if _, ok := secret.Data["accessId"]; !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("bakDataSource").Child("ossAccessSecret"), res.BakDataSource.OSSAccessSecret, "accessId field not found in given OSSAccessSecret"))
}
if _, ok := secret.Data["accessKey"]; !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source").Child("restore").Child("bakDataSource").Child("ossAccessSecret"), res.BakDataSource.OSSAccessSecret, "accessKey field not found in given OSSAccessSecret"))
}
}
}

if len(allErrs) == 0 {
return nil
}
Expand Down
45 changes: 5 additions & 40 deletions api/v1alpha1/obtenantbackuppolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type OBTenantBackupPolicySpec struct {
// Important: Run "make" to regenerate code after modifying this file

ObClusterName string `json:"obClusterName"`
TenantName string `json:"tenantName"`
TenantSecret string `json:"tenantSecret"`
TenantName string `json:"tenantName"` // Name of obtenant resource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the tenant name in obcluster or cr's name, the previous implementation is tenant name in obcluster

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field had been changed to refer to name of CR in commit 6966330

TenantSecret string `json:"tenantSecret,omitempty"` // Deprecated
JobKeepWindow string `json:"jobKeepWindow,omitempty"`
Suspend bool `json:"suspend,omitempty"`
LogArchive LogArchiveConfig `json:"logArchive"`
Expand All @@ -47,6 +47,7 @@ type OBTenantBackupPolicyStatus struct {

NextFull string `json:"nextFull,omitempty"`
NextIncremental string `json:"nextIncremental,omitempty"`
TenantCR *OBTenant `json:"tenantCR,omitempty"`
TenantInfo *model.OBTenant `json:"tenantInfo,omitempty"`
LatestFullBackupJob *model.OBBackupJob `json:"latestFullBackupJob,omitempty"`
LatestIncrementalJob *model.OBBackupJob `json:"latestIncrementalJob,omitempty"`
Expand Down Expand Up @@ -97,11 +98,13 @@ func (in *OBTenantBackupPolicyStatus) DeepCopy() *OBTenantBackupPolicyStatus {
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.status`
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+kubebuilder:printcolumn:name="TenantName",type=string,JSONPath=`.spec.tenantName`
//+kubebuilder:printcolumn:name="NextFull",type=string,JSONPath=`.status.nextFull`
//+kubebuilder:printcolumn:name="NextIncremental",type=string,JSONPath=`.status.nextIncremental`
//+kubebuilder:printcolumn:name="FullCrontab",type=string,JSONPath=`.spec.dataBackup.fullCrontab`
//+kubebuilder:printcolumn:name="IncrementalCrontab",type=string,JSONPath=`.spec.dataBackup.incrementalCrontab`
//+kubebuilder:resource:shortName=obtbp

// OBTenantBackupPolicy is the Schema for the obtenantbackuppolicies API
type OBTenantBackupPolicy struct {
Expand Down Expand Up @@ -147,44 +150,6 @@ type CleanPolicy struct {
Disabled string `json:"disabled,omitempty"`
}

type JobOverview struct {
NextFull string `json:"nextFull,omitempty"`
NextIncremental string `json:"nextIncremental,omitempty"`
LatestBackupJob *model.OBBackupJob `json:"latestBackupJob,omitempty"`
LatestArchiveLogJob *model.OBArchiveLogJob `json:"latestArchiveLogJob,omitempty"`
LatestBackupCleanJob *model.OBBackupCleanJob `json:"latestBackupCleanJob,omitempty"`
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *JobOverview) DeepCopyInto(out *JobOverview) {
*out = *in
if in.LatestBackupJob != nil {
in, out := &in.LatestBackupJob, &out.LatestBackupJob
*out = new(model.OBBackupJob)
**out = **in
}
if in.LatestArchiveLogJob != nil {
in, out := &in.LatestArchiveLogJob, &out.LatestArchiveLogJob
*out = new(model.OBArchiveLogJob)
**out = **in
}
if in.LatestBackupCleanJob != nil {
in, out := &in.LatestBackupCleanJob, &out.LatestBackupCleanJob
*out = new(model.OBBackupCleanJob)
**out = **in
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new JobOverview.
func (in *JobOverview) DeepCopy() *JobOverview {
if in == nil {
return nil
}
out := new(JobOverview)
in.DeepCopyInto(out)
return out
}

func (in *OBTenantBackupPolicy) CopyStatus(out *OBTenantBackupPolicy) {
in.Status = out.Status
}
Loading