Skip to content

Commit

Permalink
Merge pull request #269 from rattboi/bradon/filtered-packaging
Browse files Browse the repository at this point in the history
feat(image): pass kinds to LoadPackageDirectory for filtering
  • Loading branch information
evghen1 authored Jan 23, 2025
2 parents 5fb5eab + d86fef1 commit a545b20
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 38 deletions.
2 changes: 1 addition & 1 deletion internal/configuration/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Configuration) LoadStdinArchive(ctx context.Context, config *rest.Confi

// Load configuration package from directory
func (c *Configuration) LoadDirectory(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, path string) error {
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, path)
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, path, []string{"Configuration", "CompositeResourceDefinition", "Composition"})
if err != nil {
return err
}
Expand Down
52 changes: 29 additions & 23 deletions internal/configuration/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,49 @@ func loadServed(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Con
}

for _, e := range packFiles {
res := &metav1.TypeMeta{}
yamlFile, err := os.ReadFile(fmt.Sprintf("%s/%s", path, e.Name()))
if err != nil {
logger.Error(err)
}
err = yaml.Unmarshal(yamlFile, res)
if err != nil {
logger.Error(err)
}
if e.Type().IsRegular() {
if filepath.Ext(e.Name()) != ".yaml" {
continue
}

if res.Kind == "Configuration" {
ccfg := &cmv1.Configuration{}
err = yaml.Unmarshal(yamlFile, ccfg)
res := &metav1.TypeMeta{}
yamlFile, err := os.ReadFile(fmt.Sprintf("%s/%s", path, e.Name()))
if err != nil {
logger.Error(err)
}
cfgName := fmt.Sprintf("%s:0.0.0", ccfg.GetName())
cfg := New(cfgName)

logger.Debugf("Upgrade Configuration: %s", cfg)
err := cfg.UpgradeConfiguration(ctx, config, dc)

logger.Infof("Changes detected, apply configuration: %s", ccfg.GetName())
err = yaml.Unmarshal(yamlFile, res)
if err != nil {
logger.Error(err)
} else {
}

logger.Debugf("Loading Configuration: %s", cfg)
err = cfg.LoadDirectory(ctx, config, logger, path)
if res.Kind == "Configuration" {
ccfg := &cmv1.Configuration{}
err = yaml.Unmarshal(yamlFile, ccfg)
if err != nil {
logger.Error(err)
}
cfgName := fmt.Sprintf("%s:0.0.0", ccfg.GetName())
cfg := New(cfgName)

logger.Debugf("Upgrade Configuration: %s", cfg)
err := cfg.UpgradeConfiguration(ctx, config, dc)

logger.Infof("Changes detected, apply configuration: %s", ccfg.GetName())
if err != nil {
logger.Error(err)
} else {

logger.Debugf("Loading Configuration: %s", cfg)
err = cfg.Apply(ctx, config, logger)
err = cfg.LoadDirectory(ctx, config, logger, path)
if err != nil {
logger.Error(err)
} else {

logger.Debugf("Loading Configuration: %s", cfg)
err = cfg.Apply(ctx, config, logger)
if err != nil {
logger.Error(err)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/function/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *Function) LoadDirectory(ctx context.Context, config *rest.Config, logge
}

logger.Debug("Loading function package...")
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), packages.PackagePath))
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), packages.PackagePath), []string{"Function", "CustomResourceDefinition"})
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions internal/function/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ func loadServed(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Con

for _, e := range packFiles {
if e.Type().IsRegular() {
if filepath.Ext(e.Name()) != ".yaml" {
continue
}

res := &metav1.TypeMeta{}
yamlFile, err := os.ReadFile(fmt.Sprintf("%s/%s", packagePath, e.Name()))
if err != nil {
logger.Error(err)
}
err = yaml.Unmarshal(yamlFile, res)
if err != nil {
logger.Infof("Found non YAML file in package directory: %s. Please move it out before build package.", e.Name())
continue
logger.Error(err)
}
logger.Debugf("Package found with kind: %s", res.Kind)
if res.Kind == "Function" {
Expand Down
41 changes: 31 additions & 10 deletions internal/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"archive/tar"
"bytes"
"context"
"fmt"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"slices"

"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -81,20 +83,39 @@ func LoadBinaryLayer(content []byte, fileName string, permissions fs.FileMode) (
}

// Load configuration package from directory
func LoadPackageLayerDirectory(ctx context.Context, config *rest.Config, path string) (v1.Layer, error) {
files, err := os.ReadDir(path)
func LoadPackageLayerDirectory(ctx context.Context, config *rest.Config, path string, kindsFilter []string) (v1.Layer, error) {
var files []string

err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && filepath.Ext(path) == ".yaml" {
res := &metav1.TypeMeta{}
yamlFile, err := os.ReadFile(path)
if err != nil {
return errors.New("can't read")
}
err = yaml.Unmarshal(yamlFile, res)
if err != nil {
return errors.New("can't unmarshal")
}

if slices.Contains(kindsFilter, res.Kind) {
files = append(files, path)
}
}
return nil
})

if err != nil {
return nil, err
}

pkgContent := [][]byte{}
for _, file := range files {
if file.Type().IsRegular() && filepath.Ext(file.Name()) == ".yaml" {
fileContent, err := os.ReadFile(fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), file.Name()))
if err != nil {
return nil, err
}
pkgContent = append(pkgContent, fileContent)
fileContent, err := os.ReadFile(file)
if err != nil {
return nil, err
}
pkgContent = append(pkgContent, fileContent)
}

layer, err := crane.Layer(map[string][]byte{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (p *Provider) LoadDirectory(ctx context.Context, config *rest.Config, logge
}

logger.Debug("Loading provider package...")
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), packages.PackagePath))
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), packages.PackagePath), []string{"Provider", "CustomResourceDefinition"})
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func loadServed(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Con

for _, e := range packFiles {
if e.Type().IsRegular() {
if filepath.Ext(e.Name()) != ".yaml" {
continue
}

res := &metav1.TypeMeta{}
yamlFile, err := os.ReadFile(fmt.Sprintf("%s/%s", packagePath, e.Name()))
if err != nil {
Expand Down

0 comments on commit a545b20

Please sign in to comment.