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

changed reconciler, UT and e2e to be v1beta1 #1264

Merged
merged 16 commits into from
Jun 12, 2020
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
8 changes: 8 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ func newConversionController(ctx context.Context, _ configmap.Watcher, gcpas *gc
eventsv1beta1_: &eventsv1beta1.CloudStorageSource{},
},
},
eventsv1alpha1.Kind("CloudBuildSource"): {
DefinitionName: events.CloudBuildSourcesResource.String(),
HubVersion: eventsv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1alpha1_: &eventsv1alpha1.CloudBuildSource{},
eventsv1beta1_: &eventsv1beta1.CloudBuildSource{},
},
},
// intevents
inteventsv1alpha1.Kind("PullSubscription"): {
DefinitionName: intevents.PullSubscriptionsResource.String(),
Expand Down
4 changes: 2 additions & 2 deletions config/core/resources/cloudauditlogssource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ spec:
versions:
- name: v1alpha1
served: true
storage: true
storage: false
- name: v1beta1
served: true
storage: false
storage: true
validation:
openAPIV3Schema:
type: object
Expand Down
14 changes: 9 additions & 5 deletions config/core/resources/cloudbuildsource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ spec:
subresources:
status: {}
preserveUnknownFields: false
conversion:
strategy: Webhook
webhookClientConfig:
service:
name: webhook
namespace: cloud-run-events
additionalPrinterColumns:
- name: Ready
type: string
Expand All @@ -52,6 +58,9 @@ spec:
JSONPath: .metadata.creationTimestamp
versions:
- name: v1alpha1
served: true
storage: false
- name: v1beta1
served: true
storage: true
validation:
Expand Down Expand Up @@ -125,11 +134,6 @@ spec:
description: >
Google Cloud Project ID of the project into which the topic should be created. If omitted uses
the Project ID from the GKE cluster metadata service.
topic:
type: string
description: >
ID of the Cloud Pub/Sub Topic to Subscribe to. It must be `cloud-builds` if
specified. Defaults to `cloud-builds`.
status:
type: object
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/core/resources/cloudpubsubsource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ spec:
versions:
- name: v1alpha1
served: true
storage: true
storage: false
- name: v1beta1
served: true
storage: false
storage: true
validation:
openAPIV3Schema:
type: object
Expand Down
4 changes: 2 additions & 2 deletions config/core/resources/cloudschedulersource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ spec:
versions:
- name: v1alpha1
served: true
storage: true
storage: false
- name: v1beta1
served: true
storage: false
storage: true
validation:
openAPIV3Schema:
type: object
Expand Down
4 changes: 2 additions & 2 deletions config/core/resources/cloudstoragesource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ spec:
versions:
- name: v1alpha1
served: true
storage: true
storage: false
- name: v1beta1
served: true
storage: false
storage: true
validation:
openAPIV3Schema:
type: object
Expand Down
4 changes: 2 additions & 2 deletions config/core/resources/pullsubscription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ spec:
versions:
- name: v1alpha1
served: true
storage: true
storage: false
- name: v1beta1
served: true
storage: false
storage: true
# All versions happen to have the same schema today. They will likely diverge in the future.
validation:
openAPIV3Schema:
Expand Down
4 changes: 2 additions & 2 deletions config/core/resources/topic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ spec:
versions:
- name: v1alpha1
served: true
storage: true
storage: false
- name: v1beta1
served: true
storage: false
storage: true
# All versions happen to have the same schema today. They will likely diverge in the future.
validation:
openAPIV3Schema:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/cloudbuildsource/cloudbuildsource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: events.cloud.google.com/v1alpha1
apiVersion: events.cloud.google.com/v1beta1
kind: CloudBuildSource
metadata:
name: cloudbuildsource-test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: events.cloud.google.com/v1alpha1
apiVersion: events.cloud.google.com/v1beta1
kind: CloudSchedulerSource
metadata:
name: scheduler-test
Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/duck/v1beta1/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package v1beta1
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
capri-xiyue marked this conversation as resolved.
Show resolved Hide resolved
metadataClient "github.com/google/knative-gcp/pkg/gclient/metadata"
"github.com/google/knative-gcp/pkg/utils"
"math"
"strconv"

Expand All @@ -33,6 +36,8 @@ const (
// 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"
Expand Down Expand Up @@ -144,6 +149,9 @@ func validateAnnotation(annotations map[string]string, annotation string, minimu
}

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
}
Expand All @@ -161,3 +169,28 @@ func validateAnnotationNotExists(annotations map[string]string, annotation strin
}
return errs
}

// 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)
}
}
}

