consul: refactor job mutation hook (#19699)

The job mutation logic for Nomad CE and Nomad ENT are nearly identical except
for a prelude that grabs the correct default cluster. Factor this out into a
method that can be shared between both code bases.
This commit is contained in:
Tim Gross
2024-01-10 16:29:05 -05:00
committed by GitHub
parent c9cd8480fa
commit 1254468600
2 changed files with 61 additions and 44 deletions

View File

@@ -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: "=",
}
}

View File

@@ -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
}