Skip to content

Commit

Permalink
Replace timestamp strings with time.Time (#143)
Browse files Browse the repository at this point in the history
* bugfix swaggerReplace timestamp strings with time.Time

Also set "created" statuses equal to parent created status when missing

* fix jobStatus

* go mod tidy

* merge in main

* change Updated to time.Time

* update documentation

* update documentation

* remove unused example comment

* set job created as nullable
  • Loading branch information
Richard87 authored Nov 28, 2024
1 parent c31c72e commit 835c730
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 167 deletions.
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ Request from application container URLs

## Developing

You need Go installed. Make sure `GOPATH` and `GOROOT` are properly set up.

Clone the repo into your `GOPATH` and run `go mod download`.

Also needed:

- [`go-swagger`](https://github.com/go-swagger/go-swagger) (on a Mac, you can install it with Homebrew: `brew install go-swagger`)
- [`statik`](https://github.com/rakyll/statik) (install with `go get github.com/rakyll/statik`)

Clone the repo into your `GOPATH` and run `go mod download`.
You need Go installed. Run `make bootstrap` to install required tools.

#### Update version
We follow the [semantic version](https://semver.org/) as recommended by [go](https://blog.golang.org/publishing-go-modules).
Expand Down Expand Up @@ -59,7 +50,7 @@ We use gomock to generate mocks used in unit test. [https://github.com/golang/mo
You need to regenerate mocks if you make changes to any of the interfaces in the code, e.g. the job Handler interface
Run `make mock` to regenerate mocks
Run `make mocks` to regenerate mocks
#### Update version
We follow the [semantic version](https://semver.org/) as recommended by [go](https://blog.golang.org/publishing-go-modules).
Expand All @@ -76,4 +67,4 @@ Want to contribute? Read our [contributing guidelines](./CONTRIBUTING.md)
## Security
This is how we handle [security issues](./SECURITY.md)
This is how we handle [security issues](./SECURITY.md)
2 changes: 1 addition & 1 deletion api/v1/batches/batch_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (handler *batchHandler) getBatchStatusFromRadixBatch(radixBatch *modelsv2.R
JobStatus: modelsv1.JobStatus{
Name: radixBatch.Name,
BatchId: getBatchId(radixBatch),
Created: radixBatch.CreationTime,
Created: &radixBatch.CreationTime,
Started: radixBatch.Started,
Ended: radixBatch.Ended,
Status: string(handler.getBatchStatus(radixBatch)),
Expand Down
42 changes: 21 additions & 21 deletions api/v1/controllers/batches/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

commonUtils "github.com/equinor/radix-common/utils"
"github.com/equinor/radix-common/utils/pointers"
apiErrors "github.com/equinor/radix-job-scheduler/api/errors"
"github.com/equinor/radix-job-scheduler/api/test"
api "github.com/equinor/radix-job-scheduler/api/v1/batches"
Expand Down Expand Up @@ -37,8 +37,8 @@ func TestGetBatches(t *testing.T) {
batchState := modelsV1.BatchStatus{
JobStatus: modelsV1.JobStatus{
Name: "batchname",
Started: commonUtils.FormatTimestamp(time.Now()),
Ended: commonUtils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "batchstatus",
},
BatchType: string(kube.RadixBatchTypeBatch),
Expand All @@ -62,8 +62,8 @@ func TestGetBatches(t *testing.T) {
require.NoError(t, err)
assert.Len(t, returnedBatches, 1)
assert.Equal(t, batchState.JobStatus.Name, returnedBatches[0].Name)
assert.Equal(t, batchState.JobStatus.Started, returnedBatches[0].Started)
assert.Equal(t, batchState.JobStatus.Ended, returnedBatches[0].Ended)
assert.WithinDuration(t, *batchState.JobStatus.Started, *returnedBatches[0].Started, 1)
assert.WithinDuration(t, *batchState.JobStatus.Ended, *returnedBatches[0].Ended, 1)
assert.Equal(t, batchState.JobStatus.Status, returnedBatches[0].Status)
}
})
Expand Down Expand Up @@ -107,8 +107,8 @@ func TestGetBatch(t *testing.T) {
batchState := modelsV1.BatchStatus{
JobStatus: modelsV1.JobStatus{
Name: batchName,
Started: commonUtils.FormatTimestamp(time.Now()),
Ended: commonUtils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "batchstatus",
},
BatchType: string(kube.RadixBatchTypeBatch),
Expand All @@ -131,8 +131,8 @@ func TestGetBatch(t *testing.T) {
err := test.GetResponseBody(response, &returnedBatch)
require.NoError(t, err)
assert.Equal(t, batchState.Name, returnedBatch.Name)
assert.Equal(t, batchState.Started, returnedBatch.Started)
assert.Equal(t, batchState.Ended, returnedBatch.Ended)
assert.WithinDuration(t, *batchState.Started, *returnedBatch.Started, 1)
assert.WithinDuration(t, *batchState.Ended, *returnedBatch.Ended, 1)
assert.Equal(t, batchState.Status, returnedBatch.Status)
}
})
Expand Down Expand Up @@ -205,8 +205,8 @@ func TestCreateBatch(t *testing.T) {
createdBatch := modelsV1.BatchStatus{
JobStatus: modelsV1.JobStatus{
Name: "newbatch",
Started: commonUtils.FormatTimestamp(time.Now()),
Ended: commonUtils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "batchstatus",
},
BatchType: string(kube.RadixBatchTypeBatch),
Expand All @@ -229,8 +229,8 @@ func TestCreateBatch(t *testing.T) {
err := test.GetResponseBody(response, &returnedBatch)
require.NoError(t, err)
assert.Equal(t, createdBatch.Name, returnedBatch.Name)
assert.Equal(t, createdBatch.Started, returnedBatch.Started)
assert.Equal(t, createdBatch.Ended, returnedBatch.Ended)
assert.WithinDuration(t, *createdBatch.Started, *returnedBatch.Started, 1)
assert.WithinDuration(t, *createdBatch.Ended, *returnedBatch.Ended, 1)
assert.Equal(t, createdBatch.Status, returnedBatch.Status)
}
})
Expand Down Expand Up @@ -265,8 +265,8 @@ func TestCreateBatch(t *testing.T) {
createdBatch := modelsV1.BatchStatus{
JobStatus: modelsV1.JobStatus{
Name: "newbatch",
Started: commonUtils.FormatTimestamp(time.Now()),
Ended: commonUtils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "batchstatus",
},
BatchType: string(kube.RadixBatchTypeBatch),
Expand All @@ -289,8 +289,8 @@ func TestCreateBatch(t *testing.T) {
err := test.GetResponseBody(response, &returnedBatch)
require.NoError(t, err)
assert.Equal(t, createdBatch.Name, returnedBatch.Name)
assert.Equal(t, createdBatch.Started, returnedBatch.Started)
assert.Equal(t, createdBatch.Ended, returnedBatch.Ended)
assert.WithinDuration(t, *createdBatch.Started, *returnedBatch.Started, 1)
assert.WithinDuration(t, *createdBatch.Ended, *returnedBatch.Ended, 1)
assert.Equal(t, createdBatch.Status, returnedBatch.Status)
}
})
Expand Down Expand Up @@ -656,8 +656,8 @@ func TestGetBatchJob(t *testing.T) {
jobHandler := mock.NewMockBatchHandler(ctrl)
jobState := modelsV1.JobStatus{
Name: jobName,
Started: commonUtils.FormatTimestamp(time.Now()),
Ended: commonUtils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "jobstatus",
}
ctx := context.Background()
Expand All @@ -679,8 +679,8 @@ func TestGetBatchJob(t *testing.T) {
err := test.GetResponseBody(response, &returnedJob)
require.NoError(t, err)
assert.Equal(t, jobState.Name, returnedJob.Name)
assert.Equal(t, jobState.Started, returnedJob.Started)
assert.Equal(t, jobState.Ended, returnedJob.Ended)
assert.WithinDuration(t, *jobState.Started, *returnedJob.Started, 1)
assert.WithinDuration(t, *jobState.Ended, *returnedJob.Ended, 1)
assert.Equal(t, jobState.Status, returnedJob.Status)
}
})
Expand Down
34 changes: 17 additions & 17 deletions api/v1/controllers/jobs/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/equinor/radix-common/utils"
"github.com/equinor/radix-common/utils/pointers"
apiErrors "github.com/equinor/radix-job-scheduler/api/errors"
"github.com/equinor/radix-job-scheduler/api/test"
"github.com/equinor/radix-job-scheduler/api/v1/jobs"
Expand All @@ -35,8 +35,8 @@ func TestGetJobs(t *testing.T) {
jobHandler := mock.NewMockJobHandler(ctrl)
jobState := modelsV1.JobStatus{
Name: "jobname",
Started: utils.FormatTimestamp(time.Now()),
Ended: utils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "jobstatus",
}
ctx := context.Background()
Expand All @@ -59,8 +59,8 @@ func TestGetJobs(t *testing.T) {
assert.Len(t, returnedJobs, 1)
assert.Equal(t, jobState.Name, returnedJobs[0].Name)
assert.Equal(t, "", returnedJobs[0].BatchName)
assert.Equal(t, jobState.Started, returnedJobs[0].Started)
assert.Equal(t, jobState.Ended, returnedJobs[0].Ended)
assert.WithinDuration(t, *jobState.Started, *returnedJobs[0].Started, 1)
assert.WithinDuration(t, *jobState.Ended, *returnedJobs[0].Ended, 1)
assert.Equal(t, jobState.Status, returnedJobs[0].Status)
}
})
Expand Down Expand Up @@ -103,8 +103,8 @@ func TestGetJob(t *testing.T) {
jobHandler := mock.NewMockJobHandler(ctrl)
jobState := modelsV1.JobStatus{
Name: jobName,
Started: utils.FormatTimestamp(time.Now()),
Ended: utils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "jobstatus",
}
ctx := context.Background()
Expand All @@ -126,8 +126,8 @@ func TestGetJob(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, jobState.Name, returnedJob.Name)
assert.Equal(t, "", returnedJob.BatchName)
assert.Equal(t, jobState.Started, returnedJob.Started)
assert.Equal(t, jobState.Ended, returnedJob.Ended)
assert.WithinDuration(t, *jobState.Started, *returnedJob.Started, 1)
assert.WithinDuration(t, *jobState.Ended, *returnedJob.Ended, 1)
assert.Equal(t, jobState.Status, returnedJob.Status)
}
})
Expand Down Expand Up @@ -199,8 +199,8 @@ func TestCreateJob(t *testing.T) {
jobScheduleDescription := models.JobScheduleDescription{}
createdJob := modelsV1.JobStatus{
Name: "newjob",
Started: utils.FormatTimestamp(time.Now()),
Ended: utils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "jobstatus",
}
jobHandler := mock.NewMockJobHandler(ctrl)
Expand All @@ -222,8 +222,8 @@ func TestCreateJob(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, createdJob.Name, returnedJob.Name)
assert.Equal(t, "", returnedJob.BatchName)
assert.Equal(t, createdJob.Started, returnedJob.Started)
assert.Equal(t, createdJob.Ended, returnedJob.Ended)
assert.WithinDuration(t, *createdJob.Started, *returnedJob.Started, 1)
assert.WithinDuration(t, *createdJob.Ended, *returnedJob.Ended, 1)
assert.Equal(t, createdJob.Status, returnedJob.Status)
}
})
Expand Down Expand Up @@ -253,8 +253,8 @@ func TestCreateJob(t *testing.T) {
}
createdJob := modelsV1.JobStatus{
Name: "newjob",
Started: utils.FormatTimestamp(time.Now()),
Ended: utils.FormatTimestamp(time.Now().Add(1 * time.Minute)),
Started: pointers.Ptr(time.Now()),
Ended: pointers.Ptr(time.Now().Add(1 * time.Minute)),
Status: "jobstatus",
}
jobHandler := mock.NewMockJobHandler(ctrl)
Expand All @@ -276,8 +276,8 @@ func TestCreateJob(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, createdJob.Name, returnedJob.Name)
assert.Equal(t, "", returnedJob.BatchName)
assert.Equal(t, createdJob.Started, returnedJob.Started)
assert.Equal(t, createdJob.Ended, returnedJob.Ended)
assert.WithinDuration(t, *createdJob.Started, *returnedJob.Started, 1)
assert.WithinDuration(t, *createdJob.Ended, *returnedJob.Ended, 1)
assert.Equal(t, createdJob.Status, returnedJob.Status)
}
})
Expand Down
12 changes: 5 additions & 7 deletions api/v1/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
)

