|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package registry |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + registryimage "github.com/operator-framework/operator-registry/pkg/image" |
| 26 | + "github.com/operator-framework/operator-registry/pkg/image/containerdregistry" |
| 27 | + registrybundle "github.com/operator-framework/operator-registry/pkg/lib/bundle" |
| 28 | + log "github.com/sirupsen/logrus" |
| 29 | + |
| 30 | + // TODO: replace `gopkg.in/yaml.v2` with `sigs.k8s.io/yaml` once operator-registry has `json` tags in the |
| 31 | + // annotations struct. |
| 32 | + yaml "gopkg.in/yaml.v3" |
| 33 | +) |
| 34 | + |
| 35 | +// Labels is a set of key:value labels from an operator-registry object. |
| 36 | +type Labels map[string]string |
| 37 | + |
| 38 | +// GetManifestsDir returns the manifests directory name in ls using |
| 39 | +// a predefined key, or false if it does not exist. |
| 40 | +func (ls Labels) GetManifestsDir() (string, bool) { |
| 41 | + value, hasLabel := ls.getLabel(registrybundle.ManifestsLabel) |
| 42 | + return filepath.Clean(value), hasLabel |
| 43 | +} |
| 44 | + |
| 45 | +// getLabel returns the string by key in ls, or an empty string and false |
| 46 | +// if key is not found in ls. |
| 47 | +func (ls Labels) getLabel(key string) (string, bool) { |
| 48 | + value, hasLabel := ls[key] |
| 49 | + return value, hasLabel |
| 50 | +} |
| 51 | + |
| 52 | +// GetImageLabels returns the set of labels on image. |
| 53 | +func GetImageLabels(ctx context.Context, logger *log.Entry, image string, local bool) (Labels, error) { |
| 54 | + // Create a containerd registry for socket-less image layer reading. |
| 55 | + reg, err := containerdregistry.NewRegistry(containerdregistry.WithLog(logger)) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("error creating new image registry: %v", err) |
| 58 | + } |
| 59 | + defer func() { |
| 60 | + if err := reg.Destroy(); err != nil { |
| 61 | + logger.WithError(err).Warn("Error destroying local cache") |
| 62 | + } |
| 63 | + }() |
| 64 | + |
| 65 | + // Pull the image if it isn't present locally. |
| 66 | + if !local { |
| 67 | + if err := reg.Pull(ctx, registryimage.SimpleReference(image)); err != nil { |
| 68 | + return nil, fmt.Errorf("error pulling image %s: %v", image, err) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Query the image reference for its labels. |
| 73 | + labels, err := reg.Labels(ctx, registryimage.SimpleReference(image)) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("error reading image %s labels: %v", image, err) |
| 76 | + } |
| 77 | + |
| 78 | + return labels, err |
| 79 | +} |
| 80 | + |
| 81 | +// FindMetadataDir walks bundleRoot searching for metadata, and returns that directory if found. |
| 82 | +// If one is not found, an error is returned. |
| 83 | +func FindMetadataDir(bundleRoot string) (metadataDir string, err error) { |
| 84 | + err = filepath.Walk(bundleRoot, func(path string, info os.FileInfo, err error) error { |
| 85 | + if err != nil || !info.IsDir() { |
| 86 | + return err |
| 87 | + } |
| 88 | + // Already found the first metadata dir, do not overwrite it. |
| 89 | + if metadataDir != "" { |
| 90 | + return nil |
| 91 | + } |
| 92 | + // The annotations file is well-defined. |
| 93 | + _, err = os.Stat(filepath.Join(path, registrybundle.AnnotationsFile)) |
| 94 | + if err == nil || errors.Is(err, os.ErrExist) { |
| 95 | + metadataDir = path |
| 96 | + return nil |
| 97 | + } |
| 98 | + return err |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return "", err |
| 102 | + } |
| 103 | + if metadataDir == "" { |
| 104 | + return "", fmt.Errorf("metadata dir not found in %s", bundleRoot) |
| 105 | + } |
| 106 | + |
| 107 | + return metadataDir, nil |
| 108 | +} |
| 109 | + |
| 110 | +// GetMetadataLabels reads annotations from file(s) in metadataDir and returns them as Labels. |
| 111 | +func GetMetadataLabels(metadataDir string) (Labels, error) { |
| 112 | + // The annotations file is well-defined. |
| 113 | + annotationsPath := filepath.Join(metadataDir, registrybundle.AnnotationsFile) |
| 114 | + b, err := ioutil.ReadFile(annotationsPath) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + // Use the arbitrarily-labelled bundle representation of the annotations file |
| 120 | + // for forwards and backwards compatibility. |
| 121 | + meta := registrybundle.AnnotationMetadata{ |
| 122 | + Annotations: make(map[string]string), |
| 123 | + } |
| 124 | + if err = yaml.Unmarshal(b, &meta); err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + return meta.Annotations, nil |
| 129 | +} |
0 commit comments