Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
[Plain] Add support for validation and deployment of plain bundles
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha Prasad Narsing <[email protected]>
  • Loading branch information
varshaprasad96 committed Sep 6, 2023
1 parent 9821226 commit 863cf15
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
7 changes: 4 additions & 3 deletions internal/controllers/v1alpha2/controllers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"path/filepath"

"github.com/operator-framework/rukpak/internal/util"
"github.com/spf13/afero"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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
}
Expand All @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions internal/controllers/v1alpha2/deployer/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
32 changes: 13 additions & 19 deletions internal/controllers/v1alpha2/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand All @@ -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
}
35 changes: 14 additions & 21 deletions internal/convert/registryv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"strings"
"testing/fstest"
"time"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/spf13/afero"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 863cf15

Please sign in to comment.