Skip to content

Commit

Permalink
feat: custom registry support added
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Dec 11, 2024
1 parent b5dd67c commit 440f744
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 343 deletions.
29 changes: 23 additions & 6 deletions e2e/registry/suite_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
"github.com/stretchr/testify/suite"

"github.com/celestiaorg/knuu/e2e"
"github.com/celestiaorg/knuu/pkg/builder/kaniko"
"github.com/celestiaorg/knuu/pkg/builder/registry"
"github.com/celestiaorg/knuu/pkg/k8s"
"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/celestiaorg/knuu/pkg/minio"
"github.com/celestiaorg/knuu/pkg/system"
)

const (
Expand All @@ -33,19 +36,33 @@ func (s *Suite) SetupSuite() {
logger = logrus.New()
)

logger.SetLevel(logrus.DebugLevel)

k8sClient, err := k8s.NewClient(ctx, knuu.DefaultScope(), logger)
s.Require().NoError(err, "Error creating k8s client")

minioClient, err := minio.New(ctx, k8sClient, logger)
s.Require().NoError(err, "Error creating minio client")

// registry, err := registry.NewScaleway("fr-par", "test-moji", "username", "password")
// s.Require().NoError(err)

registry := registry.Registry(nil)

imageBuilder, err := kaniko.New(
&system.SystemDependencies{
K8sClient: k8sClient,
MinioClient: minioClient,
Logger: logger,
},
registry,
)
s.Require().NoError(err)

s.Knuu, err = knuu.New(ctx, knuu.Options{
LocalRegistryEnabled: true,
K8sClient: k8sClient,
MinioClient: minioClient, // needed for build from git tests
Timeout: testTimeout,
K8sClient: k8sClient,
MinioClient: minioClient, // needed for build from git tests
Timeout: testTimeout,
ImageBuilder: imageBuilder,
Logger: logger,
})
s.Require().NoError(err)

Expand Down
46 changes: 5 additions & 41 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"

"github.com/celestiaorg/knuu/pkg/builder/registry"
)

const (
DefaultRegistryAddress = "ttl.sh"
DefaultImageTTL = "24h" // used as a tag for the ephemeral images on ttl.sh
DefaultImageTag = "latest"
DefaultCacheRepoName = "cl-kaniko-cache" // only abcdefghijklmnopqrstuvwxyz0123456789_-./ are allowed
DefaultImageTTL = "24h" // used as a tag for the ephemeral images on ttl.sh
DefaultCacheRepoName = "cl-kaniko-cache" // only abcdefghijklmnopqrstuvwxyz0123456789_-./ are allowed
)

type Builder interface {
Build(ctx context.Context, b BuilderOptions) (logs string, err error)
ResolveImageName(buildContext string) (*ResolvedImage, error)
DefaultImage(buildContext string) (*registry.ResolvedImage, error)
CacheOptions() *CacheOptions
}

Expand All @@ -34,24 +32,6 @@ type CacheOptions struct {
Repo string
}

type ResolvedImage struct {
Name string
Registry string
Tag string
}

func (r *ResolvedImage) ToString() string {
return fmt.Sprintf("%s/%s:%s", r.Registry, r.Name, r.Tag)
}

func DefaultCacheOptions() *CacheOptions {
return &CacheOptions{
Enabled: true,
Dir: "",
Repo: ImageWithRegistry(DefaultCacheRepoName, DefaultRegistryAddress),
}
}

func DefaultImageName(buildContext string) (string, error) {
if buildContext == "" {
return "", ErrBuildContextEmpty
Expand All @@ -60,22 +40,6 @@ func DefaultImageName(buildContext string) (string, error) {
return hashString(buildContext)
}

func IsImageWithTag(image string) bool {
return strings.Contains(image, ":")
}

func IsImageWithRegistry(image string) bool {
return strings.Contains(image, "/")
}

func ImageWithRegistry(name, registry string) string {
return fmt.Sprintf("%s/%s", registry, name)
}

func ImageWithTag(name, tag string) string {
return fmt.Sprintf("%s:%s", name, tag)
}

func hashString(s string) (string, error) {
hash := sha256.New()
if _, err := hash.Write([]byte(s)); err != nil {
Expand Down
35 changes: 28 additions & 7 deletions pkg/builder/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,36 @@ import (
"k8s.io/client-go/kubernetes"

"github.com/celestiaorg/knuu/pkg/builder"
"github.com/celestiaorg/knuu/pkg/builder/registry"
"github.com/celestiaorg/knuu/pkg/k8s"
)

type Docker struct {
K8sClientset kubernetes.Interface
K8sNamespace string
// currently works only with the default registry (ttl.sh)
// No need to implement other registries, as this pkg (docker builder) is going to be deprecated
registry registry.Registry
}

var _ builder.Builder = &Docker{}

func New(k8sClient k8s.KubeManager, reg registry.Registry) (*Docker, error) {
if reg == nil {
reg = registry.NewDefault()
}

if k8sClient == nil {
return nil, ErrK8sClientRequired
}

return &Docker{
K8sClientset: k8sClient.Clientset(),
K8sNamespace: k8sClient.Namespace(),
registry: reg,
}, nil
}

func (d *Docker) Build(_ context.Context, b builder.BuilderOptions) (logs string, err error) {
if builder.IsGitContext(b.BuildContext) {
return "", ErrGitContextNotSupported
Expand Down Expand Up @@ -74,19 +95,19 @@ func (d *Docker) Build(_ context.Context, b builder.BuilderOptions) (logs string
}

func (d *Docker) CacheOptions() *builder.CacheOptions {
return builder.DefaultCacheOptions()
return &builder.CacheOptions{
Enabled: true,
Dir: "",
Repo: d.registry.ResolvedImage(builder.DefaultCacheRepoName, "").ToString(),
}
}

func (d *Docker) ResolveImageName(buildContext string) (*builder.ResolvedImage, error) {
func (d *Docker) DefaultImage(buildContext string) (*registry.ResolvedImage, error) {
imageName, err := builder.DefaultImageName(buildContext)
if err != nil {
return nil, err
}
return &builder.ResolvedImage{
Name: imageName,
Registry: builder.DefaultRegistryAddress,
Tag: builder.DefaultImageTTL,
}, nil
return d.registry.ResolvedImage(imageName, builder.DefaultImageTTL), nil
}

func runCommand(cmd *exec.Cmd) (logs string, err error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/builder/docker/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ var (
ErrFailedToPushImage = errors.New("FailedToPushImage", "failed to push image")
ErrFailedToRemoveContextDir = errors.New("FailedToRemoveContextDir", "failed to remove context directory")
ErrGitContextNotSupported = errors.New("GitContextNotSupported", "git context is not supported in the docker builder")
ErrK8sClientRequired = errors.New("K8sClientRequired", "K8s client is required")
)
4 changes: 4 additions & 0 deletions pkg/builder/kaniko/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ var (
ErrDeletingMinioContent = errors.New("DeletingMinioContent", "error deleting Minio content")
ErrParsingQuantity = errors.New("ParsingQuantity", "error parsing quantity")
ErrMinioFailedToGetDeployment = errors.New("MinioFailedToGetDeployment", "Minio failed to get deployment")
ErrSystemDependenciesRequired = errors.New("SystemDependenciesRequired", "system dependencies are required")
ErrK8sClientRequired = errors.New("K8sClientRequired", "K8s client is required")
ErrMinioClientRequired = errors.New("MinioClientRequired", "Minio client is required")
ErrSettingUpRegistryConfig = errors.New("SettingUpRegistryConfig", "error setting up registry config")
)
Loading

0 comments on commit 440f744

Please sign in to comment.