|
| 1 | +package source |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/cli-runtime/pkg/resource" |
| 14 | + "sigs.k8s.io/yaml" |
| 15 | + |
| 16 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 17 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 18 | + |
| 19 | + "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle" |
| 20 | + registry "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/operator-registry" |
| 21 | +) |
| 22 | + |
| 23 | +type BundleSource interface { |
| 24 | + GetBundle() (bundle.RegistryV1, error) |
| 25 | +} |
| 26 | + |
| 27 | +// identitySource is a bundle source that returns itself |
| 28 | +type identitySource bundle.RegistryV1 |
| 29 | + |
| 30 | +func (r identitySource) GetBundle() (bundle.RegistryV1, error) { |
| 31 | + return bundle.RegistryV1(r), nil |
| 32 | +} |
| 33 | + |
| 34 | +func FromBundle(rv1 bundle.RegistryV1) BundleSource { |
| 35 | + return identitySource(rv1) |
| 36 | +} |
| 37 | + |
| 38 | +// FromFS returns a BundleSource that loads a registry+v1 bundle from a filesystem. |
| 39 | +// The filesystem is expected to conform to the registry+v1 format: |
| 40 | +// metadata/annotations.yaml |
| 41 | +// metadata/properties.yaml |
| 42 | +// manifests/ |
| 43 | +// - csv.yaml |
| 44 | +// - ... |
| 45 | +// |
| 46 | +// manifests directory should not contain subdirectories |
| 47 | +func FromFS(fs fs.FS) BundleSource { |
| 48 | + return fsBundleSource{ |
| 49 | + FS: fs, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +type fsBundleSource struct { |
| 54 | + FS fs.FS |
| 55 | +} |
| 56 | + |
| 57 | +func (f fsBundleSource) GetBundle() (bundle.RegistryV1, error) { |
| 58 | + reg := bundle.RegistryV1{} |
| 59 | + annotationsFileData, err := fs.ReadFile(f.FS, filepath.Join("metadata", "annotations.yaml")) |
| 60 | + if err != nil { |
| 61 | + return reg, err |
| 62 | + } |
| 63 | + annotationsFile := registry.AnnotationsFile{} |
| 64 | + if err := yaml.Unmarshal(annotationsFileData, &annotationsFile); err != nil { |
| 65 | + return reg, err |
| 66 | + } |
| 67 | + reg.PackageName = annotationsFile.Annotations.PackageName |
| 68 | + |
| 69 | + const manifestsDir = "manifests" |
| 70 | + foundCSV := false |
| 71 | + if err := fs.WalkDir(f.FS, manifestsDir, func(path string, e fs.DirEntry, err error) error { |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + if e.IsDir() { |
| 76 | + if path == manifestsDir { |
| 77 | + return nil |
| 78 | + } |
| 79 | + return fmt.Errorf("subdirectories are not allowed within the %q directory of the bundle image filesystem: found %q", manifestsDir, path) |
| 80 | + } |
| 81 | + manifestFile, err := f.FS.Open(path) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer manifestFile.Close() |
| 86 | + |
| 87 | + result := resource.NewLocalBuilder().Unstructured().Flatten().Stream(manifestFile, path).Do() |
| 88 | + if err := result.Err(); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if err := result.Visit(func(info *resource.Info, err error) error { |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + switch info.Object.GetObjectKind().GroupVersionKind().Kind { |
| 96 | + case "ClusterServiceVersion": |
| 97 | + csv := v1alpha1.ClusterServiceVersion{} |
| 98 | + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &csv); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + reg.CSV = csv |
| 102 | + foundCSV = true |
| 103 | + case "CustomResourceDefinition": |
| 104 | + crd := apiextensionsv1.CustomResourceDefinition{} |
| 105 | + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(info.Object.(*unstructured.Unstructured).Object, &crd); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + reg.CRDs = append(reg.CRDs, crd) |
| 109 | + default: |
| 110 | + reg.Others = append(reg.Others, *info.Object.(*unstructured.Unstructured)) |
| 111 | + } |
| 112 | + return nil |
| 113 | + }); err != nil { |
| 114 | + return fmt.Errorf("error parsing objects in %q: %v", path, err) |
| 115 | + } |
| 116 | + return nil |
| 117 | + }); err != nil { |
| 118 | + return reg, err |
| 119 | + } |
| 120 | + |
| 121 | + if !foundCSV { |
| 122 | + return reg, fmt.Errorf("no ClusterServiceVersion found in %q", manifestsDir) |
| 123 | + } |
| 124 | + |
| 125 | + if err := copyMetadataPropertiesToCSV(®.CSV, f.FS); err != nil { |
| 126 | + return reg, err |
| 127 | + } |
| 128 | + |
| 129 | + return reg, nil |
| 130 | +} |
| 131 | + |
| 132 | +// copyMetadataPropertiesToCSV copies properties from `metadata/propeties.yaml` (in the filesystem fsys) into |
| 133 | +// the CSV's `.metadata.annotations['olm.properties']` value, preserving any properties that are already |
| 134 | +// present in the annotations. |
| 135 | +func copyMetadataPropertiesToCSV(csv *v1alpha1.ClusterServiceVersion, fsys fs.FS) error { |
| 136 | + var allProperties []property.Property |
| 137 | + |
| 138 | + // First load existing properties from the CSV. We want to preserve these. |
| 139 | + if csvPropertiesJSON, ok := csv.Annotations["olm.properties"]; ok { |
| 140 | + var csvProperties []property.Property |
| 141 | + if err := json.Unmarshal([]byte(csvPropertiesJSON), &csvProperties); err != nil { |
| 142 | + return fmt.Errorf("failed to unmarshal csv.metadata.annotations['olm.properties']: %w", err) |
| 143 | + } |
| 144 | + allProperties = append(allProperties, csvProperties...) |
| 145 | + } |
| 146 | + |
| 147 | + // Next, load properties from the metadata/properties.yaml file, if it exists. |
| 148 | + metadataPropertiesJSON, err := fs.ReadFile(fsys, filepath.Join("metadata", "properties.yaml")) |
| 149 | + if err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 150 | + return fmt.Errorf("failed to read properties.yaml file: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + // If there are no properties, we can stick with whatever |
| 154 | + // was already present in the CSV annotations. |
| 155 | + if len(metadataPropertiesJSON) == 0 { |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + // Otherwise, we need to parse the properties.yaml file and |
| 160 | + // append its properties into the CSV annotation. |
| 161 | + type registryV1Properties struct { |
| 162 | + Properties []property.Property `json:"properties"` |
| 163 | + } |
| 164 | + |
| 165 | + var metadataProperties registryV1Properties |
| 166 | + if err := yaml.Unmarshal(metadataPropertiesJSON, &metadataProperties); err != nil { |
| 167 | + return fmt.Errorf("failed to unmarshal metadata/properties.yaml: %w", err) |
| 168 | + } |
| 169 | + allProperties = append(allProperties, metadataProperties.Properties...) |
| 170 | + |
| 171 | + // Lastly re-marshal all the properties back into a JSON array and update the CSV annotation |
| 172 | + allPropertiesJSON, err := json.Marshal(allProperties) |
| 173 | + if err != nil { |
| 174 | + return fmt.Errorf("failed to marshal registry+v1 properties to json: %w", err) |
| 175 | + } |
| 176 | + csv.Annotations["olm.properties"] = string(allPropertiesJSON) |
| 177 | + return nil |
| 178 | +} |
0 commit comments