|
| 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 | +} |
0 commit comments