// GetJobStatusFromRadixBatchJobsStatus Get Job status from RadixBatchJob
func GetJobStatusFromRadixBatchJobsStatus(batchName string, jobStatus modelsv2.RadixBatchJobStatus) modelsv1.JobStatus {
func GetJobStatusFromRadixBatchJobsStatus(radixBatch *modelsv2.RadixBatch, jobStatus modelsv2.RadixBatchJobStatus) modelsv1.JobStatus {
return modelsv1.JobStatus{
JobId: jobStatus.JobId,
BatchName: batchName,
BatchName: getBatchName(radixBatch),
Name: jobStatus.Name,
Created: jobStatus.CreationTime,
Started: jobStatus.Started,
Expand All @@ -35,9 +35,8 @@ func GetJobStatusFromRadixBatchJobsStatus(batchName string, jobStatus modelsv2.R
func GetJobStatusFromRadixBatchJobsStatuses(radixBatches ...modelsv2.RadixBatch) []modelsv1.JobStatus {
jobStatuses := make([]modelsv1.JobStatus, 0, len(radixBatches))
for _, radixBatch := range radixBatches {
jobStatusBatchName := getBatchName(&radixBatch)
for _, jobStatus := range radixBatch.JobStatuses {
jobStatuses = append(jobStatuses, GetJobStatusFromRadixBatchJobsStatus(jobStatusBatchName, jobStatus))
jobStatuses = append(jobStatuses, GetJobStatusFromRadixBatchJobsStatus(&radixBatch, jobStatus))
}
}
return jobStatuses
Expand All @@ -62,12 +61,11 @@ func GetBatchJob(ctx context.Context, handlerApiV2 apiv2.Handler, batchName, job
if err != nil {
return nil, err
}
jobStatusBatchName := getBatchName(radixBatch)
for _, jobStatus := range radixBatch.JobStatuses {
if !strings.EqualFold(jobStatus.Name, jobName) {
continue
}
jobsStatus := GetJobStatusFromRadixBatchJobsStatus(jobStatusBatchName, jobStatus)
jobsStatus := GetJobStatusFromRadixBatchJobsStatus(radixBatch, jobStatus)
return &jobsStatus, nil
}
return nil, fmt.Errorf("not found")
Expand All @@ -78,7 +76,7 @@ func GetPodStatus(podStatuses []modelsv2.RadixBatchJobPodStatus) []modelsv1.PodS
return slice.Map(podStatuses, func(status modelsv2.RadixBatchJobPodStatus) modelsv1.PodStatus {
return modelsv1.PodStatus{
Name: status.Name,
Created: status.Created,
Created: &status.Created,
StartTime: status.StartTime,
EndTime: status.EndTime,
ContainerStarted: status.StartTime,
Expand Down
1 change: 1 addition & 0 deletions api/v1/jobs/job_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func getSingleJobStatusFromRadixBatchJob(radixBatch *modelsv2.RadixBatch) (*mode
return nil, fmt.Errorf("batch should have only one job")
}
radixBatchJobStatus := radixBatch.JobStatuses[0]

jobStatus := modelsv1.JobStatus{
JobId: radixBatchJobStatus.JobId,
Name: radixBatchJobStatus.Name,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.5

require (
dario.cat/mergo v1.0.0
github.com/equinor/radix-common v1.9.3
github.com/equinor/radix-common v1.9.4
github.com/equinor/radix-operator v1.58.1
github.com/gin-gonic/gin v1.10.0
github.com/go-swagger/go-swagger v0.31.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/go-restful/v3 v3.12.1 h1:PJMDIM/ak7btuL8Ex0iYET9hxM3CI2sjZtzpL63nKAU=
github.com/emicklei/go-restful/v3 v3.12.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/equinor/radix-common v1.9.3 h1:dLKFzYy8/XyEG9Zygi0rMWIYGCddai/ILwUqjBiGYxQ=
github.com/equinor/radix-common v1.9.3/go.mod h1:+g0Wj0D40zz29DjNkYKVmCVeYy4OsFWKI7Qi9rA6kpY=
github.com/equinor/radix-common v1.9.4 h1:ErSnB2tqlRwaQuQdaA0qzsReDtHDgubcvqRO098ncEw=
github.com/equinor/radix-common v1.9.4/go.mod h1:+g0Wj0D40zz29DjNkYKVmCVeYy4OsFWKI7Qi9rA6kpY=
github.com/equinor/radix-operator v1.58.1 h1:Wb/UOP1m4wUdWCL/gynPcnf6axz01Z24fBvK2DRL5m0=
github.com/equinor/radix-operator v1.58.1/go.mod h1:zCdAiP/wxyvlUO4qGoJuLW3O+ZSt9kTyHMnjmsR3fCU=
github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func runApiServer(ctx context.Context, kubeUtil *kube.Kube, env *models.Env, rad
}

go func() {
log.Info().Msgf("Radix job API is serving on port %s", *port)
log.Info().Msgf("Radix job API is serving on port %s, http://localhost:%s/swaggerui", *port, *port)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("Radix job API server failed to start")
}
Expand Down
Loading

0 comments on commit 835c730

Please sign in to comment.