Skip to content

Commit

Permalink
Refactor tasks and flows in OBCluster package (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
powerfooI authored Mar 21, 2024
1 parent abf3b93 commit 496df7c
Show file tree
Hide file tree
Showing 68 changed files with 3,316 additions and 3,057 deletions.
9 changes: 5 additions & 4 deletions api/v1alpha1/obresourcerescue_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import (
var obresourcerescuelog = logf.Log.WithName("obresourcerescue-resource")

var rescueTypeMapping = map[string]struct{}{
"delete": {},
"reset": {},
"retry": {},
"skip": {},
"delete": {},
"reset": {},
"retry": {},
"skip": {},
"ignore-deletion": {},
}

func (r *OBResourceRescue) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down
102 changes: 102 additions & 0 deletions cmd/generator/task/task-register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
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 main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"strings"
"text/template"
)

const genTemplate = `// Code generated by go generate; DO NOT EDIT.
package {{.PackageName}}
func init() {
{{- range .Tasks }}
taskMap.Register(t{{.}}, {{.}})
{{- end }}
}
`

type Task string

func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s <source_file>", os.Args[0])
}
sourceFile := os.Args[1]

fset := token.NewFileSet()
node, err := parser.ParseFile(fset, sourceFile, nil, 0)
if err != nil {
log.Fatalf("Failed to parse source file: %v", err)
}

taskFuncs := []Task{}
ast.Inspect(node, func(n ast.Node) bool {
fn, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
// Get return type of function and check whether it is a func(resource T) TaskError
if len(fn.Type.Params.List) == 1 && len(fn.Type.Results.List) == 1 {
if strings.HasSuffix(exprToString(fn.Type.Results.List[0].Type), "TaskError") {
taskFuncs = append(taskFuncs, Task(fn.Name.Name))
}
}

return true
})

tmpl, err := template.New("registration").Parse(genTemplate)
if err != nil {
log.Fatalf("Failed to parse template: %v", err)
}

outputFile := sourceFile[:len(sourceFile)-3] + "_gen.go"

f, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Failed to create output file: %v", err)
}
defer f.Close()

err = tmpl.Execute(f, struct {
PackageName string
Tasks []Task
}{
PackageName: node.Name.Name,
Tasks: taskFuncs,
})
if err != nil {
log.Printf("Failed to execute template: %v", err)
}
}

func exprToString(expr ast.Expr) string {
switch e := expr.(type) {
case *ast.Ident:
return e.Name
case *ast.SelectorExpr:
return exprToString(e.X) + "." + e.Sel.Name
case *ast.StarExpr:
return "*" + exprToString(e.X)
default:
return fmt.Sprintf("unknown(%T)", e)
}
}
36 changes: 36 additions & 0 deletions internal/const/oceanbase/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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 oceanbase

const (
AnnotationCalicoValidate = "cni.projectcalico.org/podIP"
AnnotationCalicoIpAddrs = "cni.projectcalico.org/ipAddrs"
)

const (
AnnotationsIndependentPVCLifecycle = "oceanbase.oceanbase.com/independent-pvc-lifecycle"
AnnotationsSinglePVC = "oceanbase.oceanbase.com/single-pvc"
AnnotationsMode = "oceanbase.oceanbase.com/mode"
AnnotationsSourceClusterAddress = "oceanbase.oceanbase.com/source-cluster-address"
AnnotationsIgnoreDeletion = "oceanbase.oceanbase.com/ignore-deletion"
)

const (
ModeStandalone = "standalone"
ModeService = "service"
)

