Skip to content

Commit

Permalink
validate connect block allowed only within group.service
Browse files Browse the repository at this point in the history
  • Loading branch information
roccoblues committed Jan 20, 2021
1 parent f577134 commit bbebfeb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,12 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup,
}
}
}

// Task services can't have a connect block. We still convert it so that
// we can later return a validation error.
if service.Connect != nil {
structsTask.Services[i].Connect = ApiConsulConnectToStructs(service.Connect)
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6954,6 +6954,11 @@ func validateServices(t *Task, tgNetworks Networks) error {
}
}

// connect block is only allowed on group level
if service.Connect != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("service %q cannot have \"connect\" block, only services defined in a \"group\" block can", service.Name))
}

// Ensure that check names are unique and have valid ports
knownChecks := make(map[string]struct{})
for _, check := range service.Checks {
Expand Down
7 changes: 7 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,13 @@ func TestTask_Validate_Service_Check_AddressMode(t *testing.T) {
},
ErrContains: `invalid: check requires a port but neither check nor service`,
},
{
Service: &Service{
Name: "conect-block-on-task-level",
Connect: &ConsulConnect{SidecarService: &ConsulSidecarService{}},
},
ErrContains: `cannot have "connect" block`,
},
}

for _, tc := range cases {
Expand Down

0 comments on commit bbebfeb

Please sign in to comment.