Skip to content

Commit 23c8cff

Browse files
Merge pull request #21704 from bparees/instancefail
properly handle object creation error in template instantiate
2 parents 85a0623 + 0b08b1c commit 23c8cff

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

pkg/template/controller/templateinstance_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ func (c *TemplateInstanceController) instantiate(templateInstance *templatev1.Te
501501
}
502502
}
503503
if createErr != nil {
504-
allErrors = append(allErrors, mappingErr)
504+
allErrors = append(allErrors, createErr)
505505
continue
506506
}
507507

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package templates
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
g "github.com/onsi/ginkgo"
8+
o "github.com/onsi/gomega"
9+
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/util/wait"
13+
14+
templatev1 "github.com/openshift/api/template/v1"
15+
templatecontroller "github.com/openshift/origin/pkg/template/controller"
16+
exutil "github.com/openshift/origin/test/extended/util"
17+
)
18+
19+
var _ = g.Describe("[Conformance][templates] templateinstance creation with invalid object reports error", func() {
20+
defer g.GinkgoRecover()
21+
22+
var (
23+
cli = exutil.NewCLI("templates", exutil.KubeConfigPath())
24+
templatefixture = exutil.FixturePath("testdata", "templates", "templateinstance_badobject.yaml")
25+
)
26+
27+
g.Context("", func() {
28+
g.BeforeEach(func() {
29+
g.By("waiting for default service account")
30+
err := exutil.WaitForServiceAccount(cli.KubeClient().Core().ServiceAccounts(cli.Namespace()), "default")
31+
o.Expect(err).NotTo(o.HaveOccurred())
32+
})
33+
34+
g.It("should report a failure on creation", func() {
35+
err := cli.Run("create").Args("-f", templatefixture).Execute()
36+
o.Expect(err).NotTo(o.HaveOccurred())
37+
38+
g.By("waiting for error to appear")
39+
var templateinstance *templatev1.TemplateInstance
40+
err = wait.Poll(time.Second, 1*time.Minute, func() (bool, error) {
41+
templateinstance, err = cli.TemplateClient().TemplateV1().TemplateInstances(cli.Namespace()).Get("invalidtemplateinstance", metav1.GetOptions{})
42+
if err != nil {
43+
return false, err
44+
}
45+
if templatecontroller.TemplateInstanceHasCondition(templateinstance, templatev1.TemplateInstanceInstantiateFailure, corev1.ConditionTrue) {
46+
return true, nil
47+
}
48+
return false, nil
49+
})
50+
if err != nil {
51+
fmt.Fprintf(g.GinkgoWriter, "error waiting for instantiate failure: %v\n%#v", err, templateinstance)
52+
}
53+
o.Expect(err).NotTo(o.HaveOccurred())
54+
})
55+
})
56+
})

test/extended/testdata/bindata.go

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
kind: List
2+
apiVersion: v1
3+
items:
4+
- kind: TemplateInstance
5+
apiVersion: template.openshift.io/v1
6+
metadata:
7+
name: invalidtemplateinstance
8+
spec:
9+
template:
10+
kind: Template
11+
apiVersion: v1
12+
metadata:
13+
name: template
14+
objects:
15+
- kind: Deployment
16+
apiVersion: apps/v1
17+
metadata:
18+
name: "invalidname!@#$%^&*"
19+
spec:
20+
replicas: 0
21+
selector:
22+
matchLabels:
23+
key: value
24+
template:
25+
metadata:
26+
labels:
27+
key: value
28+
spec:
29+
containers:
30+
- name: hello-openshift
31+
image: openshift/hello-openshift

0 commit comments

Comments
 (0)