Repository: hashicorp/nomad
File: client/allocrunner/taskrunner/script_check_hook.go (Go)

Code before:
	checkIDs := h.arHookResources.GetConsulCheckIDs()
	interpolatedGroupServices := taskenv.InterpolateServices(h.taskEnv, tg.Services)
	for i, service := range interpolatedGroupServices {
		for j, check := range service.Checks {
			if check.Type != structs.ServiceCheckScript {
				continue
			}
			if !h.associated(h.task.Name, service.TaskName, check.TaskName) {
				continue
			}
			groupTaskName := "group-" + tg.Name
			serviceID := serviceregistration.MakeAllocServiceID(
				h.alloc.ID, groupTaskName, service)
			sc := newScriptCheck(&scriptCheckConfig{
				consulNamespace: h.groupConsulNamespace,
				allocID:         h.alloc.ID,
				taskName:        groupTaskName,
				check:           check,
				serviceID:       serviceID,
				ttlUpdater:      h.consul,
				driverExec:      h.driverExec,
				taskEnv:         h.taskEnv,
				logger:          h.logger,
				shutdownCh:      h.shutdownCh,
				isGroup:         true,
				checkID:         checkIDs[i][j],
			})
			if sc != nil {
				scriptChecks[sc.id] = sc
			}
		}
	}
	return scriptChecks
}

// associated returns true if the script check is associated with the task. This
// would be the case if the check.task is the same as task, or if the service.task
// is the same as the task _and_ check.task is not configured (i.e. the check
// inherits the task of the service).
func (*scriptCheckHook) associated(task, serviceTask, checkTask string) bool {
	if checkTask == task {
		return true
	}
	if serviceTask == task && checkTask == "" {
		return true
	}
	return false
}

// TTLUpdater is the subset of consul agent functionality needed by script
// checks to heartbeat

Review comment (suggestion):
The `GetConsulCheckIDs` function can return nil, do we need any bounds checks here? I think we can avoid it given that `checkIDs` and `interpolatedGroupServices` are built using the same data and ordering, but thought it was worth a check.

Code after:
	checkIDs := h.arHookResources.GetConsulCheckIDs()
	interpolatedGroupServices := taskenv.InterpolateServices(h.taskEnv, tg.Services)
	for i, service := range interpolatedGroupServices {
		for j, check := range service.Checks {
			if check.Type != structs.ServiceCheckScript {
				continue
			}
			if !h.associated(h.task.Name, service.TaskName, check.TaskName) {
				continue
			}
			groupTaskName := "group-" + tg.Name
			serviceID := serviceregistration.MakeAllocServiceID(
				h.alloc.ID, groupTaskName, service)
			var checkID string
			if checkIDs != nil {
				checkID = checkIDs[i][j]
			}
			sc := newScriptCheck(&scriptCheckConfig{
				consulNamespace: h.groupConsulNamespace,
				allocID:         h.alloc.ID,
				taskName:        groupTaskName,
				check:           check,
				serviceID:       serviceID,
				ttlUpdater:      h.consul,
				driverExec:      h.driverExec,
				taskEnv:         h.taskEnv,
				logger:          h.logger,
				shutdownCh:      h.shutdownCh,
				isGroup:         true,
				checkID:         checkID,
			})
			if sc != nil {
				scriptChecks[sc.id] = sc
			}
		}
	}
	return scriptChecks
}

// associated returns true if the script check is associated with the task. This
// would be the case if the check.task is the same as task, or if the service.task
// is the same as the task _and_ check.task is not configured (i.e. the check
// inherits the task of the service).
func (*scriptCheckHook) associated(task, serviceTask, checkTask string) bool {
	if checkTask == task {
		return true
	}
	if serviceTask == task && checkTask == "" {
		return true
	}
	return false