Skip to content

Commit

Permalink
feat(log): update log url for job (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
D0m021ng authored May 10, 2023
1 parent ad8ebfd commit 926e8f4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
6 changes: 0 additions & 6 deletions cmd/server/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ func JobFlags(jobConf *config.JobConfig) []cli.Flag {
Usage: "the salt of job log token",
Destination: &jobConf.Log.SaltStr,
},
&cli.StringFlag{
Name: "log-time-format",
Value: jobConf.Log.TimeFormat,
Usage: "the time format of job log token",
Destination: &jobConf.Log.TimeFormat,
},
}
}

Expand Down
18 changes: 7 additions & 11 deletions pkg/apiserver/controller/job/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ var (
}
UpdateTime = time.Now()

defaultSaltStr = "paddleflow"
defaultTimeFormat = "2006-01-02 15"
LogURLFormat = "http://%s:%s/v1/containers/%s/log?jobID=%s&token=%s"
defaultSaltStr = "paddleflow"
LogURLFormat = "http://%s:%s/v1/containers/%s/log?jobID=%s&token=%s&t=%d"
)

func init() {
Expand Down Expand Up @@ -408,23 +407,20 @@ func GenerateLogURL(task model.JobTask) string {
containerID = items[1]
}
}
tokenStr := getLogToken(task.JobID, containerID)
tokenStr, t := getLogToken(task.JobID, containerID)
hash := md5.Sum([]byte(tokenStr))
token := hex.EncodeToString(hash[:])
log.Debugf("log url token for task %s/%s is %s", task.JobID, containerID, token)

return fmt.Sprintf(LogURLFormat, config.GlobalServerConfig.Job.Log.ServiceHost,
config.GlobalServerConfig.Job.Log.ServicePort, containerID, task.JobID, token)
config.GlobalServerConfig.Job.Log.ServicePort, containerID, task.JobID, token, t)
}

func getLogToken(jobID, containerID string) string {
func getLogToken(jobID, containerID string) (string, int64) {
saltStr := config.GlobalServerConfig.Job.Log.SaltStr
if saltStr == "" {
saltStr = defaultSaltStr
}
timeFormat := config.GlobalServerConfig.Job.Log.TimeFormat
if timeFormat == "" {
timeFormat = defaultTimeFormat
}
return fmt.Sprintf("%s/%s/%s@%s", jobID, containerID, saltStr, time.Now().Format(timeFormat))
timeStamp := time.Now().Unix()
return fmt.Sprintf("%s/%s/%s@%d", jobID, containerID, saltStr, timeStamp), timeStamp
}
7 changes: 4 additions & 3 deletions pkg/apiserver/controller/job/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGenerateLogURL(t *testing.T) {
ExtRuntimeStatusJSON: taskStatus,
},
containerID: "8517d2e225a5e580470d56c7e039208b538cb78b942cdabb028e235d1aee54b6",
expectURL: "http://127.0.0.1:8080/v1/containers/%s/log?jobID=test-job-id&token=%s",
expectURL: "http://127.0.0.1:8080/v1/containers/%s/log?jobID=test-job-id&token=%s&t=%d",
},
}

Expand All @@ -83,10 +83,11 @@ func TestGenerateLogURL(t *testing.T) {
assert.Equal(t, nil, err)
// generate log url
url := GenerateLogURL(task)
tokenStr := getLogToken(task.JobID, tc.containerID)
tokenStr, timeStamp := getLogToken(task.JobID, tc.containerID)
token := md5.Sum([]byte(tokenStr))
expectURL := fmt.Sprintf(tc.expectURL, tc.containerID, hex.EncodeToString(token[:]))
expectURL := fmt.Sprintf(tc.expectURL, tc.containerID, hex.EncodeToString(token[:]), timeStamp)
assert.Equal(t, expectURL, url)
t.Logf("log url %s", expectURL)
})
}
}
1 change: 0 additions & 1 deletion pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ type JobLogConfig struct {
ServiceHost string `yaml:"serviceHost"`
ServicePort string `yaml:"servicePort"`
SaltStr string `yaml:"saltStr"`
TimeFormat string `yaml:"timeFormat"`
}

type ImageConfig struct {
Expand Down

0 comments on commit 926e8f4

Please sign in to comment.