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

KafkaSource and binding v1beta1<>v1alpha1 conversion #1341

Merged
merged 5 commits into from
Jun 26, 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
k8s.io/api v0.18.1
k8s.io/apimachinery v0.18.1
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
k8s.io/utils v0.0.0-20200124190032-861946025e34
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
Expand Down
140 changes: 140 additions & 0 deletions kafka/source/pkg/apis/bindings/v1alpha1/kafka_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2020 The Knative Authors

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"
aliok marked this conversation as resolved.
Show resolved Hide resolved

"knative.dev/eventing-contrib/kafka/source/pkg/apis/bindings/v1beta1"
bindingsv1beta1 "knative.dev/eventing-contrib/kafka/source/pkg/apis/bindings/v1beta1"
"knative.dev/pkg/apis"
)

// ConvertTo implements apis.Convertible.
// Converts source (from v1alpha1.KafkaBinding) into v1beta1.KafkaBinding
func (source *KafkaBinding) ConvertTo(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *v1beta1.KafkaBinding:
kafkaAuthSpec := bindingsv1beta1.KafkaAuthSpec{}
if err := source.Spec.KafkaAuthSpec.ConvertTo(ctx, &kafkaAuthSpec); err != nil {
return err
}

sink.ObjectMeta = source.ObjectMeta
sink.Spec = v1beta1.KafkaBindingSpec{
BindingSpec: source.Spec.BindingSpec,
KafkaAuthSpec: kafkaAuthSpec,
}
sink.Status.Status = source.Status.Status
source.Status.Status.ConvertTo(ctx, &sink.Status.Status)
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", sink)
}
}

// ConvertFrom implements apis.Convertible.
// Converts obj from v1beta1.KafkaBinding into v1alpha1.KafkaBinding
func (sink *KafkaBinding) ConvertFrom(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *v1beta1.KafkaBinding:
kafkaAuthSpec := KafkaAuthSpec{}
if err := kafkaAuthSpec.ConvertFrom(ctx, &source.Spec.KafkaAuthSpec); err != nil {
return err
}

sink.ObjectMeta = source.ObjectMeta
sink.Spec = KafkaBindingSpec{
BindingSpec: source.Spec.BindingSpec,
KafkaAuthSpec: kafkaAuthSpec,
}
sink.Status.Status = source.Status.Status
source.Status.Status.ConvertTo(ctx, &source.Status.Status)
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", source)
}
}

// ConvertTo implements apis.Convertible.
// Converts source (from v1alpha1.KafkaAuthSpec) into v1beta1.KafkaAuthSpec
func (source *KafkaAuthSpec) ConvertTo(_ context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *v1beta1.KafkaAuthSpec:
sink.BootstrapServers = source.BootstrapServers
sink.Net = bindingsv1beta1.KafkaNetSpec{
SASL: bindingsv1beta1.KafkaSASLSpec{
Enable: source.Net.SASL.Enable,
User: bindingsv1beta1.SecretValueFromSource{
SecretKeyRef: source.Net.SASL.User.SecretKeyRef,
},
Password: bindingsv1beta1.SecretValueFromSource{
SecretKeyRef: source.Net.SASL.Password.SecretKeyRef},
},
TLS: bindingsv1beta1.KafkaTLSSpec{
Enable: source.Net.TLS.Enable,
Cert: bindingsv1beta1.SecretValueFromSource{
SecretKeyRef: source.Net.TLS.Cert.SecretKeyRef,
},
Key: bindingsv1beta1.SecretValueFromSource{
SecretKeyRef: source.Net.TLS.Key.SecretKeyRef,
},
CACert: bindingsv1beta1.SecretValueFromSource{
SecretKeyRef: source.Net.TLS.CACert.SecretKeyRef,
},
},
}
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", sink)
}
}

// ConvertFrom implements apis.Convertible.
// Converts obj from v1beta1.KafkaAuthSpec into v1alpha1.KafkaAuthSpec
func (sink *KafkaAuthSpec) ConvertFrom(_ context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *v1beta1.KafkaAuthSpec:
sink.BootstrapServers = source.BootstrapServers
sink.Net = KafkaNetSpec{
SASL: KafkaSASLSpec{
Enable: source.Net.SASL.Enable,
User: SecretValueFromSource{
SecretKeyRef: source.Net.SASL.User.SecretKeyRef,
},
Password: SecretValueFromSource{
SecretKeyRef: source.Net.SASL.Password.SecretKeyRef},
},
TLS: KafkaTLSSpec{
Enable: source.Net.TLS.Enable,
Cert: SecretValueFromSource{
SecretKeyRef: source.Net.TLS.Cert.SecretKeyRef,
},
Key: SecretValueFromSource{
SecretKeyRef: source.Net.TLS.Key.SecretKeyRef,
},
CACert: SecretValueFromSource{
SecretKeyRef: source.Net.TLS.CACert.SecretKeyRef,
},
},
}
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", source)
}
}
Loading