Skip to content

Commit

Permalink
cherry pick pingcap#3034 to release-1.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
cofyc authored and ti-srebot committed Jul 30, 2020
1 parent cd8a176 commit 4353fbd
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: TZ
value: {{ .Values.timezone | default "UTC" }}
volumeMounts:
{{- if eq .Values.admissionWebhook.apiservice.insecureSkipTLSVerify false }}
- mountPath: /var/serving-cert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: TZ
value: {{ .Values.timezone | default "UTC" }}
resources:
{{ toYaml .Values.advancedStatefulset.resources | indent 12 }}
{{- with .Values.advancedStatefulset.nodeSelector }}
Expand Down
2 changes: 1 addition & 1 deletion charts/tidb-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ apiserver:
# kubectl apply -f manifests/advanced-statefulset-crd.v1.yaml # k8s version >= 1.16.0
advancedStatefulset:
create: false
image: pingcap/advanced-statefulset:v0.3.3
image: pingcap/advanced-statefulset:v0.4.0
imagePullPolicy: IfNotPresent
serviceAccount: advanced-statefulset-controller
logLevel: 2
Expand Down
3 changes: 2 additions & 1 deletion pkg/backup/backup/backup_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
backuputil "github.com/pingcap/tidb-operator/pkg/backup/util"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
"github.com/pingcap/tidb-operator/pkg/util"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -143,7 +144,7 @@ func (bc *backupCleaner) makeCleanJob(backup *v1alpha1.Backup) (*batchv1.Job, st
Image: controller.TidbBackupManagerImage,
Args: args,
ImagePullPolicy: corev1.PullIfNotPresent,
Env: storageEnv,
Env: util.AppendEnvIfPresent(storageEnv, "TZ"),
Resources: backup.Spec.ResourceRequirements,
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/backup/backup/backup_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (bm *backupManager) makeExportJob(backup *v1alpha1.Backup) (*batchv1.Job, s
VolumeMounts: []corev1.VolumeMount{
{Name: label.BackupJobLabelVal, MountPath: constants.BackupRootPath},
},
Env: envVars,
Env: util.AppendEnvIfPresent(envVars, "TZ"),
Resources: backup.Spec.ResourceRequirements,
},
},
Expand Down Expand Up @@ -360,7 +360,7 @@ func (bm *backupManager) makeBackupJob(backup *v1alpha1.Backup) (*batchv1.Job, s
Args: args,
ImagePullPolicy: corev1.PullIfNotPresent,
VolumeMounts: volumeMounts,
Env: envVars,
Env: util.AppendEnvIfPresent(envVars, "TZ"),
Resources: backup.Spec.ResourceRequirements,
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/backup/restore/restore_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (rm *restoreManager) makeImportJob(restore *v1alpha1.Restore) (*batchv1.Job
VolumeMounts: []corev1.VolumeMount{
{Name: label.RestoreJobLabelVal, MountPath: constants.BackupRootPath},
},
Env: envVars,
Env: util.AppendEnvIfPresent(envVars, "TZ"),
Resources: restore.Spec.ResourceRequirements,
},
},
Expand Down Expand Up @@ -346,7 +346,7 @@ func (rm *restoreManager) makeRestoreJob(restore *v1alpha1.Restore) (*batchv1.Jo
Args: args,
ImagePullPolicy: corev1.PullIfNotPresent,
VolumeMounts: volumeMounts,
Env: envVars,
Env: util.AppendEnvIfPresent(envVars, "TZ"),
Resources: restore.Spec.ResourceRequirements,
},
},
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package util
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -252,3 +253,19 @@ func RetainManagedFields(desiredSvc, existedSvc *corev1.Service) {
}
}
}

// AppendEnvIfPresent appends the given environment if present
func AppendEnvIfPresent(envs []corev1.EnvVar, name string) []corev1.EnvVar {
for _, e := range envs {
if e.Name == name {
return envs
}
}
if val, ok := os.LookupEnv(name); ok {
envs = append(envs, corev1.EnvVar{
Name: name,
Value: val,
})
}
return envs
}
92 changes: 92 additions & 0 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package util

import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -187,3 +188,94 @@ func TestAppendEnv(t *testing.T) {
})
}
}

func TestAppendEnvIfPresent(t *testing.T) {
tests := []struct {
name string
a []corev1.EnvVar
envs map[string]string
n string
want []corev1.EnvVar
}{
{
"does not exist",
[]corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
nil,
"TEST_ENV",
[]corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
},
{
"does exist",
[]corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
map[string]string{
"TEST_ENV": "TEST_VAL",
},
"TEST_ENV",
[]corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
{
Name: "TEST_ENV",
Value: "TEST_VAL",
},
},
},
{
"already exist",
[]corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
{
Name: "TEST_ENV",
Value: "TEST_OLD_VAL",
},
},
map[string]string{
"TEST_ENV": "TEST_VAL",
},
"TEST_ENV",
[]corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
{
Name: "TEST_ENV",
Value: "TEST_OLD_VAL",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Clearenv()
for k, v := range tt.envs {
os.Setenv(k, v)
}
got := AppendEnvIfPresent(tt.a, tt.n)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("unwant (-want, +got): %s", diff)
}
})
}
}

0 comments on commit 4353fbd

Please sign in to comment.