Skip to content
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
4 changes: 2 additions & 2 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ chmod +x ${CODEGEN_PKG}/generate-groups.sh
# instead of the $GOPATH directly. For normal projects this can be dropped.
${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \
knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \
"eventing:v1beta1 messaging:v1beta1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 configs:v1alpha1" \
"eventing:v1beta1 eventing:v1 messaging:v1beta1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 configs:v1alpha1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

# Deep copy config
Expand All @@ -65,7 +65,7 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy" \
chmod +x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh
${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \
knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \
"eventing:v1beta1 messaging:v1beta1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 duck:v1alpha1 duck:v1beta1 duck:v1 configs:v1alpha1" \
"eventing:v1beta1 eventing:v1 messaging:v1beta1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 duck:v1alpha1 duck:v1beta1 duck:v1 configs:v1alpha1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

# Make sure our dependencies are up-to-date
Expand Down
34 changes: 34 additions & 0 deletions pkg/apis/eventing/v1/broker_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 v1

import (
"context"
"fmt"

"knative.dev/pkg/apis"
)

// ConvertTo implements apis.Convertible
func (source *Broker) ConvertTo(ctx context.Context, sink apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", sink)
}

// ConvertFrom implements apis.Convertible
func (sink *Broker) ConvertFrom(ctx context.Context, source apis.Convertible) error {
return fmt.Errorf("v1 is the highest known version, got: %T", source)
}
34 changes: 34 additions & 0 deletions pkg/apis/eventing/v1/broker_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 v1

import (
"context"
"testing"
)

func TestBrokerConversionBadType(t *testing.T) {
good, bad := &Broker{}, &Broker{}

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)
}
}
47 changes: 47 additions & 0 deletions pkg/apis/eventing/v1/broker_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 v1

import (
"context"

"knative.dev/eventing/pkg/apis/config"
"knative.dev/eventing/pkg/apis/eventing"
"knative.dev/pkg/apis"
)

func (b *Broker) SetDefaults(ctx context.Context) {
// Default Spec fields.
withNS := apis.WithinParent(ctx, b.ObjectMeta)
b.Spec.SetDefaults(withNS)
eventing.DefaultBrokerClassIfUnset(withNS, &b.ObjectMeta)
}

func (bs *BrokerSpec) SetDefaults(ctx context.Context) {
if bs.Config != nil {
// Default the namespace if not given
bs.Config.SetDefaults(ctx)
return
}

cfg := config.FromContextOrDefaults(ctx)
c, err := cfg.Defaults.GetBrokerConfig(apis.ParentMeta(ctx).Namespace)
if err == nil {
c.SetDefaults(ctx)
bs.Config = c
}
}
249 changes: 249 additions & 0 deletions pkg/apis/eventing/v1/broker_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/*
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 v1

import (
"context"
"testing"

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

"knative.dev/eventing/pkg/apis/config"
"knative.dev/eventing/pkg/apis/eventing"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

var (
defaultConfig = &config.Config{
Defaults: &config.Defaults{
// NamespaceDefaultsConfig are the default Broker Configs for each namespace.
// Namespace is the key, the value is the KReference to the config.
NamespaceDefaultsConfig: map[string]*config.ClassAndKRef{
"mynamespace": {
KReference: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "kafka-channel",
},
},
"mynamespace2": {
BrokerClass: "mynamespace2class",
KReference: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "natss-channel",
},
},
"mynamespace3": {
BrokerClass: "mynamespace3class",
KReference: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Name: "natss-channel",
},
},
},
ClusterDefault: &config.ClassAndKRef{
BrokerClass: eventing.MTChannelBrokerClassValue,
KReference: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "imc-channel",
},
},
},
}
)

func TestBrokerSetDefaults(t *testing.T) {
testCases := map[string]struct {
initial Broker
expected Broker
}{
"default everything from cluster": {
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue,
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "imc-channel",
},
},
},
},
"default annotation from cluster": {
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue,
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "imc-channel",
},
},
},
initial: Broker{
Spec: BrokerSpec{
Config: &duckv1.KReference{
APIVersion: "v1",
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "imc-channel",
},
},
},
},
"config already specified, adds annotation": {
initial: Broker{
Spec: BrokerSpec{
Config: &duckv1.KReference{
APIVersion: SchemeGroupVersion.String(),
Kind: "OtherChannel",
},
},
},
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue,
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
APIVersion: SchemeGroupVersion.String(),
Kind: "OtherChannel",
},
},
},
},
"no config, uses namespace broker config, cluster class": {
initial: Broker{
ObjectMeta: metav1.ObjectMeta{Namespace: "mynamespace"},
},
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Namespace: "mynamespace",
Annotations: map[string]string{
eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue,
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "kafka-channel",
APIVersion: "v1",
},
},
},
},
"no config, uses namespace broker config, defaults namespace, cluster class": {
initial: Broker{
ObjectMeta: metav1.ObjectMeta{Namespace: "mynamespace3"},
},
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Namespace: "mynamespace3",
Annotations: map[string]string{
eventing.BrokerClassKey: "mynamespace3class",
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Kind: "ConfigMap",
Namespace: "mynamespace3",
Name: "natss-channel",
APIVersion: "v1",
},
},
},
},
"no config, uses namespace broker config and class": {
initial: Broker{
ObjectMeta: metav1.ObjectMeta{Namespace: "mynamespace2"},
},
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Namespace: "mynamespace2",
Annotations: map[string]string{
eventing.BrokerClassKey: "mynamespace2class",
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Kind: "ConfigMap",
Namespace: "knative-eventing",
Name: "natss-channel",
APIVersion: "v1",
},
},
},
},
"config, missing namespace, defaulted": {
initial: Broker{
ObjectMeta: metav1.ObjectMeta{Name: "rando", Namespace: "randons"},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Kind: "ConfigMap",
Name: "natss-channel",
APIVersion: "v1",
},
},
},
expected: Broker{
ObjectMeta: metav1.ObjectMeta{
Name: "rando",
Namespace: "randons",
Annotations: map[string]string{
eventing.BrokerClassKey: "MTChannelBasedBroker",
},
},
Spec: BrokerSpec{
Config: &duckv1.KReference{
Kind: "ConfigMap",
Namespace: "randons",
Name: "natss-channel",
APIVersion: "v1",
},
},
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
tc.initial.SetDefaults(config.ToContext(context.Background(), defaultConfig))
if diff := cmp.Diff(tc.expected, tc.initial); diff != "" {
t.Fatalf("Unexpected defaults (-want, +got): %s", diff)
}
})
}
}
Loading