Skip to content

Commit

Permalink
chore: refactor minio and kaniko to new k8s obj
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Jun 4, 2024
1 parent 03eb826 commit a01ae29
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 118 deletions.
21 changes: 10 additions & 11 deletions pkg/builder/kaniko/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/celestiaorg/knuu/pkg/builder"
"github.com/celestiaorg/knuu/pkg/k8s"
"github.com/celestiaorg/knuu/pkg/minio"
"github.com/celestiaorg/knuu/pkg/names"
)
Expand All @@ -31,10 +31,9 @@ const (
)

type Kaniko struct {
K8sClientset kubernetes.Interface
K8sNamespace string
Minio *minio.Minio // Minio service to store the build context if it's a directory
ContentName string // Name of the content pushed to Minio
K8s k8s.KubeManager
Minio *minio.Minio // Minio service to store the build context if it's a directory
ContentName string // Name of the content pushed to Minio
}

var _ builder.Builder = &Kaniko{}
Expand All @@ -45,7 +44,7 @@ func (k *Kaniko) Build(ctx context.Context, b *builder.BuilderOptions) (logs str
return "", ErrPreparingJob.Wrap(err)
}

cJob, err := k.K8sClientset.BatchV1().Jobs(k.K8sNamespace).Create(ctx, job, metav1.CreateOptions{})
cJob, err := k.K8s.Clientset().BatchV1().Jobs(k.K8s.Namespace()).Create(ctx, job, metav1.CreateOptions{})
if err != nil {
return "", ErrCreatingJob.Wrap(err)
}
Expand Down Expand Up @@ -77,7 +76,7 @@ func (k *Kaniko) Build(ctx context.Context, b *builder.BuilderOptions) (logs str
}

