Skip to content

Commit

Permalink
Merge pull request #2 from lapras-inc/verbose-logs
Browse files Browse the repository at this point in the history
verbose logs
  • Loading branch information
yktakaha4 authored Dec 29, 2023
2 parents 6d74049 + d8c5766 commit 4f6fd32
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- name: Upload release binaries
uses: alexellis/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.ORG_GITHUB_SCOUTEA_PUBLIC_PAT }}
with:
asset_paths: '["./bin/*"]'
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,32 @@ vendor
bin
.config.yml
kube-job
coverage/

# Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# End of https://www.toptal.com/developers/gitignore/api/go
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ windows: mod
zip $(OUTDIR)/$(OUTPUT)_${VERSION}_windows_amd64.zip $(OUTPUT).exe

test:
go test -cover -v ./pkg/...
mkdir -p coverage
go test -coverprofile=coverage/coverage.out -cover -v ./pkg/...
go tool cover -html=coverage.out -o coverage/reports.html

e2e: build
which kind ginkgo > /dev/null
Expand Down
7 changes: 5 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ func runJobCmd() *cobra.Command {

func (r *runJob) run(cmd *cobra.Command, args []string) {
config, verbose := generalConfig()
log.SetLevel(log.DebugLevel)
if !verbose {
if verbose {
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.JSONFormatter{})
log.SetReportCaller(true)
} else {
log.SetLevel(log.WarnLevel)
}
if r.cleanup != job.All.String() && r.cleanup != job.Succeeded.String() && r.cleanup != job.Failed.String() {
Expand Down
61 changes: 58 additions & 3 deletions pkg/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Job struct {
Args []string
// Target docker image.
Image string
// Target resources.
// Target resources.
Resources corev1.ResourceRequirements
// Target namespace
Namespace string
Expand Down Expand Up @@ -237,18 +237,49 @@ func (j *Job) WaitJob(ctx context.Context, job *v1.Job, ignoreSidecar bool) erro
// If the job is failed, this function returns error.
// If the job is succeeded, this function returns nil.
func (j *Job) WaitJobComplete(ctx context.Context, job *v1.Job, ignoreSidecar bool) error {
log.WithContext(ctx).WithFields(log.Fields{
"job": job.Name,
"ignoreSidecar": ignoreSidecar,
}).Debug("WaitJobComplete start")
retry:
for {
time.Sleep(3 * time.Second)
running, err := j.client.BatchV1().Jobs(job.Namespace).Get(ctx, job.Name, metav1.GetOptions{})
runningName := ""
if err == nil {
runningName = running.Name
}
log.WithContext(ctx).WithFields(log.Fields{
"runningName": runningName,
"err": err,
}).Debug("WaitJobComplete Get")
if err != nil {
return err
}
if running.Status.Active == 0 && (running.Status.Succeeded == 1 || running.Status.Failed == 1) {
log.WithContext(ctx).WithFields(log.Fields{
"running.Name": running.Name,
"jobConditions": checkJobConditions(running.Status.Conditions),
}).Debug("WaitJobComplete finished")
return checkJobConditions(running.Status.Conditions)
}
if ignoreSidecar {
log.WithContext(ctx).WithFields(log.Fields{
"running.Name": running.Name,
"ignoreSidecar": ignoreSidecar,
}).Debug("WaitJobComplete ignoreSidecar")
pods, err := j.FindPods(ctx, running)
podNames := []string{}
if err == nil {
for _, pod := range pods {
podNames = append(podNames, pod.Name)
}
}
log.WithContext(ctx).WithFields(log.Fields{
"running.Name": running.Name,
"podNames": podNames,
"err": err,
}).Debug("WaitJobComplete FindPods")
if err != nil {
return err
}
Expand Down Expand Up @@ -291,29 +322,49 @@ func checkJobConditions(conditions []v1.JobCondition) error {
// checkPodConditions check all pods related a job.
// Returns true, if all containers in the pods which are matched container name is completed.
func checkPodConditions(pods []corev1.Pod, containerName string) (bool, error) {
podNames := []string{}
results := []bool{}
errs := []error{}
for _, pod := range pods {
if podIncludeContainer(pod, containerName) {
finished, err := containerIsCompleted(pod, containerName)
podNames = append(podNames, pod.Name)
results = append(results, finished)
errs = append(errs, err)
}
}
log.WithFields(log.Fields{
"podNames": podNames,
"results": results,
"errs": errs,
}).Debug("checkPodConditions check result")
if len(results) == 0 {
return false, nil
}
for _, r := range results {
for i, r := range results {
if !r {
log.WithFields(log.Fields{
"podName": podNames[i],
"result": r,
"returnValue": false,
}).Debug("checkPodConditions result false")
return false, nil
}
}
var err error
for _, e := range errs {
for i, e := range errs {
if e != nil {
log.WithFields(log.Fields{
"podName": podNames[i],
"error": e,
}).Debug("checkPodConditions error")
err = e
}
}
log.WithFields(log.Fields{
"err": err,
"returnValue": true,
}).Debug("checkPodConditions finished")
return true, err
}

Expand Down Expand Up @@ -353,6 +404,10 @@ func (j *Job) Cleanup() error {
log.Infof("Removing the job: %s", j.CurrentJob.Name)
options := metav1.DeleteOptions{}
err := j.client.BatchV1().Jobs(j.CurrentJob.Namespace).Delete(ctx, j.CurrentJob.Name, options)
log.WithContext(ctx).WithFields(log.Fields{
"jobName": j.CurrentJob.Name,
"err": err,
}).Debug("Delete job")
if err != nil {
return err
}
Expand Down
40 changes: 20 additions & 20 deletions pkg/job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ func TestRunJob(t *testing.T) {
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
"memory": resource.MustParse("64Mi"),
"cpu": resource.MustParse("250m"),
"cpu": resource.MustParse("250m"),
},
Limits: v1core.ResourceList{
"memory": resource.MustParse("128Mi"),
"cpu": resource.MustParse("500m"),
"cpu": resource.MustParse("500m"),
},
},
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
client: mockedKubernetes{
mockedBatch: batchV1Mock,
},
Expand Down Expand Up @@ -195,16 +195,16 @@ func TestWaitJobCompleteWithWaitAll(t *testing.T) {
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
"memory": resource.MustParse("64Mi"),
"cpu": resource.MustParse("250m"),
"cpu": resource.MustParse("250m"),
},
Limits: v1core.ResourceList{
"memory": resource.MustParse("128Mi"),
"cpu": resource.MustParse("500m"),
"cpu": resource.MustParse("500m"),
},
},
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
client: mockedKubernetes{
mockedBatch: batchV1Mock,
},
Expand Down Expand Up @@ -298,16 +298,16 @@ func TestWaitJobCompleteForContainer(t *testing.T) {
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
"memory": resource.MustParse("64Mi"),
"cpu": resource.MustParse("250m"),
"cpu": resource.MustParse("250m"),
},
Limits: v1core.ResourceList{
"memory": resource.MustParse("128Mi"),
"cpu": resource.MustParse("500m"),
"cpu": resource.MustParse("500m"),
},
},
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
client: mockedKubernetes{
mockedBatch: batchV1Mock,
mockedCore: coreV1Mock,
Expand Down Expand Up @@ -557,16 +557,16 @@ func TestRemovePods(t *testing.T) {
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
"memory": resource.MustParse("64Mi"),
"cpu": resource.MustParse("250m"),
"cpu": resource.MustParse("250m"),
},
Limits: v1core.ResourceList{
"memory": resource.MustParse("128Mi"),
"cpu": resource.MustParse("500m"),
"cpu": resource.MustParse("500m"),
},
},
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
Namespace: "default",
Container: "alpine",
Timeout: 10 * time.Minute,
client: mockedKubernetes{
mockedCore: coreV1Mock,
},
Expand Down
Loading

0 comments on commit 4f6fd32

Please sign in to comment.