diff --git a/internal/controllers/v1alpha2/controllers/util/util.go b/internal/controllers/v1alpha2/controllers/util/util.go index 929ccdae..3aad6ffb 100644 --- a/internal/controllers/v1alpha2/controllers/util/util.go +++ b/internal/controllers/v1alpha2/controllers/util/util.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/operator-framework/rukpak/internal/util" + "github.com/spf13/afero" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -13,8 +14,8 @@ const ( manifestsDir = "manifests" ) -func GetBundleObjects(bundleFS fs.FS) ([]client.Object, error) { - entries, err := fs.ReadDir(bundleFS, manifestsDir) +func GetBundleObjects(bundleFS afero.Fs) ([]client.Object, error) { + entries, err := afero.ReadDir(bundleFS, manifestsDir) if err != nil { return nil, err } @@ -34,7 +35,7 @@ func GetBundleObjects(bundleFS fs.FS) ([]client.Object, error) { return bundleObjects, nil } -func getObjects(bundle fs.FS, manifest fs.DirEntry) ([]client.Object, error) { +func getObjects(bundle afero.Fs, manifest fs.FileInfo) ([]client.Object, error) { manifestPath := filepath.Join(manifestsDir, manifest.Name()) manifestReader, err := bundle.Open(manifestPath) if err != nil { diff --git a/internal/controllers/v1alpha2/deployer/deploy.go b/internal/controllers/v1alpha2/deployer/deploy.go index 74803042..233c3ccf 100644 --- a/internal/controllers/v1alpha2/deployer/deploy.go +++ b/internal/controllers/v1alpha2/deployer/deploy.go @@ -223,9 +223,9 @@ func (bd *helmDeployer) fetchChart(fs afero.Fs, bundleDeployment *v1alpha2.Bundl case v1alpha2.FormatHelm: return getChartFromHelmBundle(fs, bundleDeployment) case v1alpha2.FormatRegistryV1: - return getChartFromRegistryBundle(fs, *bundleDeployment) + return getChartFromRegistryBundle(fs, bundleDeployment) case v1alpha2.FormatPlain: - return getChartFromPlainBundle() + return getChartFromPlainBundle(fs, bundleDeployment) default: return nil, nil, errors.New("unknown format to convert into chart") } @@ -281,13 +281,17 @@ func loadValues(bd *v1alpha2.BundleDeployment) (chartutil.Values, error) { return values, nil } -func getChartFromRegistryBundle(chartfs afero.Fs, bd v1alpha2.BundleDeployment) (*chart.Chart, chartutil.Values, error) { +func getChartFromRegistryBundle(chartfs afero.Fs, bd *v1alpha2.BundleDeployment) (*chart.Chart, chartutil.Values, error) { plainFS, err := convert.RegistryV1ToPlain(chartfs) if err != nil { return nil, nil, fmt.Errorf("error converting registry+v1 bundle to plain+v0 bundle: %v", err) } - objects, err := v1alpha2util.GetBundleObjects(plainFS) + return getChartFromPlainBundle(plainFS, bd) +} + +func getChartFromPlainBundle(chartfs afero.Fs, bd *v1alpha2.BundleDeployment) (*chart.Chart, chartutil.Values, error) { + objects, err := v1alpha2util.GetBundleObjects(chartfs) if err != nil { return nil, nil, fmt.Errorf("error fetching objects from bundle manifests: %v", err) } @@ -314,10 +318,6 @@ func getChartFromRegistryBundle(chartfs afero.Fs, bd v1alpha2.BundleDeployment) return chrt, nil, nil } -func getChartFromPlainBundle() (*chart.Chart, chartutil.Values, error) { - return nil, nil, nil -} - type postrenderer struct { labels map[string]string cascade postrender.PostRenderer diff --git a/internal/controllers/v1alpha2/validator/validator.go b/internal/controllers/v1alpha2/validator/validator.go index 80122bce..6b1e5e6a 100644 --- a/internal/controllers/v1alpha2/validator/validator.go +++ b/internal/controllers/v1alpha2/validator/validator.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/fs" "github.com/operator-framework/rukpak/api/v1alpha2" "github.com/operator-framework/rukpak/internal/controllers/v1alpha2/controllers/util" @@ -45,29 +44,13 @@ func (r *registryV1Validator) Validate(ctx context.Context, fs afero.Fs, bundleD if err != nil { return fmt.Errorf("error converting registry+v1 bundle to plain+v0 bundle: %v", err) } - - if err := r.validateBundle(plainFS); err != nil { - return err - } - return nil -} - -func (r *registryV1Validator) validateBundle(fs fs.FS) error { - objects, err := util.GetBundleObjects(fs) - if err != nil { - return fmt.Errorf("error fetching objects from bundle manifests: %v", err) - } - if len(objects) == 0 { - return errors.New("invalid bundle: found zero objects: plain+v0 bundles are required to contain at least one object") - } - return nil + return validateBundleObjects(plainFS) } type plainValidator struct{} func (p *plainValidator) Validate(ctx context.Context, fs afero.Fs, bundleDeployment *v1alpha2.BundleDeployment) error { - // re-write existing validation to use afero.Fs - return nil + return validateBundleObjects(fs) } type helmValidator struct{} @@ -76,3 +59,14 @@ func (h *helmValidator) Validate(ctx context.Context, fs afero.Fs, bundleDeploym // validate whether a single directory exists in its root and contains chart.yaml. return nil } + +func validateBundleObjects(fs afero.Fs) error { + objects, err := util.GetBundleObjects(fs) + if err != nil { + return fmt.Errorf("error fetching objects from bundle manifests: %v", err) + } + if len(objects) == 0 { + return errors.New("invalid bundle: found zero objects: plain+v0 bundles are required to contain at least one object") + } + return nil +} diff --git a/internal/convert/registryv1.go b/internal/convert/registryv1.go index 6baa8bc7..42949441 100644 --- a/internal/convert/registryv1.go +++ b/internal/convert/registryv1.go @@ -10,7 +10,6 @@ import ( "path/filepath" "strings" "testing/fstest" - "time" "github.com/operator-framework/api/pkg/operators/v1alpha1" "github.com/spf13/afero" @@ -44,7 +43,7 @@ type Plain struct { Objects []client.Object } -func RegistryV1ToPlain(rv1 afero.Fs) (fs.FS, error) { +func RegistryV1ToPlain(rv1 afero.Fs) (afero.Fs, error) { reg := RegistryV1{} fileData, err := afero.ReadFile(rv1, filepath.Join("metadata", "annotations.yaml")) if err != nil { @@ -122,29 +121,23 @@ func RegistryV1ToPlain(rv1 afero.Fs) (fs.FS, error) { } } - now := time.Now() - // TODO: Use afero.NewMemMapFs() instead for uniformity - plainFS := fstest.MapFS{ - ".": &fstest.MapFile{ - Data: nil, - Mode: fs.ModeDir | 0755, - ModTime: now, - }, - "manifests": &fstest.MapFile{ - Data: nil, - Mode: fs.ModeDir | 0755, - ModTime: now, - }, - "manifests/manifest.yaml": &fstest.MapFile{ - Data: manifest.Bytes(), - Mode: 0644, - ModTime: now, - }, + plainFS := afero.NewMemMapFs() + if err := afero.WriteFile(plainFS, ".", nil, fs.ModeDir|0755); err != nil { + return nil, err + } + if err := afero.WriteFile(plainFS, "manifests", nil, fs.ModeDir|0755); err != nil { + return nil, err + } + if err := afero.WriteFile(plainFS, "manifests/manifests.yaml", manifest.Bytes(), 0644); err != nil { + return nil, err } - return plainFS, nil } +type MapFSAdaptoer struct { + fs *fstest.MapFS +} + func validateTargetNamespaces(supportedInstallModes sets.Set[string], installNamespace string, targetNamespaces []string) error { set := sets.New[string](targetNamespaces...) switch set.Len() {