func trackChange()

in pkg/plan/track_change.go [58:112]


func trackChange(params TrackChangeParams, c chan<- TrackResponse, ticker *time.Ticker) {
	// Close the channel before the function returns. This particularly
	// important so that clients consuming this channel can use it in
	// a for loop and assume that when the foor loop ends, the change is
	// complete.
	defer close(c)

	// retries is used as a simple counter which is incremented every time an
	// error occurs, or when the returned pending plan slice is 0.
	var retries int

	// changedResources is a list of resource IDs which have been seen to have
	// a pending plan. It's used to filter out any resources which weren't
	// part of the last plan change.
	var changedResources []string
	for range ticker.C {
		// After the retries number is higher or equal to MaxRetries, the plan
		// changed is considered complete. In which case, the current plan or
		// the last plan in the plan history is checked to obtain the last plan
		// step log, and decode any errors which might've happened or mark the
		// plan as succeeded.
		if retries >= params.Config.MaxRetries {
			var checkRetries int
			checkCurrentStatus(params, c, changedResources, checkRetries)
			return
		}

		res, err := params.V1API.Deployments.GetDeployment(
			deployments.NewGetDeploymentParams().
				WithDeploymentID(params.DeploymentID).
				WithShowPlanLogs(ec.Bool(true)).
				WithShowPlans(ec.Bool(true)),
			params.AuthWriter,
		)
		if err != nil {
			retries++
			continue
		}

		var plans = buildTrackResponse(res.Payload.Resources, false)
		if len(plans) == 0 {
			retries++
		}

		for _, p := range plans {
			changedResources = append(changedResources, p.ID)
			p.DeploymentID = *res.Payload.ID
			ignoreChange := params.ResourceID != p.ID && params.IgnoreDownstream
			if ignoreChange {
				continue
			}
			c <- p
		}
	}
}