Skip to content

Commit

Permalink
resource/job: set namespace on monitor
Browse files Browse the repository at this point in the history
When monitoring the job deployment and allocations the provider needs to
set the job namespace in the request instead of relying on the provider
configuration as it may be different from the job.
  • Loading branch information
lgfa29 committed Jul 12, 2023
1 parent 92d7b40 commit dd2b3f8
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions nomad/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func resourceJobRegister(d *schema.ResourceData, meta interface{}) error {

if d.Get("detach") == false && resp.EvalID != "" {
log.Printf("[DEBUG] will monitor scheduling/deployment of job '%s'", *job.ID)
deployment, err := monitorDeployment(client, timeout, resp.EvalID)
deployment, err := monitorDeployment(client, timeout, *job.Namespace, resp.EvalID)
if err != nil {
return fmt.Errorf(
"error waiting for job '%s' to schedule/deploy successfully: %s",
Expand All @@ -416,12 +416,12 @@ func resourceJobRegister(d *schema.ResourceData, meta interface{}) error {

// monitorDeployment monitors the evalution(s) from a job create/update and,
// if they result in a deployment, monitors that deployment until completion.
func monitorDeployment(client *api.Client, timeout time.Duration, initialEvalID string) (*api.Deployment, error) {
func monitorDeployment(client *api.Client, timeout time.Duration, namespace string, initialEvalID string) (*api.Deployment, error) {

stateConf := &resource.StateChangeConf{
Pending: []string{MonitoringEvaluation},
Target: []string{EvaluationComplete},
Refresh: evaluationStateRefreshFunc(client, initialEvalID),
Refresh: evaluationStateRefreshFunc(client, namespace, initialEvalID),
Timeout: timeout,
Delay: 0,
MinTimeout: 3 * time.Second,
Expand All @@ -441,7 +441,7 @@ func monitorDeployment(client *api.Client, timeout time.Duration, initialEvalID
stateConf = &resource.StateChangeConf{
Pending: []string{MonitoringDeployment},
Target: []string{DeploymentSuccessful},
Refresh: deploymentStateRefreshFunc(client, evaluation.DeploymentID),
Refresh: deploymentStateRefreshFunc(client, namespace, evaluation.DeploymentID),
Timeout: timeout,
Delay: 0,
MinTimeout: 5 * time.Second,
Expand All @@ -456,7 +456,7 @@ func monitorDeployment(client *api.Client, timeout time.Duration, initialEvalID

// evaluationStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// the evaluation(s) from a job create/update
func evaluationStateRefreshFunc(client *api.Client, initialEvalID string) resource.StateRefreshFunc {
func evaluationStateRefreshFunc(client *api.Client, namespace string, initialEvalID string) resource.StateRefreshFunc {

// evalID is the evaluation that we are currently monitoring. This will change
// along with follow-up evaluations.
Expand All @@ -465,7 +465,9 @@ func evaluationStateRefreshFunc(client *api.Client, initialEvalID string) resour
return func() (interface{}, string, error) {
// monitor the eval
log.Printf("[DEBUG] monitoring evaluation '%s'", evalID)
eval, _, err := client.Evaluations().Info(evalID, nil)
eval, _, err := client.Evaluations().Info(evalID, &api.QueryOptions{
Namespace: namespace,
})
if err != nil {
log.Printf("[ERROR] error on Evaluation.Info during deploymentStateRefresh: %s", err)
return nil, "", err
Expand Down Expand Up @@ -494,11 +496,13 @@ func evaluationStateRefreshFunc(client *api.Client, initialEvalID string) resour

// deploymentStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// the deployment from a job create/update
func deploymentStateRefreshFunc(client *api.Client, deploymentID string) resource.StateRefreshFunc {
func deploymentStateRefreshFunc(client *api.Client, namespace string, deploymentID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
// monitor the deployment
var state string
deployment, _, err := client.Deployments().Info(deploymentID, nil)
deployment, _, err := client.Deployments().Info(deploymentID, &api.QueryOptions{
Namespace: namespace,
})
if err != nil {
log.Printf("[ERROR] error on Deployment.Info during deploymentStateRefresh: %s", err)
return nil, "", err
Expand Down Expand Up @@ -576,7 +580,7 @@ func resourceJobRead(d *schema.ResourceData, meta interface{}) error {
}
log.Printf("[DEBUG] found job %q in namespace %q", *job.Name, *job.Namespace)

allocStubs, _, err := client.Jobs().Allocations(id, false, nil)
allocStubs, _, err := client.Jobs().Allocations(id, false, opts)
if err != nil {
log.Printf("[WARN] error listing allocations for Job %q, will return empty list", id)
}
Expand Down

0 comments on commit dd2b3f8

Please sign in to comment.