Skip to content

Commit

Permalink
Merge pull request #12189 from hashicorp/f-gh-264
Browse files Browse the repository at this point in the history
jobspec: add service block provider parameter
  • Loading branch information
jrasell authored Mar 14, 2022
2 parents b10547d + 9215d0c commit 4251860
Show file tree
Hide file tree
Showing 14 changed files with 617 additions and 65 deletions.
1 change: 1 addition & 0 deletions api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ func TestJobs_Canonicalize(t *testing.T) {
PortLabel: "db",
AddressMode: "auto",
OnUpdate: "require_healthy",
Provider: "consul",
Checks: []ServiceCheck{
{
Name: "alive",
Expand Down
14 changes: 14 additions & 0 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,21 @@ type Service struct {
CanaryMeta map[string]string `hcl:"canary_meta,block"`
TaskName string `mapstructure:"task" hcl:"task,optional"`
OnUpdate string `mapstructure:"on_update" hcl:"on_update,optional"`

// Provider defines which backend system provides the service registration
// mechanism for this service. This supports either structs.ProviderConsul
// or structs.ProviderNomad and defaults for the former.
Provider string `hcl:"provider,optional"`
}

const (
OnUpdateRequireHealthy = "require_healthy"
OnUpdateIgnoreWarn = "ignore_warnings"
OnUpdateIgnore = "ignore"

// ServiceProviderConsul is the default provider for services when no
// parameter is set.
ServiceProviderConsul = "consul"
)

// Canonicalize the Service by ensuring its name and address mode are set. Task
Expand All @@ -145,6 +154,11 @@ func (s *Service) Canonicalize(t *Task, tg *TaskGroup, job *Job) {
s.OnUpdate = OnUpdateRequireHealthy
}

// Default the service provider.
if s.Provider == "" {
s.Provider = ServiceProviderConsul
}

s.Connect.Canonicalize()

// Canonicalize CheckRestart on Checks and merge Service.CheckRestart
Expand Down
1 change: 1 addition & 0 deletions api/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestService_Canonicalize(t *testing.T) {
require.Equal(t, fmt.Sprintf("%s-%s-%s", *j.Name, *tg.Name, task.Name), s.Name)
require.Equal(t, "auto", s.AddressMode)
require.Equal(t, OnUpdateRequireHealthy, s.OnUpdate)
require.Equal(t, ServiceProviderConsul, s.Provider)
}

func TestServiceCheck_Canonicalize(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ func ApiServicesToStructs(in []*api.Service, group bool) []*structs.Service {
Meta: helper.CopyMapStringString(s.Meta),
CanaryMeta: helper.CopyMapStringString(s.CanaryMeta),
OnUpdate: s.OnUpdate,
Provider: s.Provider,
}

if l := len(s.Checks); l != 0 {
Expand Down
2 changes: 2 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2902,6 +2902,7 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Services: []*structs.Service{
{
Name: "groupserviceA",
Provider: "consul",
Tags: []string{"a", "b"},
CanaryTags: []string{"d", "e"},
EnableTagOverride: true,
Expand Down Expand Up @@ -2993,6 +2994,7 @@ func TestJobs_ApiJobToStructsJob(t *testing.T) {
Services: []*structs.Service{
{
Name: "serviceA",
Provider: "consul",
Tags: []string{"1", "2"},
CanaryTags: []string{"3", "4"},
EnableTagOverride: true,
Expand Down
1 change: 1 addition & 0 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func parseService(o *ast.ObjectItem) (*api.Service, error) {
"meta",
"canary_meta",
"on_update",
"provider",
}
if err := checkHCLKeys(o.Val, valid); err != nil {
return nil, err
Expand Down
26 changes: 26 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,32 @@ func TestParse(t *testing.T) {
},
false,
},
{
"service-provider.hcl",
&api.Job{
ID: stringToPtr("service-provider"),
Name: stringToPtr("service-provider"),
TaskGroups: []*api.TaskGroup{
{
Count: intToPtr(5),
Name: stringToPtr("group"),
Tasks: []*api.Task{
{
Name: "task",
Driver: "docker",
Services: []*api.Service{
{
Name: "service-provider",
Provider: "nomad",
},
},
},
},
},
},
},
false,
},
}

for _, tc := range cases {
Expand Down
14 changes: 14 additions & 0 deletions jobspec/test-fixtures/service-provider.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
job "service-provider" {
group "group" {
count = 5

task "task" {
driver = "docker"

service {
name = "service-provider"
provider = "nomad"
}
}
}
}
8 changes: 6 additions & 2 deletions nomad/structs/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ func (j *Job) ConsulUsages() map[string]*ConsulUsage {

// Gather group services
for _, service := range tg.Services {
m[namespace].Services = append(m[namespace].Services, service.Name)
if service.Provider == ServiceProviderConsul {
m[namespace].Services = append(m[namespace].Services, service.Name)
}
}

// Gather task services and KV usage
for _, task := range tg.Tasks {
for _, service := range task.Services {
m[namespace].Services = append(m[namespace].Services, service.Name)
if service.Provider == ServiceProviderConsul {
m[namespace].Services = append(m[namespace].Services, service.Name)
}
}
if len(task.Templates) > 0 {
m[namespace].KV = true
Expand Down
103 changes: 103 additions & 0 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,12 @@ func TestTaskGroupDiff(t *testing.T) {
Old: "",
New: "",
},
{
Type: DiffTypeNone,
Name: "Provider",
Old: "",
New: "",
},
{
Type: DiffTypeEdited,
Name: "TaskName",
Expand Down Expand Up @@ -5601,6 +5607,10 @@ func TestTaskDiff(t *testing.T) {
Old: "foo",
New: "bar",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeAdded,
Name: "TaskName",
Expand Down Expand Up @@ -5745,6 +5755,10 @@ func TestTaskDiff(t *testing.T) {
Type: DiffTypeNone,
Name: "PortLabel",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Name: "TaskName",
Expand Down Expand Up @@ -6264,6 +6278,10 @@ func TestTaskDiff(t *testing.T) {
Old: "",
New: "",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Name: "TaskName",
Expand Down Expand Up @@ -7347,6 +7365,10 @@ func TestServicesDiff(t *testing.T) {
Old: "http",
New: "https",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Name: "TaskName",
Expand Down Expand Up @@ -7432,6 +7454,10 @@ func TestServicesDiff(t *testing.T) {
Name: "PortLabel",
New: "http",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Name: "TaskName",
Expand Down Expand Up @@ -7491,6 +7517,10 @@ func TestServicesDiff(t *testing.T) {
Name: "PortLabel",
New: "https",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Name: "TaskName",
Expand Down Expand Up @@ -7556,6 +7586,9 @@ func TestServicesDiff(t *testing.T) {
Name: "PortLabel",
Old: "http",
New: "https-redirect",
}, {
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Expand Down Expand Up @@ -7627,6 +7660,10 @@ func TestServicesDiff(t *testing.T) {
Old: "http",
New: "http",
},
{
Type: DiffTypeNone,
Name: "Provider",
},
{
Type: DiffTypeNone,
Name: "TaskName",
Expand Down Expand Up @@ -7654,6 +7691,72 @@ func TestServicesDiff(t *testing.T) {
},
},
},
{
Name: "Service with different provider",
Contextual: true,
Old: []*Service{
{
Name: "webapp",
Provider: "nomad",
PortLabel: "http",
},
},
New: []*Service{
{
Name: "webapp",
Provider: "consul",
PortLabel: "http",
},
},
Expected: []*ObjectDiff{
{
Type: DiffTypeEdited,
Name: "Service",
Fields: []*FieldDiff{
{
Type: DiffTypeNone,
Name: "AddressMode",
},
{
Type: DiffTypeNone,
Name: "EnableTagOverride",
Old: "false",
New: "false",
},
{
Type: DiffTypeNone,
Name: "Name",
Old: "webapp",
New: "webapp",
},
{
Type: DiffTypeNone,
Name: "Namespace",
},
{
Type: DiffTypeNone,
Name: "OnUpdate",
},
{
Type: DiffTypeNone,
Name: "PortLabel",
Old: "http",
New: "http",
},
{
Type: DiffTypeEdited,
Name: "Provider",
Old: "nomad",
New: "consul",
},
{
Type: DiffTypeNone,
Name: "TaskName",
},
},
},
},
},
}

for _, c := range cases {
Expand Down
Loading

0 comments on commit 4251860

Please sign in to comment.