[go: nahoru, domu]

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

Promote CloudBuildSource to v1 API #1657

Merged
merged 2 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Promote CloudBuildSource to v1 API
  • Loading branch information
danyinggu committed Sep 1, 2020
commit 67b50b28e139b58560df6a90c4fea4dfc0623d36
37 changes: 37 additions & 0 deletions pkg/apis/events/v1/cloudbuildsource_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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 v1

import (
"context"

"knative.dev/pkg/apis"

"github.com/google/knative-gcp/pkg/apis/duck"
metadataClient "github.com/google/knative-gcp/pkg/gclient/metadata"
)

func (bs *CloudBuildSource) SetDefaults(ctx context.Context) {
ctx = apis.WithinParent(ctx, bs.ObjectMeta)
bs.Spec.SetDefaults(ctx)
duck.SetClusterNameAnnotation(&bs.ObjectMeta, metadataClient.NewDefaultMetadataClient())
duck.SetAutoscalingAnnotationsDefaults(ctx, &bs.ObjectMeta)
}

func (bss *CloudBuildSourceSpec) SetDefaults(ctx context.Context) {
bss.SetPubSubDefaults(ctx)
}
134 changes: 134 additions & 0 deletions pkg/apis/events/v1/cloudbuildsource_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
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 v1

import (
"testing"

"github.com/google/go-cmp/cmp"
gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"
"github.com/google/knative-gcp/pkg/apis/duck"
duckv1 "github.com/google/knative-gcp/pkg/apis/duck/v1"
testingMetadataClient "github.com/google/knative-gcp/pkg/gclient/metadata/testing"

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

func TestBuildSourceDefaults(t *testing.T) {
tests := []struct {
name string
start *CloudBuildSource
want *CloudBuildSource
}{{
name: "defaults present",
start: &CloudBuildSource{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
duck.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
},
},
Spec: CloudBuildSourceSpec{
PubSubSpec: duckv1.PubSubSpec{
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "my-cloud-key",
},
Key: "test.json",
},
},
},
},
want: &CloudBuildSource{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
duck.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
},
},
Spec: CloudBuildSourceSpec{
PubSubSpec: duckv1.PubSubSpec{
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "my-cloud-key",
},
Key: "test.json",
},
},
},
},
}, {
// Due to the limitation mentioned in https://github.com/google/knative-gcp/issues/1037, specifying the cluster name annotation.
name: "missing defaults, except cluster name annotations",
start: &CloudBuildSource{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
duck.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
},
},
Spec: CloudBuildSourceSpec{},
},
want: &CloudBuildSource{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
duck.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
},
},
Spec: CloudBuildSourceSpec{
PubSubSpec: duckv1.PubSubSpec{
Secret: &gcpauthtesthelper.Secret,
},
},
},
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.start
got.SetDefaults(gcpauthtesthelper.ContextWithDefaults())

if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("failed to get expected (-want, +got) = %v", diff)
}
})
}
}

func TestCloudBuildSourceDefaults_NoChange(t *testing.T) {
want := &CloudBuildSource{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
duck.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
},
},
Spec: CloudBuildSourceSpec{
PubSubSpec: duckv1.PubSubSpec{
Secret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "my-cloud-key",
},
Key: "test.json",
},
},
},
}

got := want.DeepCopy()
got.SetDefaults(gcpauthtesthelper.ContextWithDefaults())
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("failed to get expected (-want, +got) = %v", diff)
}
}
41 changes: 41 additions & 0 deletions pkg/apis/events/v1/cloudbuildsource_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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 v1

import (
"knative.dev/pkg/apis"
)

// GetCondition returns the condition currently associated with the given type, or nil.
func (bs *CloudBuildSourceStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return buildCondSet.Manage(bs).GetCondition(t)
}

// GetTopLevelCondition returns the top level condition.
func (bs *CloudBuildSourceStatus) GetTopLevelCondition() *apis.Condition {
return buildCondSet.Manage(bs).GetTopLevelCondition()
}

// IsReady returns true if the resource is ready overall.
func (bs *CloudBuildSourceStatus) IsReady() bool {
return buildCondSet.Manage(bs).IsHappy()
}

