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

Only add KPA label to a K8S service of a revision if KPA is used #3498

Merged
merged 6 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion pkg/reconciler/v1alpha1/configuration/resources/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/knative/pkg/kmeta"
"github.com/knative/serving/pkg/apis/autoscaling"
"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -38,11 +39,15 @@ func MakeRevision(config *v1alpha1.Configuration, buildRef *corev1.ObjectReferen

UpdateRevisionLabels(rev, config)

// Populate the Configuration Generation annotation.
if rev.Annotations == nil {
rev.Annotations = make(map[string]string)
}

// Set KPA annotation as default if it is empty
if _, ok := rev.Annotations[autoscaling.ClassAnnotationKey]; !ok {
rev.Annotations[autoscaling.ClassAnnotationKey] = autoscaling.KPA
}

// Populate OwnerReferences so that deletes cascade.
rev.OwnerReferences = append(rev.OwnerReferences, *kmeta.NewControllerRef(config))

Expand Down
21 changes: 15 additions & 6 deletions pkg/reconciler/v1alpha1/configuration/resources/revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/knative/serving/pkg/apis/autoscaling"
"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
)
Expand Down Expand Up @@ -55,7 +56,9 @@ func TestMakeRevisions(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "no",
GenerateName: "build-",
Annotations: map[string]string{},
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.KPA,
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Kind: "Configuration",
Expand Down Expand Up @@ -108,7 +111,9 @@ func TestMakeRevisions(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "with",
GenerateName: "build-",
Annotations: map[string]string{},
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.KPA,
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Kind: "Configuration",
Expand Down Expand Up @@ -162,7 +167,9 @@ func TestMakeRevisions(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "with",
GenerateName: "labels-",
Annotations: map[string]string{},
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.KPA,
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Kind: "Configuration",
Expand All @@ -175,8 +182,8 @@ func TestMakeRevisions(t *testing.T) {
serving.ConfigurationGenerationLabelKey: "100",
serving.DeprecatedConfigurationMetadataGenerationLabelKey: "100",
serving.ServiceLabelKey: "",
"foo": "bar",
"baz": "blah",
"foo": "bar",
"baz": "blah",
},
},
Spec: v1alpha1.RevisionSpec{
Expand All @@ -186,7 +193,7 @@ func TestMakeRevisions(t *testing.T) {
},
},
}, {
name: "with annotations",
name: "with annotations including autoscaling.knative.dev/class=hpa",
configuration: &v1alpha1.Configuration{
ObjectMeta: metav1.ObjectMeta{
Namespace: "with",
Expand All @@ -199,6 +206,7 @@ func TestMakeRevisions(t *testing.T) {
Annotations: map[string]string{
"foo": "bar",
"baz": "blah",
autoscaling.ClassAnnotationKey: autoscaling.HPA,
},
},
Spec: v1alpha1.RevisionSpec{
Expand Down Expand Up @@ -229,6 +237,7 @@ func TestMakeRevisions(t *testing.T) {
Annotations: map[string]string{
"foo": "bar",
"baz": "blah",
autoscaling.ClassAnnotationKey: autoscaling.HPA,
},
},
Spec: v1alpha1.RevisionSpec{
Expand Down
7 changes: 6 additions & 1 deletion pkg/reconciler/v1alpha1/revision/resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import (
// serving.RevisionLabelKey label. Traffic is routed to queue-proxy port.
func MakeK8sService(rev *v1alpha1.Revision) *corev1.Service {
labels := makeLabels(rev)
labels[autoscaling.KPALabelKey] = names.KPA(rev)
// Set KPALabelKey label if KPA is used for this Revision. If ClassAnnotationKey
// is empty, default to KPA class for backward compatibility.
if pa, ok := rev.Annotations[autoscaling.ClassAnnotationKey]; !ok || pa == autoscaling.KPA {
labels[autoscaling.KPALabelKey] = names.KPA(rev)
}

return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: names.K8sService(rev),
Expand Down
61 changes: 58 additions & 3 deletions pkg/reconciler/v1alpha1/revision/resources/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestMakeK8sService(t *testing.T) {
rev *v1alpha1.Revision
want *corev1.Service
}{{
name: "name is bar",
name: "name is bar and use KPA by default",
rev: &v1alpha1.Revision{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Expand Down Expand Up @@ -81,12 +81,15 @@ func TestMakeK8sService(t *testing.T) {
},
},
}, {
name: "name is baz",
name: "name is baz and use KPA explicitly",
rev: &v1alpha1.Revision{
ObjectMeta: metav1.ObjectMeta{
Namespace: "blah",
Name: "baz",
UID: "1234",
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.KPA,
},
},
Spec: v1alpha1.RevisionSpec{
Container: corev1.Container{
Expand All @@ -106,7 +109,9 @@ func TestMakeK8sService(t *testing.T) {
serving.RevisionUID: "1234",
AppLabelKey: "baz",
},
Annotations: map[string]string{},
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.KPA,
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Kind: "Revision",
Expand All @@ -133,6 +138,56 @@ func TestMakeK8sService(t *testing.T) {
},
},
},
}, {
name: "use HPA explicitly",
rev: &v1alpha1.Revision{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
UID: "1234",
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.HPA,
},
},
},
want: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-service",
Labels: map[string]string{
serving.RevisionLabelKey: "bar",
serving.RevisionUID: "1234",
AppLabelKey: "bar",
},
Annotations: map[string]string{
autoscaling.ClassAnnotationKey: autoscaling.HPA,
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: v1alpha1.SchemeGroupVersion.String(),
Kind: "Revision",
Name: "bar",
UID: "1234",
Controller: &boolTrue,
BlockOwnerDeletion: &boolTrue,
}},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{{
Name: ServicePortNameHTTP1,
Protocol: corev1.ProtocolTCP,
Port: ServicePort,
TargetPort: intstr.FromString(v1alpha1.RequestQueuePortName),
}, {
Name: MetricsPortName,
Protocol: corev1.ProtocolTCP,
Port: MetricsPort,
TargetPort: intstr.FromString(v1alpha1.RequestQueueMetricsPortName),
}},
Selector: map[string]string{
serving.RevisionLabelKey: "bar",
},
},
},
}}

for _, test := range tests {
Expand Down