Skip to content

Commit

Permalink
Merge pull request #3452 from jamsman94/bugfix/workflowLogTemplate
Browse files Browse the repository at this point in the history
remove useless log in workflow
  • Loading branch information
jamsman94 authored Mar 27, 2024
2 parents 60695d3 + 9036803 commit 2841718
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 66 deletions.
2 changes: 1 addition & 1 deletion pkg/microservice/jobexecutor/core/service/step/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func handleCmdOutput(pipe io.ReadCloser, needPersistentLog bool, logFile string,
break
}

fmt.Printf("%s %s", time.Now().Format(setting.WorkflowTimeFormat), maskSecretEnvs(string(lineBytes), secretEnvs))
fmt.Printf("%s %s\n", time.Now().Format(setting.WorkflowTimeFormat), maskSecretEnvs(string(lineBytes), secretEnvs))

if needPersistentLog {
err := util.WriteFile(logFile, lineBytes, 0700)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewArchiveStep(spec interface{}, workspace string, envs, secretEnvs []strin
func (s *ArchiveStep) Run(ctx context.Context) error {
start := time.Now()
defer func() {
log.Infof("%s Archive ended. Duration: %.2f seconds", time.Now().Format(setting.WorkflowTimeFormat), time.Since(start).Seconds())
log.Infof("Archive ended. Duration: %.2f seconds", time.Now().Format(setting.WorkflowTimeFormat), time.Since(start).Seconds())
}()

for _, upload := range s.spec.UploadDetail {
Expand Down
10 changes: 5 additions & 5 deletions pkg/microservice/jobexecutor/core/service/step/step_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ func (s *DebugStep) Run(ctx context.Context) (err error) {
}
cm.Data[types.JobDebugStatusKey] = s.Type
if s.updater.UpdateWithRetry(cm, 3, 3*time.Second) != nil {
log.Errorf("%s debug step unexpected update configmap error: %v", time.Now().Format(setting.WorkflowTimeFormat), err)
log.Errorf("debug step unexpected update configmap error: %v", err)
return err
}
defer func() {
cm, err = s.updater.Get()
if err != nil {
log.Errorf("%s debug step unexpected get configmap error: %v", time.Now().Format(setting.WorkflowTimeFormat), err)
log.Errorf("debug step unexpected get configmap error: %v", err)
return
}
cm.Data[types.JobDebugStatusKey] = types.JobDebugStatusNotIn
if s.updater.UpdateWithRetry(cm, 3, 3*time.Second) != nil {
log.Errorf("%s debug step unexpected update configmap error: %v", time.Now().Format(setting.WorkflowTimeFormat), err)
log.Errorf("debug step unexpected update configmap error: %v", err)
}
}()

log.Infof("%s Running debugger %s job, Use debugger console.", time.Now().Format(setting.WorkflowTimeFormat), s.Type)
log.Infof("Running debugger %s job, Use debugger console.", s.Type)
for _, err := os.Stat(path); err == nil; {
time.Sleep(time.Second)
_, err = os.Stat(path)
}
log.Infof("%s debug step %s done", time.Now().Format(setting.WorkflowTimeFormat), s.Type)
log.Infof("debug step %s done", s.Type)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/hashicorp/go-multierror"
"github.com/koderover/zadig/v2/pkg/setting"
"gopkg.in/yaml.v2"

"github.com/koderover/zadig/v2/pkg/tool/log"
Expand All @@ -53,7 +52,7 @@ func NewDistributeImageStep(spec interface{}, workspace string, envs, secretEnvs
}

func (s *DistributeImageStep) Run(ctx context.Context) error {
log.Info("%s Start distribute images.", time.Now().Format(setting.WorkflowTimeFormat))
log.Info("Start distribute images.")
if s.spec.SourceRegistry == nil || s.spec.TargetRegistry == nil {
return errors.New("image registry infos are missing")
}
Expand Down Expand Up @@ -84,7 +83,7 @@ func (s *DistributeImageStep) Run(ctx context.Context) error {
appendError(errors.New(errMsg))
return
}
log.Infof("%s pull source image [%s] succeed", time.Now().Format(setting.WorkflowTimeFormat), target.SourceImage)
log.Infof("pull source image [%s] succeed", target.SourceImage)

tagCmd := dockerTagCmd(target.SourceImage, target.TargetImage)
out = bytes.Buffer{}
Expand All @@ -95,14 +94,14 @@ func (s *DistributeImageStep) Run(ctx context.Context) error {
appendError(errors.New(errMsg))
return
}
log.Infof("%s tag image [%s] to [%s] succeed", time.Now().Format(setting.WorkflowTimeFormat), target.SourceImage, target.TargetImage)
log.Infof("tag image [%s] to [%s] succeed", target.SourceImage, target.TargetImage)
}(target)
}
wg.Wait()
if err := errList.ErrorOrNil(); err != nil {
return fmt.Errorf("prepare source images error: %v", err)
}
log.Infof("%s Finish prepare source images.", time.Now().Format(setting.WorkflowTimeFormat))
log.Infof("Finish prepare source images.")

if err := s.loginTargetRegistry(); err != nil {
return err
Expand All @@ -120,20 +119,20 @@ func (s *DistributeImageStep) Run(ctx context.Context) error {
appendError(errors.New(errMsg))
return
}
log.Infof("%s push image [%s] succeed", time.Now().Format(setting.WorkflowTimeFormat), target.TargetImage)
log.Infof("push image [%s] succeed", target.TargetImage)
}(target)
}
wg.Wait()
if err := errList.ErrorOrNil(); err != nil {
return fmt.Errorf("push target images error: %v", err)
}

log.Info("%s Finish distribute images.", time.Now().Format(setting.WorkflowTimeFormat))
log.Info("Finish distribute images.")
return nil
}

func (s *DistributeImageStep) loginSourceRegistry() error {
log.Info("Logining Docker Source Registry.")
log.Info("Logging in Docker Source Registry.")
startTimeDockerLogin := time.Now()
loginCmd := dockerLogin(s.spec.SourceRegistry.AccessKey, s.spec.SourceRegistry.SecretKey, s.spec.SourceRegistry.RegAddr)
var out bytes.Buffer
Expand All @@ -147,7 +146,7 @@ func (s *DistributeImageStep) loginSourceRegistry() error {
}

func (s *DistributeImageStep) loginTargetRegistry() error {
log.Info("%s Logging in Docker Target Registry.", time.Now().Format(setting.WorkflowTimeFormat))
log.Info("Logging in Docker Target Registry.")
startTimeDockerLogin := time.Now()
loginCmd := dockerLogin(s.spec.TargetRegistry.AccessKey, s.spec.TargetRegistry.SecretKey, s.spec.TargetRegistry.RegAddr)
var out bytes.Buffer
Expand All @@ -156,7 +155,7 @@ func (s *DistributeImageStep) loginTargetRegistry() error {
if err := loginCmd.Run(); err != nil {
return fmt.Errorf("failed to login docker registry: %s %s", err, out.String())
}
log.Infof("%s Login ended. Duration: %.2f seconds.", time.Now().Format(setting.WorkflowTimeFormat), time.Since(startTimeDockerLogin).Seconds())
log.Infof("Login ended. Duration: %.2f seconds.", time.Since(startTimeDockerLogin).Seconds())
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ func NewDockerBuildStep(spec interface{}, workspace string, envs, secretEnvs []s
}

func (s *DockerBuildStep) Run(ctx context.Context) error {
start := time.Now()
log.Infof("%s Start docker build.", time.Now().Format(setting.WorkflowTimeFormat))
defer func() {
log.Infof("%s Docker build ended. Duration: %.2f seconds.", time.Now().Format(setting.WorkflowTimeFormat), time.Since(start).Seconds())
}()
log.Infof("Start docker build.")

envMap := makeEnvMap(s.envs, s.secretEnvs)
if image, ok := envMap["IMAGE"]; ok {
Expand All @@ -82,7 +78,7 @@ func (s DockerBuildStep) dockerLogin() error {
return nil
}
if s.spec.DockerRegistry.UserName != "" {
fmt.Printf("Logining Docker Registry: %s.\n", s.spec.DockerRegistry.Host)
log.Infof("Logging in Docker Registry: %s.", s.spec.DockerRegistry.Host)
startTimeDockerLogin := time.Now()
cmd := dockerLogin(s.spec.DockerRegistry.UserName, s.spec.DockerRegistry.Password, s.spec.DockerRegistry.Host)
var out bytes.Buffer
Expand Down Expand Up @@ -114,7 +110,7 @@ func (s DockerBuildStep) dockerLogin() error {
return fmt.Errorf("failed to login docker registry: %s %s", err, out.String())
}

fmt.Printf("%s Login ended. Duration: %.2f seconds.\n", time.Now().Format(setting.WorkflowTimeFormat), time.Since(startTimeDockerLogin).Seconds())
log.Infof("Login ended. Duration: %.2f seconds.", time.Since(startTimeDockerLogin).Seconds())
}
return nil
}
Expand All @@ -124,19 +120,19 @@ func (s *DockerBuildStep) runDockerBuild() error {
return nil
}

fmt.Printf("%s Preparing Dockerfile.\n", time.Now().Format(setting.WorkflowTimeFormat))
log.Infof("Preparing Dockerfile.")
startTimePrepareDockerfile := time.Now()
err := prepareDockerfile(s.spec.Source, s.spec.DockerTemplateContent)
if err != nil {
return fmt.Errorf("failed to prepare dockerfile: %s", err)
}
fmt.Printf("Preparation ended. Duration: %.2f seconds.\n", time.Since(startTimePrepareDockerfile).Seconds())
log.Infof("Preparation ended. Duration: %.2f seconds.", time.Since(startTimePrepareDockerfile).Seconds())

if s.spec.Proxy != nil {
setProxy(s.spec)
}

fmt.Printf("%s Running Docker Build.\n", time.Now().Format(setting.WorkflowTimeFormat))
log.Infof("Running Docker Build.")
startTimeDockerBuild := time.Now()
envs := s.envs
for _, c := range s.dockerCommands() {
Expand Down Expand Up @@ -171,7 +167,7 @@ func (s *DockerBuildStep) runDockerBuild() error {
return fmt.Errorf("failed to run docker build: %s", err)
}
}
fmt.Printf("%s Docker build ended. Duration: %.2f seconds.\n", time.Now().Format(setting.WorkflowTimeFormat), time.Since(startTimeDockerBuild).Seconds())
log.Infof("Docker build ended. Duration: %.2f seconds.", time.Since(startTimeDockerBuild).Seconds())

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewDownloadArchiveStep(spec interface{}, workspace string, envs, secretEnvs
func (s *DownloadArchiveStep) Run(ctx context.Context) error {
start := time.Now()
defer func() {
log.Infof("%s Download Archive ended. Duration: %.2f seconds", time.Now().Format(setting.WorkflowTimeFormat), time.Since(start).Seconds())
log.Infof("Download Archive ended. Duration: %.2f seconds", time.Since(start).Seconds())
}()

envmaps := makeEnvMap(s.envs, s.secretEnvs)
Expand All @@ -72,7 +72,7 @@ func (s *DownloadArchiveStep) Run(ctx context.Context) error {
client, err := s3.NewClient(s.spec.S3.Endpoint, s.spec.S3.Ak, s.spec.S3.Sk, s.spec.S3.Region, s.spec.S3.Insecure, forcedPathStyle)
if err != nil {
if s.spec.IgnoreErr {
log.Errorf("%s failed to create s3 client to upload file, err: %s", time.Now().Format(setting.WorkflowTimeFormat), err)
log.Errorf("failed to create s3 client to upload file, err: %s", err)
return nil
} else {
return fmt.Errorf("failed to create s3 client to upload file, err: %s", err)
Expand All @@ -85,7 +85,7 @@ func (s *DownloadArchiveStep) Run(ctx context.Context) error {
err = client.Download(s.spec.S3.Bucket, objectKey, destPath)
if err != nil {
if s.spec.IgnoreErr {
log.Errorf("%s download archive err, bucketName: %s, objectKey: %s, err: %v", time.Now().Format(setting.WorkflowTimeFormat), s.spec.S3.Bucket, objectKey, err)
log.Errorf("download archive err, bucketName: %s, objectKey: %s, err: %v", s.spec.S3.Bucket, objectKey, err)
return nil
} else {
return fmt.Errorf("failed to download archive from s3, bucketName: %s, objectKey: %s, destPath: %s, err: %s", s.spec.S3.Bucket, objectKey, destPath, err)
Expand All @@ -99,7 +99,7 @@ func (s *DownloadArchiveStep) Run(ctx context.Context) error {
err = client.Download(s.spec.S3.Bucket, objectKey, sourceFilename)
if err != nil {
if s.spec.IgnoreErr {
log.Errorf("%s failed to download archive from s3, bucketName: %s, objectKey: %s, err: %v", time.Now().Format(setting.WorkflowTimeFormat), s.spec.S3.Bucket, objectKey, err)
log.Errorf("failed to download archive from s3, bucketName: %s, objectKey: %s, err: %v", s.spec.S3.Bucket, objectKey, err)
return nil
} else {
return fmt.Errorf("failed to download archive from s3, bucketName: %s, objectKey: %s, source: %s, err: %s", s.spec.S3.Bucket, objectKey, sourceFilename, err)
Expand All @@ -109,7 +109,7 @@ func (s *DownloadArchiveStep) Run(ctx context.Context) error {
destPath = s.spec.DestDir
if err = os.MkdirAll(destPath, os.ModePerm); err != nil {
if s.spec.IgnoreErr {
log.Errorf("%s failed to MkdirAll destPath %s, err: %s", time.Now().Format(setting.WorkflowTimeFormat), destPath, err)
log.Errorf("failed to MkdirAll destPath %s, err: %s", destPath, err)
return nil
} else {
return fmt.Errorf("failed to MkdirAll destPath %s, err: %s", destPath, err)
Expand All @@ -120,15 +120,15 @@ func (s *DownloadArchiveStep) Run(ctx context.Context) error {
cmd.Stderr = out
if err := cmd.Run(); err != nil {
if s.spec.IgnoreErr {
log.Errorf("%s cmd: %s, err: %s %v", time.Now().Format(setting.WorkflowTimeFormat), cmd.String(), out.String(), err)
log.Errorf("cmd: %s, err: %s %v", cmd.String(), out.String(), err)
return nil
} else {
return fmt.Errorf("cmd: %s, err: %s %v", cmd.String(), out.String(), err)
}
}
} else {
if s.spec.IgnoreErr {
log.Errorf("%s failed to GenerateTmpFile, err: %s", time.Now().Format(setting.WorkflowTimeFormat), err)
log.Errorf("failed to GenerateTmpFile, err: %s", err)
return nil
} else {
return fmt.Errorf("failed to GenerateTmpFile, err: %s", err)
Expand Down
12 changes: 6 additions & 6 deletions pkg/microservice/jobexecutor/core/service/step/step_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func NewGitStep(spec interface{}, workspace string, envs, secretEnvs []string) (

func (s *GitStep) Run(ctx context.Context) error {
start := time.Now()
log.Infof("%s Start git clone.", time.Now().Format(setting.WorkflowTimeFormat))
log.Infof("Start git clone.", time.Now().Format(setting.WorkflowTimeFormat))
defer func() {
log.Infof("$s Git clone ended. Duration: %.2f seconds.", time.Now().Format(setting.WorkflowTimeFormat), time.Since(start).Seconds())
log.Infof("Git clone ended. Duration: %.2f seconds.", time.Now().Format(setting.WorkflowTimeFormat), time.Since(start).Seconds())
}()
return s.runGitCmds()
}
Expand Down Expand Up @@ -207,11 +207,11 @@ func (s *GitStep) buildGitCommands(repo *types.Repository, hostNames sets.String
// 预防非正常退出导致git被锁住
indexLockPath := path.Join(workDir, "/.git/index.lock")
if err := os.RemoveAll(indexLockPath); err != nil {
log.Errorf("%s Failed to remove %s: %s", time.Now().Format(setting.WorkflowTimeFormat), indexLockPath, err)
log.Errorf("Failed to remove %s: %s", indexLockPath, err)
}
shallowLockPath := path.Join(workDir, "/.git/shallow.lock")
if err := os.RemoveAll(shallowLockPath); err != nil {
log.Errorf("%s Failed to remove %s: %s", time.Now().Format(setting.WorkflowTimeFormat), shallowLockPath, err)
log.Errorf("Failed to remove %s: %s", shallowLockPath, err)
}

if isDirEmpty(filepath.Join(workDir, ".git")) {
Expand Down Expand Up @@ -248,7 +248,7 @@ func (s *GitStep) buildGitCommands(repo *types.Repository, hostNames sets.String
host := getHost(repo.Address)
if !hostNames.Has(host) {
if err := writeSSHFile(repo.SSHKey, host); err != nil {
log.Errorf("%s failed to write ssh file %s: %s", time.Now().Format(setting.WorkflowTimeFormat), repo.SSHKey, err)
log.Errorf("failed to write ssh file %s: %s", repo.SSHKey, err)
}
hostNames.Insert(host)
}
Expand All @@ -264,7 +264,7 @@ func (s *GitStep) buildGitCommands(repo *types.Repository, hostNames sets.String
} else if repo.AuthType == types.PrivateAccessTokenAuthType {
u, err := url.Parse(repo.Address)
if err != nil {
log.Errorf("%s failed to parse url,err:%s", time.Now().Format(setting.WorkflowTimeFormat), err)
log.Errorf("failed to parse url,err:%s", err)
} else {
host := strings.TrimSuffix(strings.Join([]string{u.Host, u.Path}, "/"), "/")
cmds = append(cmds, &c.Command{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewJunitReportStep(spec interface{}, workspace string, envs, secretEnvs []s
}

func (s *JunitReportStep) Run(ctx context.Context) error {
log.Info("%s Start merge ginkgo test results.", time.Now().Format(setting.WorkflowTimeFormat))
log.Info("Start merge ginkgo test results.")
if err := os.MkdirAll(s.spec.DestDir, os.ModePerm); err != nil {
return fmt.Errorf("create dest dir: %s error: %s", s.spec.DestDir, err)
}
Expand All @@ -75,9 +75,9 @@ func (s *JunitReportStep) Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to merge test result: %s", err)
}
log.Info("%s Finish merge ginkgo test results.", time.Now().Format(setting.WorkflowTimeFormat))
log.Info("Finish merge ginkgo test results.")

log.Infof("%s Start archive %s.", time.Now().Format(setting.WorkflowTimeFormat), s.spec.FileName)
log.Infof("Start archive %s.", s.spec.FileName)
if s.spec.S3DestDir == "" || s.spec.FileName == "" {
return nil
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *JunitReportStep) Run(ctx context.Context) error {
return err
}
}
log.Infof("%s Finish archive %s.", time.Now().Format(setting.WorkflowTimeFormat), s.spec.FileName)
log.Infof("Finish archive %s.", s.spec.FileName)
if failedCaseCount > 0 {
return fmt.Errorf("%d case(s) failed", failedCaseCount)
}
Expand Down Expand Up @@ -146,12 +146,12 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
for _, file := range files {
if filepath.Ext(file.Name()) == ".xml" {
filePath := path.Join(testResultPath, file.Name())
log.Infof("%s name %s mod time: %v", time.Now().Format(setting.WorkflowTimeFormat), file.Name(), file.ModTime())
log.Infof("name %s mod time: %v", file.Name(), file.ModTime())

// 1. read file
xmlBytes, err2 := ioutil.ReadFile(filePath)
if err2 != nil {
log.Warningf("%s Read file [%s], error: %v", time.Now().Format(setting.WorkflowTimeFormat), filePath, err2)
log.Warningf("Read file [%s], error: %v", filePath, err2)
continue
}

Expand All @@ -161,7 +161,7 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
var results *meta.TestSuites
err2 = xml.Unmarshal(xmlBytes, &results)
if err2 != nil {
log.Warningf("%s Unmarshal xml file [%s], error: %v\n", time.Now().Format(setting.WorkflowTimeFormat), filePath, err2)
log.Warningf("Unmarshal xml file [%s], error: %v\n", filePath, err2)
continue
}
for _, testSuite := range results.TestSuites {
Expand All @@ -181,7 +181,7 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
var result *meta.TestSuite
err2 = xml.Unmarshal(xmlBytes, &result)
if err2 != nil {
log.Warningf("%s Unmarshal xml file [%s], error: %v\n", time.Now().Format(setting.WorkflowTimeFormat), filePath, err2)
log.Warningf("Unmarshal xml file [%s], error: %v\n", filePath, err2)
continue
}
// 4. process summary result attribute
Expand Down Expand Up @@ -223,7 +223,7 @@ func mergeGinkgoTestResults(testResultFile, testResultPath, testUploadPath strin
return failedCaseCount, err
}

log.Infof("%s merge test results files %s succeeded", time.Now().Format(setting.WorkflowTimeFormat), testResultFile)
log.Infof("merge test results files %s succeeded", testResultFile)
return summaryResult.Failures, nil
}

Expand Down
Loading

0 comments on commit 2841718

Please sign in to comment.