aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/PartialContainer.java
blob: 22146767e0178830c3c6d0881aaf4c7be2415c7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.container;

import com.yahoo.config.provision.DockerImage;

import java.time.Instant;
import java.util.Map;
import java.util.Objects;

/**
 * A partial container, containing only fields returned by a container list command such as 'podman ps'.
 *
 * @author mpolden
 */
public class PartialContainer {

    private final ContainerId id;
    private final ContainerName name;
    private final Instant createdAt;
    private final State state;
    private final String imageId;
    private final DockerImage image;
    private final Map<String, String> labels;
    private final int pid;
    private final boolean managed;

    public PartialContainer(ContainerId id, ContainerName name, Instant createdAt, State state, String imageId,
                            DockerImage image, Map<String, String> labels, int pid, boolean managed) {
        this.id = Objects.requireNonNull(id);
        this.name = Objects.requireNonNull(name);
        this.createdAt = Objects.requireNonNull(createdAt);
        this.state = Objects.requireNonNull(state);
        this.imageId = Objects.requireNonNull(imageId);
        this.image = Objects.requireNonNull(image);
        this.labels = Map.copyOf(Objects.requireNonNull(labels));
        this.pid = pid;
        this.managed = managed;
    }

    /** A unique identifier for this. Typically generated by the container engine */
    public ContainerId id() {
        return id;
    }

    /** The given name of this */
    public ContainerName name() {
        return name;
    }

    /** Timestamp when this container was created */
    public Instant createdAt() {
        return createdAt;
    }

    /** Current state of this */
    public State state() {
        return state;
    }

    /** A unique identifier for the image in use by this */
    public String imageId() {
        return imageId;
    }

    /** The image in use by this */
    public DockerImage image() {
        return image;
    }

    /** The labels set on this */
    public Map<String, String> labels() {
        return labels;
    }

    /** The PID of this */
    public int pid() {
        return pid;
    }

    /** Returns whether this container is managed by node-admin */
    public boolean managed() {
        return managed;
    }

    /** Returns the value of given label key */
    public String label(String key) {
        String labelValue = labels.get(key);
        if (labelValue == null) throw new IllegalArgumentException("No such label '" + key + "'");
        return labelValue;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PartialContainer that = (PartialContainer) o;
        return pid == that.pid && managed == that.managed && id.equals(that.id) && name.equals(that.name) && createdAt.equals(that.createdAt) && state == that.state && imageId.equals(that.imageId) && image.equals(that.image) && labels.equals(that.labels);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, createdAt, state, imageId, image, labels, pid, managed);
    }

    /** The state of a container */
    public enum State {

        unknown,
        configured,
        created,
        running,
        stopped,
        paused,
        exited,
        removing,
        stopping;

        public boolean isRunning() {
            return this == running;
        }

        public static Container.State from(String state) {
            switch (state) {
                case "unknown": return unknown;
                case "configured": return configured;
                case "created": return created;
                case "running": return running;
                case "stopped": return stopped;
                case "paused": return paused;
                case "exited": return exited;
                case "removing": return removing;
                case "stopping": return stopping;
            }
            throw new IllegalArgumentException("Invalid state '" + state + "'");
        }

    }

}