Skip to content

Commit a238eb4

Browse files
authored
fix long warmup name (#71)
1 parent 74bfe71 commit a238eb4

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

internal/controller/warmup_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (o *onceHandler) sync(ctx context.Context, wu *juicefsiov1.WarmUp, cgName s
156156
}
157157
return reconcile.Result{}, nil
158158
}
159-
jobName := common.GenJobName(wu.Name)
159+
jobName := utils.GenJobName(wu.Name)
160160
var job batchv1.Job
161161
if err := o.Get(ctx, client.ObjectKey{Namespace: wu.Namespace, Name: jobName}, &job); err != nil {
162162
if utils.IsNotFound(err) {
@@ -271,7 +271,7 @@ func (c *cronHandler) sync(ctx context.Context, wu *juicefsiov1.WarmUp, cgName s
271271
cronjobBuilder := builder.NewJobBuilder(wu, &podList.Items[0])
272272
newCronJob := cronjobBuilder.NewWarmUpCronJob()
273273

274-
if err := c.Get(ctx, client.ObjectKey{Namespace: wu.Namespace, Name: common.GenJobName(wu.Name)}, &cronjob); err != nil {
274+
if err := c.Get(ctx, client.ObjectKey{Namespace: wu.Namespace, Name: newCronJob.Name}, &cronjob); err != nil {
275275
if utils.IsNotFound(err) {
276276
l.Info("create warmup cronjob", "cronjob", newCronJob.Name)
277277
err = c.Create(ctx, newCronJob)

internal/controller/warmup_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
juicefsiov1 "github.com/juicedata/juicefs-operator/api/v1"
3232
"github.com/juicedata/juicefs-operator/pkg/common"
33+
"github.com/juicedata/juicefs-operator/pkg/utils"
3334
)
3435

3536
var _ = Describe("WarmUp Controller", func() {
@@ -156,7 +157,7 @@ var _ = Describe("WarmUp Controller", func() {
156157
job := &batchv1.Job{}
157158
err = k8sClient.Get(ctx, types.NamespacedName{
158159
Namespace: namespace,
159-
Name: common.GenJobName(warmup.Name),
160+
Name: utils.GenJobName(warmup.Name),
160161
}, job)
161162
Expect(err).NotTo(HaveOccurred())
162163
})

pkg/builder/job.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (j *JobBuilder) NewWarmUpCronJob() *batchv1.CronJob {
9090

9191
cronJob := &batchv1.CronJob{
9292
ObjectMeta: metav1.ObjectMeta{
93-
Name: common.GenJobName(j.wu.Name),
93+
Name: utils.GenCronJobName(j.wu.Name),
9494
Namespace: j.wu.Namespace,
9595
OwnerReferences: GetWarmUpOwnerReference(j.wu),
9696
Labels: map[string]string{
@@ -124,7 +124,7 @@ func (j *JobBuilder) NewWarmUpCronJob() *batchv1.CronJob {
124124
func (j *JobBuilder) genBaseJob() *batchv1.Job {
125125
job := &batchv1.Job{
126126
ObjectMeta: metav1.ObjectMeta{
127-
Name: common.GenJobName(j.wu.Name),
127+
Name: utils.GenJobName(j.wu.Name),
128128
Namespace: j.wu.Namespace,
129129
OwnerReferences: GetWarmUpOwnerReference(j.wu),
130130
Labels: map[string]string{

pkg/common/common.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const (
6666
AnnoBackupWorker = "juicefs.io/backup-worker"
6767
AnnoWaitingDeleteWorker = "juicefs.io/waiting-delete-worker"
6868

69-
LabelMaxLength = 63
69+
LabelMaxLength = 63
70+
CronJobNameMaxLength = 52
7071

7172
InitConfigVolumeName = "init-config"
7273
InitConfigVolumeKey = "initconfig"
@@ -118,10 +119,6 @@ func GenWorkerName(cgName string, nodeName string) string {
118119
return fmt.Sprintf("%s-%s-%s", WorkerNamePrefix, cgName, nodeName)
119120
}
120121

121-
func GenJobName(wuName string) string {
122-
return fmt.Sprintf("%s-%s", WarmUpNamePrefix, wuName)
123-
}
124-
125122
func GenCleanCacheJobName(cgName, nodeName string) string {
126123
return fmt.Sprintf("%s-%s-%s", CleanCacheContainerName, cgName, nodeName)
127124
}

pkg/utils/job.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package utils
1818

1919
import (
20+
"fmt"
2021
"time"
2122

23+
"github.com/juicedata/juicefs-operator/pkg/common"
2224
v1 "k8s.io/api/batch/v1"
2325
)
2426

@@ -39,3 +41,13 @@ func CalculateDuration(creationTime time.Time, finishTime time.Time) string {
3941
}
4042
return finishTime.Sub(creationTime).Round(time.Second).String()
4143
}
44+
45+
func GenJobName(wuName string) string {
46+
n := fmt.Sprintf("%s-%s", common.WarmUpNamePrefix, wuName)
47+
return TruncateLabelValue(n)
48+
}
49+
50+
func GenCronJobName(wuName string) string {
51+
n := fmt.Sprintf("%s-%s", common.WarmUpNamePrefix, wuName)
52+
return TruncateLabelValueWithLength(n, common.CronJobNameMaxLength)
53+
}

pkg/utils/utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ func ParseYamlOrJson(source string) (map[string]interface{}, error) {
146146
// If the input string is longer than the maximum length, it computes an MD5 hash of
147147
// the string, and appends it to the input string
148148
func TruncateLabelValue(s string) string {
149-
length := common.LabelMaxLength
149+
return TruncateLabelValueWithLength(s, common.LabelMaxLength)
150+
}
151+
152+
func TruncateLabelValueWithLength(s string, length int) string {
150153
if len(s) <= length {
151154
return s
152155
}

test/e2e/e2e_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
corev1 "k8s.io/api/core/v1"
3030

3131
"github.com/juicedata/juicefs-operator/pkg/common"
32+
pkgutils "github.com/juicedata/juicefs-operator/pkg/utils"
3233
"github.com/juicedata/juicefs-operator/test/utils"
3334
)
3435

@@ -771,7 +772,7 @@ var _ = Describe("controller", Ordered, func() {
771772
fmt.Println("pods:")
772773
fmt.Println(string(result))
773774

774-
cmd = exec.Command("kubectl", "get", "po", "-l", fmt.Sprintf("job-name=%s", common.GenJobName(wuName)), "-n", namespace, "--no-headers", "-o", "jsonpath='{.items[*].metadata.name}'")
775+
cmd = exec.Command("kubectl", "get", "po", "-l", fmt.Sprintf("job-name=%s", pkgutils.GenJobName(wuName)), "-n", namespace, "--no-headers", "-o", "jsonpath='{.items[*].metadata.name}'")
775776
result, _ = utils.Run(cmd)
776777
if len(utils.GetNonEmptyLines(string(result))) == 1 && strings.TrimSpace(string(result)) != "''" {
777778
fmt.Println("warmup pod describe:")
@@ -817,7 +818,7 @@ var _ = Describe("controller", Ordered, func() {
817818

818819
By("validating warmup job running")
819820
verifyWarmUpJobRunning := func() error {
820-
cmd := exec.Command("kubectl", "get", "job", common.GenJobName(wuName), "-n", namespace, "-o", "jsonpath={.status.active}")
821+
cmd := exec.Command("kubectl", "get", "job", pkgutils.GenJobName(wuName), "-n", namespace, "-o", "jsonpath={.status.active}")
821822
result, err := utils.Run(cmd)
822823
if err != nil {
823824
return fmt.Errorf("get warmup job failed, %+v", err)
@@ -839,7 +840,7 @@ var _ = Describe("controller", Ordered, func() {
839840
if string(status) != "Running" {
840841
return fmt.Errorf("warmup in %s status", status)
841842
}
842-
cmd = exec.Command("kubectl", "get", "po", "-l", fmt.Sprintf("job-name=%s", common.GenJobName(wuName)), "-n", namespace, "--no-headers")
843+
cmd = exec.Command("kubectl", "get", "po", "-l", fmt.Sprintf("job-name=%s", pkgutils.GenJobName(wuName)), "-n", namespace, "--no-headers")
843844
result, err := utils.Run(cmd)
844845
if err != nil {
845846
return fmt.Errorf("get warmup job pod failed, %+v", err)

0 commit comments

Comments
 (0)