| 129 | } |
| 130 | |
| 131 | func (m *monitor) monitor(ctx context.Context) ([]change, error) { |
| 132 | containers, err := m.client.Containers(ctx, fmt.Sprintf("labels.%q", restart.StatusLabel)) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | var changes []change |
| 137 | for _, c := range containers { |
| 138 | var ( |
| 139 | task containerd.Task |
| 140 | status containerd.Status |
| 141 | err error |
| 142 | ) |
| 143 | labels, err := c.Labels(ctx) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | desiredStatus := containerd.ProcessStatus(labels[restart.StatusLabel]) |
| 148 | if task, err = c.Task(ctx, nil); err == nil { |
| 149 | if status, err = task.Status(ctx); err == nil { |
| 150 | if desiredStatus == status.Status { |
| 151 | continue |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Task or Status return error, only desired to running |
| 157 | if err != nil { |
| 158 | log.G(ctx).WithError(err).Error("monitor") |
| 159 | if desiredStatus == containerd.Stopped { |
| 160 | continue |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Known issue: |
| 165 | // The status may be empty when task failed but was deleted, |
| 166 | // which will result in an `on-failure` restart policy reconcile error. |
| 167 | switch desiredStatus { |
| 168 | case containerd.Running: |
| 169 | switch status.Status { |
| 170 | case containerd.Paused, containerd.Pausing: |
| 171 | continue |
| 172 | default: |
| 173 | } |
| 174 | if !restart.Reconcile(status, labels) { |
| 175 | continue |
| 176 | } |
| 177 | |
| 178 | restartCount, _ := strconv.Atoi(labels[restart.CountLabel]) |
| 179 | if labels["containerd.io/restart.logpath"] != "" { |
| 180 | log.G(ctx).Warn(`Label "containerd.io/restart.logpath" is no longer supported since containerd v2.0. Use "containerd.io/restart.loguri" instead.`) |
| 181 | } |
| 182 | changes = append(changes, &startChange{ |
| 183 | container: c, |
| 184 | logURI: labels[restart.LogURILabel], |
| 185 | count: restartCount + 1, |
| 186 | }) |
| 187 | case containerd.Stopped: |
| 188 | changes = append(changes, &stopChange{ |