Skip to content

Commit 16852b7

Browse files
mattmoorknative-prow-robot
authored andcommitted
This makes us use the RawExtension introduced in knative#2116. (knative#2118)
This adds an e2e test that a fully-specified Build (w/ TypeMeta) succeeds.
1 parent 44ccaed commit 16852b7

File tree

9 files changed

+129
-35
lines changed

9 files changed

+129
-35
lines changed

pkg/apis/serving/v1alpha1/configuration_types.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
build "github.com/knative/build/pkg/apis/build/v1alpha1"
2120
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221
"k8s.io/apimachinery/pkg/runtime/schema"
2322

@@ -79,7 +78,7 @@ type ConfigurationSpec struct {
7978
// Build optionally holds the specification for the build to
8079
// perform to produce the Revision's container image.
8180
// +optional
82-
Build *build.BuildSpec `json:"build,omitempty"`
81+
Build *RawExtension `json:"build,omitempty"`
8382

8483
// RevisionTemplate holds the latest specification for the Revision to
8584
// be stamped out. If a Build specification is provided, then the

pkg/apis/serving/v1alpha1/zz_generated.deepcopy.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/reconciler/v1alpha1/configuration/configuration_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,18 @@ func TestReconcile(t *testing.T) {
347347
}
348348

349349
func cfgWithBuildAndStatus(name, namespace string, generation int64, build *buildv1alpha1.BuildSpec, status v1alpha1.ConfigurationStatus) *v1alpha1.Configuration {
350+
var bld *v1alpha1.RawExtension
351+
if build != nil {
352+
bld = &v1alpha1.RawExtension{BuildSpec: build}
353+
}
350354
return &v1alpha1.Configuration{
351355
ObjectMeta: metav1.ObjectMeta{
352356
Name: name,
353357
Namespace: namespace,
354358
},
355359
Spec: v1alpha1.ConfigurationSpec{
356360
Generation: generation,
357-
Build: build,
361+
Build: bld,
358362
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
359363
Spec: revisionSpec,
360364
},

pkg/reconciler/v1alpha1/configuration/resources/build.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,26 @@ func MakeBuild(config *v1alpha1.Configuration) *unstructured.Unstructured {
3333
return nil
3434
}
3535

36-
b := &buildv1alpha1.Build{
37-
TypeMeta: metav1.TypeMeta{
38-
APIVersion: "build.knative.dev/v1alpha1",
39-
Kind: "Build",
40-
},
41-
ObjectMeta: metav1.ObjectMeta{
42-
Namespace: config.Namespace,
43-
Name: names.Build(config),
44-
OwnerReferences: []metav1.OwnerReference{*kmeta.NewControllerRef(config)},
45-
},
46-
Spec: *config.Spec.Build,
36+
u := &unstructured.Unstructured{}
37+
if err := config.Spec.Build.As(u); err != nil {
38+
b := &buildv1alpha1.Build{
39+
TypeMeta: metav1.TypeMeta{
40+
APIVersion: "build.knative.dev/v1alpha1",
41+
Kind: "Build",
42+
},
43+
}
44+
if err := config.Spec.Build.As(&b.Spec); err != nil {
45+
// TODO(mattmoor): Return errors.
46+
panic(err.Error())
47+
}
48+
49+
u = MustToUnstructured(b)
4750
}
4851

49-
return MustToUnstructured(b)
52+
u.SetNamespace(config.Namespace)
53+
u.SetName(names.Build(config))
54+
u.SetOwnerReferences([]metav1.OwnerReference{*kmeta.NewControllerRef(config)})
55+
return u
5056
}
5157

5258
func MustToUnstructured(build *buildv1alpha1.Build) *unstructured.Unstructured {

pkg/reconciler/v1alpha1/configuration/resources/build_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ func TestBuilds(t *testing.T) {
6161
},
6262
Spec: v1alpha1.ConfigurationSpec{
6363
Generation: 31,
64-
Build: &buildv1alpha1.BuildSpec{
64+
Build: &v1alpha1.RawExtension{BuildSpec: &buildv1alpha1.BuildSpec{
6565
Steps: []corev1.Container{{
6666
Image: "busybox",
6767
}},
68-
},
68+
}},
6969
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
7070
Spec: v1alpha1.RevisionSpec{
7171
Container: corev1.Container{
@@ -115,15 +115,15 @@ func TestBuilds(t *testing.T) {
115115
},
116116
Spec: v1alpha1.ConfigurationSpec{
117117
Generation: 42,
118-
Build: &buildv1alpha1.BuildSpec{
118+
Build: &v1alpha1.RawExtension{BuildSpec: &buildv1alpha1.BuildSpec{
119119
Template: &buildv1alpha1.TemplateInstantiationSpec{
120120
Name: "buildpacks",
121121
Arguments: []buildv1alpha1.ArgumentSpec{{
122122
Name: "foo",
123123
Value: "bar",
124124
}},
125125
},
126-
},
126+
}},
127127
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
128128
Spec: v1alpha1.RevisionSpec{
129129
Container: corev1.Container{

pkg/reconciler/v1alpha1/configuration/resources/names/names_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ func TestNamer(t *testing.T) {
7979
},
8080
Spec: v1alpha1.ConfigurationSpec{
8181
Generation: 999,
82-
Build: &buildv1alpha1.BuildSpec{},
82+
Build: &v1alpha1.RawExtension{
83+
BuildSpec: &buildv1alpha1.BuildSpec{},
84+
},
8385
},
8486
},
8587
f: Build,

pkg/reconciler/v1alpha1/configuration/resources/revision_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ func TestRevisions(t *testing.T) {
8484
},
8585
Spec: v1alpha1.ConfigurationSpec{
8686
Generation: 99,
87-
Build: &buildv1alpha1.BuildSpec{
87+
Build: &v1alpha1.RawExtension{BuildSpec: &buildv1alpha1.BuildSpec{
8888
Steps: []corev1.Container{{
8989
Image: "busybox",
9090
}},
91-
},
91+
}},
9292
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
9393
Spec: v1alpha1.RevisionSpec{
9494
Container: corev1.Container{

test/crd.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828

29-
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
3029
"github.com/knative/pkg/test/logging"
3130
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
3231
)
@@ -114,7 +113,7 @@ func Configuration(namespace string, names ResourceNames, imagePath string, opti
114113
return config
115114
}
116115

117-
func ConfigurationWithBuild(namespace string, names ResourceNames, build *buildv1alpha1.BuildSpec, imagePath string) *v1alpha1.Configuration {
116+
func ConfigurationWithBuild(namespace string, names ResourceNames, build *v1alpha1.RawExtension, imagePath string) *v1alpha1.Configuration {
118117
return &v1alpha1.Configuration{
119118
ObjectMeta: metav1.ObjectMeta{
120119
Namespace: namespace,

test/e2e/build_test.go

+94-10
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,83 @@ import (
2828
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
2929
pkgTest "github.com/knative/pkg/test"
3030
"github.com/knative/pkg/test/logging"
31+
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
3132
"github.com/knative/serving/test"
3233
)
3334

35+
func TestBuildSpecAndServe(t *testing.T) {
36+
clients := Setup(t)
37+
38+
// Add test case specific name to its own logger.
39+
logger := logging.GetContextLogger("TestBuildAndServe")
40+
41+
imagePath := test.ImagePath("helloworld")
42+
43+
logger.Infof("Creating a new Route and Configuration with build")
44+
names := test.ResourceNames{
45+
Config: test.AppendRandomString(configName, logger),
46+
Route: test.AppendRandomString(routeName, logger),
47+
}
48+
49+
build := &v1alpha1.RawExtension{
50+
BuildSpec: &buildv1alpha1.BuildSpec{
51+
Steps: []corev1.Container{{
52+
Image: "ubuntu",
53+
Args: []string{"echo", "built"},
54+
}},
55+
},
56+
}
57+
58+
if _, err := clients.ServingClient.Configs.Create(test.ConfigurationWithBuild(test.ServingNamespace, names, build, imagePath)); err != nil {
59+
t.Fatalf("Failed to create Configuration: %v", err)
60+
}
61+
if _, err := clients.ServingClient.Routes.Create(test.Route(test.ServingNamespace, names)); err != nil {
62+
t.Fatalf("Failed to create Route: %v", err)
63+
}
64+
65+
test.CleanupOnInterrupt(func() { TearDown(clients, names, logger) }, logger)
66+
defer TearDown(clients, names, logger)
67+
68+
logger.Infof("When the Revision can have traffic routed to it, the Route is marked as Ready.")
69+
if err := test.WaitForRouteState(clients.ServingClient, names.Route, test.IsRouteReady, "RouteIsReady"); err != nil {
70+
t.Fatalf("The Route %s was not marked as Ready to serve traffic: %v", names.Route, err)
71+
}
72+
73+
route, err := clients.ServingClient.Routes.Get(names.Route, metav1.GetOptions{})
74+
if err != nil {
75+
t.Fatalf("Error fetching Route %s: %v", names.Route, err)
76+
}
77+
domain := route.Status.Domain
78+
79+
endState := pkgTest.Retrying(pkgTest.MatchesBody(helloWorldExpectedOutput), http.StatusNotFound)
80+
if _, err := pkgTest.WaitForEndpointState(clients.KubeClient, logger, domain, endState, "HelloWorldServesText", test.ServingFlags.ResolvableDomain); err != nil {
81+
t.Fatalf("The endpoint for Route %s at domain %s didn't serve the expected text \"%s\": %v", names.Route, domain, helloWorldExpectedOutput, err)
82+
}
83+
84+
// Get Configuration's latest ready Revision's Build, and check that the Build was successful.
85+
logger.Infof("Revision is ready and serving, checking Build status.")
86+
config, err := clients.ServingClient.Configs.Get(names.Config, metav1.GetOptions{})
87+
if err != nil {
88+
t.Fatalf("Failed to get Configuration after it was seen to be live: %v", err)
89+
}
90+
rev, err := clients.ServingClient.Revisions.Get(config.Status.LatestReadyRevisionName, metav1.GetOptions{})
91+
if err != nil {
92+
t.Fatalf("Failed to get latest Revision: %v", err)
93+
}
94+
buildName := rev.Spec.BuildName
95+
logger.Infof("Latest ready Revision is %q", rev.Name)
96+
logger.Infof("Revision's Build is %q", buildName)
97+
b, err := clients.BuildClient.Builds.Get(buildName, metav1.GetOptions{})
98+
if err != nil {
99+
t.Fatalf("Failed to get build for latest revision: %v", err)
100+
}
101+
if cond := b.Status.GetCondition(buildv1alpha1.BuildSucceeded); cond == nil {
102+
t.Fatalf("Condition for build %q was nil", buildName)
103+
} else if cond.Status != corev1.ConditionTrue {
104+
t.Fatalf("Build %q was not successful", buildName)
105+
}
106+
}
107+
34108
func TestBuildAndServe(t *testing.T) {
35109
clients := Setup(t)
36110

@@ -45,11 +119,19 @@ func TestBuildAndServe(t *testing.T) {
45119
Route: test.AppendRandomString(routeName, logger),
46120
}
47121

48-
build := &buildv1alpha1.BuildSpec{
49-
Steps: []corev1.Container{{
50-
Image: "ubuntu",
51-
Args: []string{"echo", "built"},
52-
}},
122+
build := &v1alpha1.RawExtension{
123+
Object: &buildv1alpha1.Build{
124+
TypeMeta: metav1.TypeMeta{
125+
APIVersion: "build.knative.dev/v1alpha1",
126+
Kind: "Build",
127+
},
128+
Spec: buildv1alpha1.BuildSpec{
129+
Steps: []corev1.Container{{
130+
Image: "ubuntu",
131+
Args: []string{"echo", "built"},
132+
}},
133+
},
134+
},
53135
}
54136

55137
if _, err := clients.ServingClient.Configs.Create(test.ConfigurationWithBuild(test.ServingNamespace, names, build, imagePath)); err != nil {
@@ -114,11 +196,13 @@ func TestBuildFailure(t *testing.T) {
114196
}
115197

116198
// Request a build that doesn't succeed.
117-
build := &buildv1alpha1.BuildSpec{
118-
Steps: []corev1.Container{{
119-
Image: "ubuntu",
120-
Args: []string{"false"}, // build will fail.
121-
}},
199+
build := &v1alpha1.RawExtension{
200+
BuildSpec: &buildv1alpha1.BuildSpec{
201+
Steps: []corev1.Container{{
202+
Image: "ubuntu",
203+
Args: []string{"false"}, // build will fail.
204+
}},
205+
},
122206
}
123207

124208
imagePath := test.ImagePath("helloworld")

0 commit comments

Comments
 (0)