Skip to content

Commit

Permalink
Tests for tenant restore progress; definition of API types; Fix lints (
Browse files Browse the repository at this point in the history
…#68)

tenant restore api definition, optimize lints
  • Loading branch information
powerfooI authored Sep 21, 2023
1 parent 10074b7 commit 0624ec5
Show file tree
Hide file tree
Showing 82 changed files with 2,464 additions and 494 deletions.
14 changes: 12 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ linters:
- gocritic
- gofmt
- goimports
- gosimple
# - gosimple
- govet
- ineffassign
- misspell
Expand All @@ -16,7 +16,7 @@ linters:
- sqlclosecheck
- staticcheck
- typecheck
- unused
# - unused

issues:
exclude-rules:
Expand All @@ -35,6 +35,16 @@ linters-settings:
revive:
enable-all-rules: true
rules:
- name: struct-tag
disabled: true
- name: var-naming
disabled: true
- name: comment-spacings
disabled: true
- name: exported
disabled: true
- name: unused-receiver
disabled: true
- name: file-header
disabled: true
- name: line-length-limit
Expand Down
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ENVTEST_K8S_VERSION = 1.26.1
YQ_DOWNLOAD_URL = https://github.com/mikefarah/yq/releases/download/v4.35.1/yq_linux_amd64
SEMVER_DOWNLOAD_URL = https://raw.githubusercontent.com/fsaintjacques/semver-tool/master/src/semver

GOLANG_CI_VERSION ?= v1.54.2

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down Expand Up @@ -188,3 +190,19 @@ $(ENVTEST): $(LOCALBIN)

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

.PHONY: GOLANGCI_LINT
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
$(GOLANG_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
13 changes: 13 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ resources:
kind: OBTenant
path: github.com/oceanbase/ob-operator/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down Expand Up @@ -123,4 +127,13 @@ resources:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: oceanbase.com
group: oceanbase
kind: OBTenantOperation
path: github.com/oceanbase/ob-operator/api/v1alpha1
version: v1alpha1
version: "3"
24 changes: 24 additions & 0 deletions api/constants/restore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright (c) 2023 OceanBase
ob-operator is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

package constants

type RestoreJobStatus string

const (
RestoreJobStarting RestoreJobStatus = "STARTING"
RestoreJobRunning RestoreJobStatus = "RUNNING"
RestoreJobFailed RestoreJobStatus = "FAILED"
RestoreJobCanceling RestoreJobStatus = "CANCELING"
RestoreJobSuccessful RestoreJobStatus = "SUCCESSFUL"
RestoreJobCanceled RestoreJobStatus = "CANCELED"
)
37 changes: 37 additions & 0 deletions api/constants/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright (c) 2023 OceanBase
ob-operator is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

package constants

type TenantRole string

const (
TenantRolePrimary TenantRole = "PRIMARY"
TenantRoleStandby TenantRole = "STANDBY"
)

type TenantOperationType string

const (
TenantOpSwitchover TenantOperationType = "SWITCHOVER"
TenantOpFailover TenantOperationType = "FAILOVER"
TenantOpChangePwd TenantOperationType = "CHANGE_PASSWORD"
)

type TenantOperationStatus string

const (
TenantOpStarting TenantOperationStatus = "STARTING"
TenantOpRunning TenantOperationStatus = "RUNNING"
TenantOpSuccessful TenantOperationStatus = "SUCCESSFUL"
TenantOpFailed TenantOperationStatus = "FAILED"
)
76 changes: 76 additions & 0 deletions api/v1alpha1/obtenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package v1alpha1
import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/oceanbase/ob-operator/api/constants"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -42,6 +44,35 @@ type OBTenantSpec struct {
ConnectWhiteList string `json:"connectWhiteList,omitempty"`

Pools []ResourcePoolSpec `json:"pools"`

//+kubebuilder:default=PRIMARY
TenantRole constants.TenantRole `json:"tenantRole,omitempty"`
Source *TenantSourceSpec `json:"source,omitempty"`
Credentials TenantCredentials `json:"credentials,omitempty"`
}

type TenantCredentials struct {
Root string `json:"root,omitempty"`
StandbyRO string `json:"standbyRo,omitempty"`
}

// Source for restoring or creating standby
type TenantSourceSpec struct {
Tenant *string `json:"tenant,omitempty"`
Restore *RestoreSourceSpec `json:"restore,omitempty"`
}

type RestoreSourceSpec struct {
SourceUri string `json:"sourceUri"`
Until RestoreUntilConfig `json:"until"`
Description *string `json:"description,omitempty"`
ReplayLogUntil *RestoreUntilConfig `json:"replayLogUntil,omitempty"`
}

type RestoreUntilConfig struct {
Timestamp *string `json:"timestamp,omitempty"`
Scn *string `json:"scn,omitempty"`
Unlimited bool `json:"unlimited,omitempty"`
}

type ResourcePoolSpec struct {
Expand Down Expand Up @@ -71,6 +102,7 @@ type UnitConfig struct {
LogDiskSize resource.Quantity `json:"logDiskSize,omitempty"`
}

// +kubebuilder:object:generate=false
// OBTenantStatus defines the observed state of OBTenant
type OBTenantStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand All @@ -79,6 +111,50 @@ type OBTenantStatus struct {
Pools []ResourcePoolStatus `json:"resourcePool"`
OperationContext *OperationContext `json:"operationContext,omitempty"`
TenantRecordInfo TenantRecordInfo `json:"tenantRecordInfo,omitempty"`

TenantRole constants.TenantRole `json:"tenantRole,omitempty"`
Source *TenantSourceStatus `json:"source,omitempty"`
}

type TenantSourceStatus struct {
Tenant *string `json:"tenant,omitempty"`
Restore *OBTenantRestoreStatus `json:"restore,omitempty"`
}

func (in *OBTenantStatus) DeepCopyInto(out *OBTenantStatus) {
*out = *in
if in.Pools != nil {
in, out := &in.Pools, &out.Pools
*out = make([]ResourcePoolStatus, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.OperationContext != nil {
in, out := &in.OperationContext, &out.OperationContext
*out = new(OperationContext)
(*in).DeepCopyInto(*out)
}
out.TenantRecordInfo = in.TenantRecordInfo
if in.Source != nil {
in, out := &in.Source, &out.Source
*out = new(TenantSourceStatus)
(*in).DeepCopyInto(*out)
}
}

func (in *TenantSourceStatus) DeepCopyInto(out *TenantSourceStatus) {
*out = *in
if in.Tenant != nil {
in, out := &in.Tenant, &out.Tenant
*out = new(string)
**out = **in
}
if in.Restore != nil {
in, out := &in.Restore, &out.Restore
*out = new(OBTenantRestoreStatus)
**out = **in
}
}

type ResourcePoolStatus struct {
Expand Down
91 changes: 91 additions & 0 deletions api/v1alpha1/obtenant_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2023.
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 (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/oceanbase/ob-operator/api/constants"
)

// log is for logging in this package.
var _ = logf.Log.WithName("obtenant-resource")

func (r *OBTenant) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

//+kubebuilder:webhook:path=/mutate-oceanbase-oceanbase-com-v1alpha1-obtenant,mutating=true,failurePolicy=fail,sideEffects=None,groups=oceanbase.oceanbase.com,resources=obtenants,verbs=create;update,versions=v1alpha1,name=mobtenant.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &OBTenant{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *OBTenant) Default() {
if r.Spec.TenantRole == "" {
r.Spec.TenantRole = constants.TenantRolePrimary
}
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-oceanbase-oceanbase-com-v1alpha1-obtenant,mutating=false,failurePolicy=fail,sideEffects=None,groups=oceanbase.oceanbase.com,resources=obtenants,verbs=create;update,versions=v1alpha1,name=vobtenant.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &OBTenant{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *OBTenant) ValidateCreate() (admission.Warnings, error) {
// TODO(user): fill in your validation logic upon object creation.
return nil, r.validateMutation()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *OBTenant) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
_ = old
// TODO(user): fill in your validation logic upon object update.
return nil, r.validateMutation()
}

func (r *OBTenant) validateMutation() error {
var allErrs field.ErrorList

// 1. Standby tenant must have a source
if r.Spec.TenantRole == constants.TenantRoleStandby {
if r.Spec.Source == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("source"), r.Spec.Source, "Standby tenant must have non-nil source field"))
} else if r.Spec.Source.Restore == nil && r.Spec.Source.Tenant == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("tenantRole"), r.Spec.TenantRole, "Standby must have a source option, but both restore and tenantRef are nil now"))
}
}

if len(allErrs) == 0 {
return nil
}
return apierrors.NewInvalid(GroupVersion.WithKind("OBTenant").GroupKind(), r.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *OBTenant) ValidateDelete() (admission.Warnings, error) {
// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
}
3 changes: 2 additions & 1 deletion api/v1alpha1/obtenantbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ See the Mulan PSL v2 for more details.
package v1alpha1

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

constants "github.com/oceanbase/ob-operator/api/constants"
"github.com/oceanbase/ob-operator/pkg/oceanbase/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/obtenantbackuppolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ See the Mulan PSL v2 for more details.
package v1alpha1

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

constants "github.com/oceanbase/ob-operator/api/constants"
"github.com/oceanbase/ob-operator/pkg/oceanbase/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down
Loading

0 comments on commit 0624ec5

Please sign in to comment.