Skip to content

Commit

Permalink
Merge pull request #259 from web-seven/257-featprovider-provider-serv…
Browse files Browse the repository at this point in the history
…e-command

added provider serve command and updated watched of function serve
  • Loading branch information
evghen1 authored Jan 11, 2025
2 parents c6800cb + 4d42d62 commit 8798885
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 54 deletions.
2 changes: 1 addition & 1 deletion cmd/overlock/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ type Cmd struct {
Apply applyCmd `cmd:"" help:"Apply Crossplane Function."`
List listCmd `cmd:"" help:"Apply Crossplane Function."`
Load loadCmd `cmd:"" help:"Load Crossplane Function from archive."`
Serve serveCmd `cmd:"" help:"Watch chnages of function and load."`
Serve serveCmd `cmd:"" help:"Watch changes of Function, build and load."`
Delete deleteCmd `cmd:"" help:"Delete Crossplane Function."`
}
4 changes: 2 additions & 2 deletions cmd/overlock/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package provider

type Cmd struct {
Install installCmd `cmd:"" help:"Install Crossplane Provider."`
Load loadCmd `cmd:"" help:"Load Crossplane Provider."`
List listCmd `cmd:"" help:"List all Crossplane Providers."`
Load loadCmd `cmd:"" help:"Load Crossplane Provider."`
Serve serveCmd `cmd:"" help:"Watch changes of Provider, build and load."`
Delete deleteCmd `cmd:"" help:"Delete Crossplane Provider."`
Apply applyCmd `cmd:"" help:"Apply Crossplane Provider."`
}
20 changes: 20 additions & 0 deletions cmd/overlock/provider/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package provider

import (
"context"

"go.uber.org/zap"

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

type serveCmd struct {
Path string `default:"./" arg:"" help:"Path to package directory"`
MainPath string `default:"cmd/provider" arg:"" help:"Path to main module"`
}

func (c *serveCmd) Run(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Config, logger *zap.SugaredLogger) error {
return provider.Serve(ctx, dc, config, logger, c.Path, c.MainPath)
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.4
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/docker/docker v24.0.7+incompatible
github.com/fsnotify/fsnotify v1.7.0
github.com/go-logr/logr v1.4.1
github.com/pkg/errors v0.9.1
github.com/posener/complete v1.2.3
Expand All @@ -15,6 +16,7 @@ require (
k8s.io/apimachinery v0.29.1
k8s.io/kubectl v0.29.1
sigs.k8s.io/controller-runtime v0.17.0
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -60,7 +62,6 @@ require (
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-errors/errors v1.4.2 // indirect
Expand Down Expand Up @@ -179,7 +180,6 @@ require (
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

require (
Expand All @@ -195,6 +195,7 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pterm/pterm v0.12.79
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rjeczalik/notify v0.9.3
github.com/rodaine/table v1.1.1
github.com/spf13/afero v1.11.0
github.com/willabides/kongplete v0.4.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab h1:ZjX6I48eZSFetPb41dHudEyVr5v953N15TsNZXlkcWY=
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab/go.mod h1:/PfPXh0EntGc3QAAyUaviy4S9tzy4Zp0e2ilq4voC6E=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
github.com/rodaine/table v1.1.1 h1:zBliy3b4Oj6JRmncse2Z85WmoQvDrXOYuy0JXCt8Qz8=
github.com/rodaine/table v1.1.1/go.mod h1:iqTRptjn+EVcrVBYtNMlJ2wrJZa3MpULUmcXFpfcziA=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand Down Expand Up @@ -598,6 +600,7 @@ golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
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, logger, path)
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, path)
if err != nil {
return err
}
Expand Down
14 changes: 6 additions & 8 deletions internal/function/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
)

const (
tagDelim = ":"
regRepoDelimiter = "/"
functionFileName = "function"
fileMode os.FileMode = 0o777
annotationKey string = "io.crossplane.xpkg"
packageAnnotation string = "base"
tagDelim = ":"
regRepoDelimiter = "/"
functionFileName = "function"
fileMode os.FileMode = 0o777
)

func (c *Function) UpgradeFunction(ctx context.Context, config *rest.Config, dc *dynamic.DynamicClient) error {
Expand Down Expand Up @@ -76,11 +74,11 @@ func (c *Function) LoadDirectory(ctx context.Context, config *rest.Config, logge
}

logger.Debug("Loading function package...")
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, logger, fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), packages.PackagePath))
packageLayer, err := image.LoadPackageLayerDirectory(ctx, config, fmt.Sprintf("%s/%s", strings.TrimRight(path, "/"), packages.PackagePath))
if err != nil {
return err
}

logger.Debug("Function package loaded.")
cfg, err := c.Image.ConfigFile()
if err != nil {
return err
Expand Down
41 changes: 13 additions & 28 deletions internal/function/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"

cmv1beta1 "github.com/crossplane/crossplane/apis/pkg/meta/v1beta1"
"github.com/fsnotify/fsnotify"
"github.com/rjeczalik/notify"
"github.com/web-seven/overlock/internal/packages"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -21,43 +21,28 @@ func Serve(ctx context.Context, dc *dynamic.DynamicClient, config *rest.Config,

loadServed(ctx, dc, config, logger, path)

watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Error(err)
c := make(chan notify.EventInfo, 1)

if err := notify.Watch(fmt.Sprintf("%s/%s", path, "..."), c, notify.Create, notify.Write, notify.Rename, notify.Remove); err != nil {
return err
}
defer watcher.Close()
defer notify.Stop(c)

go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) ||
event.Has(fsnotify.Create) ||
event.Has(fsnotify.Remove) ||
event.Has(fsnotify.Rename) {
fileExt := filepath.Ext(event.Name)
if fileExt == ".yaml" || fileExt == ".go" {
logger.Debugf("Changed file: %s", event)
loadServed(ctx, dc, config, logger, path)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
case ev := <-c:
fileExt := filepath.Ext(ev.Path())
if fileExt == ".yaml" || fileExt == ".go" {
logger.Debugf("Changed file: %s", ev)
loadServed(ctx, dc, config, logger, path)
}
logger.Error(err)
case <-ctx.Done():
return
}
}
}()

err = watcher.Add(path)
if err != nil {
logger.Error(err)
}

<-make(chan struct{})
return nil
}
Expand Down
6 changes: 2 additions & 4 deletions internal/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"go.uber.org/zap"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -72,10 +71,10 @@ func LoadBinaryLayer(content []byte, fileName string, permissions fs.FileMode) (
}

