Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into f/upgrade016
Browse files Browse the repository at this point in the history
  • Loading branch information
yolocs committed Jul 7, 2020
2 parents dd9509d + a80246e commit e00f4f1
Show file tree
Hide file tree
Showing 225 changed files with 5,163 additions and 2,049 deletions.
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
cloud.google.com/go/storage v1.8.0
github.com/cloudevents/sdk-go v1.2.0
github.com/cloudevents/sdk-go/protocol/pubsub/v2 v2.0.1-0.20200602143929-d07dc0510d45
github.com/cloudevents/sdk-go/v2 v2.0.1-0.20200608152019-2ab697c8fc0b
github.com/cloudevents/sdk-go/v2 v2.0.1-0.20200630063327-b91da81265fe
github.com/fsnotify/fsnotify v1.4.9
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.4.1
Expand All @@ -30,10 +30,9 @@ require (
k8s.io/api v0.18.1
k8s.io/apimachinery v0.18.1
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
knative.dev/eventing v0.15.1-0.20200625220028-1e3a03b620b6
knative.dev/pkg v0.0.0-20200626022628-f1ee372577e1
knative.dev/serving v0.15.1-0.20200626061427-ce9c1723e56a
knative.dev/test-infra v0.0.0-20200625195728-62651036b559 // indirect
knative.dev/eventing v0.15.1-0.20200707170044-e018fe794753
knative.dev/pkg v0.0.0-20200702222342-ea4d6e985ba0
knative.dev/serving v0.16.0
sigs.k8s.io/yaml v1.2.0
)

