Skip to content

Commit

Permalink
refactor!: have knuu as an object (#356)
Browse files Browse the repository at this point in the history
* chore: refactor knuu to an object

* fix: clone method

* chore: comments added to old file

* chore: add deprecated annotation to the old code

* chore: refactor knuu helper funcs for minio

* chore: added a sample unittest

* fix: increase timeout limit

* chore: refactor proxy to fit into the new knuu pkg

* chore: rename for more clarity
  • Loading branch information
mojtaba-esk authored May 29, 2024
1 parent 44c3873 commit 4efa658
Show file tree
Hide file tree
Showing 28 changed files with 2,900 additions and 1,784 deletions.
33 changes: 15 additions & 18 deletions e2e/basic/build_from_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package basic

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

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

Expand All @@ -18,20 +18,18 @@ func TestBuildFromGit(t *testing.T) {
t.Parallel()
// Setup

// This code is a bit dirty due to the current limitations of knuu
// After refactoring knuu, this test must be either removed or updated
require.NoError(t, os.Setenv("KNUU_BUILDER", "kubernetes"), "Error setting KNUU_BUILDER Env")
require.NoError(t, knuu.CleanUp(), "Error cleaning up knuu")
require.NoError(t, knuu.Initialize(), "Error initializing knuu")

instance, err := knuu.NewInstance("my-instance")
require.NoError(t, err, "Error creating instance")

ctx, cancel := context.WithTimeout(context.Background(), 45*time.Minute)
defer cancel()

// The default image builder is kaniko here
kn, err := knuu.New(ctx)
require.NoError(t, err, "Error creating knuu")

sampleInstance, err := kn.NewInstance("git-builder")
require.NoError(t, err, "Error creating instance")

// This is a blocking call which builds the image from git repo
err = instance.SetGitRepo(ctx, builder.GitContext{
err = sampleInstance.SetGitRepo(ctx, builder.GitContext{
Repo: "https://github.com/celestiaorg/celestia-app.git",
Branch: "main",
// Commit: "5ce94f4f010e366df301d25cd5d797c3147ff884",
Expand All @@ -40,21 +38,20 @@ func TestBuildFromGit(t *testing.T) {
})
require.NoError(t, err, "Error setting git repo")

require.NoError(t, instance.SetCommand("sleep", "infinity"), "Error setting command")
require.NoError(t, sampleInstance.SetCommand("sleep", "infinity"), "Error setting command")

err = instance.AddFileBytes([]byte("Hello, world!"), "/home/hello.txt", "root:root")
err = sampleInstance.AddFileBytes([]byte("Hello, world!"), "/home/hello.txt", "root:root")
require.NoError(t, err, "Error adding file")

require.NoError(t, instance.Commit(), "Error committing instance")
require.NoError(t, sampleInstance.Commit(), "Error committing instance")

t.Cleanup(func() {
require.NoError(t, knuu.BatchDestroy(instance))
require.NoError(t, instance.BatchDestroy(ctx, sampleInstance))
})

// Test logic
require.NoError(t, instance.Start(), "Error starting instance")
require.NoError(t, sampleInstance.Start(ctx), "Error starting instance")

data, err := instance.GetFileBytes("/home/hello.txt")
data, err := sampleInstance.GetFileBytes(ctx, "/home/hello.txt")
require.NoError(t, err, "Error getting file bytes")

require.Equal(t, []byte("Hello, world!"), data, "File bytes do not match")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
2 changes: 1 addition & 1 deletion pkg/knuu/bittwister.go → pkg/instance/bittwister.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package knuu
package instance

import (
"context"
Expand Down
12 changes: 4 additions & 8 deletions pkg/knuu/instance_destroy.go → pkg/instance/destroy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package knuu
package instance

import (
"context"
Expand All @@ -9,7 +9,7 @@ import (

// Destroy destroys the instance
// This function can only be called in the state 'Started' or 'Destroyed'
func (i *Instance) Destroy() error {
func (i *Instance) Destroy(ctx context.Context) error {
if i.state == Destroyed {
return nil
}
Expand All @@ -18,10 +18,6 @@ func (i *Instance) Destroy() error {
return ErrDestroyingNotAllowed.WithParams(i.state.String())
}

// TODO: receive context from the user in the breaking refactor
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

if err := i.destroyPod(ctx); err != nil {
return ErrDestroyingPod.WithParams(i.k8sName).Wrap(err)
}
Expand All @@ -45,7 +41,7 @@ func (i *Instance) Destroy() error {
}

// BatchDestroy destroys a list of instances.
func BatchDestroy(instances ...*Instance) error {
func BatchDestroy(ctx context.Context, instances ...*Instance) error {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
logrus.Info("Skipping cleanup")
return nil
Expand All @@ -55,7 +51,7 @@ func BatchDestroy(instances ...*Instance) error {
if instance == nil {
continue
}
if err := instance.Destroy(); err != nil {
if err := instance.Destroy(ctx); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit 4efa658

Please sign in to comment.