github.com/hernad/nomad@v1.6.112/nomad/structs/consul.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package structs 5 6 // Consul represents optional per-group consul configuration. 7 type Consul struct { 8 // Namespace in which to operate in Consul. 9 Namespace string 10 } 11 12 // Copy the Consul block. 13 func (c *Consul) Copy() *Consul { 14 if c == nil { 15 return nil 16 } 17 return &Consul{ 18 Namespace: c.Namespace, 19 } 20 } 21 22 // Equal returns whether c and o are the same. 23 func (c *Consul) Equal(o *Consul) bool { 24 if c == nil || o == nil { 25 return c == o 26 } 27 return c.Namespace == o.Namespace 28 } 29 30 // Validate returns whether c is valid. 31 func (c *Consul) Validate() error { 32 // nothing to do here 33 return nil 34 } 35 36 // ConsulUsage is provides meta information about how Consul is used by a job, 37 // noting which connect services and normal services will be registered, and 38 // whether the keystore will be read via template. 39 type ConsulUsage struct { 40 Services []string 41 KV bool 42 } 43 44 // Used returns true if Consul is used for registering services or reading from 45 // the keystore. 46 func (cu *ConsulUsage) Used() bool { 47 switch { 48 case cu.KV: 49 return true 50 case len(cu.Services) > 0: 51 return true 52 } 53 return false 54 } 55 56 // ConsulUsages returns a map from Consul namespace to things that will use Consul, 57 // including ConsulConnect TaskKinds, Consul Services from groups and tasks, and 58 // a boolean indicating if Consul KV is in use. 59 func (j *Job) ConsulUsages() map[string]*ConsulUsage { 60 m := make(map[string]*ConsulUsage) 61 62 for _, tg := range j.TaskGroups { 63 namespace := j.ConsulNamespace 64 if tgNamespace := tg.Consul.GetNamespace(); tgNamespace != "" { 65 namespace = tgNamespace 66 } 67 if _, exists := m[namespace]; !exists { 68 m[namespace] = new(ConsulUsage) 69 } 70 71 // Gather group services 72 for _, service := range tg.Services { 73 if service.Provider == ServiceProviderConsul { 74 m[namespace].Services = append(m[namespace].Services, service.Name) 75 } 76 } 77 78 // Gather task services and KV usage 79 for _, task := range tg.Tasks { 80 for _, service := range task.Services { 81 if service.Provider == ServiceProviderConsul { 82 m[namespace].Services = append(m[namespace].Services, service.Name) 83 } 84 } 85 if len(task.Templates) > 0 { 86 m[namespace].KV = true 87 } 88 } 89 } 90 91 return m 92 }