Skip to content

Commit 2dc570f

Browse files
authored
feat(syncer): add resource syncer (#6595)
1 parent 37be4e9 commit 2dc570f

10 files changed

Lines changed: 818 additions & 2 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ MOCK_BOILERPLATE_FILE = $(ROOT)/hack/boilerplate/boilerplate.txt
2727
KUBE_OPT = -n tidb-admin --context kind-tidb-operator
2828
GO_TOOL_BIN = register-gen deepcopy-gen controller-gen mockgen golangci-lint license-eye mdtoc helm kind ginkgo kubectl
2929

30-
ALL_CMD = tidb-operator prestop-checker testing-workload tidb-backup-manager
30+
ALL_CMD = tidb-operator prestop-checker testing-workload tidb-backup-manager resource-syncer
3131
.PHONY: build
3232
build: $(addprefix build/,$(ALL_CMD))
3333
build/%:

cmd/resource-syncer/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Resource Syncer
2+
3+
This binary is designed for syncing resources(secrets, configmap, etc...) of kubernetes into pods

cmd/resource-syncer/main.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"flag"
19+
"fmt"
20+
"os"
21+
"path/filepath"
22+
23+
"go.uber.org/zap/zapcore"
24+
"k8s.io/apimachinery/pkg/labels"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/cache"
27+
"sigs.k8s.io/controller-runtime/pkg/healthz"
28+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
29+
30+
"github.com/spf13/afero"
31+
"github.com/spf13/pflag"
32+
33+
"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
34+
"github.com/pingcap/tidb-operator/v2/pkg/controllers/resourcesyncer/secret"
35+
)
36+
37+
var setupLog = ctrl.Log.WithName("setup")
38+
39+
type Config struct {
40+
Namespace string
41+
Labels map[string]string
42+
BaseDir string
43+
SecretDirName string
44+
45+
ProbeAddr string
46+
}
47+
48+
func (c *Config) AddFlags(fs *pflag.FlagSet) {
49+
fs.StringVarP(&c.Namespace, "namespace", "n", "default", "namespace of syncer")
50+
fs.StringVarP(&c.BaseDir, "base-dir", "d", "", "base dir of data")
51+
fs.StringVar(&c.SecretDirName, "secret-dir-name", "secrets", "dir name of secret data")
52+
fs.StringToStringVarP(&c.Labels, "labels", "l", map[string]string{
53+
v1alpha1.LabelKeyManagedBy: v1alpha1.LabelValManagedByOperator,
54+
}, "labels of secrets")
55+
56+
fs.StringVar(&c.ProbeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
57+
}
58+
59+
func main() {
60+
opts := zap.Options{
61+
Development: false,
62+
StacktraceLevel: zapcore.PanicLevel, // stacktrace on panic only
63+
// use console encoder now for development, switch to json if needed later
64+
Encoder: zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
65+
TimeKey: "T",
66+
LevelKey: "L",
67+
NameKey: "N",
68+
CallerKey: "C",
69+
FunctionKey: zapcore.OmitKey,
70+
MessageKey: "M",
71+
StacktraceKey: "S",
72+
LineEnding: zapcore.DefaultLineEnding,
73+
EncodeLevel: zapcore.CapitalLevelEncoder,
74+
EncodeTime: zapcore.ISO8601TimeEncoder,
75+
EncodeDuration: zapcore.StringDurationEncoder,
76+
EncodeCaller: zapcore.ShortCallerEncoder,
77+
}),
78+
}
79+
opts.BindFlags(flag.CommandLine)
80+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
81+
82+
cfg := Config{}
83+
cfg.AddFlags(pflag.CommandLine)
84+
pflag.Parse()
85+
86+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
87+
88+
setupLog.Info("current config", "config", &cfg)
89+
90+
if err := run(&cfg); err != nil {
91+
setupLog.Error(err, "unable to run resource syncer")
92+
os.Exit(1)
93+
}
94+
}
95+
96+
func run(cfg *Config) error {
97+
kubeconfig := ctrl.GetConfigOrDie()
98+
cacheOpt := cache.Options{
99+
DefaultNamespaces: map[string]cache.Config{
100+
cfg.Namespace: {
101+
LabelSelector: labels.SelectorFromSet(labels.Set(cfg.Labels)),
102+
},
103+
},
104+
}
105+
mgr, err := ctrl.NewManager(kubeconfig, ctrl.Options{
106+
HealthProbeBindAddress: cfg.ProbeAddr,
107+
Cache: cacheOpt,
108+
})
109+
if err != nil {
110+
return fmt.Errorf("unable to new manager: %w", err)
111+
}
112+
113+
if err := secret.EnsureDirExists(afero.NewBasePathFs(afero.NewOsFs(), cfg.BaseDir), cfg.SecretDirName); err != nil {
114+
return fmt.Errorf("unable to ensure secret dir exists: %w", err)
115+
}
116+
117+
if err := secret.Setup(mgr, filepath.Join(cfg.BaseDir, cfg.SecretDirName), cfg.Namespace, cfg.Labels); err != nil {
118+
return fmt.Errorf("unable to setup secret controller: %w", err)
119+
}
120+
121+
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
122+
return fmt.Errorf("unable to set up health check: %w", err)
123+
}
124+
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
125+
return fmt.Errorf("unable to set up ready check: %w", err)
126+
}
127+
ctx := ctrl.SetupSignalHandler()
128+
129+
setupLog.Info("starting manager")
130+
if err := mgr.Start(ctx); err != nil {
131+
return fmt.Errorf("start manager failed: %w", err)
132+
}
133+
134+
return nil
135+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ require (
6060
github.com/prometheus/client_model v0.6.1
6161
github.com/prometheus/common v0.62.0
6262
github.com/prometheus/prom2json v1.3.3
63+
github.com/spf13/afero v1.15.0
6364
github.com/spf13/cobra v1.8.1
6465
github.com/spf13/pflag v1.0.10
6566
github.com/stretchr/testify v1.11.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
464464
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
465465
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
466466
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
467+
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
468+
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
467469
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
468470
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
469471
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

hack/lib/build.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ function build::all() {
4747
shift
4848
done
4949
if [[ ${#targets[@]} -eq 0 ]]; then
50-
targets=("tidb-operator" "prestop-checker" "testing-workload" "tidb-backup-manager")
50+
targets=(
51+
"tidb-operator"
52+
"prestop-checker"
53+
"testing-workload"
54+
"tidb-backup-manager"
55+
"resource-syncer"
56+
)
5157
fi
5258

5359
local platforms

hack/lib/image.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CACHE_DIR=$OUTPUT_DIR/cache
2828

2929
declare -A NEED_PREFIX
3030
NEED_PREFIX["prestop-checker"]=1
31+
NEED_PREFIX["resource-syncer"]=1
3132

3233
function image::build() {
3334
local targets=()

image/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,16 @@ WORKDIR /
8080
COPY --chown=pingcap:pingcap --from=builder /data/_output/$TARGETPLATFORM/bin/tidb-backup-manager tidb-backup-manager
8181

8282
ENTRYPOINT ["/tidb-backup-manager"]
83+
84+
85+
FROM --platform=$TARGETPLATFORM ghcr.io/pingcap-qe/bases/pingcap-base:v1.10.0@sha256:af691c27330cb8e018add3dd16e5403dcd881b23eae815920ddb2c6ae87cbd9b AS resource-syncer
86+
87+
ARG TARGETPLATFORM
88+
89+
USER 1000:2000
90+
91+
WORKDIR /
92+
93+
COPY --chown=pingcap:pingcap --from=builder /data/_output/$TARGETPLATFORM/bin/resource-syncer resource-syncer
94+
95+
ENTRYPOINT ["/resource-syncer"]

0 commit comments

Comments
 (0)