const (
CNICalico = "calico"
CNIUnknown = "unknown"
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

package obtenantrestore
package oceanbase

import (
"github.com/oceanbase/ob-operator/pkg/task"
const (
LabelRefOBCluster = "ref-obcluster"
LabelRefOBZone = "ref-obzone"
LabelRefOBServer = "ref-observer"
LabelRefUID = "ref-uid"
LabelJobName = "job-name"
LabelRefBackupPolicy = "ref-backuppolicy"
)

func init() {
// tenant-level restore
task.GetRegistry().Register(fStartRestoreFlow, StartRestoreJob)
task.GetRegistry().Register(fRestoreAsPrimaryFlow, RestoreAsPrimary)
task.GetRegistry().Register(fRestoreAsStandbyFlow, RestoreAsStandby)
}
const (
LabelTenantName = "oceanbase.oceanbase.com/tenant-name"
LabelSecondaryTenant = "oceanbase.oceanbase.com/secondary-tenant"
LabelBackupType = "oceanbase.oceanbase.com/backup-type"
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

package obparameter
package oceanbase

import (
"github.com/oceanbase/ob-operator/pkg/task"
const (
LogLevelDefault = 0
LogLevelInfo = 0
LogLevelDebug = 1
LogLevelTrace = 2
)

func init() {
// obparameter
task.GetRegistry().Register(fSetOBParameter, SetOBParameter)
}
101 changes: 0 additions & 101 deletions internal/const/oceanbase/oceanbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,9 @@ See the Mulan PSL v2 for more details.

package oceanbase

import "k8s.io/apimachinery/pkg/api/resource"

var UpgradeEssentialParameters = [...]string{"server_permanent_offline_time", "enable_rebalance", "enable_rereplication"}
var ReservedParameters = [...]string{"cpu_count", "datafile_size", "log_disk_size", "enable_syslog_recycle", "max_syslog_file_count"}

const (
BootstrapTimeoutSeconds = 300
LocalityChangeTimeoutSeconds = 3600
DefaultStateWaitTimeout = 300
TimeConsumingStateWaitTimeout = 3600
ServerDeleteTimeoutSeconds = 86400
GigaConverter = 1 << 30
MegaConverter = 1 << 20
)

const (
DefaultDiskExpandPercent = 10
DefaultLogPercent = 80
InitialDataDiskUsePercent = 20
DefaultDiskUsePercent = 95
DefaultMemoryLimitPercent = 90
)

const (
DefaultMemoryLimitSize = "0M"
DefaultDatafileMaxSize = "0M"
DefaultDatafileNextSize = "1G"
)

var (
MinMemorySize = resource.MustParse("8Gi")
MinDataDiskSize = resource.MustParse("30Gi")
MinRedoLogDiskSize = resource.MustParse("30Gi")
MinLogDiskSize = resource.MustParse("10Gi")
)

const (
SqlPort = 2881
RpcPort = 2882
Expand All @@ -58,38 +25,6 @@ const (
RpcPortName = "rpc"
)

const (
ProbeCheckPeriodSeconds = 2
ProbeCheckDelaySeconds = 5
GetConnectionMaxRetries = 10
CheckConnectionInterval = 3
CheckJobInterval = 3
CheckJobMaxRetries = 100
CommonCheckInterval = 5
)

const (
AnnotationCalicoValidate = "cni.projectcalico.org/podIP"
AnnotationCalicoIpAddrs = "cni.projectcalico.org/ipAddrs"
)

const (
AnnotationsIndependentPVCLifecycle = "oceanbase.oceanbase.com/independent-pvc-lifecycle"
AnnotationsSinglePVC = "oceanbase.oceanbase.com/single-pvc"
AnnotationsMode = "oceanbase.oceanbase.com/mode"
AnnotationsSourceClusterAddress = "oceanbase.oceanbase.com/source-cluster-address"
)

const (
ModeStandalone = "standalone"
ModeService = "service"
)

const (
CNICalico = "calico"
CNIUnknown = "unknown"
)

const (
ContainerName = "observer"
InstallPath = "/home/admin/oceanbase"
Expand Down Expand Up @@ -127,15 +62,6 @@ const (
DefaultRegion = "default"
)

const (
LabelRefOBCluster = "ref-obcluster"
LabelRefOBZone = "ref-obzone"
LabelRefOBServer = "ref-observer"
LabelRefUID = "ref-uid"
LabelJobName = "job-name"
LabelRefBackupPolicy = "ref-backuppolicy"
)

const (
OBServerVersionKey = "observer-version"
EssentialParametersKey = "essential-parameters"
Expand All @@ -146,37 +72,10 @@ const (
SelectPrivilege = "select"
)

const (
LabelTenantName = "oceanbase.oceanbase.com/tenant-name"
LabelSecondaryTenant = "oceanbase.oceanbase.com/secondary-tenant"
LabelBackupType = "oceanbase.oceanbase.com/backup-type"
)

const (
OceanbaseAllScope = "oceanbase.*"
)

const (
TenantOpRetryTimes = 9
TenantOpRetryGapSeconds = 9
)

const (
TaskMaxRetryTimes = 99
TaskRetryBackoffThreshold = 16
)

const (
LogLevelDefault = 0
LogLevelInfo = 0
LogLevelDebug = 1
LogLevelTrace = 2
)

const (
TolerateServerPodNotReadyMinutes = 5
)

const (
ClusterNameParam = "cluster"
ClusterIdParam = "cluster_id"
Expand Down
38 changes: 38 additions & 0 deletions internal/const/oceanbase/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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 oceanbase

import "k8s.io/apimachinery/pkg/api/resource"

const (
DefaultDiskExpandPercent = 10
DefaultLogPercent = 80
InitialDataDiskUsePercent = 20
DefaultDiskUsePercent = 95
DefaultMemoryLimitPercent = 90
GigaConverter = 1 << 30
MegaConverter = 1 << 20
)

const (
DefaultMemoryLimitSize = "0M"
DefaultDatafileMaxSize = "0M"
DefaultDatafileNextSize = "1G"
)

var (
MinMemorySize = resource.MustParse("8Gi")
MinDataDiskSize = resource.MustParse("30Gi")
MinRedoLogDiskSize = resource.MustParse("30Gi")
MinLogDiskSize = resource.MustParse("10Gi")
)
Loading

0 comments on commit 496df7c

Please sign in to comment.