Skip to content

Commit

Permalink
*: Fix the sidecar container printing invalid log
Browse files Browse the repository at this point in the history
  • Loading branch information
runkecheng authored and acekingke committed Sep 9, 2021
1 parent ce4f24a commit bd4fbd8
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 49 deletions.
4 changes: 4 additions & 0 deletions backup/syncer/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions cluster/container/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions cluster/container/init_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
68 changes: 38 additions & 30 deletions cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand Down
File renamed without changes.
69 changes: 56 additions & 13 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"fmt"
"os"
"strconv"
"strings"

// "strings"
"text/template"

"github.com/blang/semver"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 <args> --target-dir=<backup-dir> <extra-args>
user := "root"
Expand Down
13 changes: 7 additions & 6 deletions utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bd4fbd8

Please sign in to comment.