// CheckImmutableClusterNameAnnotation checks non-empty cluster-name annotation is immutable.
func CheckImmutableClusterNameAnnotation(current *metav1.ObjectMeta, original *metav1.ObjectMeta, errs *apis.FieldError) *apis.FieldError {
if _, ok := original.Annotations[ClusterNameAnnotation]; ok {
if diff := cmp.Diff(original.Annotations[ClusterNameAnnotation], current.Annotations[ClusterNameAnnotation]); diff != "" {
return errs.Also(&apis.FieldError{
Message: "Immutable fields changed (-old +new)",
Paths: []string{fmt.Sprintf("metadata.annotations[%s]", ClusterNameAnnotation)},
Details: diff,
})
}
}
return errs
}
6 changes: 6 additions & 0 deletions pkg/apis/events/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "k8s.io/apimachinery/pkg/runtime/schema"

const (
GroupName = "events.cloud.google.com"
CloudBuildTopic = "cloud-builds"
)

var (
Expand All @@ -44,4 +45,9 @@ var (
Group: GroupName,
Resource: "cloudstoragesources",
}
// CloudBuildSourcesResource represents a CloudBuildSource.
CloudBuildSourcesResource = schema.GroupResource{
capri-xiyue marked this conversation as resolved.
Show resolved Hide resolved
Group: GroupName,
Resource: "cloudbuildsources",
}
)
55 changes: 55 additions & 0 deletions pkg/apis/events/v1alpha1/cloudbuildsource_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 v1alpha1

import (
"context"
"fmt"
"github.com/google/knative-gcp/pkg/apis/convert"
"github.com/google/knative-gcp/pkg/apis/events/v1beta1"
"knative.dev/pkg/apis"
)

// ConvertTo implements apis.Convertible.
// Converts source (from v1beta1.CloudBuildSource) into v1alpha1.CloudBuildSource.
func (source *CloudBuildSource) ConvertTo(_ context.Context, to apis.Convertible) error {
switch sink := to.(type) {
case *v1beta1.CloudBuildSource:
sink.ObjectMeta = source.ObjectMeta
sink.Spec.PubSubSpec = convert.ToV1beta1PubSubSpec(source.Spec.PubSubSpec)
sink.Status.PubSubStatus = convert.ToV1beta1PubSubStatus(source.Status.PubSubStatus)
return nil
default:
return fmt.Errorf("unknown conversion, got: %T", sink)

}
}

// ConvertFrom implements apis.Convertible.
// Converts obj from v1alpha1.CloudBuildSource into v1beta1.CloudBuildSource.
func (sink *CloudBuildSource) ConvertFrom(_ context.Context, from apis.Convertible) error {
switch source := from.(type) {
case *v1beta1.CloudBuildSource:
sink.ObjectMeta = source.ObjectMeta
sink.Spec.PubSubSpec = convert.FromV1beta1PubSubSpec(source.Spec.PubSubSpec)
sink.Status.PubSubStatus = convert.FromV1beta1PubSubStatus(source.Status.PubSubStatus)
return nil
default:
return fmt.Errorf("unknown conversion, got: %T", source)
}
}

100 changes: 100 additions & 0 deletions pkg/apis/events/v1alpha1/cloudbuildsource_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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 v1alpha1

import (
"context"
"net/url"
"testing"

"github.com/google/knative-gcp/pkg/apis/events/v1beta1"

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

// These variables are used to create a 'complete' version of CloudBuildSource where every field is
// filled in.
var (
// completeCloudBuildSource is a CloudBuildSource with every field filled in, except TypeMeta.
// TypeMeta is excluded because conversions do not convert it and this variable was created to
// test conversions.
completeCloudBuildSource = &CloudBuildSource{
ObjectMeta: completeObjectMeta,
Spec: CloudBuildSourceSpec{
PubSubSpec: completePubSubSpec,
},
Status: CloudBuildSourceStatus{
PubSubStatus: completePubSubStatus,
},
}
)

func TestCloudBuildSourceConversionBadType(t *testing.T) {
good, bad := &CloudBuildSource{}, &CloudStorageSource{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestClouBuildSourceConversion(t *testing.T) {
// Just one for now, just adding the for loop for ease of future changes.
versions := []apis.Convertible{&v1beta1.CloudBuildSource{}}

tests := []struct {
name string
in *CloudBuildSource
}{{
name: "min configuration",
in: &CloudBuildSource{
ObjectMeta: metav1.ObjectMeta{
Name: "ps-name",
Namespace: "ps-ns",
Generation: 17,
},
Spec: CloudBuildSourceSpec{},
},
}, {
name: "full configuration",
in: completeCloudBuildSource,
}}
for _, test := range tests {
for _, version := range versions {
t.Run(test.name, func(t *testing.T) {
ver := version
if err := test.in.ConvertTo(context.Background(), ver); err != nil {
t.Errorf("ConvertTo() = %v", err)
}
got := &CloudBuildSource{}
if err := got.ConvertFrom(context.Background(), ver); err != nil {
t.Errorf("ConvertFrom() = %v", err)
}
ignoreUsername := cmp.AllowUnexported(url.Userinfo{})
if diff := cmp.Diff(test.in, got, ignoreUsername); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}
}

Loading