github.com/demonoid81/containerd@v1.3.4/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  )
    28  
    29  func adaptImage(o interface{}) filters.Adaptor {
    30  	obj := o.(images.Image)
    31  	return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
    32  		if len(fieldpath) == 0 {
    33  			return "", false
    34  		}
    35  
    36  		switch fieldpath[0] {
    37  		case "name":
    38  			return obj.Name, len(obj.Name) > 0
    39  		case "target":
    40  			if len(fieldpath) < 2 {
    41  				return "", false
    42  			}
    43  
    44  			switch fieldpath[1] {
    45  			case "digest":
    46  				return obj.Target.Digest.String(), len(obj.Target.Digest) > 0
    47  			case "mediatype":
    48  				return obj.Target.MediaType, len(obj.Target.MediaType) > 0
    49  			}
    50  		case "labels":
    51  			return checkMap(fieldpath[1:], obj.Labels)
    52  			// TODO(stevvooe): Greater/Less than filters would be awesome for
    53  			// size. Let's do it!
    54  		case "annotations":
    55  			return checkMap(fieldpath[1:], obj.Target.Annotations)
    56  		}
    57  
    58  		return "", false
    59  	})
    60  }
    61  func adaptContainer(o interface{}) filters.Adaptor {
    62  	obj := o.(containers.Container)
    63  	return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
    64  		if len(fieldpath) == 0 {
    65  			return "", false
    66  		}
    67  
    68  		switch fieldpath[0] {
    69  		case "id":
    70  			return obj.ID, len(obj.ID) > 0
    71  		case "runtime":
    72  			if len(fieldpath) <= 1 {
    73  				return "", false
    74  			}
    75  
    76  			switch fieldpath[1] {
    77  			case "name":
    78  				return obj.Runtime.Name, len(obj.Runtime.Name) > 0
    79  			default:
    80  				return "", false
    81  			}
    82  		case "image":
    83  			return obj.Image, len(obj.Image) > 0
    84  		case "labels":
    85  			return checkMap(fieldpath[1:], obj.Labels)
    86  		}
    87  
    88  		return "", false
    89  	})
    90  }
    91  
    92  func adaptContentInfo(info content.Info) filters.Adaptor {
    93  	return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
    94  		if len(fieldpath) == 0 {
    95  			return "", false
    96  		}
    97  
    98  		switch fieldpath[0] {
    99  		case "digest":
   100  			return info.Digest.String(), true
   101  		case "size":
   102  			// TODO: support size based filtering
   103  		case "labels":
   104  			return checkMap(fieldpath[1:], info.Labels)
   105  		}
   106  
   107  		return "", false
   108  	})
   109  }
   110  
   111  func adaptContentStatus(status content.Status) filters.Adaptor {
   112  	return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
   113  		if len(fieldpath) == 0 {
   114  			return "", false
   115  		}
   116  		switch fieldpath[0] {
   117  		case "ref":
   118  			return status.Ref, true
   119  		}
   120  
   121  		return "", false
   122  	})
   123  }
   124  
   125  func adaptLease(lease leases.Lease) filters.Adaptor {
   126  	return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
   127  		if len(fieldpath) == 0 {
   128  			return "", false
   129  		}
   130  
   131  		switch fieldpath[0] {
   132  		case "id":
   133  			return lease.ID, len(lease.ID) > 0
   134  		case "labels":
   135  			return checkMap(fieldpath[1:], lease.Labels)
   136  		}
   137  
   138  		return "", false
   139  	})
   140  }
   141  
   142  func checkMap(fieldpath []string, m map[string]string) (string, bool) {
   143  	if len(m) == 0 {
   144  		return "", false
   145  	}
   146  
   147  	value, ok := m[strings.Join(fieldpath, ".")]
   148  	return value, ok
   149  }