github.com/hernad/nomad@v1.6.112/nomad/structs/job.go (about)

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