Skip to content

Commit

Permalink
chore: fix lint error, finish partial implementation of restore
Browse files Browse the repository at this point in the history
  • Loading branch information
powerfooI committed Sep 21, 2023
1 parent 9a535d6 commit 10644dc
Show file tree
Hide file tree
Showing 64 changed files with 1,003 additions and 341 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
4 changes: 4 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
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"
)
12 changes: 8 additions & 4 deletions api/v1alpha1/obtenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1alpha1

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

Expand Down Expand Up @@ -47,9 +46,14 @@ type OBTenantSpec struct {
Pools []ResourcePoolSpec `json:"pools"`

//+kubebuilder:default=PRIMARY
TenantRole constants.TenantRole `json:"tenantRole,omitempty"`
Source *TenantSourceSpec `json:"source,omitempty"`
Credentials []corev1.SecretReference `json:"credentials,omitempty"`
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
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
6 changes: 4 additions & 2 deletions api/v1alpha1/obtenantbackuppolicy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"regexp"

"github.com/oceanbase/ob-operator/api/constants"
"github.com/robfig/cron/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -29,10 +28,12 @@ import (
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 obtenantbackuppolicylog = logf.Log.WithName("obtenantbackuppolicy-resource")
var _ = logf.Log.WithName("obtenantbackuppolicy-resource")

func (r *OBTenantBackupPolicy) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
Expand Down Expand Up @@ -76,6 +77,7 @@ func (r *OBTenantBackupPolicy) ValidateCreate() (admission.Warnings, error) {

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *OBTenantBackupPolicy) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
_ = old
return nil, r.validateBackupPolicy()
}

Expand Down
5 changes: 2 additions & 3 deletions api/v1alpha1/obtenantoperation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1alpha1

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

"github.com/oceanbase/ob-operator/api/constants"
Expand Down Expand Up @@ -47,8 +46,8 @@ type OBTenantOpFailoverSpec struct {
}

type OBTenantOpChangePwdSpec struct {
Tenant string `json:"tenant"`
SecretRef corev1.SecretReference `json:"secretRef"`
Tenant string `json:"tenant"`
SecretRef string `json:"secretRef"`
}

// OBTenantOperationStatus defines the observed state of OBTenantOperation
Expand Down
25 changes: 5 additions & 20 deletions api/v1alpha1/obtenantrestore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ See the Mulan PSL v2 for more details.
package v1alpha1

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

"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 All @@ -37,9 +38,9 @@ type OBTenantRestoreSpec struct {
type OBTenantRestoreStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Status RestoreJobStatus `json:"status"`
RestoreProgress *model.RestoreHistory `json:"restoreProgress,omitempty"`
OperationContext *OperationContext `json:"operationContext,omitempty"`
Status constants.RestoreJobStatus `json:"status"`
RestoreProgress *model.RestoreHistory `json:"restoreProgress,omitempty"`
OperationContext *OperationContext `json:"operationContext,omitempty"`
}

func (in *OBTenantRestoreStatus) DeepCopyInto(out *OBTenantRestoreStatus) {
Expand Down Expand Up @@ -81,19 +82,3 @@ type OBTenantRestoreList struct {
func init() {
SchemeBuilder.Register(&OBTenantRestore{}, &OBTenantRestoreList{})
}

type RestoreJobType string

const (
RestoreJobRestore RestoreJobType = "RESTORE"
RestoreJobActivate RestoreJobType = "ACTIVATE"
)

type RestoreJobStatus string

const (
RestoreJobRunning RestoreJobStatus = "RUNNING"
RestoreJobFailed RestoreJobStatus = "FAILED"
RestoreJobSuccessful RestoreJobStatus = "SUCCESSFUL"
RestoreJobCanceled RestoreJobStatus = "CANCELED"
)
3 changes: 3 additions & 0 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ var _ = BeforeSuite(func() {
err = (&OBTenantBackupPolicy{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&OBTenant{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
32 changes: 26 additions & 6 deletions api/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "OBTenantOperation")
os.Exit(1)
}
if err = (&v1alpha1.OBTenant{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OBTenant")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Loading

0 comments on commit 10644dc

Please sign in to comment.