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

Move resource synthesis for Service controller into resource subpackage. #1443

Merged
merged 3 commits into from
Jul 2, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package service
package resources

import (
"errors"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/knative/serving/pkg/controller"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// MakeServiceConfiguration creates a Configuration from a Service object.
func MakeServiceConfiguration(service *v1alpha1.Service) (*v1alpha1.Configuration, error) {
// MakeConfiguration creates a Configuration from a Service object.
func MakeConfiguration(service *v1alpha1.Service) (*v1alpha1.Configuration, error) {
c := &v1alpha1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Name: controller.GetServiceConfigurationName(service),
Namespace: service.Namespace,
OwnerReferences: []metav1.OwnerReference{
*controller.NewServiceControllerRef(service),
},
Labels: MakeServingResourceLabels(service),
Labels: MakeLabels(service),
},
}

Expand Down
81 changes: 81 additions & 0 deletions pkg/controller/service/resources/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"testing"

"github.com/knative/serving/pkg/apis/serving"
)

func TestRunLatest(t *testing.T) {
s := createServiceWithRunLatest()
c, _ := MakeConfiguration(s)
if got, want := c.Name, testServiceName; got != want {
t.Errorf("expected %q for service name got %q", want, got)
}
if got, want := c.Namespace, testServiceNamespace; got != want {
t.Errorf("expected %q for service namespace got %q", want, got)
}
if got, want := c.Spec.RevisionTemplate.Spec.Container.Name, testContainerNameRunLatest; got != want {
t.Errorf("expected %q for container name got %q", want, got)
}
expectOwnerReferencesSetCorrectly(t, c.OwnerReferences)

if got, want := len(c.Labels), 2; got != want {
t.Errorf("expected %d labels got %d", want, got)
}
if got, want := c.Labels[testLabelKey], testLabelValueRunLatest; got != want {
t.Errorf("expected %q labels got %q", want, got)
}
if got, want := c.Labels[serving.ServiceLabelKey], testServiceName; got != want {
t.Errorf("expected %q labels got %q", want, got)
}
}

func TestPinned(t *testing.T) {
s := createServiceWithPinned()
c, _ := MakeConfiguration(s)
if got, want := c.Name, testServiceName; got != want {
t.Errorf("expected %q for service name got %q", want, got)
}
if got, want := c.Namespace, testServiceNamespace; got != want {
t.Errorf("expected %q for service namespace got %q", want, got)
}
if got, want := c.Spec.RevisionTemplate.Spec.Container.Name, testContainerNamePinned; got != want {
t.Errorf("expected %q for container name got %q", want, got)
}
expectOwnerReferencesSetCorrectly(t, c.OwnerReferences)

if got, want := len(c.Labels), 2; got != want {
t.Errorf("expected %d labels got %d", want, got)
}
if got, want := c.Labels[testLabelKey], testLabelValuePinned; got != want {
t.Errorf("expected %q labels got %q", want, got)
}
if got, want := c.Labels[serving.ServiceLabelKey], testServiceName; got != want {
t.Errorf("expected %q labels got %q", want, got)
}
}

func TestMalformed(t *testing.T) {
s := createServiceMeta()
c, err := MakeConfiguration(s)
if err == nil {
t.Errorf("MakeConfiguration() = %v, wanted error", c)
}
}
19 changes: 19 additions & 0 deletions pkg/controller/service/resources/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package resources holds simple functions for synthesizing child resources
// from a Service resource and any relevant Service controller configuration.
package resources
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
package resources

import (
"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
)

// MakeServingResourceLabels constructs the labels we will apply to Route and Configuration
// resources.
func MakeServingResourceLabels(s *v1alpha1.Service) map[string]string {
// MakeLabels constructs the labels we will apply to Route and Configuration resources.
func MakeLabels(s *v1alpha1.Service) map[string]string {
labels := make(map[string]string, len(s.ObjectMeta.Labels)+1)
labels[serving.ServiceLabelKey] = s.Name

// Pass through the labels on the Service to child resources.
for k, v := range s.ObjectMeta.Labels {
labels[k] = v
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/controller/service/resources/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
)

func TestMakeLabels(t *testing.T) {
tests := []struct {
name string
svc *v1alpha1.Service
want map[string]string
}{{
name: "just service name",
svc: &v1alpha1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
},
want: map[string]string{
serving.ServiceLabelKey: "bar",
},
}, {
name: "pass through labels",
svc: &v1alpha1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: "baz",
Name: "blah",
Labels: map[string]string{
"asdf": "bazinga",
"ooga": "booga",
},
},
},
want: map[string]string{
serving.ServiceLabelKey: "blah",
"asdf": "bazinga",
"ooga": "booga",
},
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := MakeLabels(test.svc)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("MakeLabels (-want, +got) = %v", diff)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package service
package resources

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/knative/serving/pkg/controller"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// MakeServiceRoute creates a Route from a Service object.
func MakeServiceRoute(service *v1alpha1.Service) *v1alpha1.Route {
// MakeRoute creates a Route from a Service object.
func MakeRoute(service *v1alpha1.Service) *v1alpha1.Route {
c := &v1alpha1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: controller.GetServiceRouteName(service),
Namespace: service.Namespace,
OwnerReferences: []metav1.OwnerReference{
*controller.NewServiceControllerRef(service),
},
Labels: MakeServingResourceLabels(service),
Labels: MakeLabels(service),
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package service
package resources

import (
"testing"
Expand All @@ -23,7 +26,7 @@ import (
func TestRouteRunLatest(t *testing.T) {
s := createServiceWithRunLatest()
testConfigName := controller.GetServiceConfigurationName(s)
r := MakeServiceRoute(s)
r := MakeRoute(s)
if got, want := r.Name, testServiceName; got != want {
t.Errorf("expected %q for service name got %q", want, got)
}
Expand Down Expand Up @@ -58,7 +61,7 @@ func TestRouteRunLatest(t *testing.T) {

func TestRoutePinned(t *testing.T) {
s := createServiceWithPinned()
r := MakeServiceRoute(s)
r := MakeRoute(s)
if got, want := r.Name, testServiceName; got != want {
t.Errorf("expected %q for service name got %q", want, got)
}
Expand Down
Loading