Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a check for the set bundle max used in the pipeline controller. #1572

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/bundle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
func BuildTektonBundle(contents []string, log io.Writer) (v1.Image, error) {
img := empty.Image

if len(contents) > tkremote.MaximumBundleObjects {
return nil, fmt.Errorf("bundle contains more than the maximum %d allow objects", tkremote.MaximumBundleObjects)
}

fmt.Fprint(log, "Creating Tekton Bundle:\n")

// For each block of input, attempt to parse all of the YAML/JSON objects as Tekton objects and compress them into
Expand Down Expand Up @@ -95,5 +99,9 @@ func getObjectName(obj runtime.Object) (string, error) {
if !ok {
return "", errors.New("object is not a registered kubernetes resource")
}
return metaObj.GetName(), nil
name := metaObj.GetName()
if name == "" {
return "", errors.New("kubernetes resources should have a name")
}
return name, nil
}
103 changes: 103 additions & 0 deletions pkg/bundle/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package bundle
import (
"archive/tar"
"bytes"
"errors"
"fmt"
"io"
"testing"

Expand Down Expand Up @@ -105,3 +107,104 @@ func TestBuildTektonBundle(t *testing.T) {
t.Error(diff)
}
}

func TestBadObj(t *testing.T) {
task := v1beta1.Task{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1beta1.TaskSpec{Description: "foobar"},
}

raw, err := yaml.Marshal(task)
if err != nil {
t.Error(err)
return
}
_, err = BuildTektonBundle([]string{string(raw)}, &bytes.Buffer{})
noNameErr := errors.New("kubernetes resources should have a name")
if err == nil {
t.Errorf("expected error: %v", noNameErr)
}
}

func TestLessThenMaxBundle(t *testing.T) {
task := v1beta1.Task{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: v1beta1.TaskSpec{Description: "foobar"},
}

raw, err := yaml.Marshal(task)
if err != nil {
t.Error(err)
return
}
// no error for less then max
_, err = BuildTektonBundle([]string{string(raw)}, &bytes.Buffer{})
if err != nil {
t.Error(err)
}

}

func TestJustEnoughBundleSize(t *testing.T) {
var justEnoughObj []string
for i := 0; i == tkremote.MaximumBundleObjects; i++ {
name := fmt.Sprintf("%d-task", i)
task := v1beta1.Task{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
ObjectMeta: metav1.ObjectMeta{Name: name},
Spec: v1beta1.TaskSpec{Description: "foobar"},
}

raw, err := yaml.Marshal(task)
if err != nil {
t.Error(err)
return
}
justEnoughObj = append(justEnoughObj, string(raw))
}
// no error for the max
_, err := BuildTektonBundle(justEnoughObj, &bytes.Buffer{})
if err != nil {
t.Error(err)
}
}

func TestTooManyInBundle(t *testing.T) {
toManyObjErr := fmt.Sprintf("contained more than the maximum %d allow objects", tkremote.MaximumBundleObjects)
var toMuchObj []string
for i := 0; i <= tkremote.MaximumBundleObjects; i++ {
name := fmt.Sprintf("%d-task", i)
task := v1beta1.Task{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "Task",
},
ObjectMeta: metav1.ObjectMeta{Name: name},
Spec: v1beta1.TaskSpec{Description: "foobar"},
}

raw, err := yaml.Marshal(task)
if err != nil {
t.Error(err)
return
}
toMuchObj = append(toMuchObj, string(raw))
}

// expect error when we hit the max
_, err := BuildTektonBundle(toMuchObj, &bytes.Buffer{})
if err == nil {
t.Errorf("expected error: %v", toManyObjErr)
}
}