|
| 1 | +package etcd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + "k8s.io/apimachinery/pkg/watch" |
| 12 | + apirequest "k8s.io/apiserver/pkg/endpoints/request" |
| 13 | + "k8s.io/apiserver/pkg/registry/rest" |
| 14 | + "k8s.io/client-go/tools/cache" |
| 15 | + |
| 16 | + imageapi "github.com/openshift/origin/pkg/image/apis/image" |
| 17 | +) |
| 18 | + |
| 19 | +// ImageLayerIndex is a cache of image digests to the layers they contain. |
| 20 | +// Because a very large number of images can exist on a cluster, we only |
| 21 | +// hold in memory a small subset of the full image object. |
| 22 | +type ImageLayerIndex interface { |
| 23 | + cache.SharedIndexInformer |
| 24 | +} |
| 25 | + |
| 26 | +type ImageStore interface { |
| 27 | + rest.Watcher |
| 28 | + rest.Lister |
| 29 | +} |
| 30 | + |
| 31 | +// NewImageLayerIndex creates a new index over a store that must return |
| 32 | +// images. |
| 33 | +func NewImageLayerIndex(store ImageStore) ImageLayerIndex { |
| 34 | + ctx := apirequest.NewContext() |
| 35 | + informer := cache.NewSharedIndexInformer(&cache.ListWatch{ |
| 36 | + ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 37 | + obj, err := store.List(ctx, &metainternalversion.ListOptions{ |
| 38 | + ResourceVersion: options.ResourceVersion, |
| 39 | + Limit: options.Limit, |
| 40 | + Continue: options.Continue, |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + list, ok := obj.(*imageapi.ImageList) |
| 46 | + if !ok { |
| 47 | + return nil, fmt.Errorf("unexpected store type %T for layer index", obj) |
| 48 | + } |
| 49 | + // reduce the full image list to a smaller subset. |
| 50 | + out := &metainternalversion.List{ |
| 51 | + Items: make([]runtime.Object, len(list.Items)), |
| 52 | + } |
| 53 | + out.Continue = list.Continue |
| 54 | + out.ResourceVersion = list.ResourceVersion |
| 55 | + for i, image := range list.Items { |
| 56 | + out.Items[i] = &ImageLayers{ |
| 57 | + Name: image.Name, |
| 58 | + Layers: image.DockerImageLayers, |
| 59 | + Manifest: manifestFromImage(&image), |
| 60 | + } |
| 61 | + } |
| 62 | + return out, nil |
| 63 | + }, |
| 64 | + WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 65 | + w, err := store.Watch(ctx, &metainternalversion.ListOptions{ |
| 66 | + ResourceVersion: options.ResourceVersion, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + return watch.Filter(w, func(in watch.Event) (out watch.Event, keep bool) { |
| 72 | + if in.Object == nil { |
| 73 | + return in, true |
| 74 | + } |
| 75 | + // reduce each object to the minimal subset we need for the cache |
| 76 | + image, ok := in.Object.(*imageapi.Image) |
| 77 | + if !ok { |
| 78 | + return in, true |
| 79 | + } |
| 80 | + in.Object = &ImageLayers{ |
| 81 | + Name: image.Name, |
| 82 | + ResourceVersion: image.ResourceVersion, |
| 83 | + Layers: image.DockerImageLayers, |
| 84 | + Manifest: manifestFromImage(image), |
| 85 | + } |
| 86 | + return in, true |
| 87 | + }), nil |
| 88 | + }, |
| 89 | + }, &ImageLayers{}, 0, cache.Indexers{ |
| 90 | + // layers allows fast access to the images with a given layer |
| 91 | + "layers": func(obj interface{}) ([]string, error) { |
| 92 | + entry, ok := obj.(*ImageLayers) |
| 93 | + if !ok { |
| 94 | + return nil, fmt.Errorf("unexpected cache object %T", obj) |
| 95 | + } |
| 96 | + keys := make([]string, 0, len(entry.Layers)) |
| 97 | + for _, layer := range entry.Layers { |
| 98 | + keys = append(keys, layer.Name) |
| 99 | + } |
| 100 | + return keys, nil |
| 101 | + }, |
| 102 | + }) |
| 103 | + return informer |
| 104 | +} |
| 105 | + |
| 106 | +// manifestFromImage attempts to find a manifest blob description from |
| 107 | +// an image. Images older than schema2 in Docker do not have a manifest blob. |
| 108 | +func manifestFromImage(image *imageapi.Image) *imageapi.ImageLayer { |
| 109 | + if image.DockerImageManifestMediaType != "application/vnd.docker.distribution.manifest.v2+json" { |
| 110 | + return nil |
| 111 | + } |
| 112 | + return &imageapi.ImageLayer{ |
| 113 | + Name: image.DockerImageMetadata.ID, |
| 114 | + MediaType: image.DockerImageManifestMediaType, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// ImageLayers is the minimal set of data we need to retain to provide the cache. |
| 119 | +// Unlike a more general informer cache, we do not retain the full object because of |
| 120 | +// the potential size of the objects being stored. Even a small cluster may have 20k |
| 121 | +// or more images in active use. |
| 122 | +type ImageLayers struct { |
| 123 | + Name string |
| 124 | + ResourceVersion string |
| 125 | + Manifest *imageapi.ImageLayer |
| 126 | + Layers []imageapi.ImageLayer |
| 127 | +} |
| 128 | + |
| 129 | +var ( |
| 130 | + _ runtime.Object = &ImageLayers{} |
| 131 | + _ metav1.Object = &ImageLayers{} |
| 132 | +) |
| 133 | + |
| 134 | +func (l *ImageLayers) GetObjectKind() schema.ObjectKind { return &metav1.TypeMeta{} } |
| 135 | +func (l *ImageLayers) DeepCopyObject() runtime.Object { |
| 136 | + var layers []imageapi.ImageLayer |
| 137 | + if l.Layers != nil { |
| 138 | + layers = make([]imageapi.ImageLayer, len(l.Layers)) |
| 139 | + copy(layers, l.Layers) |
| 140 | + } |
| 141 | + return &ImageLayers{ |
| 142 | + Name: l.Name, |
| 143 | + Layers: layers, |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// client-go/cache.SharedIndexInformer hardcodes the key function to assume ObjectMeta. |
| 148 | +// Here we implement the relevant accessors to allow a minimal index to be created. |
| 149 | +// SharedIndexInformer will be refactored to require a more minimal subset of actions |
| 150 | +// in the near future. |
| 151 | + |
| 152 | +func (l *ImageLayers) GetName() string { return l.Name } |
| 153 | +func (l *ImageLayers) GetNamespace() string { return "" } |
| 154 | +func (l *ImageLayers) GetResourceVersion() string { return l.ResourceVersion } |
| 155 | +func (l *ImageLayers) SetResourceVersion(version string) { l.ResourceVersion = version } |
| 156 | + |
| 157 | +// These methods are unused stubs to satisfy meta.Object. |
| 158 | + |
| 159 | +func (l *ImageLayers) SetNamespace(namespace string) {} |
| 160 | +func (l *ImageLayers) SetName(name string) {} |
| 161 | +func (l *ImageLayers) GetGenerateName() string { return "" } |
| 162 | +func (l *ImageLayers) SetGenerateName(name string) {} |
| 163 | +func (l *ImageLayers) GetUID() types.UID { return "" } |
| 164 | +func (l *ImageLayers) SetUID(uid types.UID) {} |
| 165 | +func (l *ImageLayers) GetGeneration() int64 { return 0 } |
| 166 | +func (l *ImageLayers) SetGeneration(generation int64) {} |
| 167 | +func (l *ImageLayers) GetSelfLink() string { return "" } |
| 168 | +func (l *ImageLayers) SetSelfLink(selfLink string) {} |
| 169 | +func (l *ImageLayers) GetCreationTimestamp() metav1.Time { return metav1.Time{} } |
| 170 | +func (l *ImageLayers) SetCreationTimestamp(timestamp metav1.Time) {} |
| 171 | +func (l *ImageLayers) GetDeletionTimestamp() *metav1.Time { return nil } |
| 172 | +func (l *ImageLayers) SetDeletionTimestamp(timestamp *metav1.Time) {} |
| 173 | +func (l *ImageLayers) GetDeletionGracePeriodSeconds() *int64 { return nil } |
| 174 | +func (l *ImageLayers) SetDeletionGracePeriodSeconds(*int64) {} |
| 175 | +func (l *ImageLayers) GetLabels() map[string]string { return nil } |
| 176 | +func (l *ImageLayers) SetLabels(labels map[string]string) {} |
| 177 | +func (l *ImageLayers) GetAnnotations() map[string]string { return nil } |
| 178 | +func (l *ImageLayers) SetAnnotations(annotations map[string]string) {} |
| 179 | +func (l *ImageLayers) GetInitializers() *metav1.Initializers { return nil } |
| 180 | +func (l *ImageLayers) SetInitializers(initializers *metav1.Initializers) {} |
| 181 | +func (l *ImageLayers) GetFinalizers() []string { return nil } |
| 182 | +func (l *ImageLayers) SetFinalizers(finalizers []string) {} |
| 183 | +func (l *ImageLayers) GetOwnerReferences() []metav1.OwnerReference { return nil } |
| 184 | +func (l *ImageLayers) SetOwnerReferences([]metav1.OwnerReference) {} |
| 185 | +func (l *ImageLayers) GetClusterName() string { return "" } |
| 186 | +func (l *ImageLayers) SetClusterName(clusterName string) {} |
0 commit comments