Skip to content

Commit

Permalink
Merge pull request #247 from web-seven/246-featdevelop-serve-configur…
Browse files Browse the repository at this point in the history
…ation-package-from-local-directory

added configuration serve command
  • Loading branch information
evghen1 authored Dec 3, 2024
2 parents 11942e4 + 51107b2 commit 0e97493
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 59 deletions.
3 changes: 2 additions & 1 deletion cmd/overlock/configuration/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type applyCmd struct {
}

func (c *applyCmd) Run(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Config, logger *zap.SugaredLogger) error {
configuration.ApplyConfiguration(ctx, c.Link, config, logger)
cfg := configuration.New(c.Link)
cfg.Apply(ctx, config, logger)
if !c.Wait {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions cmd/overlock/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ type Cmd struct {
Apply applyCmd `cmd:"" help:"Apply Crossplane Configuration."`
List listCmd `cmd:"" help:"Apply Crossplane Configuration."`
Load loadCmd `cmd:"" help:"Load Crossplane Configuration from archive."`
Serve serveCmd `cmd:"" help:"Serve Crossplane Configuration from filesystem."`
Delete deleteCmd `cmd:"" help:"Delete Crossplane Configuration."`
}
69 changes: 20 additions & 49 deletions cmd/overlock/configuration/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
"os"

"github.com/web-seven/overlock/internal/configuration"
"github.com/web-seven/overlock/internal/kube"
"github.com/web-seven/overlock/internal/loader"
"github.com/web-seven/overlock/internal/packages"
"github.com/web-seven/overlock/internal/registry"
"go.uber.org/zap"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
Expand All @@ -24,52 +20,33 @@ type loadCmd struct {
}

func (c *loadCmd) Run(ctx context.Context, config *rest.Config, dc *dynamic.DynamicClient, logger *zap.SugaredLogger) error {

client, err := kube.Client(config)
if err != nil {
return err
}

isLocal, err := registry.IsLocalRegistry(ctx, client)
if !isLocal || err != nil {
reg := registry.NewLocal()
reg.SetDefault(true)
err := reg.Create(ctx, config, logger)
if err != nil {
return err
}
}

cfg := configuration.Configuration{}
cfg.Name = c.Name

cfgs := configuration.GetConfigurations(ctx, dc)
var pkgs []packages.Package
for _, c := range cfgs {
pkg := packages.Package{
Name: c.Name,
Url: c.Spec.Package,
}
pkgs = append(pkgs, pkg)
}
cfg := configuration.New(c.Name)
if c.Upgrade {
cfg.Name, err = cfg.UpgradeVersion(ctx, dc, cfg.Name, pkgs)
if err != nil {
return err
}
cfg.UpgradeConfiguration(ctx, config, dc)
}

logger.Debugf("Loading image to: %s", cfg.Name)
if c.Path != "" {
logger.Debugf("Loading from path: %s", c.Path)
cfg.Image, err = loader.LoadPathArchive(c.Path)
fi, err := os.Stat(c.Path)
if err != nil {
return err
}
switch mode := fi.Mode(); {
case mode.IsDir():
logger.Debugf("Loading from directory: %s", c.Path)
err = cfg.LoadDirectory(ctx, config, logger, c.Path)
if err != nil {
return err
}
case mode.IsRegular():
logger.Debugf("Loading from file: %s", c.Path)
err = cfg.LoadPathArchive(ctx, config, logger, c.Path)
if err != nil {
return err
}
}
} else if c.Stdin {
logger.Debug("Loading from STDIN")
reader := bufio.NewReader(os.Stdin)
err = cfg.LoadStdinArchive(reader)
err := cfg.LoadStdinArchive(ctx, config, logger, reader)
if err != nil {
return err
}
Expand All @@ -78,15 +55,9 @@ func (c *loadCmd) Run(ctx context.Context, config *rest.Config, dc *dynamic.Dyna
return nil
}

logger.Debug("Pushing to local registry")
err = registry.PushLocalRegistry(ctx, cfg.Name, cfg.Image, config, logger)
if err != nil {
return err
}
logger.Infof("Image archive %s loaded to local registry.", cfg.Name)

if c.Apply {
return configuration.ApplyConfiguration(ctx, cfg.Name, config, logger)
return cfg.Apply(ctx, config, logger)
}

return nil
}
19 changes: 19 additions & 0 deletions cmd/overlock/configuration/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package configuration

import (
"context"

"go.uber.org/zap"

"github.com/web-seven/overlock/internal/configuration"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)

type serveCmd struct {
Path string `default:"./" arg:"" help:"Path to package directory"`
}

func (c *serveCmd) Run(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Config, logger *zap.SugaredLogger) error {
return configuration.Watch(ctx, dc, config, logger, c.Path)
}
5 changes: 3 additions & 2 deletions internal/configuration/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func HealthCheck(ctx context.Context, dc dynamic.Interface, links string, wait b
}
}

func ApplyConfiguration(ctx context.Context, links string, config *rest.Config, logger *zap.SugaredLogger) error {
func (c *Configuration) Apply(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger) error {

_, err := engine.VerifyApi(ctx, config, apiName)
if err != nil {
Expand All @@ -67,8 +67,9 @@ func ApplyConfiguration(ctx context.Context, links string, config *rest.Config,
scheme := runtime.NewScheme()
crossv1.AddToScheme(scheme)
if kube, err := client.New(config, client.Options{Scheme: scheme}); err == nil {
for _, link := range strings.Split(links, ",") {
for _, link := range strings.Split(c.Name, ",") {
cfg := &crossv1.Configuration{}
logger.Debugf("Building package %s", link)
engine.BuildPack(cfg, link, map[string]string{})
pa := resource.NewAPIPatchingApplicator(kube)

Expand Down
6 changes: 6 additions & 0 deletions internal/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type Configuration struct {
packages.Package
}

func New(name string) *Configuration {
return &Configuration{
Name: name,
}
}

func CheckHealthStatus(status []condition.Condition) bool {
healthStatus := false
for _, condition := range status {
Expand Down
146 changes: 139 additions & 7 deletions internal/configuration/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,57 @@ package configuration

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/fs"
"os"
"strings"

"archive/tar"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/web-seven/overlock/internal/kube"
"github.com/web-seven/overlock/internal/loader"
"github.com/web-seven/overlock/internal/packages"
"github.com/web-seven/overlock/internal/registry"
"go.uber.org/zap"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)

const (
tagDelim = ":"
regRepoDelimiter = "/"
)
func (c *Configuration) UpgradeConfiguration(ctx context.Context, config *rest.Config, dc *dynamic.DynamicClient) error {
cfgs := GetConfigurations(ctx, dc)
var pkgs []packages.Package
for _, c := range cfgs {
pkg := packages.Package{
Name: c.Name,
Url: c.Spec.Package,
}
pkgs = append(pkgs, pkg)
}
var err error
c.Name, err = c.UpgradeVersion(ctx, dc, c.Name, pkgs)
if err != nil {
return err
}
return nil
}

// Load configuration package from path
func (c *Configuration) LoadPathArchive(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, path string) error {
var err error
c.Image, err = loader.LoadPathArchive(path)
if err != nil {
return err
}
return c.load(ctx, config, logger)
}

// Load configuration package from STDIN
func (c *Configuration) LoadStdinArchive(stream *bufio.Reader) error {
func (c *Configuration) LoadStdinArchive(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, stream *bufio.Reader) error {
stdin, err := io.ReadAll(stream)
if err != nil {
return err
Expand All @@ -24,9 +62,103 @@ func (c *Configuration) LoadStdinArchive(stream *bufio.Reader) error {
return err
}
tmpFile.Write(stdin)
c.Image, err = loader.LoadPathArchive(tmpFile.Name())
if err != nil {
return err
}
c.Image, err = loader.LoadPathArchive(tmpFile.Name())
return err
return c.load(ctx, config, logger)
}

// Load configuration package from directory
func (c *Configuration) LoadDirectory(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, path string) error {
files, err := os.ReadDir(path)
if err != nil {
logger.Error(err)
}

pkgFile, err := os.CreateTemp("", "overlock-configuration-*")
if err != nil {
return err
}
layerFile, err := os.CreateTemp("", "overlock-configuration-*")
if err != nil {
return err
}

pkgContent := [][]byte{}
for _, file := range files {
if file.Type().IsRegular() {
fileContent, err := os.ReadFile(fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), file.Name()))
if err != nil {
return err
}
pkgContent = append(pkgContent, fileContent)
}
}
os.WriteFile(pkgFile.Name(), bytes.Join(pkgContent, []byte("---\n")), fs.ModeAppend)
err = addToArchive(createArchive(layerFile), pkgFile, "package.yaml")
if err != nil {
return err
}

logger.Debugf("Archive %s created, loading to registry.", layerFile)
c.Image, err = crane.Append(empty.Image, layerFile.Name())
if err != nil {
return err
}
return c.load(ctx, config, logger)
}

func (c *Configuration) load(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger) error {
client, err := kube.Client(config)
if err != nil {
return err
}
isLocal, err := registry.IsLocalRegistry(ctx, client)
if !isLocal || err != nil {
reg := registry.NewLocal()
reg.SetDefault(true)
err := reg.Create(ctx, config, logger)
if err != nil {
return err
}
}

err = registry.PushLocalRegistry(ctx, c.Name, c.Image, config, logger)
if err != nil {
return err
}
logger.Infof("Image archive %s loaded to local registry.", c.Name)

return nil
}

func createArchive(buf io.Writer) *tar.Writer {
tw := tar.NewWriter(buf)
return tw
}

func addToArchive(tw *tar.Writer, file *os.File, filename string) error {
defer file.Close()

info, err := file.Stat()
if err != nil {
return err
}

header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
header.Name = filename
err = tw.WriteHeader(header)
if err != nil {
return err
}
_, err = io.Copy(tw, file)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 0e97493

Please sign in to comment.