// Load configuration package from directory
func LoadPackageLayerDirectory(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger, path string) (v1.Layer, error) {
func LoadPackageLayerDirectory(ctx context.Context, config *rest.Config, path string) (v1.Layer, error) {
files, err := os.ReadDir(path)
if err != nil {
logger.Error(err)
return nil, err
}
pkgContent := [][]byte{}
for _, file := range files {
Expand All @@ -94,6 +93,5 @@ func LoadPackageLayerDirectory(ctx context.Context, config *rest.Config, logger
if err != nil {
return nil, err
}
logger.Debugf("Package layer created.")
return layer, nil
}
32 changes: 32 additions & 0 deletions internal/provider/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"strings"

crossv1 "github.com/crossplane/crossplane/apis/pkg/v1"
"go.uber.org/zap"
Expand Down Expand Up @@ -44,3 +45,34 @@ func (p *Provider) ApplyProvider(ctx context.Context, links []string, config *re
logger.Info("Provider(s) applied successfully.")
return nil
}

func (p *Provider) ApplyPackage(ctx context.Context, config *rest.Config, logger *zap.SugaredLogger) error {

_, err := engine.VerifyApi(ctx, config, apiName)
if err != nil {
logger.Debug(err)
logger.Infoln("Crossplane not installed in current context.")
logger.Infoln("Provider not applied.")
return nil
}

scheme := runtime.NewScheme()
crossv1.AddToScheme(scheme)
if kube, err := client.New(config, client.Options{Scheme: scheme}); err == nil {
for _, link := range strings.Split(p.Name, ",") {
cfg := &crossv1.Provider{}
logger.Debugf("Building package %s", link)
engine.BuildPack(cfg, link, map[string]string{})
pa := resource.NewAPIPatchingApplicator(kube)

if err := pa.Apply(ctx, cfg); err != nil {
return errors.Wrap(err, "Error apply Provider(s).")
}
}
} else {
return err
}

logger.Info("Provider(s) applied successfully.")
return nil
}
Loading

0 comments on commit 8798885

Please sign in to comment.