Skip to content

Commit

Permalink
Add cronjob for readreplica rotator (#2242)
Browse files Browse the repository at this point in the history
* Implement sample read replica rotation logic

* Add external-snapshotter in go.mod

* Add readreplica initial implementation

* Add service implementation of readreplica rotator

* Add readreplica rotate config

* Refactor to read labels

* replace id to _MY_TARGET_REPLICA_ID_

* Add snapshot k8s client

* Format

* Fix snapshot client initialization

* Add Apache License to main.go

* Use GetConfigOrDie

* Use internal/k8s client

* Refactor

* Use controller-runtime for CRUD in readreplica

* Remove snapshot client and use controller-runtime

* Move LabelSelector into internal/k8s

* Report error to span

* Update go modules

* Remove old example

* Refactor

* Disable exhaustruct for now

* Fix predeclared

* Fix stylecheck

* nolint:gomnd

* Add test template

* Add test for getNewBaseName

* Add readreplica-rotate build scripts

* Add service account and clusterroles for read replica rotator

* Update charts

* Update comment

* update charts

* Update docker image name in Makefile.d/docker.mk

* Fix scheme

* Update docs

* Fix comment

* style: format code with Gofumpt and Prettier

This commit fixes the style issues introduced in 6dba952 according to the output
from Gofumpt and Prettier.

Details: #2242

* Fix indent

* Add network policy for readreplica rotator

* Update README.md

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
ykadowak and deepsource-autofix[bot] authored Dec 6, 2023
1 parent bf77201 commit 4fddfb0
Show file tree
Hide file tree
Showing 16 changed files with 3,131 additions and 772 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/dockers-readreplica-rotate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# Copyright (C) 2019-2023 vdaas.org vald team <[email protected]>
#
# 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
#
# https://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.
#
name: "Build docker image: readreplica-rotate"
on:
push:
branches:
- main
tags:
- "*.*.*"
- "v*.*.*"
- "*.*.*-*"
- "v*.*.*-*"
paths:
- ".github/actions/docker-build/actions.yaml"
- ".github/workflows/dockers-readreplica-rotate.yml"
- "go.mod"
- "go.sum"
- "internal/**"
- "!internal/**/*_test.go"
- "!internal/db/**"
- "!internal/k8s/**"
- "apis/grpc/**"
- "pkg/index/job/readreplica/rotate/**"
- "cmd/index/job/readreplica/rotate/**"
- "dockers/index/job/readreplica/rotate/Dockerfile"
- "versions/GO_VERSION"
pull_request:
paths:
- ".github/actions/docker-build/actions.yaml"
- ".github/workflows/_docker-image.yaml"
- ".github/workflows/dockers-readreplica-rotate.yml"
- "go.mod"
- "go.sum"
- "internal/**"
- "!internal/**/*_test.go"
- "!internal/db/**"
- "!internal/k8s/**"
- "apis/grpc/**"
- "pkg/index/job/readreplica/rotate/**"
- "cmd/index/job/readreplica/rotate/**"
- "dockers/index/job/readreplica/rotate/Dockerfile"
- "versions/GO_VERSION"
pull_request_target:
paths:
- ".github/actions/docker-build/actions.yaml"
- ".github/workflows/_docker-image.yaml"
- ".github/workflows/dockers-readreplica-rotate.yml"
- "go.mod"
- "go.sum"
- "internal/**"
- "!internal/**/*_test.go"
- "!internal/db/**"
- "!internal/k8s/**"
- "apis/grpc/**"
- "pkg/index/job/readreplica/rotate/**"
- "cmd/index/job/readreplica/rotate/**"
- "dockers/index/job/readreplica/rotate/Dockerfile"
- "versions/GO_VERSION"

jobs:
build:
uses: ./.github/workflows/_docker-image.yaml
with:
target: readreplica-rotate
secrets: inherit
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ LOADTEST_IMAGE = $(NAME)-loadtest
INDEX_CORRECTION_IMAGE = $(NAME)-index-correction
INDEX_CREATION_IMAGE = $(NAME)-index-creation
INDEX_SAVE_IMAGE = $(NAME)-index-save
READREPLICA_ROTATE_IMAGE = $(NAME)-readreplica-rotate
MANAGER_INDEX_IMAGE = $(NAME)-manager-index
MAINTAINER = "$(ORG).org $(NAME) team <$(NAME)@$(ORG).org>"

Expand Down
29 changes: 29 additions & 0 deletions Makefile.d/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,35 @@ cmd/index/job/save/index-save: \
$(dir $@)main.go
$@ -version

cmd/index/job/readreplica/rotate/readreplica-rotate: \
$(GO_SOURCES_INTERNAL) \
$(PBGOS) \
$(shell find $(ROOTDIR)/cmd/index/job/readreplica/rotate -type f -name '*.go' -not -name '*_test.go' -not -name 'doc.go') \
$(shell find $(ROOTDIR)/pkg/index/job/readreplica/rotate -type f -name '*.go' -not -name '*_test.go' -not -name 'doc.go')
$(eval CGO_ENABLED = 0)
CGO_ENABLED=$(CGO_ENABLED) \
GO111MODULE=on \
GOPRIVATE=$(GOPRIVATE) \
go build \
--ldflags "-w -extldflags=-static \
-X '$(GOPKG)/internal/info.Version=$(VERSION)' \
-X '$(GOPKG)/internal/info.GitCommit=$(GIT_COMMIT)' \
-X '$(GOPKG)/internal/info.BuildTime=$(DATETIME)' \
-X '$(GOPKG)/internal/info.GoVersion=$(GO_VERSION)' \
-X '$(GOPKG)/internal/info.GoOS=$(GOOS)' \
-X '$(GOPKG)/internal/info.GoArch=$(GOARCH)' \
-X '$(GOPKG)/internal/info.CGOEnabled=$(CGO_ENABLED)' \
-X '$(GOPKG)/internal/info.BuildCPUInfoFlags=$(CPU_INFO_FLAGS)' \
-buildid=" \
-mod=readonly \
-modcacherw \
-a \
-tags "osusergo netgo static_build" \
-trimpath \
-o $@ \
$(dir $@)main.go
$@ -version

.PHONY: binary/build/zip
## build all binaries and zip them
binary/build/zip: \
Expand Down
14 changes: 14 additions & 0 deletions Makefile.d/docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,17 @@ docker/build/index-save:
-t $(ORG)/$(INDEX_SAVE_IMAGE):$(TAG) . \
--build-arg MAINTAINER=$(MAINTAINER) \
--build-arg GO_VERSION=$(GO_VERSION)

.PHONY: docker/name/readreplica-rotate
docker/name/readreplica-rotate:
@echo "$(ORG)/$(READREPLICA_ROTATE_IMAGE)"

.PHONY: docker/build/readreplica-rotate
## build readreplica-rotate image
docker/build/readreplica-rotate:
$(DOCKER) build \
$(DOCKER_OPTS) \
-f dockers/index/job/readreplica/rotate/Dockerfile \
-t $(ORG)/$(READREPLICA_ROTATE_IMAGE):$(TAG) . \
--build-arg MAINTAINER=$(MAINTAINER) \
--build-arg GO_VERSION=$(GO_VERSION)
8 changes: 8 additions & 0 deletions Makefile.d/k8s.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ k8s/manifest/update: \
mkdir -p k8s/gateway
mkdir -p k8s/manager
mkdir -p k8s/index/job
mkdir -p k8s/index/job/readreplica
mv $(TEMP_DIR)/vald/templates/agent k8s/agent
mv $(TEMP_DIR)/vald/templates/discoverer k8s/discoverer
mv $(TEMP_DIR)/vald/templates/gateway/lb k8s/gateway/lb
mv $(TEMP_DIR)/vald/templates/manager/index k8s/manager/index
mv $(TEMP_DIR)/vald/templates/index/job/correction k8s/index/job/correction
mv $(TEMP_DIR)/vald/templates/index/job/creation k8s/index/job/creation
mv $(TEMP_DIR)/vald/templates/index/job/save k8s/index/job/save
mv $(TEMP_DIR)/vald/templates/index/job/readreplica/rotate k8s/index/job/readreplica/rotate
rm -rf $(TEMP_DIR)

.PHONY: k8s/manifest/helm-operator/clean
Expand Down Expand Up @@ -87,6 +89,9 @@ k8s/vald/deploy:
kubectl apply -f $(TEMP_DIR)/vald/templates/discoverer || true
kubectl apply -f $(TEMP_DIR)/vald/templates/gateway/lb || true
kubectl apply -f $(TEMP_DIR)/vald/templates/index/job/correction || true
kubectl apply -f $(TEMP_DIR)/vald/templates/index/job/creation || true
kubectl apply -f $(TEMP_DIR)/vald/templates/index/job/save || true
kubectl apply -f $(TEMP_DIR)/vald/templates/index/job/readreplica/rotate || true
rm -rf $(TEMP_DIR)
kubectl get pods -o jsonpath="{.items[*].spec.containers[*].image}" | tr " " "\n"

Expand All @@ -104,6 +109,9 @@ k8s/vald/delete:
--set manager.index.image.repository=$(CRORG)/$(MANAGER_INDEX_IMAGE) \
--output-dir $(TEMP_DIR) \
charts/vald
kubectl delete -f $(TEMP_DIR)/vald/templates/index/job/readreplica/rotate
kubectl delete -f $(TEMP_DIR)/vald/templates/index/job/save
kubectl delete -f $(TEMP_DIR)/vald/templates/index/job/creation
kubectl delete -f $(TEMP_DIR)/vald/templates/index/job/correction
kubectl delete -f $(TEMP_DIR)/vald/templates/gateway/lb
kubectl delete -f $(TEMP_DIR)/vald/templates/manager/index
Expand Down
Loading

0 comments on commit 4fddfb0

Please sign in to comment.