Skip to content

リファクタリング #11

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

Merged
merged 7 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.20"
go-version: "1.24.1"

- name: Test
run: go test -v ./...
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bin/sakura-controller
bin/db-controller
1 change: 0 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
# some cases, their employer may be the copyright holder. To see the full list
# of contributors, see the revision history in source control.

Yamato Sugawara <[email protected]>
Shuichi Ohkubo <[email protected]>
15 changes: 6 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.PHONY: all
all: format test vet

.PHONY: sakura-all
sakura-all: all sakura-build
all: format test vet build

.PHONY: format
format:
Expand All @@ -19,18 +16,18 @@ ci: format test vet
vet:
go vet ./...

.PHONY: sakura-build
sakura-build:
go build -o bin/sakura-controller ./cmd/sakura-controller
.PHONY: build
build:
go build -o bin/db-controller ./cmd/db-controller

.PHONY: check-license
check-license:
go-licenses check ./cmd/sakura-controller
go-licenses check ./cmd/db-controller

.PHONY: add-license
add-license:
addlicense -c "The distributed-mariadb-controller Authors" .
go-licenses check ./cmd/sakura-controller
go-licenses check ./cmd/db-controller

.PHONY: tool
tool:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The distributed-mariadb-controller Authors
// Copyright 2025 The distributed-mariadb-controller Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,15 +19,14 @@ import (

"github.com/labstack/echo/v4"
"github.com/sakura-internet/distributed-mariadb-controller/pkg/controller"
"github.com/sakura-internet/distributed-mariadb-controller/pkg/controller/sakura"
)

const (
controllerStateCtxKey = "controllerState"
)

// UseControllerState is an echo middleware that injects the current state of the db-controller into othe request context.
func UseControllerState(ctrler *sakura.SAKURAController) func(echo.HandlerFunc) echo.HandlerFunc {
func UseControllerState(ctrler *controller.Controller) func(echo.HandlerFunc) echo.HandlerFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set(controllerStateCtxKey, ctrler.GetState())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The distributed-mariadb-controller Authors
// Copyright 2025 The distributed-mariadb-controller Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The distributed-mariadb-controller Authors
// Copyright 2025 The distributed-mariadb-controller Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The distributed-mariadb-controller Authors
// Copyright 2025 The distributed-mariadb-controller Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
91 changes: 91 additions & 0 deletions cmd/db-controller/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2025 The distributed-mariadb-controller Authors
//
// 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 main

import (
"flag"
"fmt"
)

var (
// logLevelFlag is a cli-flag that specifies the log level on the db-controller.
logLevelFlag string
// lockFilePathFlag is a cli-flag that specifies the filepath of the exclusive lock.
lockFilePathFlag string
// dbServingPortFlag is a cli-flag that specifies portnumber of database service
dbServingPortFlag int
// dbReplicaUserNameFlag is a cli-flag that specifies the username for replication
dbReplicaUserNameFlag string
// dbReplicaPasswordFilePathFlag is a cli-flag that specifies the filepath of the DB replica password.
dbReplicaPasswordFilePathFlag string
// globalInterfaceNameFlag is a cli-flag that specifies the global network interface for get my IPaddress.
globalInterfaceNameFlag string
// chainNameForDBAclFlag is a cli-flag that specifies the nftables chain name for DB access control list.
chainNameForDBAclFlag string

// mainPollingSpanSecondFlag is a cli-flag that specifies the span seconds of the loop in main.go.
mainPollingSpanSecondFlag int
// httpAPIServerPortFlag is a cli-flag that specifies the port the HTTP API server listens.
httpAPIServerPortFlag int
// prometheusExporterPortFlag is a cli-flag that specifies the port the prometheus exporter listens.
prometheusExporterPortFlag int
// dbReplicaSourcePortFlag is a cli-flag that specifies the port of primary as replication source.
dbReplicaSourcePortFlag int

// enablePrometheusExporterFlag is a cli-flag that enables the prometheus exporter.
enablePrometheusExporterFlag bool
// enableHTTPAPIFlag is a cli-flag that enables the http api server.
enableHTTPAPIFlag bool
)

// ParseAllFlags parses all defined cmd-flags.
func parseAllFlags(args []string) error {
fs := flag.NewFlagSet("db-controller", flag.PanicOnError)

fs.StringVar(&logLevelFlag, "log-level", "warning", "the log level(debug/info/warning/error)")
fs.StringVar(&lockFilePathFlag, "lock-filepath", "/var/run/db-controller/lock", "the filepath of the exclusive lock")
fs.StringVar(&dbReplicaPasswordFilePathFlag, "db-repilica-password-filepath", "/var/run/db-controller/.db-replica-password", "the filepath of the DB replica password")
fs.StringVar(&globalInterfaceNameFlag, "global-interface-name", "eth0", "the interface name of global")
fs.StringVar(&chainNameForDBAclFlag, "chain-name-for-db-acl", "mariadb", "the chain name for DB access control")
fs.StringVar(&dbReplicaUserNameFlag, "db-replica-user-name", "repl", "the username for replication")

fs.IntVar(&mainPollingSpanSecondFlag, "main-polling-span-second", 4, "the span seconds of the loop in main.go")
fs.IntVar(&httpAPIServerPortFlag, "http-api-server-port", 54545, "the port the http api server listens")
fs.IntVar(&prometheusExporterPortFlag, "prometheus-exporter-port", 50505, "the port the prometheus exporter listens")
fs.IntVar(&dbReplicaSourcePortFlag, "db-replica-source-port", 13306, "the port of primary as replication source")
fs.IntVar(&dbServingPortFlag, "db-serving-port", 3306, "the port of database service")

fs.BoolVar(&enablePrometheusExporterFlag, "prometheus-exporter", true, "enables the prometheus exporter")
fs.BoolVar(&enableHTTPAPIFlag, "http-api", true, "enables the http api server")

return fs.Parse(args)
}

// ValidateAllFlags validates all cmd flags.
func validateAllFlags() error {
if !isValidLogLevelFlag(logLevelFlag) {
return fmt.Errorf("--log-level must be one of debug/info/warning/error")
}

if prometheusExporterPortFlag < 0 || 65535 < prometheusExporterPortFlag {
return fmt.Errorf("--prometheus-exporter-port must be the range of uint16(tcp port)")
}

return nil
}

func isValidLogLevelFlag(l string) bool {
return l == "debug" || l == "info" || l == "warning" || l == "error"
}
Loading