Skip to content

Commit

Permalink
Merge pull request #126 from kndpio/118-load-configurations-from-loca…
Browse files Browse the repository at this point in the history
…l-docker-images

118 load configurations from local docker images
  • Loading branch information
evghen1 authored Jun 7, 2024
2 parents b8b950b + 68b83af commit 3446463
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 14 deletions.
3 changes: 1 addition & 2 deletions cmd/kndp/configuration/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/kndpio/kndp/internal/configuration"

"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"

"github.com/charmbracelet/log"
Expand All @@ -15,6 +14,6 @@ type applyCmd struct {
Link string `arg:"" required:"" help:"Link URL (or multiple comma separated) to Crossplane configuration to be applied to Environment."`
}

func (c *applyCmd) Run(ctx context.Context, config *rest.Config, dynamicClient *dynamic.DynamicClient, logger *log.Logger) error {
func (c *applyCmd) Run(ctx context.Context, config *rest.Config, logger *log.Logger) error {
return configuration.ApplyConfiguration(ctx, c.Link, config, logger)
}
5 changes: 3 additions & 2 deletions cmd/kndp/configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package configuration

type Cmd struct {
Apply applyCmd `cmd:"" help:"Apply Crossplane Configuration."`
List listCmd `cmd:"" help:"Apply Crossplane Configuration."`
Apply applyCmd `cmd:"" help:"Apply Crossplane Configuration."`
List listCmd `cmd:"" help:"Apply Crossplane Configuration."`
Load loadCmd `cmd:"" help:"Load Crossplane Configuration from archive."`
Delete deleteCmd `cmd:"" help:"Delete Crossplane Configuration."`
}
60 changes: 60 additions & 0 deletions cmd/kndp/configuration/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package configuration

import (
"bufio"
"context"
"os"

"github.com/charmbracelet/log"
cfg "github.com/kndpio/kndp/internal/configuration"
"github.com/kndpio/kndp/internal/kube"
"github.com/kndpio/kndp/internal/registry"
"k8s.io/client-go/rest"
)

type loadCmd struct {
Name string `arg:"" help:"Name of configuration."`
Path string `help:"Path to configuration package archive."`
Stdin bool `help:"Load configuration package from STDIN."`
}

func (c *loadCmd) Run(ctx context.Context, config *rest.Config, logger *log.Logger) error {

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

if !registry.IsLocalRegistry(ctx, client) {
logger.Warn("Local registry is not installed.")
return nil
}

cfg := cfg.Configuration{}
cfg.Name = c.Name
logger.Debugf("Loading image to: %s", cfg.Name)
if c.Path != "" {
logger.Debugf("Loading from path: %s", c.Path)
err = cfg.LoadPathArchive(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)
if err != nil {
return err
}
} else {
logger.Warn("Archive path or STDIN required for load configuration.")
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)
return nil
}
7 changes: 5 additions & 2 deletions cmd/kndp/registry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ type createCmd struct {
Username string `required:"" help:"is your Username."`
Password string `required:"" help:"is your Password."`
Email string `required:"" help:"is your Email."`
Default bool `help:"Set registry as default."`
Local bool `help:"Create local registry."`
}

func (c *createCmd) Run(ctx context.Context, client *kubernetes.Clientset, config *rest.Config, logger *log.Logger) error {

reg := registry.New(c.RegistryServer, c.Username, c.Password, c.Email)

verr := reg.Validate()
reg.SetDefault(c.Default)
reg.SetLocal(c.Local)
verr := reg.Validate(logger)
if verr != nil {
errs := verr.(validator.ValidationErrors)
for _, err := range errs {
Expand Down
6 changes: 5 additions & 1 deletion cmd/kndp/registry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
)

type deleteCmd struct {
Name string `required:"" help:"Registry name."`
Name string `required:"" help:"Registry name."`
Default bool `help:"Remove from default."`
Local bool `help:"Remove associated local registry."`
}

func (c deleteCmd) Run(ctx context.Context, config *rest.Config, logger *log.Logger) error {
reg := registry.Registry{}
reg.Name = c.Name
reg.SetDefault(c.Default)
reg.SetLocal(c.Local)
err := reg.Delete(ctx, config, logger)
if err != nil {
logger.Error(err)
Expand Down
6 changes: 6 additions & 0 deletions internal/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/charmbracelet/log"
configuration "github.com/crossplane/crossplane/apis/pkg/v1"
regv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/kndpio/kndp/internal/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -22,6 +23,11 @@ const (
apiPlural = "configurations"
)

type Configuration struct {
Name string
Image regv1.Image
}

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

import (
"bufio"
"io"
"os"

"github.com/google/go-containerregistry/pkg/v1/tarball"
)

// Load configuration package from TAR archive path
func (c *Configuration) LoadPathArchive(path string) error {
image, err := tarball.ImageFromPath(path, nil)
if err != nil {
return err
}
c.Image = image
return nil
}

// Load configuration package from STDIN
func (c *Configuration) LoadStdinArchive(stream *bufio.Reader) error {
stdin, err := io.ReadAll(stream)
if err != nil {
return err
}
tmpFile, err := os.CreateTemp("", "kndp-configuration-*")
if err != nil {
return err
}
tmpFile.Write(stdin)
if err != nil {
return err
}

return c.LoadPathArchive(tmpFile.Name())
}
1 change: 1 addition & 0 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
"xpkg.upbound.io/crossplane-contrib/provider-kubernetes:v0.13.0",
},
},
"args": []string{},
}
)

Expand Down
Loading

0 comments on commit 3446463

Please sign in to comment.