From 0b08b1c7b4ff91218a39aa422eda08d47da0b220 Mon Sep 17 00:00:00 2001 From: Ben Parees Date: Sun, 23 Dec 2018 23:11:39 -0500 Subject: [PATCH] properly handle object creation error in template instantiate https://bugzilla.redhat.com/show_bug.cgi?id=1661845 --- .../controller/templateinstance_controller.go | 2 +- .../templates/templateinstance_fail.go | 56 +++++++++++++++++++ test/extended/testdata/bindata.go | 51 +++++++++++++++++ .../templates/templateinstance_badobject.yaml | 31 ++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 test/extended/templates/templateinstance_fail.go create mode 100644 test/extended/testdata/templates/templateinstance_badobject.yaml diff --git a/pkg/template/controller/templateinstance_controller.go b/pkg/template/controller/templateinstance_controller.go index f5c9eb98419a..dd981f69494e 100644 --- a/pkg/template/controller/templateinstance_controller.go +++ b/pkg/template/controller/templateinstance_controller.go @@ -501,7 +501,7 @@ func (c *TemplateInstanceController) instantiate(templateInstance *templatev1.Te } } if createErr != nil { - allErrors = append(allErrors, mappingErr) + allErrors = append(allErrors, createErr) continue } diff --git a/test/extended/templates/templateinstance_fail.go b/test/extended/templates/templateinstance_fail.go new file mode 100644 index 000000000000..febbf555b346 --- /dev/null +++ b/test/extended/templates/templateinstance_fail.go @@ -0,0 +1,56 @@ +package templates + +import ( + "fmt" + "time" + + g "github.com/onsi/ginkgo" + o "github.com/onsi/gomega" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" + + templatev1 "github.com/openshift/api/template/v1" + templatecontroller "github.com/openshift/origin/pkg/template/controller" + exutil "github.com/openshift/origin/test/extended/util" +) + +var _ = g.Describe("[Conformance][templates] templateinstance creation with invalid object reports error", func() { + defer g.GinkgoRecover() + + var ( + cli = exutil.NewCLI("templates", exutil.KubeConfigPath()) + templatefixture = exutil.FixturePath("testdata", "templates", "templateinstance_badobject.yaml") + ) + + g.Context("", func() { + g.BeforeEach(func() { + g.By("waiting for default service account") + err := exutil.WaitForServiceAccount(cli.KubeClient().Core().ServiceAccounts(cli.Namespace()), "default") + o.Expect(err).NotTo(o.HaveOccurred()) + }) + + g.It("should report a failure on creation", func() { + err := cli.Run("create").Args("-f", templatefixture).Execute() + o.Expect(err).NotTo(o.HaveOccurred()) + + g.By("waiting for error to appear") + var templateinstance *templatev1.TemplateInstance + err = wait.Poll(time.Second, 1*time.Minute, func() (bool, error) { + templateinstance, err = cli.TemplateClient().TemplateV1().TemplateInstances(cli.Namespace()).Get("invalidtemplateinstance", metav1.GetOptions{}) + if err != nil { + return false, err + } + if templatecontroller.TemplateInstanceHasCondition(templateinstance, templatev1.TemplateInstanceInstantiateFailure, corev1.ConditionTrue) { + return true, nil + } + return false, nil + }) + if err != nil { + fmt.Fprintf(g.GinkgoWriter, "error waiting for instantiate failure: %v\n%#v", err, templateinstance) + } + o.Expect(err).NotTo(o.HaveOccurred()) + }) + }) +}) diff --git a/test/extended/testdata/bindata.go b/test/extended/testdata/bindata.go index 9e6d16018fef..9e5e2d2ec568 100644 --- a/test/extended/testdata/bindata.go +++ b/test/extended/testdata/bindata.go @@ -204,6 +204,7 @@ // test/extended/testdata/samplepipeline-withenvs.yaml // test/extended/testdata/service-serving-cert/nginx-serving-cert.conf // test/extended/testdata/signer-buildconfig.yaml +// test/extended/testdata/templates/templateinstance_badobject.yaml // test/extended/testdata/templates/templateinstance_objectkinds.yaml // test/extended/testdata/templates/templateservicebroker_bind.yaml // test/extended/testdata/test-cli-debug.yaml @@ -11613,6 +11614,54 @@ func testExtendedTestdataSignerBuildconfigYaml() (*asset, error) { return a, nil } +var _testExtendedTestdataTemplatesTemplateinstance_badobjectYaml = []byte(`kind: List +apiVersion: v1 +items: +- kind: TemplateInstance + apiVersion: template.openshift.io/v1 + metadata: + name: invalidtemplateinstance + spec: + template: + kind: Template + apiVersion: v1 + metadata: + name: template + objects: + - kind: Deployment + apiVersion: apps/v1 + metadata: + name: "invalidname!@#$%^&*" + spec: + replicas: 0 + selector: + matchLabels: + key: value + template: + metadata: + labels: + key: value + spec: + containers: + - name: hello-openshift + image: openshift/hello-openshift +`) + +func testExtendedTestdataTemplatesTemplateinstance_badobjectYamlBytes() ([]byte, error) { + return _testExtendedTestdataTemplatesTemplateinstance_badobjectYaml, nil +} + +func testExtendedTestdataTemplatesTemplateinstance_badobjectYaml() (*asset, error) { + bytes, err := testExtendedTestdataTemplatesTemplateinstance_badobjectYamlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "test/extended/testdata/templates/templateinstance_badobject.yaml", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + var _testExtendedTestdataTemplatesTemplateinstance_objectkindsYaml = []byte(`kind: List apiVersion: v1 items: @@ -32776,6 +32825,7 @@ var _bindata = map[string]func() (*asset, error){ "test/extended/testdata/samplepipeline-withenvs.yaml": testExtendedTestdataSamplepipelineWithenvsYaml, "test/extended/testdata/service-serving-cert/nginx-serving-cert.conf": testExtendedTestdataServiceServingCertNginxServingCertConf, "test/extended/testdata/signer-buildconfig.yaml": testExtendedTestdataSignerBuildconfigYaml, + "test/extended/testdata/templates/templateinstance_badobject.yaml": testExtendedTestdataTemplatesTemplateinstance_badobjectYaml, "test/extended/testdata/templates/templateinstance_objectkinds.yaml": testExtendedTestdataTemplatesTemplateinstance_objectkindsYaml, "test/extended/testdata/templates/templateservicebroker_bind.yaml": testExtendedTestdataTemplatesTemplateservicebroker_bindYaml, "test/extended/testdata/test-cli-debug.yaml": testExtendedTestdataTestCliDebugYaml, @@ -33281,6 +33331,7 @@ var _bintree = &bintree{nil, map[string]*bintree{ }}, "signer-buildconfig.yaml": &bintree{testExtendedTestdataSignerBuildconfigYaml, map[string]*bintree{}}, "templates": &bintree{nil, map[string]*bintree{ + "templateinstance_badobject.yaml": &bintree{testExtendedTestdataTemplatesTemplateinstance_badobjectYaml, map[string]*bintree{}}, "templateinstance_objectkinds.yaml": &bintree{testExtendedTestdataTemplatesTemplateinstance_objectkindsYaml, map[string]*bintree{}}, "templateservicebroker_bind.yaml": &bintree{testExtendedTestdataTemplatesTemplateservicebroker_bindYaml, map[string]*bintree{}}, }}, diff --git a/test/extended/testdata/templates/templateinstance_badobject.yaml b/test/extended/testdata/templates/templateinstance_badobject.yaml new file mode 100644 index 000000000000..31766824cbd4 --- /dev/null +++ b/test/extended/testdata/templates/templateinstance_badobject.yaml @@ -0,0 +1,31 @@ +kind: List +apiVersion: v1 +items: +- kind: TemplateInstance + apiVersion: template.openshift.io/v1 + metadata: + name: invalidtemplateinstance + spec: + template: + kind: Template + apiVersion: v1 + metadata: + name: template + objects: + - kind: Deployment + apiVersion: apps/v1 + metadata: + name: "invalidname!@#$%^&*" + spec: + replicas: 0 + selector: + matchLabels: + key: value + template: + metadata: + labels: + key: value + spec: + containers: + - name: hello-openshift + image: openshift/hello-openshift