Skip to content

Commit 5abffd3

Browse files
committed
Add annotations to list and state output
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent 88bb59e commit 5abffd3

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

libcontainer/specconv/spec_linux.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,16 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
165165
if !filepath.IsAbs(rootfsPath) {
166166
rootfsPath = filepath.Join(cwd, rootfsPath)
167167
}
168+
labels := []string{}
169+
for k, v := range spec.Annotations {
170+
labels = append(labels, fmt.Sprintf("%s=%s", k, v))
171+
}
168172
config := &configs.Config{
169173
Rootfs: rootfsPath,
170174
NoPivotRoot: opts.NoPivotRoot,
171175
Readonlyfs: spec.Root.Readonly,
172176
Hostname: spec.Hostname,
173-
Labels: []string{
174-
"bundle=" + cwd,
175-
},
177+
Labels: append(labels, fmt.Sprintf("bundle=%s", cwd)),
176178
}
177179

178180
exists := false

libcontainer/utils/utils.go

+19
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,22 @@ func SearchLabels(labels []string, query string) string {
100100
}
101101
return ""
102102
}
103+
104+
// Annotations returns the bundle path and user defined annotations from the
105+
// libcontianer state. We need to remove the bundle because that is a label
106+
// added by libcontainer.
107+
func Annotations(labels []string) (bundle string, userAnnotations map[string]string) {
108+
userAnnotations = make(map[string]string)
109+
for _, l := range labels {
110+
parts := strings.SplitN(l, "=", 2)
111+
if len(parts) < 2 {
112+
continue
113+
}
114+
if parts[0] == "bundle" {
115+
bundle = parts[1]
116+
} else {
117+
userAnnotations[parts[0]] = parts[1]
118+
}
119+
}
120+
return
121+
}

list.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type containerState struct {
3131
Bundle string `json:"bundle"`
3232
// Created is the unix timestamp for the creation time of the container in UTC
3333
Created time.Time `json:"created"`
34+
// Annotations is the user defined annotations added to the config.
35+
Annotations map[string]string `json:"annotations,omitempty"`
3436
}
3537

3638
var listCommand = cli.Command{
@@ -116,12 +118,15 @@ func getContainers(context *cli.Context) ([]containerState, error) {
116118
if err != nil {
117119
return nil, err
118120
}
121+
bundle, annotations := utils.Annotations(state.Config.Labels)
119122
s = append(s, containerState{
120123
ID: state.BaseState.ID,
121124
InitProcessPid: state.BaseState.InitProcessPid,
122125
Status: containerStatus.String(),
123-
Bundle: utils.SearchLabels(state.Config.Labels, "bundle"),
124-
Created: state.BaseState.Created})
126+
Bundle: bundle,
127+
Created: state.BaseState.Created,
128+
Annotations: annotations,
129+
})
125130
}
126131
}
127132
return s, nil

state.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type cState struct {
3030
Status string `json:"status"`
3131
// Created is the unix timestamp for the creation time of the container in UTC
3232
Created time.Time `json:"created"`
33+
// Annotations is the user defined annotations added to the config.
34+
Annotations map[string]string `json:"annotations,omitempty"`
3335
}
3436

3537
var stateCommand = cli.Command{
@@ -53,14 +55,17 @@ instance of a container.`,
5355
if err != nil {
5456
return err
5557
}
58+
bundle, annotations := utils.Annotations(state.Config.Labels)
5659
cs := cState{
5760
Version: state.BaseState.Config.Version,
5861
ID: state.BaseState.ID,
5962
InitProcessPid: state.BaseState.InitProcessPid,
6063
Status: containerStatus.String(),
61-
Bundle: utils.SearchLabels(state.Config.Labels, "bundle"),
64+
Bundle: bundle,
6265
Rootfs: state.BaseState.Config.Rootfs,
63-
Created: state.BaseState.Created}
66+
Created: state.BaseState.Created,
67+
Annotations: annotations,
68+
}
6469
data, err := json.MarshalIndent(cs, "", " ")
6570
if err != nil {
6671
return err

0 commit comments

Comments
 (0)