// InitializeConditions sets relevant unset conditions to Unknown state.
func (bs *CloudBuildSourceStatus) InitializeConditions() {
buildCondSet.Manage(bs).InitializeConditions()
}
158 changes: 158 additions & 0 deletions pkg/apis/events/v1/cloudbuildsource_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
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 v1

import (
duckv1 "github.com/google/knative-gcp/pkg/apis/duck/v1"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
)

func TestCloudBuildSourceStatusIsReady(t *testing.T) {
tests := []struct {
name string
s *CloudBuildSourceStatus
wantConditionStatus corev1.ConditionStatus
want bool
}{
{
name: "uninitialized",
s: &CloudBuildSourceStatus{},
want: false,
}, {
name: "initialized",
s: func() *CloudBuildSourceStatus {
s := &CloudBuildSource{}
s.Status.InitializeConditions()
return &s.Status
}(),
wantConditionStatus: corev1.ConditionUnknown,
want: false,
},
{
name: "the status of pullsubscription is false",
s: func() *CloudBuildSourceStatus {
s := &CloudBuildSource{}
s.Status.InitializeConditions()
s.Status.MarkPullSubscriptionFailed(s.ConditionSet(), "PullSubscriptionFalse", "status false test message")
return &s.Status
}(),
wantConditionStatus: corev1.ConditionFalse,
}, {
name: "the status of pullsubscription is unknown",
s: func() *CloudBuildSourceStatus {
s := &CloudBuildSource{}
s.Status.InitializeConditions()
s.Status.MarkPullSubscriptionUnknown(s.ConditionSet(), "PullSubscriptionUnknown", "status unknown test message")
return &s.Status
}(),
wantConditionStatus: corev1.ConditionUnknown,
},
{
name: "ready",
s: func() *CloudBuildSourceStatus {
s := &CloudBuildSource{}
s.Status.InitializeConditions()
s.Status.MarkPullSubscriptionReady(s.ConditionSet())
return &s.Status
}(),
wantConditionStatus: corev1.ConditionTrue,
want: true,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.wantConditionStatus != "" {
gotConditionStatus := test.s.GetTopLevelCondition().Status
if gotConditionStatus != test.wantConditionStatus {
t.Errorf("unexpected condition status: want %v, got %v", test.wantConditionStatus, gotConditionStatus)
}
}
got := test.s.IsReady()
if got != test.want {
t.Errorf("unexpected readiness: want %v, got %v", test.want, got)
}
})
}
}
func TestCloudBuildSourceStatusGetCondition(t *testing.T) {
tests := []struct {
name string
s *CloudBuildSourceStatus
condQuery apis.ConditionType
want *apis.Condition
}{{
name: "uninitialized",
s: &CloudBuildSourceStatus{},
condQuery: CloudBuildSourceConditionReady,
want: nil,
}, {
name: "initialized",
s: func() *CloudBuildSourceStatus {
s := &CloudBuildSourceStatus{}
s.InitializeConditions()
return s
}(),
condQuery: CloudBuildSourceConditionReady,
want: &apis.Condition{
Type: CloudBuildSourceConditionReady,
Status: corev1.ConditionUnknown,
},
}, {
name: "not ready",

s: func() *CloudBuildSourceStatus {
s := &CloudBuildSource{}
s.Status.InitializeConditions()
s.Status.MarkPullSubscriptionFailed(s.ConditionSet(), "NotReady", "test message")
return &s.Status
}(),
condQuery: duckv1.PullSubscriptionReady,
want: &apis.Condition{
Type: duckv1.PullSubscriptionReady,
Status: corev1.ConditionFalse,
Reason: "NotReady",
Message: "test message",
},
}, {
name: "ready",
s: func() *CloudBuildSourceStatus {
s := &CloudBuildSource{}
s.Status.InitializeConditions()
s.Status.MarkPullSubscriptionReady(s.ConditionSet())
return &s.Status
}(),
condQuery: duckv1.PullSubscriptionReady,
want: &apis.Condition{
Type: duckv1.PullSubscriptionReady,
Status: corev1.ConditionTrue,
},
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.s.GetCondition(test.condQuery)
ignoreTime := cmpopts.IgnoreFields(apis.Condition{},
"LastTransitionTime", "Severity")
if diff := cmp.Diff(test.want, got, ignoreTime); diff != "" {
t.Errorf("unexpected condition (-want, +got) = %v", diff)
}
})
}
}
Loading