Skip to content

Commit

Permalink
InitFields to Canonicalize
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Jul 20, 2016
1 parent 0bc275c commit 288aea3
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 42 deletions.
2 changes: 1 addition & 1 deletion command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *PlanCommand) Run(args []string) int {
}

// Initialize any fields that need to be.
job.InitFields()
job.Canonicalize()

// Check that the job is valid
if err := job.Validate(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *RunCommand) Run(args []string) int {
}

// Initialize any fields that need to be.
job.InitFields()
job.Canonicalize()

// Check that the job is valid
if err := job.Validate(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *ValidateCommand) Run(args []string) int {
}

// Initialize any fields that need to be.
job.InitFields()
job.Canonicalize()

// Check that the job is valid
if err := job.Validate(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions nomad/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (n *nomadFSM) applyUpsertJob(buf []byte, index uint64) interface{} {
// un-intended destructive updates in scheduler since we use
// reflect.DeepEqual. Starting Nomad 0.4.1, job submission sanatizes
// the incoming job.
job.InitFields()
req.Job.Canonicalize()

if err := n.state.UpsertJob(index, req.Job); err != nil {
n.logger.Printf("[ERR] nomad.fsm: UpsertJob failed: %v", err)
Expand Down Expand Up @@ -513,7 +513,7 @@ func (n *nomadFSM) Restore(old io.ReadCloser) error {
// un-intended destructive updates in scheduler since we use
// reflect.DeepEqual. Starting Nomad 0.4.1, job submission sanatizes
// the incoming job.
job.InitFields()
job.Canonicalize()

if err := restore.JobRestore(job); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (j *Job) Register(args *structs.JobRegisterRequest, reply *structs.JobRegis
}

// Initialize the job fields (sets defaults and any necessary init work).
args.Job.InitFields()
args.Job.Canonicalize()

// Validate the job.
if err := validateJob(args.Job); err != nil {
Expand Down Expand Up @@ -431,7 +431,7 @@ func (j *Job) Plan(args *structs.JobPlanRequest, reply *structs.JobPlanResponse)
}

// Initialize the job fields (sets defaults and any necessary init work).
args.Job.InitFields()
args.Job.Canonicalize()

// Validate the job.
if err := validateJob(args.Job); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion nomad/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func Job() *structs.Job {
ModifyIndex: 99,
JobModifyIndex: 99,
}
job.InitFields()
job.Canonicalize()
return job
}

Expand Down
2 changes: 1 addition & 1 deletion nomad/state/state_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ func TestStateStore_GetJobStatus_DeadEvalsAndAllocs(t *testing.T) {
// Create a mock alloc that is dead.
alloc := mock.Alloc()
alloc.JobID = job.ID
alloc.DesiredStatus = structs.AllocDesiredStatusFailed
alloc.DesiredStatus = structs.AllocDesiredStatusStop
if err := state.UpsertAllocs(1000, []*structs.Allocation{alloc}); err != nil {
t.Fatalf("err: %v", err)
}
Expand Down
46 changes: 20 additions & 26 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,15 @@ func (r *Resources) Merge(other *Resources) {
}
}

func (r *Resources) InitFields() {
func (r *Resources) Canonicalize() {
// Ensure that an empty and nil slices are treated the same to avoid scheduling
// problems since we use reflect DeepEquals.
if len(r.Networks) == 0 {
r.Networks = nil
}

for _, n := range r.Networks {
n.InitFields()
n.Canonicalize()
}
}

Expand Down Expand Up @@ -862,7 +862,7 @@ type NetworkResource struct {
DynamicPorts []Port // Dynamically assigned ports
}

func (n *NetworkResource) InitFields() {
func (n *NetworkResource) Canonicalize() {
// Ensure that an empty and nil slices are treated the same to avoid scheduling
// problems since we use reflect DeepEquals.
if len(n.ReservedPorts) == 0 {
Expand Down Expand Up @@ -1043,17 +1043,17 @@ type Job struct {
JobModifyIndex uint64
}

// InitFields is used to initialize fields in the Job. This should be called
// Canonicalize is used to canonicalize fields in the Job. This should be called
// when registering a Job.
func (j *Job) InitFields() {
func (j *Job) Canonicalize() {
// Ensure that an empty and nil map are treated the same to avoid scheduling
// problems since we use reflect DeepEquals.
if len(j.Meta) == 0 {
j.Meta = nil
}

for _, tg := range j.TaskGroups {
tg.InitFields(j)
tg.Canonicalize(j)
}
}

Expand Down Expand Up @@ -1459,8 +1459,8 @@ func (tg *TaskGroup) Copy() *TaskGroup {
return ntg
}

// InitFields is used to initialize fields in the TaskGroup.
func (tg *TaskGroup) InitFields(job *Job) {
// Canonicalize is used to canonicalize fields in the TaskGroup.
func (tg *TaskGroup) Canonicalize(job *Job) {
// Ensure that an empty and nil map are treated the same to avoid scheduling
// problems since we use reflect DeepEquals.
if len(tg.Meta) == 0 {
Expand All @@ -1473,7 +1473,7 @@ func (tg *TaskGroup) InitFields(job *Job) {
}

for _, task := range tg.Tasks {
task.InitFields(job, tg)
task.Canonicalize(job, tg)
}
}

Expand Down Expand Up @@ -1579,7 +1579,7 @@ func (sc *ServiceCheck) Copy() *ServiceCheck {
return nsc
}

func (sc *ServiceCheck) InitFields(serviceName string) {
func (sc *ServiceCheck) Canonicalize(serviceName string) {
// Ensure empty slices are treated as null to avoid scheduling issues when
// using DeepEquals.
if len(sc.Args) == 0 {
Expand Down Expand Up @@ -1683,9 +1683,9 @@ func (s *Service) Copy() *Service {
return ns
}

// InitFields interpolates values of Job, Task Group and Task in the Service
// Canonicalize interpolates values of Job, Task Group and Task in the Service
// Name. This also generates check names, service id and check ids.
func (s *Service) InitFields(job string, taskGroup string, task string) {
func (s *Service) Canonicalize(job string, taskGroup string, task string) {
// Ensure empty lists are treated as null to avoid scheduler issues when
// using DeepEquals
if len(s.Tags) == 0 {
Expand All @@ -1704,7 +1704,7 @@ func (s *Service) InitFields(job string, taskGroup string, task string) {
)

for _, check := range s.Checks {
check.InitFields(s.Name)
check.Canonicalize(s.Name)
}
}

Expand Down Expand Up @@ -1857,8 +1857,8 @@ func (t *Task) Copy() *Task {
return nt
}

// InitFields initializes fields in the task.
func (t *Task) InitFields(job *Job, tg *TaskGroup) {
// Canonicalize canonicalizes fields in the task.
func (t *Task) Canonicalize(job *Job, tg *TaskGroup) {
// Ensure that an empty and nil map are treated the same to avoid scheduling
// problems since we use reflect DeepEquals.
if len(t.Meta) == 0 {
Expand All @@ -1871,24 +1871,18 @@ func (t *Task) InitFields(job *Job, tg *TaskGroup) {
t.Env = nil
}

t.InitServiceFields(job.Name, tg.Name)
t.Resources.InitFields()
for _, service := range t.Services {
service.Canonicalize(job.Name, tg.Name, t.Name)
}

t.Resources.Canonicalize()

// Set the default timeout if it is not specified.
if t.KillTimeout == 0 {
t.KillTimeout = DefaultKillTimeout
}
}

// InitServiceFields interpolates values of Job, Task Group
// and Tasks in all the service Names of a Task. This also generates the service
// id, check id and check names.
func (t *Task) InitServiceFields(job string, taskGroup string) {
for _, service := range t.Services {
service.InitFields(job, taskGroup, t.Name)
}
}

func (t *Task) GoString() string {
return fmt.Sprintf("*%#v", *t)
}
Expand Down
14 changes: 7 additions & 7 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestJob_IsPeriodic(t *testing.T) {
func TestJob_SystemJob_Validate(t *testing.T) {
j := testJob()
j.Type = JobTypeSystem
j.InitFields()
j.Canonicalize()

err := j.Validate()
if err == nil || !strings.Contains(err.Error(), "exceed") {
Expand Down Expand Up @@ -712,7 +712,7 @@ func TestDistinctCheckID(t *testing.T) {

}

func TestService_InitFields(t *testing.T) {
func TestService_Canonicalize(t *testing.T) {
job := "example"
taskGroup := "cache"
task := "redis"
Expand All @@ -721,25 +721,25 @@ func TestService_InitFields(t *testing.T) {
Name: "${TASK}-db",
}

s.InitFields(job, taskGroup, task)
s.Canonicalize(job, taskGroup, task)
if s.Name != "redis-db" {
t.Fatalf("Expected name: %v, Actual: %v", "redis-db", s.Name)
}

s.Name = "db"
s.InitFields(job, taskGroup, task)
s.Canonicalize(job, taskGroup, task)
if s.Name != "db" {
t.Fatalf("Expected name: %v, Actual: %v", "redis-db", s.Name)
}

s.Name = "${JOB}-${TASKGROUP}-${TASK}-db"
s.InitFields(job, taskGroup, task)
s.Canonicalize(job, taskGroup, task)
if s.Name != "example-cache-redis-db" {
t.Fatalf("Expected name: %v, Actual: %v", "expample-cache-redis-db", s.Name)
}

s.Name = "${BASE}-db"
s.InitFields(job, taskGroup, task)
s.Canonicalize(job, taskGroup, task)
if s.Name != "example-cache-redis-db" {
t.Fatalf("Expected name: %v, Actual: %v", "expample-cache-redis-db", s.Name)
}
Expand Down Expand Up @@ -777,7 +777,7 @@ func TestJob_ExpandServiceNames(t *testing.T) {
},
}

j.InitFields()
j.Canonicalize()

service1Name := j.TaskGroups[0].Tasks[0].Services[0].Name
if service1Name != "my-job-web-frontend-default" {
Expand Down

0 comments on commit 288aea3

Please sign in to comment.