/
criobject.go
90 lines (78 loc) · 1.63 KB
/
criobject.go
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
package lxf
import (
"strconv"
"time"
"github.com/lxc/lxd/shared/api"
)
const (
cfgIsCRI = "user.cri"
cfgLabels = "user.labels"
cfgAnnotations = "user.annotations"
cfgState = "user.state"
cfgMetadata = "user.metadata"
cfgMetaAttempt = cfgMetadata + ".attempt"
cfgMetaName = cfgMetadata + ".name"
cfgMetaNamespace = cfgMetadata + ".namespace"
cfgMetaUID = cfgMetadata + ".uid"
cfgVolatile = "volatile"
)
var (
reservedConfigCRI = []string{
cfgSchema,
cfgIsCRI,
cfgCreatedAt,
}
reservedConfigPrefixesCRI = []string{
cfgLabels,
cfgAnnotations,
cfgMetadata,
}
)
// CRIObject contains common properties of containers and sandboxes
type CRIObject struct {
// Labels and Annotations to be saved provided by CRI
Labels map[string]string
Annotations map[string]string
// CreatedAt is when the resource was created
CreatedAt time.Time
}
// IsCRI checks if a object is a cri object
func (l *client) IsCRI(i interface{}) bool { // nolint: cyclop
if !IsSchemaCurrent(i) {
return false
}
var (
val string
has bool
)
switch o := i.(type) {
case api.Container:
if val, has = o.Config[cfgIsCRI]; !has {
return false
}
case *api.Container:
return l.IsCRI(*o)
case api.Profile:
if val, has = o.Config[cfgIsCRI]; !has {
return false
}
case *api.Profile:
return l.IsCRI(*o)
case api.Image:
if l.critestMode {
return isCRITestImage(o)
}
if val, has = o.Properties[cfgIsCRI]; !has {
return false
}
case *api.Image:
return l.IsCRI(*o)
default:
return false
}
is, err := strconv.ParseBool(val)
if err != nil {
return false
}
return is
}