Skip to content

Commit

Permalink
Backport of consul: refactor job mutation hook into release/1.7.x #19704
Browse files Browse the repository at this point in the history


Co-authored-by: Tim Gross <[email protected]>
  • Loading branch information
hc-github-team-nomad-core and tgross authored Jan 10, 2024
1 parent b4096b6 commit 3fbc2c3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 44 deletions.
60 changes: 60 additions & 0 deletions nomad/job_endpoint_hook_consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,63 @@ func (jobConsulHook) validateTaskPartitionMatchesGroup(groupPartition string, ta
}
return nil
}

// mutateImpl ensures that the job's Consul blocks have been configured with the
// correct Consul cluster if unset, and sets constraints on the Consul admin
// partition if set. This should be called by the Mutate method.
func (jobConsulHook) mutateImpl(job *structs.Job, defaultCluster string) *structs.Job {
for _, group := range job.TaskGroups {
if group.Consul != nil {
if group.Consul.Cluster == "" {
group.Consul.Cluster = defaultCluster
}
if group.Consul.Partition != "" {
group.Constraints = append(group.Constraints,
newConsulPartitionConstraint(group.Consul.Cluster, group.Consul.Partition))
}
}

for _, service := range group.Services {
if service.IsConsul() && service.Cluster == "" {
service.Cluster = defaultCluster
}
}

for _, task := range group.Tasks {
if task.Consul != nil {
if task.Consul.Cluster == "" {
task.Consul.Cluster = defaultCluster
}
if task.Consul.Partition != "" {
task.Constraints = append(task.Constraints,
newConsulPartitionConstraint(task.Consul.Cluster, task.Consul.Partition))
}
}
for _, service := range task.Services {
if service.IsConsul() && service.Cluster == "" {
service.Cluster = defaultCluster
}
}
}
}

return job
}

// newConsulPartitionConstraint produces a constraint on the Consul admin
// partition, based on the cluster name. In Nomad CE this will always be in the
// default cluster.
func newConsulPartitionConstraint(cluster, partition string) *structs.Constraint {
if cluster == structs.ConsulDefaultCluster || cluster == "" {
return &structs.Constraint{
LTarget: "${attr.consul.partition}",
RTarget: partition,
Operand: "=",
}
}
return &structs.Constraint{
LTarget: "${attr.consul." + cluster + ".partition}",
RTarget: partition,
Operand: "=",
}
}
45 changes: 1 addition & 44 deletions nomad/job_endpoint_hook_consul_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,51 +96,8 @@ func (h jobConsulHook) validateCluster(name string) error {
return nil
}

func consulPartitionConstraint(partition string) *structs.Constraint {
return &structs.Constraint{
LTarget: "${attr.consul.partition}",
RTarget: partition,
Operand: "=",
}
}

// Mutate ensures that the job's Consul cluster has been configured to be the
// default Consul cluster if unset
func (j jobConsulHook) Mutate(job *structs.Job) (*structs.Job, []error, error) {
for _, group := range job.TaskGroups {
if group.Consul != nil {
if group.Consul.Cluster == "" {
group.Consul.Cluster = structs.ConsulDefaultCluster
}
if group.Consul.Partition != "" {
group.Constraints = append(group.Constraints,
consulPartitionConstraint(group.Consul.Partition))
}
}

for _, service := range group.Services {
if service.IsConsul() && service.Cluster == "" {
service.Cluster = structs.ConsulDefaultCluster
}
}

for _, task := range group.Tasks {
if task.Consul != nil {
if task.Consul.Cluster == "" {
task.Consul.Cluster = structs.ConsulDefaultCluster
}
if task.Consul.Partition != "" {
task.Constraints = append(task.Constraints,
consulPartitionConstraint(task.Consul.Partition))
}
}
for _, service := range task.Services {
if service.IsConsul() && service.Cluster == "" {
service.Cluster = structs.ConsulDefaultCluster
}
}
}
}

return job, nil, nil
return j.mutateImpl(job, structs.ConsulDefaultCluster), nil, nil
}

0 comments on commit 3fbc2c3

Please sign in to comment.