func (k *Kaniko) waitForJobCompletion(ctx context.Context, job *batchv1.Job) (*batchv1.Job, error) {
watcher, err := k.K8sClientset.BatchV1().Jobs(k.K8sNamespace).Watch(ctx, metav1.ListOptions{
watcher, err := k.K8s.Clientset().BatchV1().Jobs(k.K8s.Namespace()).Watch(ctx, metav1.ListOptions{
FieldSelector: fmt.Sprintf("metadata.name=%s", job.Name),
})
if err != nil {
Expand Down Expand Up @@ -108,7 +107,7 @@ func (k *Kaniko) waitForJobCompletion(ctx context.Context, job *batchv1.Job) (*b
}

func (k *Kaniko) firstPodFromJob(ctx context.Context, job *batchv1.Job) (*v1.Pod, error) {
podList, err := k.K8sClientset.CoreV1().Pods(k.K8sNamespace).List(ctx, metav1.ListOptions{
podList, err := k.K8s.Clientset().CoreV1().Pods(k.K8s.Namespace()).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("job-name=%s", job.Name),
})
if err != nil {
Expand All @@ -131,7 +130,7 @@ func (k *Kaniko) containerLogs(ctx context.Context, pod *v1.Pod) (string, error)
Container: pod.Spec.Containers[0].Name,
}

req := k.K8sClientset.CoreV1().Pods(k.K8sNamespace).GetLogs(pod.Name, &logOptions)
req := k.K8s.Clientset().CoreV1().Pods(k.K8s.Namespace()).GetLogs(pod.Name, &logOptions)
logs, err := req.DoRaw(ctx)
if err != nil {
return "", err
Expand All @@ -141,7 +140,7 @@ func (k *Kaniko) containerLogs(ctx context.Context, pod *v1.Pod) (string, error)
}

func (k *Kaniko) cleanup(ctx context.Context, job *batchv1.Job) error {
err := k.K8sClientset.BatchV1().Jobs(k.K8sNamespace).
err := k.K8s.Clientset().BatchV1().Jobs(k.K8s.Namespace()).
Delete(ctx, job.Name, metav1.DeleteOptions{
PropagationPolicy: &[]metav1.DeletionPropagation{metav1.DeletePropagationBackground}[0],
})
Expand All @@ -150,7 +149,7 @@ func (k *Kaniko) cleanup(ctx context.Context, job *batchv1.Job) error {
}

// Delete the associated Pods
err = k.K8sClientset.CoreV1().Pods(k.K8sNamespace).
err = k.K8s.Clientset().CoreV1().Pods(k.K8s.Namespace()).
DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: fmt.Sprintf("job-name=%s", job.Name),
})
Expand Down
46 changes: 17 additions & 29 deletions pkg/builder/kaniko/kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ import (
"k8s.io/client-go/kubernetes/fake"

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

const (
k8sNamespace = "test-namespace"
k8sNamespace = "test-namespace"
testImage = "test-image"
testDestination = "registry.example.com/test-image:latest"
)

func TestKanikoBuilder(t *testing.T) {
k8sCS := fake.NewSimpleClientset()
kb := &Kaniko{
K8sClientset: k8sCS,
K8sNamespace: k8sNamespace,
K8s: k8s.NewCustom(k8sCS, k8sCS.Discovery(), nil, k8sNamespace),
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx := context.Background()

t.Run("BuildSuccess", func(t *testing.T) {
blCtx := "git://github.com/mojtaba-esk/sample-docker"
Expand All @@ -36,9 +37,9 @@ func TestKanikoBuilder(t *testing.T) {
require.NoError(t, err, "GetDefaultCacheOptions should succeed")

buildOptions := &builder.BuilderOptions{
ImageName: "test-image",
ImageName: testImage,
BuildContext: blCtx,
Destination: "registry.example.com/test-image:latest",
Destination: testDestination,
Args: []string{"--build-arg=value"},
Cache: cacheOpts,
}
Expand All @@ -54,7 +55,7 @@ func TestKanikoBuilder(t *testing.T) {
}()

// Simulate the successful completion of the Job after a short delay
time.Sleep(2 * time.Second)
time.Sleep(500 * time.Millisecond)
completeAllJobInFakeClientset(t, k8sCS, k8sNamespace)

wg.Wait()
Expand All @@ -63,40 +64,27 @@ func TestKanikoBuilder(t *testing.T) {
assert.NotEmpty(t, logs, "Build logs should not be empty")
})

t.Run("BuildFailure", func(t *testing.T) {
buildOptions := &builder.BuilderOptions{
ImageName: "test-image",
BuildContext: "invalid-context", // Simulate an invalid context
Destination: "registry.example.com/test-image:latest",
}

logs, err := kb.Build(ctx, buildOptions)

assert.Error(t, err, "Build should fail")
assert.Empty(t, logs, "Build logs should be empty")
})

t.Run("BuildWithContextCancellation", func(t *testing.T) {
buildOptions := &builder.BuilderOptions{
ImageName: "test-image",
ImageName: testImage,
BuildContext: "git://example.com/repo",
Destination: "registry.example.com/test-image:latest",
Destination: testDestination,
}

// Cancel the context to simulate cancellation during the build
ctx, cancel := context.WithCancel(ctx)
cancel()

logs, err := kb.Build(ctx, buildOptions)

assert.Error(t, err, "Build should fail due to context cancellation")
assert.Empty(t, logs, "Build logs should be empty")
assert.Error(t, err, "build should fail due to context cancellation")
assert.Empty(t, logs, "build logs should be empty")
})

}

func completeAllJobInFakeClientset(t *testing.T, clientset *fake.Clientset, namespace string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx := context.Background()

job, err := clientset.BatchV1().Jobs(namespace).List(ctx, metav1.ListOptions{})
assert.NoError(t, err)
Expand Down Expand Up @@ -125,8 +113,8 @@ func createPodFromJob(job *batchv1.Job) *v1.Pod {
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-container", // Adjust as needed
Image: "fake-image", // Adjust as needed
Name: "fake-container",
Image: "fake-image",
},
},
},
Expand Down
10 changes: 3 additions & 7 deletions pkg/knuu/knuu.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,15 @@ func New(ctx context.Context, opts ...Option) (*Knuu, error) {
}

if k.MinioCli == nil {
// TODO: minio also needs a little refactor to accept k8s obj instead
k.MinioCli = &minio.Minio{
Clientset: k.K8sCli.Clientset(),
Namespace: k.K8sCli.Namespace(),
K8s: k.K8sCli,
}
}

if k.ImageBuilder == nil {
// TODO: Also here for kaniko
k.ImageBuilder = &kaniko.Kaniko{
K8sClientset: k.K8sCli.Clientset(),
K8sNamespace: k.K8sCli.Namespace(),
Minio: k.MinioCli,
K8s: k.K8sCli,
Minio: k.MinioCli,
}
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/knuu/knuu_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ func InitializeWithScope(testScope string) error {
switch builderType {
case "kubernetes":
tmpKnuu.ImageBuilder = &kaniko.Kaniko{
K8sClientset: tmpKnuu.K8sCli.Clientset(),
K8sNamespace: tmpKnuu.K8sCli.Namespace(),
Minio: tmpKnuu.MinioCli,
K8s: tmpKnuu.K8sCli,
Minio: tmpKnuu.MinioCli,
}
case "docker", "":
tmpKnuu.ImageBuilder = &docker.Docker{
Expand Down
Loading

0 comments on commit a01ae29

Please sign in to comment.