From bd4fbd8f62c705d78a28b0162cd6efc95740a82b Mon Sep 17 00:00:00 2001 From: runkecheng <1131648942@qq.com> Date: Thu, 2 Sep 2021 14:25:44 +0800 Subject: [PATCH] *: Fix the sidecar container printing invalid log --- backup/syncer/job.go | 4 ++ cluster/container/backup.go | 4 ++ cluster/container/init_sidecar.go | 4 ++ cmd/sidecar/main.go | 68 ++++++++++-------- ...{DEPLOY.md => deploy_backup_restore_s3.md} | 0 sidecar/config.go | 69 +++++++++++++++---- utils/constants.go | 13 ++-- 7 files changed, 113 insertions(+), 49 deletions(-) rename docs/{DEPLOY.md => deploy_backup_restore_s3.md} (100%) diff --git a/backup/syncer/job.go b/backup/syncer/job.go index 2b55fb73..e71de995 100644 --- a/backup/syncer/job.go +++ b/backup/syncer/job.go @@ -123,6 +123,10 @@ func (s *jobSyncer) ensurePodSpec(in corev1.PodSpec) corev1.PodSpec { } var optTrue bool = true in.Containers[0].Env = []corev1.EnvVar{ + { + Name: "CONTAINER_TYPE", + Value: utils.ContainerBackupJobName, + }, { Name: "NAMESPACE", Value: s.backup.Namespace, diff --git a/cluster/container/backup.go b/cluster/container/backup.go index b543f680..639b0def 100644 --- a/cluster/container/backup.go +++ b/cluster/container/backup.go @@ -47,6 +47,10 @@ func (c *backupSidecar) getEnvVars() []corev1.EnvVar { sctNameBakup := c.Spec.BackupSecretName sctName := c.GetNameForResource(utils.Secret) envs := []corev1.EnvVar{ + { + Name: "CONTAINER_TYPE", + Value: utils.ContainerBackupName, + }, { Name: "NAMESPACE", Value: c.Namespace, diff --git a/cluster/container/init_sidecar.go b/cluster/container/init_sidecar.go index bd5938f2..16094529 100644 --- a/cluster/container/init_sidecar.go +++ b/cluster/container/init_sidecar.go @@ -54,6 +54,10 @@ func (c *initSidecar) getEnvVars() []corev1.EnvVar { sctName := c.GetNameForResource(utils.Secret) sctNamebackup := c.Spec.BackupSecretName envs := []corev1.EnvVar{ + { + Name: "CONTAINER_TYPE", + Value: utils.ContainerInitSidecarName, + }, { Name: "POD_HOSTNAME", ValueFrom: &corev1.EnvVarSource{ diff --git a/cmd/sidecar/main.go b/cmd/sidecar/main.go index 4048a9fc..1bd2bd8c 100644 --- a/cmd/sidecar/main.go +++ b/cmd/sidecar/main.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" "github.com/radondb/radondb-mysql-kubernetes/sidecar" + "github.com/radondb/radondb-mysql-kubernetes/utils" ) const ( @@ -50,40 +51,47 @@ var ( func main() { // setup logging logf.SetLogger(zap.New(zap.UseDevMode(true))) - cfg := sidecar.NewConfig() stop := make(chan struct{}) - initCmd := sidecar.NewInitCommand(cfg) - cmd.AddCommand(initCmd) - httpCmd := &cobra.Command{ - Use: "http", - Short: "start http server", - Run: func(cmd *cobra.Command, args []string) { - if err := sidecar.RunHttpServer(cfg, stop); err != nil { - log.Error(err, "run command failed") - os.Exit(1) - } - }, - } - cmd.AddCommand(httpCmd) + containerName := sidecar.GetContainerType() - reqBackupCmd := &cobra.Command{ - Use: "request_a_backup", - Short: "start request a backup", - Args: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return fmt.Errorf("require one arguments. ") - } - return nil - }, - Run: func(cmd *cobra.Command, args []string) { - if err := sidecar.RunRequestBackup(cfg, args[0]); err != nil { - log.Error(err, "run command failed") - os.Exit(1) - } - }, + if containerName == utils.ContainerBackupName { + backupCfg := sidecar.NewBackupConfig() + httpCmd := &cobra.Command{ + Use: "http", + Short: "start http server", + Run: func(cmd *cobra.Command, args []string) { + if err := sidecar.RunHttpServer(backupCfg, stop); err != nil { + log.Error(err, "run command failed") + os.Exit(1) + } + }, + } + cmd.AddCommand(httpCmd) + } else if containerName == utils.ContainerBackupJobName { + reqBackupCfg := sidecar.NewReqBackupConfig() + reqBackupCmd := &cobra.Command{ + Use: "request_a_backup", + Short: "start request a backup", + Args: func(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return fmt.Errorf("require one arguments. ") + } + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + if err := sidecar.RunRequestBackup(reqBackupCfg, args[0]); err != nil { + log.Error(err, "run command failed") + os.Exit(1) + } + }, + } + cmd.AddCommand(reqBackupCmd) + } else { + initCfg := sidecar.NewInitConfig() + initCmd := sidecar.NewInitCommand(initCfg) + cmd.AddCommand(initCmd) } - cmd.AddCommand(reqBackupCmd) if err := cmd.Execute(); err != nil { log.Error(err, "failed to execute command", "cmd", cmd) diff --git a/docs/DEPLOY.md b/docs/deploy_backup_restore_s3.md similarity index 100% rename from docs/DEPLOY.md rename to docs/deploy_backup_restore_s3.md diff --git a/sidecar/config.go b/sidecar/config.go index 16cf3701..5fa31e78 100644 --- a/sidecar/config.go +++ b/sidecar/config.go @@ -20,7 +20,8 @@ import ( "fmt" "os" "strconv" - "strings" + + // "strings" "text/template" "github.com/blang/semver" @@ -119,8 +120,8 @@ type Config struct { XRestoreFrom string } -// NewConfig returns a pointer to Config. -func NewConfig() *Config { +// NewInitConfig returns the configuration file needed for initialization. +func NewInitConfig() *Config { mysqlVersion, err := semver.Parse(getEnvValue("MYSQL_VERSION")) if err != nil { log.Info("MYSQL_VERSION is not a semver version") @@ -179,24 +180,66 @@ func NewConfig() *Config { AdmitDefeatHearbeatCount: int32(admitDefeatHearbeatCount), ElectionTimeout: int32(electionTimeout), - existMySQLData: existMySQLData, - ClusterName: getEnvValue("SERVICE_NAME"), - BackupUser: getEnvValue("BACKUP_USER"), - BackupPassword: getEnvValue("BACKUP_PASSWORD"), - XbstreamExtraArgs: strings.Fields(getEnvValue("XBSTREAM_EXTRA_ARGS")), - XtrabackupExtraArgs: strings.Fields(getEnvValue("XTRABACKUP_EXTRA_ARGS")), - XtrabackupPrepareExtraArgs: strings.Fields(getEnvValue("XTRABACKUP_PREPARE_EXTRA_ARGS")), - XtrabackupTargetDir: getEnvValue("XTRABACKUP_TARGET_DIR"), + existMySQLData: existMySQLData, + XRestoreFrom: getEnvValue("RESTORE_FROM"), + XCloudS3EndPoint: getEnvValue("S3_ENDPOINT"), + XCloudS3AccessKey: getEnvValue("S3_ACCESSKEY"), + XCloudS3SecretKey: getEnvValue("S3_SECRETKEY"), + XCloudS3Bucket: getEnvValue("S3_BUCKET"), + } +} + +// NewBackupConfig returns the configuration file needed for backup container. +func NewBackupConfig() *Config { + replicaStr := getEnvValue("REPLICAS") + replicas, err := strconv.ParseInt(replicaStr, 10, 32) + if err != nil { + log.Error(err, "invalid environment values", "REPLICAS", replicaStr) + panic(err) + } + + return &Config{ + NameSpace: getEnvValue("NAMESPACE"), + ServiceName: getEnvValue("SERVICE_NAME"), + Replicas: int32(replicas), + ClusterName: getEnvValue("SERVICE_NAME"), + + BackupUser: getEnvValue("BACKUP_USER"), + BackupPassword: getEnvValue("BACKUP_PASSWORD"), XCloudS3EndPoint: getEnvValue("S3_ENDPOINT"), XCloudS3AccessKey: getEnvValue("S3_ACCESSKEY"), XCloudS3SecretKey: getEnvValue("S3_SECRETKEY"), XCloudS3Bucket: getEnvValue("S3_BUCKET"), - XRestoreFrom: getEnvValue("RESTORE_FROM"), } } -// build Xtrabackup arguments +// NewReqBackupConfig returns the configuration file needed for backup job. +func NewReqBackupConfig() *Config { + replicaStr := getEnvValue("REPLICAS") + replicas, err := strconv.ParseInt(replicaStr, 10, 32) + if err != nil { + log.Error(err, "invalid environment values", "REPLICAS", replicaStr) + panic(err) + } + + return &Config{ + NameSpace: getEnvValue("NAMESPACE"), + ServiceName: getEnvValue("SERVICE_NAME"), + Replicas: int32(replicas), + + BackupUser: getEnvValue("BACKUP_USER"), + BackupPassword: getEnvValue("BACKUP_PASSWORD"), + } +} + +// GetContainerType returns the CONTAINER_TYPE of the currently running container. +// CONTAINER_TYPE used to mark the container type. +func GetContainerType() string { + return getEnvValue("CONTAINER_TYPE") +} + +//build Xtrabackup arguments func (cfg *Config) XtrabackupArgs() []string { // xtrabackup --backup --target-dir= user := "root" diff --git a/utils/constants.go b/utils/constants.go index e290217d..313b5217 100644 --- a/utils/constants.go +++ b/utils/constants.go @@ -37,12 +37,13 @@ const ( ContainerInitMysqlName = "init-mysql" // containers - ContainerMysqlName = "mysql" - ContainerXenonName = "xenon" - ContainerMetricsName = "metrics" - ContainerSlowLogName = "slowlog" - ContainerAuditLogName = "auditlog" - ContainerBackupName = "backup" + ContainerMysqlName = "mysql" + ContainerXenonName = "xenon" + ContainerMetricsName = "metrics" + ContainerSlowLogName = "slowlog" + ContainerAuditLogName = "auditlog" + ContainerBackupName = "backup" + ContainerBackupJobName = "backup-job" XBackupPortName = "xtrabackup" XBackupPort = 8082