Expand Down
178 changes: 15 additions & 163 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hack/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function init_pubsub_service_account() {
local project_id=${1}
local pubsub_service_account=${2}
echo "parameter project_id used when initiating pubsub service account is'${project_id}'"
echo "parameter control_plane_service_account used when initiating pubsub service account is'${pubsub_service_account}'"
echo "parameter data_plane_service_account used when initiating pubsub service account is'${pubsub_service_account}'"
# Enable APIs.
gcloud services enable pubsub.googleapis.com

Expand Down
2 changes: 1 addition & 1 deletion hack/update-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ set -o pipefail
cd "${ROOT_DIR}"

# Used to pin floating deps to a release version.
VERSION="master"
VERSION="release-0.16"
# The list of dependencies that we track at HEAD and periodically
# float forward in this repository.
FLOATING_DEPS=(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1
// Package duck contains Cloud Run Events API versions for duck components
package duck

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
testingMetadataClient "github.com/google/knative-gcp/pkg/gclient/metadata/testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

var (
Expand Down Expand Up @@ -119,91 +122,47 @@ func TestSetAutoscalingAnnotationsDefaults(t *testing.T) {
}
}

func TestValidateAutoscalingAnnotations(t *testing.T) {
func TestSetClusterNameAnnotation(t *testing.T) {
testCases := map[string]struct {
objMeta *v1.ObjectMeta
error bool
orig *v1.ObjectMeta
data testingMetadataClient.TestClientData
expected *v1.ObjectMeta
}{
"ok no scaling": {
objMeta: noScaling,
error: false,
},
"invalid extra resources": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
delete(obj.Annotations, AutoscalingClassAnnotation)
return obj
}(),
error: true,
},
"ok keda scaling": {
objMeta: kedaScaling,
error: false,
},
"unsupported scaling class": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[AutoscalingClassAnnotation] = "invalid"
return obj
}(),
error: true,
},
"invalid min scale": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[AutoscalingMinScaleAnnotation] = "-1"
return obj
}(),
error: true,
},
"invalid max scale": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[AutoscalingMaxScaleAnnotation] = "0"
return obj
}(),
error: true,
},
"invalid min > max": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[AutoscalingMinScaleAnnotation] = "4"
obj.Annotations[AutoscalingMaxScaleAnnotation] = "1"
return obj
}(),
error: true,
},
"invalid pollingInterval": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[KedaAutoscalingPollingIntervalAnnotation] = "1"
return obj
}(),
error: true,
},
"invalid cooldownPeriod": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[KedaAutoscalingCooldownPeriodAnnotation] = "0"
return obj
}(),
error: true,
},
"invalid subscriptionSize": {
objMeta: func() *v1.ObjectMeta {
obj := kedaScaling.DeepCopy()
obj.Annotations[KedaAutoscalingSubscriptionSizeAnnotation] = "0"
return obj
}(),
error: true,
"no annotation, successfully get the clusterName": {
orig: &v1.ObjectMeta{},
data: testingMetadataClient.TestClientData{},
expected: &v1.ObjectMeta{
Annotations: map[string]string{
ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
},
},
},
"no annotation, get clusterName failed": {
orig: &v1.ObjectMeta{},
data: testingMetadataClient.TestClientData{
ClusterNameErr: fmt.Errorf("error when get clusterName"),
},
expected: &v1.ObjectMeta{},
},
"has annotation": {
orig: &v1.ObjectMeta{
Annotations: map[string]string{
ClusterNameAnnotation: "testing-cluster-name",
},
},
data: testingMetadataClient.TestClientData{},
expected: &v1.ObjectMeta{
Annotations: map[string]string{
ClusterNameAnnotation: "testing-cluster-name",
},
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
var errs *apis.FieldError
err := ValidateAutoscalingAnnotations(context.TODO(), tc.objMeta.Annotations, errs)
if tc.error != (err != nil) {
t.Fatalf("Unexpected validation failure. Got %v", err)
SetClusterNameAnnotation(tc.orig, testingMetadataClient.NewTestClient(tc.data))
if diff := cmp.Diff(tc.expected, tc.orig); diff != "" {
t.Errorf("Unexpected differences (-want +got): %v", diff)
}
})
}
Expand Down
71 changes: 71 additions & 0 deletions pkg/apis/duck/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2020 Google LLC
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 duck contains Cloud Run Events API versions for duck components
package duck

import (
"context"

metadataClient "github.com/google/knative-gcp/pkg/gclient/metadata"
"github.com/google/knative-gcp/pkg/utils"

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

func SetAutoscalingAnnotationsDefaults(ctx context.Context, obj *metav1.ObjectMeta) {
// If autoscaling was configured, then set defaults.
if _, ok := obj.Annotations[AutoscalingClassAnnotation]; ok {
setDefaultAnnotationIfNotPresent(obj, AutoscalingMinScaleAnnotation, defaultMinScale)
setDefaultAnnotationIfNotPresent(obj, AutoscalingMaxScaleAnnotation, defaultMaxScale)
setDefaultAnnotationIfNotPresent(obj, KedaAutoscalingPollingIntervalAnnotation, defaultKedaPollingInterval)
setDefaultAnnotationIfNotPresent(obj, KedaAutoscalingCooldownPeriodAnnotation, defaultKedaCooldownPeriod)
setDefaultAnnotationIfNotPresent(obj, KedaAutoscalingSubscriptionSizeAnnotation, defaultKedaSubscriptionSize)
// If it wasn't configured, then delete any autoscaling related configuration.
} else {
deleteAnnotationIfPresent(obj, AutoscalingMinScaleAnnotation)
deleteAnnotationIfPresent(obj, AutoscalingMaxScaleAnnotation)
deleteAnnotationIfPresent(obj, KedaAutoscalingPollingIntervalAnnotation)
deleteAnnotationIfPresent(obj, KedaAutoscalingCooldownPeriodAnnotation)
deleteAnnotationIfPresent(obj, KedaAutoscalingSubscriptionSizeAnnotation)
}
}

func setDefaultAnnotationIfNotPresent(obj *metav1.ObjectMeta, annotation string, defaultValue string) {
if obj.Annotations == nil {
obj.Annotations = map[string]string{}
}
if _, ok := obj.Annotations[annotation]; !ok {
obj.Annotations[annotation] = defaultValue
}
}

func deleteAnnotationIfPresent(obj *metav1.ObjectMeta, annotation string) {
if _, ok := obj.Annotations[annotation]; ok {
delete(obj.Annotations, annotation)
}
}

// SetClusterNameAnnotation sets the cluster-name annotation when running on GKE or GCE.
func SetClusterNameAnnotation(obj *metav1.ObjectMeta, client metadataClient.Client) {
if _, ok := obj.Annotations[ClusterNameAnnotation]; !ok && client.OnGCE() {
clusterName, err := utils.ClusterName(obj.Annotations[ClusterNameAnnotation], client)
// If metadata access is disabled for some reason, leave the annotation to be empty.
if err == nil {
setDefaultAnnotationIfNotPresent(obj, ClusterNameAnnotation, clusterName)
}
}
}
56 changes: 55 additions & 1 deletion pkg/apis/duck/register.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Knative Authors
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,4 +19,58 @@ package duck

const (
GroupName = "duck.cloud.google.com"

// Autoscaling refers to the autoscaling group.
Autoscaling = "autoscaling.knative.dev"

// AutoscalingClassAnnotation is the annotation for the explicit class of
// scaler that a particular resource has opted into.
AutoscalingClassAnnotation = Autoscaling + "/class"
// ClusterNameAnnotation is the annotation for the cluster Name.
ClusterNameAnnotation = "cluster-name"

// AutoscalingMinScaleAnnotation is the annotation to specify the minimum number of pods to scale to.
AutoscalingMinScaleAnnotation = Autoscaling + "/minScale"
// AutoscalingMaxScaleAnnotation is the annotation to specify the maximum number of pods to scale to.
AutoscalingMaxScaleAnnotation = Autoscaling + "/maxScale"

// KEDA is Keda autoscaler.
KEDA = "keda.autoscaling.knative.dev"

// KedaAutoscalingPollingIntervalAnnotation is the annotation that refers to the interval in seconds Keda
// uses to poll metrics in order to inform its scaling decisions.
KedaAutoscalingPollingIntervalAnnotation = KEDA + "/pollingInterval"
// KedaAutoscalingCooldownPeriodAnnotation is the annotation that refers to the period Keda waits until it
// scales a Deployment down.
KedaAutoscalingCooldownPeriodAnnotation = KEDA + "/cooldownPeriod"
// KedaAutoscalingSubscriptionSizeAnnotation is the annotation that refers to the size of unacked messages in a
// Pub/Sub subscription that Keda uses in order to decide when and by how much to scale out.
KedaAutoscalingSubscriptionSizeAnnotation = KEDA + "/subscriptionSize"

// defaultMinScale is the default minimum set of Pods the scaler should
// downscale the resource to.
defaultMinScale = "0"
// defaultMaxScale is the default maximum set of Pods the scaler should
// upscale the resource to.
defaultMaxScale = "1"

// defaultKedaPollingInterval is the default polling interval in seconds Keda uses to poll metrics.
defaultKedaPollingInterval = "15"
// defaultKedaCooldownPeriod is the default cooldown period in seconds Keda uses to downscale resources.
defaultKedaCooldownPeriod = "120"
// defaultKedaSubscriptionSize is the default number of unacked messages in a Pub/Sub subscription that Keda
// uses to scale out resources.
defaultKedaSubscriptionSize = "100"

// minimumMinScale is the minimum allowed value for the AutoscalingMinScaleAnnotation annotation.
minimumMinScale = 0
// minimumMaxScale is the minimum allowed value for the AutoscalingMaxScaleAnnotation annotation.
minimumMaxScale = 1

// minimumKedaPollingInterval is the minimum allowed value for the KedaAutoscalingPollingIntervalAnnotation annotation.
minimumKedaPollingInterval = 5
// minimumKedaCooldownPeriod is the minimum allowed value for the KedaAutoscalingCooldownPeriodAnnotation annotation.
minimumKedaCooldownPeriod = 15
// minimumKedaSubscriptionSize is the minimum allowed value for the KedaAutoscalingSubscriptionSizeAnnotation annotation.
minimumKedaSubscriptionSize = 5
)
Loading

0 comments on commit e00f4f1

Please sign in to comment.