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

Update default credential and rename method #1214

Merged
merged 5 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions pkg/apis/duck/v1alpha1/pubsub_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ func (s *PubSubSpec) SetPubSubDefaults(ctx context.Context) {
logging.FromContext(ctx).Error("Failed to get the GCPAuthDefaults")
return
}
if s.ServiceAccountName == "" {
if s.ServiceAccountName == "" &&
(s.Secret == nil || equality.Semantic.DeepEqual(s.Secret, &corev1.SecretKeySelector{})) {
s.ServiceAccountName = ad.KSA(apis.ParentMeta(ctx).Namespace)
}
if s.Secret == nil || equality.Semantic.DeepEqual(s.Secret, &corev1.SecretKeySelector{}) {
s.Secret = ad.Secret(apis.ParentMeta(ctx).Namespace)
}
}
14 changes: 13 additions & 1 deletion pkg/apis/duck/v1alpha1/pubsub_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"context"
"testing"

gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"
Expand All @@ -29,6 +30,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
testCases := map[string]struct {
orig *PubSubSpec
expected *PubSubSpec
ctx context.Context
}{
"missing defaults": {
orig: &PubSubSpec{},
Expand All @@ -40,6 +42,12 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"missing default GCP Auth ctx": {
orig: &PubSubSpec{},
expected: &PubSubSpec{},
ctx: context.Background(),
},
"empty defaults": {
orig: &PubSubSpec{
Expand All @@ -53,6 +61,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"secret exists same key": {
orig: &PubSubSpec{
Expand All @@ -71,6 +80,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"secret exists same name": {
orig: &PubSubSpec{
Expand All @@ -89,6 +99,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "different-key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"secret exists all different": {
orig: &PubSubSpec{
Expand All @@ -107,11 +118,12 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "different-key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
tc.orig.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
tc.orig.SetPubSubDefaults(tc.ctx)
if diff := cmp.Diff(tc.expected, tc.orig); diff != "" {
t.Errorf("Unexpected differences (-want +got): %v", diff)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/apis/duck/v1beta1/pubsub_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ func (s *PubSubSpec) SetPubSubDefaults(ctx context.Context) {
logging.FromContext(ctx).Error("Failed to get the GCPAuthDefaults")
return
}
if s.ServiceAccountName == "" {
if s.ServiceAccountName == "" &&
(s.Secret == nil || equality.Semantic.DeepEqual(s.Secret, &corev1.SecretKeySelector{})) {
s.ServiceAccountName = ad.KSA(apis.ParentMeta(ctx).Namespace)
}
if s.Secret == nil || equality.Semantic.DeepEqual(s.Secret, &corev1.SecretKeySelector{}) {
s.Secret = ad.Secret(apis.ParentMeta(ctx).Namespace)
}
}
14 changes: 13 additions & 1 deletion pkg/apis/duck/v1beta1/pubsub_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"context"
"testing"

gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"
Expand All @@ -29,6 +30,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
testCases := map[string]struct {
orig *PubSubSpec
expected *PubSubSpec
ctx context.Context
}{
"missing defaults": {
orig: &PubSubSpec{},
Expand All @@ -40,6 +42,12 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"missing default GCP Auth ctx": {
orig: &PubSubSpec{},
expected: &PubSubSpec{},
ctx: context.Background(),
},
"empty defaults": {
orig: &PubSubSpec{
Expand All @@ -53,6 +61,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"secret exists same key": {
orig: &PubSubSpec{
Expand All @@ -71,6 +80,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"secret exists same name": {
orig: &PubSubSpec{
Expand All @@ -89,6 +99,7 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "different-key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
"secret exists all different": {
orig: &PubSubSpec{
Expand All @@ -107,11 +118,12 @@ func TestPubSubSpec_SetPubSubDefaults(t *testing.T) {
Key: "different-key.json",
},
},
ctx: gcpauthtesthelper.ContextWithDefaults(),
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
tc.orig.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
tc.orig.SetPubSubDefaults(tc.ctx)
if diff := cmp.Diff(tc.expected, tc.orig); diff != "" {
t.Errorf("Unexpected differences (-want +got): %v", diff)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/events/auditlogs/auditlogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestAllCases(t *testing.T) {
WithCloudAuditLogsSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudAuditLogsSourceDefaultAuthorization(),
WithCloudAuditLogsSourceDefaultGCPAuth(),
),
NewTopic(sourceName, testNS,
WithTopicSpec(inteventsv1alpha1.TopicSpec{
Expand All @@ -424,7 +424,7 @@ func TestAllCases(t *testing.T) {
WithTopicReady(testTopicID),
WithTopicAddress(testTopicURI),
WithTopicProjectID(testProject),
WithTopicDefaultAuthorization(),
WithTopicDefaultGCPAuth(),
),
},
Key: testNS + "/" + sourceName,
Expand All @@ -439,7 +439,7 @@ func TestAllCases(t *testing.T) {
WithCloudAuditLogsSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudAuditLogsSourceDefaultAuthorization(),
WithCloudAuditLogsSourceDefaultGCPAuth(),
WithCloudAuditLogsSourcePullSubscriptionUnknown("PullSubscriptionNotConfigured", failedToReconcilePullSubscriptionMsg),
),
}},
Expand All @@ -462,7 +462,7 @@ func TestAllCases(t *testing.T) {
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithPullSubscriptionOwnerReferences([]metav1.OwnerReference{sourceOwnerRef(sourceName, sourceUID)}),
WithPullSubscriptionDefaultAuthorization(),
WithPullSubscriptionDefaultGCPAuth(),
),
},
WantPatches: []clientgotesting.PatchActionImpl{
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/events/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestAllCases(t *testing.T) {
WithCloudBuildSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudBuildSourceDefaultAuthorization(),
WithCloudBuildSourceDefaultGCPAuth(),
),
newSink(),
},
Expand All @@ -182,7 +182,7 @@ func TestAllCases(t *testing.T) {
WithCloudBuildSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudBuildSourceDefaultAuthorization(),
WithCloudBuildSourceDefaultGCPAuth(),
WithCloudBuildSourcePullSubscriptionUnknown("PullSubscriptionNotConfigured", "PullSubscription has not yet been reconciled"),
),
}},
Expand All @@ -207,7 +207,7 @@ func TestAllCases(t *testing.T) {
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithPullSubscriptionOwnerReferences([]metav1.OwnerReference{ownerRef()}),
WithPullSubscriptionDefaultAuthorization(),
WithPullSubscriptionDefaultGCPAuth(),
),
},
WantPatches: []clientgotesting.PatchActionImpl{
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/events/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestAllCases(t *testing.T) {
WithCloudPubSubSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudPubSubSourceDefaultAuthorization(),
WithCloudPubSubSourceDefaultGCPAuth(),
),
newSink(),
},
Expand All @@ -180,7 +180,7 @@ func TestAllCases(t *testing.T) {
WithCloudPubSubSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudPubSubSourceDefaultAuthorization(),
WithCloudPubSubSourceDefaultGCPAuth(),
WithCloudPubSubSourcePullSubscriptionUnknown("PullSubscriptionNotConfigured", "PullSubscription has not yet been reconciled"),
),
}},
Expand All @@ -203,7 +203,7 @@ func TestAllCases(t *testing.T) {
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithPullSubscriptionOwnerReferences([]metav1.OwnerReference{ownerRef()}),
WithPullSubscriptionDefaultAuthorization(),
WithPullSubscriptionDefaultGCPAuth(),
),
},
WantPatches: []clientgotesting.PatchActionImpl{
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/events/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func TestAllCases(t *testing.T) {
WithCloudSchedulerSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudSchedulerSourceDefaultAuthorization(),
WithCloudSchedulerSourceDefaultGCPAuth(),
),
NewTopic(schedulerName, testNS,
WithTopicSpec(inteventsv1alpha1.TopicSpec{
Expand All @@ -464,7 +464,7 @@ func TestAllCases(t *testing.T) {
WithTopicReady(testTopicID),
WithTopicAddress(testTopicURI),
WithTopicProjectID(testProject),
WithTopicDefaultAuthorization(),
WithTopicDefaultGCPAuth(),
),
newSink(),
},
Expand All @@ -480,7 +480,7 @@ func TestAllCases(t *testing.T) {
WithCloudSchedulerSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudSchedulerSourceDefaultAuthorization(),
WithCloudSchedulerSourceDefaultGCPAuth(),
WithCloudSchedulerSourcePullSubscriptionUnknown("PullSubscriptionNotConfigured", failedToReconcilePullSubscriptionMsg),
),
}},
Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/events/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func TestAllCases(t *testing.T) {
WithCloudStorageSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudStorageSourceDefaultAuthorization(),
WithCloudStorageSourceDefaultGCPAuth(),
),
NewTopic(storageName, testNS,
WithTopicSpec(inteventsv1alpha1.TopicSpec{
Expand All @@ -454,7 +454,7 @@ func TestAllCases(t *testing.T) {
WithTopicReady(testTopicID),
WithTopicAddress(testTopicURI),
WithTopicProjectID(testProject),
WithTopicDefaultAuthorization(),
WithTopicDefaultGCPAuth(),
),
newSink(),
},
Expand All @@ -471,7 +471,7 @@ func TestAllCases(t *testing.T) {
WithCloudStorageSourceAnnotations(map[string]string{
duckv1alpha1.ClusterNameAnnotation: testingMetadataClient.FakeClusterName,
}),
WithCloudStorageSourceDefaultAuthorization(),
WithCloudStorageSourceDefaultGCPAuth(),
WithCloudStorageSourcePullSubscriptionUnknown("PullSubscriptionNotConfigured", failedToReconcilepullSubscriptionMsg),
),
}},
Expand Down Expand Up @@ -1163,7 +1163,7 @@ func TestAllCases(t *testing.T) {
WithDeletionTimestamp()),
}},
},
{
{
Name: "successfully deleted storage",
Objects: []runtime.Object{
NewCloudStorageSource(storageName, testNS,
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/intevents/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var (

pubsubable = rectesting.NewCloudStorageSource(name, testNS,
rectesting.WithCloudStorageSourceSinkDestination(sink),
rectesting.WithCloudStorageSourceDefaultAuthorization())
rectesting.WithCloudStorageSourceDefaultGCPAuth())

ignoreLastTransitionTime = cmp.FilterPath(func(p cmp.Path) bool {
return strings.HasSuffix(p.String(), "LastTransitionTime.Inner.Time")
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/auditlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func WithCloudAuditLogsSourceAnnotations(Annotations map[string]string) CloudAud
}
}

func WithCloudAuditLogsSourceDefaultAuthorization() CloudAuditLogsSourceOption {
func WithCloudAuditLogsSourceDefaultGCPAuth() CloudAuditLogsSourceOption {
return func(s *v1alpha1.CloudAuditLogsSource) {
s.Spec.PubSubSpec.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func WithCloudBuildSourceAnnotations(Annotations map[string]string) CloudBuildSo
}
}

func WithCloudBuildSourceDefaultAuthorization() CloudBuildSourceOption {
func WithCloudBuildSourceDefaultGCPAuth() CloudBuildSourceOption {
return func(s *v1alpha1.CloudBuildSource) {
s.Spec.PubSubSpec.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func WithCloudPubSubSourceAnnotations(Annotations map[string]string) CloudPubSub
}
}

func WithCloudPubSubSourceDefaultAuthorization() CloudPubSubSourceOption {
func WithCloudPubSubSourceDefaultGCPAuth() CloudPubSubSourceOption {
return func(s *v1alpha1.CloudPubSubSource) {
s.Spec.PubSubSpec.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/pullsubscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func WithPullSubscriptionMode(mode v1alpha1.ModeType) PullSubscriptionOption {
}
}

func WithPullSubscriptionDefaultAuthorization() PullSubscriptionOption {
func WithPullSubscriptionDefaultGCPAuth() PullSubscriptionOption {
return func(s *v1alpha1.PullSubscription) {
s.Spec.PubSubSpec.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func WithCloudSchedulerSourceAnnotations(Annotations map[string]string) CloudSch
}
}

func WithCloudSchedulerSourceDefaultAuthorization() CloudSchedulerSourceOption {
func WithCloudSchedulerSourceDefaultGCPAuth() CloudSchedulerSourceOption {
return func(s *v1alpha1.CloudSchedulerSource) {
s.Spec.PubSubSpec.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func WithCloudStorageSourceAnnotations(Annotations map[string]string) CloudStora
}
}

func WithCloudStorageSourceDefaultAuthorization() CloudStorageSourceOption {
func WithCloudStorageSourceDefaultGCPAuth() CloudStorageSourceOption {
return func(s *v1alpha1.CloudStorageSource) {
s.Spec.PubSubSpec.SetPubSubDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/testing/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func WithTopicAnnotations(annotations map[string]string) TopicOption {
}
}

func WithTopicDefaultAuthorization() TopicOption {
func WithTopicDefaultGCPAuth() TopicOption {
return func(t *v1alpha1.Topic) {
t.Spec.SetDefaults(gcpauthtesthelper.ContextWithDefaults())
}
Expand Down