From d0aa6cee2c1e4fd25c27dd4cf3373bff6a590112 Mon Sep 17 00:00:00 2001 From: William Fish Date: Fri, 22 Jul 2022 11:28:53 -0400 Subject: [PATCH] Adding a check for the set bundle max used in the pipeline controller. Currently a user does not know when they might have added to many objects untill they attempt to run an image. Adding to the bundle cmd will at least give them more imediate feedback before attempting to use the bundle. --- pkg/bundle/builder.go | 10 +++- pkg/bundle/builder_test.go | 103 +++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/pkg/bundle/builder.go b/pkg/bundle/builder.go index 0641dfe15..45d8938ec 100644 --- a/pkg/bundle/builder.go +++ b/pkg/bundle/builder.go @@ -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 @@ -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 } diff --git a/pkg/bundle/builder_test.go b/pkg/bundle/builder_test.go index 74e389770..510b46813 100644 --- a/pkg/bundle/builder_test.go +++ b/pkg/bundle/builder_test.go @@ -3,6 +3,8 @@ package bundle import ( "archive/tar" "bytes" + "errors" + "fmt" "io" "testing" @@ -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) + } +}