github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/structs/job.go (about) 1 package structs 2 3 import ( 4 "github.com/hashicorp/go-set" 5 ) 6 7 const ( 8 // JobServiceRegistrationsRPCMethod is the RPC method for listing all 9 // service registrations assigned to a specific namespaced job. 10 // 11 // Args: JobServiceRegistrationsRequest 12 // Reply: JobServiceRegistrationsResponse 13 JobServiceRegistrationsRPCMethod = "Job.GetServiceRegistrations" 14 ) 15 16 // JobServiceRegistrationsRequest is the request object used to list all 17 // service registrations belonging to the specified Job.ID. 18 type JobServiceRegistrationsRequest struct { 19 JobID string 20 QueryOptions 21 } 22 23 // JobServiceRegistrationsResponse is the response object when performing a 24 // listing of services belonging to a namespaced job. 25 type JobServiceRegistrationsResponse struct { 26 Services []*ServiceRegistration 27 QueryMeta 28 } 29 30 // NativeServiceDiscoveryUsage tracks which groups make use of the nomad service 31 // discovery provider, and also which of those groups make use of checks. This 32 // information will be used to configure implicit constraints on the job. 33 type NativeServiceDiscoveryUsage struct { 34 Basic *set.Set[string] // implies v1.3.0 + ${attr.nomad.service_discovery} 35 Checks *set.Set[string] // implies v1.4.0 36 } 37 38 // Empty returns true if no groups are using native service discovery. 39 func (u *NativeServiceDiscoveryUsage) Empty() bool { 40 return u.Basic.Size() == 0 && u.Checks.Size() == 0 41 } 42 43 // RequiredNativeServiceDiscovery identifies which task groups, if any, within 44 // the job are utilising Nomad native service discovery. 45 func (j *Job) RequiredNativeServiceDiscovery() *NativeServiceDiscoveryUsage { 46 basic := set.New[string](10) 47 checks := set.New[string](10) 48 49 for _, tg := range j.TaskGroups { 50 // It is possible for services using the Nomad provider to be 51 // configured at the group level, so check here first. 52 requiresNativeServiceDiscovery(tg.Name, tg.Services, basic, checks) 53 54 // Iterate the tasks within the task group to check the services 55 // configured at this more traditional level. 56 for _, task := range tg.Tasks { 57 requiresNativeServiceDiscovery(tg.Name, task.Services, basic, checks) 58 } 59 } 60 return &NativeServiceDiscoveryUsage{ 61 Basic: basic, 62 Checks: checks, 63 } 64 } 65 66 // requiresNativeServiceDiscovery identifies whether any of the services passed 67 // to the function are utilising Nomad native service discovery. 68 func requiresNativeServiceDiscovery(group string, services []*Service, basic, checks *set.Set[string]) { 69 for _, tgService := range services { 70 if tgService.Provider == ServiceProviderNomad { 71 basic.Insert(group) 72 if len(tgService.Checks) > 0 { 73 checks.Insert(group) 74 } 75 } 76 } 77 } 78 79 // RequiredConsulServiceDiscovery identifies which task groups, if any, within 80 // the job are utilising Consul service discovery. 81 func (j *Job) RequiredConsulServiceDiscovery() map[string]bool { 82 groups := make(map[string]bool) 83 84 for _, tg := range j.TaskGroups { 85 86 // It is possible for services using the Consul provider to be 87 // configured at the task group level, so check here first. This is 88 // a requirement for Consul Connect services. 89 if requiresConsulServiceDiscovery(tg.Services) { 90 groups[tg.Name] = true 91 continue 92 } 93 94 // Iterate the tasks within the task group to check the services 95 // configured at this more traditional level. 96 for _, task := range tg.Tasks { 97 if requiresConsulServiceDiscovery(task.Services) { 98 groups[tg.Name] = true 99 continue 100 } 101 } 102 } 103 104 return groups 105 } 106 107 // requiresConsulServiceDiscovery identifies whether any of the services passed 108 // to the function are utilising Consul service discovery. 109 func requiresConsulServiceDiscovery(services []*Service) bool { 110 for _, tgService := range services { 111 if tgService.Provider == ServiceProviderConsul || tgService.Provider == "" { 112 return true 113 } 114 } 115 return false 116 }