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

Sources v1beta1 conversions #827

Merged
merged 14 commits into from
Apr 20, 2020
37 changes: 37 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"context"

configvalidation "github.com/google/knative-gcp/pkg/apis/configs/validation"
"github.com/google/knative-gcp/pkg/apis/events"
eventsv1alpha1 "github.com/google/knative-gcp/pkg/apis/events/v1alpha1"
eventsv1beta1 "github.com/google/knative-gcp/pkg/apis/events/v1beta1"
messagingv1alpha1 "github.com/google/knative-gcp/pkg/apis/messaging/v1alpha1"
"github.com/google/knative-gcp/pkg/apis/pubsub"
pubsubv1alpha1 "github.com/google/knative-gcp/pkg/apis/pubsub/v1alpha1"
Expand Down Expand Up @@ -128,6 +130,8 @@ func NewConfigValidationController(ctx context.Context, _ configmap.Watcher) *co

func NewConversionController(ctx context.Context, _ configmap.Watcher) *controller.Impl {
var (
eventsv1alpha1_ = eventsv1alpha1.SchemeGroupVersion.Version
eventsv1beta1_ = eventsv1beta1.SchemeGroupVersion.Version
pubsubv1alpha1_ = pubsubv1alpha1.SchemeGroupVersion.Version
pubsubv1beta1_ = pubsubv1beta1.SchemeGroupVersion.Version
)
Expand All @@ -138,6 +142,39 @@ func NewConversionController(ctx context.Context, _ configmap.Watcher) *controll

// Specify the types of custom resource definitions that should be converted
map[schema.GroupKind]conversion.GroupKindConversion{
// events
eventsv1alpha1.Kind("CloudAuditLogsSource"): {
DefinitionName: events.CloudAuditLogsSourcesResource.String(),
HubVersion: eventsv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1alpha1_: &eventsv1alpha1.CloudAuditLogsSource{},
eventsv1beta1_: &eventsv1beta1.CloudAuditLogsSource{},
},
},
eventsv1alpha1.Kind("CloudPubSubSource"): {
DefinitionName: events.CloudPubSubSourcesResource.String(),
HubVersion: eventsv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1alpha1_: &eventsv1alpha1.CloudPubSubSource{},
eventsv1beta1_: &eventsv1beta1.CloudPubSubSource{},
},
},
eventsv1alpha1.Kind("CloudSchedulerSource"): {
DefinitionName: events.CloudSchedulerSourcesResource.String(),
HubVersion: eventsv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1alpha1_: &eventsv1alpha1.CloudSchedulerSource{},
eventsv1beta1_: &eventsv1beta1.CloudSchedulerSource{},
},
},
eventsv1alpha1.Kind("CloudStorageSource"): {
DefinitionName: events.CloudStorageSourcesResource.String(),
HubVersion: eventsv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1alpha1_: &eventsv1alpha1.CloudStorageSource{},
eventsv1beta1_: &eventsv1beta1.CloudStorageSource{},
},
},
// pubsub
pubsubv1beta1.Kind("PullSubscription"): {
DefinitionName: pubsub.PullSubscriptionsResource.String(),
Expand Down
9 changes: 9 additions & 0 deletions config/core/resources/cloudauditlogssource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
subresources:
status: {}
preserveUnknownFields: false
conversion:
strategy: Webhook
webhookClientConfig:
service:
name: webhook
namespace: cloud-run-events
additionalPrinterColumns:
- name: Ready
type: string
Expand All @@ -53,6 +59,9 @@ spec:
- name: v1alpha1
served: true
storage: true
- name: v1beta1
served: true
storage: false
validation:
openAPIV3Schema:
type: object
Expand Down
9 changes: 9 additions & 0 deletions config/core/resources/cloudpubsubsource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
subresources:
status: {}
preserveUnknownFields: false
conversion:
strategy: Webhook
webhookClientConfig:
service:
name: webhook
namespace: cloud-run-events
additionalPrinterColumns:
- name: Ready
type: string
Expand All @@ -53,6 +59,9 @@ spec:
- name: v1alpha1
served: true
storage: true
- name: v1beta1
served: true
storage: false
validation:
openAPIV3Schema:
type: object
Expand Down
9 changes: 9 additions & 0 deletions config/core/resources/cloudschedulersource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
subresources:
status: {}
preserveUnknownFields: false
conversion:
strategy: Webhook
webhookClientConfig:
service:
name: webhook
namespace: cloud-run-events
additionalPrinterColumns:
- name: Ready
type: string
Expand All @@ -53,6 +59,9 @@ spec:
- name: v1alpha1
served: true
storage: true
- name: v1beta1
served: true
storage: false
validation:
openAPIV3Schema:
type: object
Expand Down
9 changes: 9 additions & 0 deletions config/core/resources/cloudstoragesource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ spec:
subresources:
status: {}
preserveUnknownFields: false
conversion:
strategy: Webhook
webhookClientConfig:
service:
name: webhook
namespace: cloud-run-events
additionalPrinterColumns:
- name: Ready
type: string
Expand All @@ -56,6 +62,9 @@ spec:
- name: v1alpha1
served: true
storage: true
- name: v1beta1
served: true
storage: false
validation:
openAPIV3Schema:
type: object
Expand Down
112 changes: 112 additions & 0 deletions pkg/apis/convert/conversion_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
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 convert

import (
"context"

duckv1alpha1 "github.com/google/knative-gcp/pkg/apis/duck/v1alpha1"
duckv1beta1 "github.com/google/knative-gcp/pkg/apis/duck/v1beta1"
pkgduckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1"
pkgduckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
)

func ToV1beta1PubSubSpec(from duckv1alpha1.PubSubSpec) duckv1beta1.PubSubSpec {
to := duckv1beta1.PubSubSpec{}
to.SourceSpec = from.SourceSpec
to.IdentitySpec = ToV1beta1IdentitySpec(from.IdentitySpec)
to.Secret = from.Secret
to.Project = from.Project
return to
}
func FromV1beta1PubSubSpec(from duckv1beta1.PubSubSpec) duckv1alpha1.PubSubSpec {
to := duckv1alpha1.PubSubSpec{}
to.SourceSpec = from.SourceSpec
to.IdentitySpec = FromV1beta1IdentitySpec(from.IdentitySpec)
to.Secret = from.Secret
to.Project = from.Project
return to
}

func ToV1beta1IdentitySpec(from duckv1alpha1.IdentitySpec) duckv1beta1.IdentitySpec {
to := duckv1beta1.IdentitySpec{}
to.GoogleServiceAccount = from.GoogleServiceAccount
return to
}
func FromV1beta1IdentitySpec(from duckv1beta1.IdentitySpec) duckv1alpha1.IdentitySpec {
to := duckv1alpha1.IdentitySpec{}
to.GoogleServiceAccount = from.GoogleServiceAccount
return to
}

func ToV1beta1PubSubStatus(from duckv1alpha1.PubSubStatus) duckv1beta1.PubSubStatus {
to := duckv1beta1.PubSubStatus{}
to.IdentityStatus = ToV1beta1IdentityStatus(from.IdentityStatus)
to.SinkURI = from.SinkURI
to.CloudEventAttributes = from.CloudEventAttributes
to.ProjectID = from.ProjectID
to.TopicID = from.TopicID
to.SubscriptionID = from.SubscriptionID
return to
}
func FromV1beta1PubSubStatus(from duckv1beta1.PubSubStatus) duckv1alpha1.PubSubStatus {
to := duckv1alpha1.PubSubStatus{}
to.IdentityStatus = FromV1beta1IdentityStatus(from.IdentityStatus)
to.SinkURI = from.SinkURI
to.CloudEventAttributes = from.CloudEventAttributes
to.ProjectID = from.ProjectID
to.TopicID = from.TopicID
to.SubscriptionID = from.SubscriptionID
return to
}

func ToV1beta1IdentityStatus(from duckv1alpha1.IdentityStatus) duckv1beta1.IdentityStatus {
to := duckv1beta1.IdentityStatus{}
to.Status = from.Status
to.ServiceAccountName = from.ServiceAccountName
return to
}
func FromV1beta1IdentityStatus(from duckv1beta1.IdentityStatus) duckv1alpha1.IdentityStatus {
to := duckv1alpha1.IdentityStatus{}
to.Status = from.Status
to.ServiceAccountName = from.ServiceAccountName
return to
}

func ToV1beta1AddressStatus(ctx context.Context, from pkgduckv1alpha1.AddressStatus) (pkgduckv1beta1.AddressStatus, error) {
to := pkgduckv1beta1.AddressStatus{}
if from.Address != nil {
to.Address = &pkgduckv1beta1.Addressable{}
err := from.Address.ConvertTo(ctx, to.Address)
if err != nil {
return pkgduckv1beta1.AddressStatus{}, err
}
}
return to, nil
}

func FromV1beta1AddressStatus(ctx context.Context, from pkgduckv1beta1.AddressStatus) (pkgduckv1alpha1.AddressStatus, error) {
to := pkgduckv1alpha1.AddressStatus{}
if from.Address != nil {
to.Address = &pkgduckv1alpha1.Addressable{}
err := to.Address.ConvertFrom(ctx, from.Address)
if err != nil {
return pkgduckv1alpha1.AddressStatus{}, err
}
}
return to, nil
}
Loading