Skip to content

Commit

Permalink
add: job name override
Browse files Browse the repository at this point in the history
  • Loading branch information
ymktmk committed Mar 2, 2025
1 parent 6163bf4 commit 9b9ae0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type runJob struct {
args string
templateFile string
name string
args string
image string
resources string
namespace string
Expand All @@ -31,6 +32,7 @@ func runJobCmd() *cobra.Command {

flags := cmd.Flags()
flags.StringVarP(&r.templateFile, "template-file", "f", "", "Job template file")
flags.StringVar(&r.name, "name", "", "Name of the job")
flags.StringVar(&r.args, "args", "", "Command which you want to run")
flags.StringVar(&r.image, "image", "", "Image which you want to run")
flags.StringVar(&r.resources, "resources", "", "Resources which you want to run")
Expand All @@ -55,7 +57,7 @@ func (r *runJob) run(cmd *cobra.Command, args []string) {
}

log.Infof("Using config file: %s", config)
j, err := job.NewJob(config, r.templateFile, r.args, r.image, r.resources, r.namespace, r.container, (time.Duration(r.timeout) * time.Second))
j, err := job.NewJob(config, r.templateFile, r.name, r.args, r.image, r.resources, r.namespace, r.container, (time.Duration(r.timeout) * time.Second))
if err != nil {
log.Fatal(err)
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Job struct {

// Batch v1 job struct.
CurrentJob *v1.Job
// Target pod name
Name string
// Command which override the current job struct.
Args []string
// Target docker image.
Expand All @@ -47,7 +49,7 @@ type Job struct {

// NewJob returns a new Job struct, and initialize kubernetes client.
// It read the job definition yaml file, and unmarshal to batch/v1/Job.
func NewJob(configFile, currentFile, command, image, resources, namespace, container string, timeout time.Duration) (*Job, error) {
func NewJob(configFile, currentFile, name, command, image, resources, namespace, container string, timeout time.Duration) (*Job, error) {
if len(configFile) == 0 {
return nil, errors.New("Config file is required")
}
Expand Down Expand Up @@ -80,7 +82,11 @@ func NewJob(configFile, currentFile, command, image, resources, namespace, conta
if err != nil {
return nil, err
}
currentJob.SetName(generateRandomName(currentJob.Name))
jobName := currentJob.Name
if len(name) > 0 {
jobName = name
}
currentJob.SetName(generateRandomName(jobName))
if len(namespace) > 0 {
currentJob.SetNamespace(namespace)
}
Expand All @@ -97,6 +103,7 @@ func NewJob(configFile, currentFile, command, image, resources, namespace, conta
return &Job{
client,
&currentJob,
name,
args,
image,
resourceRequirements,
Expand Down
4 changes: 4 additions & 0 deletions pkg/job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestRunJob(t *testing.T) {
job := &Job{
CurrentJob: currentJob,
Args: []string{"hoge", "fuga"},
Name: "alpine-job",
Image: "alpine:latest",
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
Expand Down Expand Up @@ -191,6 +192,7 @@ func TestWaitJobCompleteWithWaitAll(t *testing.T) {
job := &Job{
CurrentJob: currentJob,
Args: []string{"hoge", "fuga"},
Name: "alpine-job",
Image: "alpine:latest",
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
Expand Down Expand Up @@ -294,6 +296,7 @@ func TestWaitJobCompleteForContainer(t *testing.T) {
job := &Job{
CurrentJob: currentJob,
Args: []string{"hoge", "fuga"},
Name: "alpine-job",
Image: "alpine:latest",
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
Expand Down Expand Up @@ -553,6 +556,7 @@ func TestRemovePods(t *testing.T) {
job := &Job{
CurrentJob: currentJob,
Args: []string{"hoge", "fuga"},
Name: "alpine",
Image: "alpine:latest",
Resources: v1core.ResourceRequirements{
Requests: v1core.ResourceList{
Expand Down

0 comments on commit 9b9ae0c

Please sign in to comment.