github.com/lalkh/containerd@v1.4.3/metadata/adaptors.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package metadata 18 19 import ( 20 "strings" 21 22 "github.com/containerd/containerd/containers" 23 "github.com/containerd/containerd/content" 24 "github.com/containerd/containerd/filters" 25 "github.com/containerd/containerd/images" 26 "github.com/containerd/containerd/leases" 27 "github.com/containerd/containerd/snapshots" 28 ) 29 30 func adaptImage(o interface{}) filters.Adaptor { 31 obj := o.(images.Image) 32 return filters.AdapterFunc(func(fieldpath []string) (string, bool) { 33 if len(fieldpath) == 0 { 34 return "", false 35 } 36 37 switch fieldpath[0] { 38 case "name": 39 return obj.Name, len(obj.Name) > 0 40 case "target": 41 if len(fieldpath) < 2 { 42 return "", false 43 } 44 45 switch fieldpath[1] { 46 case "digest": 47 return obj.Target.Digest.String(), len(obj.Target.Digest) > 0 48 case "mediatype": 49 return obj.Target.MediaType, len(obj.Target.MediaType) > 0 50 } 51 case "labels": 52 return checkMap(fieldpath[1:], obj.Labels) 53 // TODO(stevvooe): Greater/Less than filters would be awesome for 54 // size. Let's do it! 55 case "annotations": 56 return checkMap(fieldpath[1:], obj.Target.Annotations) 57 } 58 59 return "", false 60 }) 61 } 62 func adaptContainer(o interface{}) filters.Adaptor { 63 obj := o.(containers.Container) 64 return filters.AdapterFunc(func(fieldpath []string) (string, bool) { 65 if len(fieldpath) == 0 { 66 return "", false 67 } 68 69 switch fieldpath[0] { 70 case "id": 71 return obj.ID, len(obj.ID) > 0 72 case "runtime": 73 if len(fieldpath) <= 1 { 74 return "", false 75 } 76 77 switch fieldpath[1] { 78 case "name": 79 return obj.Runtime.Name, len(obj.Runtime.Name) > 0 80 default: 81 return "", false 82 } 83 case "image": 84 return obj.Image, len(obj.Image) > 0 85 case "labels": 86 return checkMap(fieldpath[1:], obj.Labels) 87 } 88 89 return "", false 90 }) 91 } 92 93 func adaptContentInfo(info content.Info) filters.Adaptor { 94 return filters.AdapterFunc(func(fieldpath []string) (string, bool) { 95 if len(fieldpath) == 0 { 96 return "", false 97 } 98 99 switch fieldpath[0] { 100 case "digest": 101 return info.Digest.String(), true 102 case "size": 103 // TODO: support size based filtering 104 case "labels": 105 return checkMap(fieldpath[1:], info.Labels) 106 } 107 108 return "", false 109 }) 110 } 111 112 func adaptContentStatus(status content.Status) filters.Adaptor { 113 return filters.AdapterFunc(func(fieldpath []string) (string, bool) { 114 if len(fieldpath) == 0 { 115 return "", false 116 } 117 switch fieldpath[0] { 118 case "ref": 119 return status.Ref, true 120 } 121 122 return "", false 123 }) 124 } 125 126 func adaptLease(lease leases.Lease) filters.Adaptor { 127 return filters.AdapterFunc(func(fieldpath []string) (string, bool) { 128 if len(fieldpath) == 0 { 129 return "", false 130 } 131 132 switch fieldpath[0] { 133 case "id": 134 return lease.ID, len(lease.ID) > 0 135 case "labels": 136 return checkMap(fieldpath[1:], lease.Labels) 137 } 138 139 return "", false 140 }) 141 } 142 143 func adaptSnapshot(info snapshots.Info) filters.Adaptor { 144 return filters.AdapterFunc(func(fieldpath []string) (string, bool) { 145 if len(fieldpath) == 0 { 146 return "", false 147 } 148 149 switch fieldpath[0] { 150 case "kind": 151 switch info.Kind { 152 case snapshots.KindActive: 153 return "active", true 154 case snapshots.KindView: 155 return "view", true 156 case snapshots.KindCommitted: 157 return "committed", true 158 } 159 case "name": 160 return info.Name, true 161 case "parent": 162 return info.Parent, true 163 case "labels": 164 return checkMap(fieldpath[1:], info.Labels) 165 } 166 167 return "", false 168 }) 169 } 170 171 func checkMap(fieldpath []string, m map[string]string) (string, bool) { 172 if len(m) == 0 { 173 return "", false 174 } 175 176 value, ok := m[strings.Join(fieldpath, ".")] 177 return value, ok 178 }