diff --git a/pkg/apis/config/defaults.go b/pkg/apis/config/defaults.go index 9f68edf86f8..ce114c8807c 100644 --- a/pkg/apis/config/defaults.go +++ b/pkg/apis/config/defaults.go @@ -119,6 +119,33 @@ func (d *Defaults) GetBrokerConfig(ns string, brokerClassName string) (*BrokerCo return nil, errors.New("Defaults are nil") } + // Check if the brokerClassName is empty + if brokerClassName == "" { + // We don't have the brokerClassName, check if we have the namespace default config + if d.NamespaceDefaultsConfig != nil { + // We have the namespace default config, check if we have the default broker class config for this namespace + value, present := d.NamespaceDefaultsConfig.NameSpaces[ns] + if present && value != nil { + // We have the default broker class config for this namespace, return the config + brokerClassName = value.DefaultBrokerClass + } else { + // We don't have the default broker class config for this namespace, check if we have the cluster default config + if d.ClusterDefaultConfig != nil && d.ClusterDefaultConfig.DefaultBrokerClassConfig != nil { + brokerClassName = d.ClusterDefaultConfig.DefaultBrokerClass + } else { + return nil, errors.New("Broker class name is empty and Defaults for Broker Configurations have not been set up. Cannot proceed further.") + } + } + } else { + // We don't have the namespace default config, check if we have the cluster default config + if d.ClusterDefaultConfig != nil && d.ClusterDefaultConfig.DefaultBrokerClassConfig != nil { + return d.ClusterDefaultConfig.DefaultBrokerClassConfig, nil + } else { + return nil, errors.New("Defaults for Broker Configurations have not been set up.") + } + } + } + value, present := d.NamespaceDefaultsConfig.NameSpaces[ns] if present && value != nil { // We have the namespace specific config @@ -167,7 +194,7 @@ func (d *Defaults) GetBrokerConfig(ns string, brokerClassName string) (*BrokerCo if d.ClusterDefaultConfig != nil && d.ClusterDefaultConfig.DefaultBrokerClassConfig != nil { return d.ClusterDefaultConfig.DefaultBrokerClassConfig, nil } - return nil, errors.New("Defaults for Broker Configurations have not been set up.") + return nil, errors.New("The given broker class name cannout be found in the namespace defaults config or cluster default config.") } diff --git a/pkg/apis/config/defaults_test.go b/pkg/apis/config/defaults_test.go index 2a527a7c02d..57059572846 100644 --- a/pkg/apis/config/defaults_test.go +++ b/pkg/apis/config/defaults_test.go @@ -19,12 +19,6 @@ package config import ( "testing" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - duckv1 "knative.dev/pkg/apis/duck/v1" - "knative.dev/pkg/kmp" - "knative.dev/pkg/system" - . "knative.dev/pkg/configmap/testing" _ "knative.dev/pkg/system/testing" ) @@ -42,400 +36,495 @@ func TestGetBrokerConfig(t *testing.T) { if err != nil { t.Error("NewDefaultsConfigFromConfigMap(example) =", err) } - c, err := defaults.GetBrokerConfig("rando") + + // Test cluster default, without given broker class name + // Will use cluster default broker class, as no default namespace config for ns "rando" + // The name should be config-default-cluster-class + c, err := defaults.GetBrokerConfig("rando", "") if err != nil { t.Error("GetBrokerConfig Failed =", err) } - if c.Name != "somename" { - t.Error("GetBrokerConfig Failed, wanted somename, got:", c.Name) + if c.Name != "config-default-cluster-class" { + t.Error("GetBrokerConfig Failed, wanted config-default-cluster-class, got:", c.Name) } - c, err = defaults.GetBrokerConfig("some-namespace") + + // Test cluster default, with given broker class name: cluster-class-2 + // Will use the given broker class name with the correspond config from cluster brokerClasses, as no default namespace config for ns "rando" + // The name for the config should be config-cluster-class-2 + c, err = defaults.GetBrokerConfig("rando", "cluster-class-2") if err != nil { t.Error("GetBrokerConfig Failed =", err) } - if c.Name != "someothername" { - t.Error("GetBrokerConfig Failed, wanted someothername, got:", c.Name) + if c.Name != "config-cluster-class-2" { + t.Error("GetBrokerConfig Failed, wanted config-cluster-class-2, got:", c.Name) } - // Nil and empty tests - var nilDefaults *Defaults - _, err = nilDefaults.GetBrokerConfig("rando") - if err == nil { - t.Errorf("GetBrokerConfig did not fail with nil") + // Test namespace default, without given broker class name + // Will use the namespace default broker class, and the config exist for this namespace + // The config name should be config-default-namespace-1-class + c, err = defaults.GetBrokerConfig("namespace-1", "") + if err != nil { + t.Error("GetBrokerConfig Failed =", err) } - if err.Error() != "Defaults are nil" { - t.Error("GetBrokerConfig did not fail with nil msg, got", err) + if c.Name != "config-default-namespace-1-class" { + t.Error("GetBrokerConfig Failed, wanted config-default-namespace-1-class, got:", c.Name) } - emptyDefaults := Defaults{} - _, err = emptyDefaults.GetBrokerConfig("rando") - if err == nil { - t.Errorf("GetBrokerConfig did not fail with empty") + + // Test namespace default, with given broker class name: namespace-1-class-2 + // Will use the given broker class name with the correspond config from namespace brokerClasses + // The config name should be config-namespace-1-class-2 + c, err = defaults.GetBrokerConfig("namespace-1", "namespace-1-class-2") + if err != nil { + t.Error("GetBrokerConfig Failed =", err) } - if err.Error() != "Defaults for Broker Configurations have not been set up." { - t.Error("GetBrokerConfig did not fail with non-setup msg, got", err) + if c.Name != "config-namespace-1-class-2" { + t.Error("GetBrokerConfig Failed, wanted config-namespace-1-class-2, got:", c.Name) } -} -func TestGetBrokerClass(t *testing.T) { - _, example := ConfigMapsFromTestFile(t, DefaultsConfigName) - defaults, err := NewDefaultsConfigFromConfigMap(example) + // Test namespace default, with given broker class name that doesn't have config in this namespace's brokerClasses + // Will use the cluster config for this broker class. i.e will looking for the config in cluster brokerClasses + // The config name should be config-cluster-class-2 + c, err = defaults.GetBrokerConfig("namespace-1", "cluster-class-2") if err != nil { - t.Error("NewDefaultsConfigFromConfigMap(example) =", err) + t.Error("GetBrokerConfig Failed =", err) + } + if c.Name != "config-cluster-class-2" { + t.Error("GetBrokerConfig Failed, wanted config-cluster-class-2, got:", c.Name) + } + + // Test namespace default, specify a broker class name that doesn't exist in this namespace's brokerClasses, but it is cluster's default broker class + // Will use the cluster default broker class config, as no default broker class config is set for this namespace + // The config name should be config-default-cluster-class + c, err = defaults.GetBrokerConfig("namespace-1", "default-cluster-class") + if err != nil { + t.Error("GetBrokerConfig Failed =", err) + } + if c.Name != "config-default-cluster-class" { + t.Error("GetBrokerConfig Failed, wanted config-default-cluster-class, got:", c.Name) } - c, err := defaults.GetBrokerClass("rando") + + // Shared config + // Test namespace default, with given broker class name both have config in this namespace's brokerClasses and cluster brokerClasses + // Will use the given broker class name with the correspond config from namespace brokerClasses. i.e namespace will override cluster config + // The config name should be config-shared-class-in-namespace-1 + c, err = defaults.GetBrokerConfig("namespace-1", "shared-class") + if err != nil { + t.Error("GetBrokerConfig Failed =", err) + } + if c.Name != "config-shared-class-in-namespace-1" { + t.Error("GetBrokerConfig Failed, wanted config-shared-class-in-namespace-1, got:", c.Name) + } + + // Test namespace default, with given broker class name that doesn't have config in this namespace's brokerClasses, and also doesn't have config in cluster brokerClasses + // Will return error, as no config found for this broker class in both namespace brokerClasses and cluster brokerClasses + _, err = defaults.GetBrokerConfig("namespace-1", "cluster-class-3") + if err == nil { + t.Error("GetBrokerConfig did not fail with no config found") + } + + // Test namespace default, without specifying broker class name, and the namespace default broker class's config is not set + // Will use the cluster default broker class config, as no default broker class config is set for this namespace + // The config name should be config-default-cluster-class + c, err = defaults.GetBrokerConfig("namespace-2", "") if err != nil { - t.Error("GetBrokerClass Failed =", err) + t.Error("GetBrokerConfig Failed =", err) } - if c != "MTChannelBasedBroker" { - t.Error("GetBrokerClass Failed, wanted MTChannelBasedBroker, got:", c) + if c.Name != "config-default-cluster-class" { + t.Error("GetBrokerConfig Failed, wanted config-default-cluster-class, got:", c.Name) } - c, err = defaults.GetBrokerClass("some-namespace") + + // Test namespace default, without specifying broker class name, and nothing for this namespace is set + // Will use the cluster default broker class config, as nothing is setted for this namespace + // The config name should be config-default-cluster-class + c, err = defaults.GetBrokerConfig("namespace-3", "") if err != nil { - t.Error("GetBrokerClass Failed =", err) + t.Error("GetBrokerConfig Failed =", err) } - if c != "someotherbrokerclass" { - t.Error("GetBrokerClass Failed, wanted someothername, got:", c) + if c.Name != "config-default-cluster-class" { + t.Error("GetBrokerConfig Failed, wanted config-default-cluster-class, got:", c.Name) } // Nil and empty tests var nilDefaults *Defaults - _, err = nilDefaults.GetBrokerClass("rando") + _, err = nilDefaults.GetBrokerConfig("rando", "") if err == nil { - t.Errorf("GetBrokerClass did not fail with nil") + t.Errorf("GetBrokerConfig did not fail with nil") } if err.Error() != "Defaults are nil" { - t.Error("GetBrokerClass did not fail with nil msg, got", err) + t.Error("GetBrokerConfig did not fail with nil msg, got", err) } + emptyDefaults := Defaults{} - _, err = emptyDefaults.GetBrokerClass("rando") + _, err = emptyDefaults.GetBrokerConfig("rando", "") if err == nil { - t.Errorf("GetBrokerClass did not fail with empty") + t.Errorf("GetBrokerConfig did not fail with empty") } + if err.Error() != "Defaults for Broker Configurations have not been set up." { - t.Error("GetBrokerClass did not fail with non-setup msg, got", err) + t.Error("GetBrokerConfig did not fail with non-setup msg, got", err) } } -func TestDefaultsConfiguration(t *testing.T) { - configTests := []struct { - name string - wantErr bool - wantDefaults interface{} - config *corev1.ConfigMap - }{{ - name: "defaults configuration", - wantErr: true, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{}, - }, - }, { - name: "corrupt default-br-config", - wantErr: true, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{ - "default-br-config": ` - broken YAML -`, - }, - }, - }, { - name: "all specified values", - wantErr: false, - wantDefaults: &Defaults{ - NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ - "some-namespace": { - BrokerClass: "somenamespaceclass", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothername", - Namespace: "someothernamespace", - }, - Delivery: nil, - }, - }, - "some-namespace-too": { - BrokerClass: "somenamespaceclasstoo", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothernametoo", - Namespace: "someothernamespacetoo", - }, - Delivery: nil, - }, - }, - }, - ClusterDefault: &ClassAndBrokerConfig{ - BrokerClass: "clusterbrokerclass", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - Kind: "ConfigMap", - APIVersion: "v1", - Namespace: "knative-eventing", - Name: "somename", - }, - Delivery: nil, - }, - }, - }, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{ - "default-br-config": ` - clusterDefault: - brokerClass: clusterbrokerclass - apiVersion: v1 - kind: ConfigMap - name: somename - namespace: knative-eventing - namespaceDefaults: - some-namespace: - brokerClass: somenamespaceclass - apiVersion: v1 - kind: ConfigMap - name: someothername - namespace: someothernamespace - some-namespace-too: - brokerClass: somenamespaceclasstoo - apiVersion: v1 - kind: ConfigMap - name: someothernametoo - namespace: someothernamespacetoo -`, - }, - }, - }, { - name: "only clusterdefault specified values", - wantErr: false, - wantDefaults: &Defaults{ - // NamespaceDefaultsConfig: map[string]*duckv1.KReference{}, - ClusterDefault: &ClassAndBrokerConfig{ - BrokerClass: "clusterbrokerclass", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - Kind: "ConfigMap", - APIVersion: "v1", - Namespace: "knative-eventing", - Name: "somename", - }, - Delivery: nil, - }, - }, - }, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{ - "default-br-config": ` - clusterDefault: - brokerClass: clusterbrokerclass - apiVersion: v1 - kind: ConfigMap - name: somename - namespace: knative-eventing -`, - }, - }, - }, { - name: "only namespace defaults", - wantErr: false, - wantDefaults: &Defaults{ - NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ - "some-namespace": { - BrokerClass: "brokerclassnamespace", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothername", - Namespace: "someothernamespace", - }, - Delivery: nil, - }, - }, - "some-namespace-too": { - BrokerClass: "brokerclassnamespacetoo", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothernametoo", - Namespace: "someothernamespacetoo", - }, - Delivery: nil, - }, - }, - }, - }, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{ - "default-br-config": ` - namespaceDefaults: - some-namespace: - brokerClass: brokerclassnamespace - apiVersion: v1 - kind: ConfigMap - name: someothername - namespace: someothernamespace - some-namespace-too: - brokerClass: brokerclassnamespacetoo - apiVersion: v1 - kind: ConfigMap - name: someothernametoo - namespace: someothernamespacetoo -`, - }, - }, - }, { - name: "only namespace config default, cluster brokerclass", - wantErr: false, - wantDefaults: &Defaults{ - ClusterDefault: &ClassAndBrokerConfig{ - BrokerClass: "clusterbrokerclass", - }, - NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ - "some-namespace": { - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothername", - Namespace: "someothernamespace", - }, - Delivery: nil, - }, - }, - "some-namespace-too": { - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothernametoo", - Namespace: "someothernamespacetoo", - }, - Delivery: nil, - }, - }, - }, - }, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{ - "default-br-config": ` - clusterDefault: - brokerClass: clusterbrokerclass - namespaceDefaults: - some-namespace: - apiVersion: v1 - kind: ConfigMap - name: someothername - namespace: someothernamespace - some-namespace-too: - apiVersion: v1 - kind: ConfigMap - name: someothernametoo - namespace: someothernamespacetoo -`, - }, - }, - }, { - name: "one namespace config default, namespace config default with class, cluster brokerclass", - wantErr: false, - wantDefaults: &Defaults{ - ClusterDefault: &ClassAndBrokerConfig{ - BrokerClass: "clusterbrokerclass", - }, - NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ - "some-namespace": { - BrokerClass: "namespacebrokerclass", - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothername", - Namespace: "someothernamespace", - }, - Delivery: nil, - }, - }, - "some-namespace-too": { - BrokerConfig: &BrokerConfig{ - KReference: &duckv1.KReference{ - APIVersion: "v1", - Kind: "ConfigMap", - Name: "someothernametoo", - Namespace: "someothernamespacetoo", - }, - Delivery: nil, - }, - }, - }, - }, - config: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: system.Namespace(), - Name: DefaultsConfigName, - }, - Data: map[string]string{ - "default-br-config": ` - clusterDefault: - brokerClass: clusterbrokerclass - namespaceDefaults: - some-namespace: - brokerClass: namespacebrokerclass - apiVersion: v1 - kind: ConfigMap - name: someothername - namespace: someothernamespace - some-namespace-too: - apiVersion: v1 - kind: ConfigMap - name: someothernametoo - namespace: someothernamespacetoo -`, - }, - }, - }} - - for _, tt := range configTests { - t.Run(tt.name, func(t *testing.T) { - actualDefaults, err := NewDefaultsConfigFromConfigMap(tt.config) - - if (err != nil) != tt.wantErr { - t.Fatalf("Test: %q; NewDefaultsConfigFromConfigMap() error = %v, WantErr %v", tt.name, err, tt.wantErr) - } - if !tt.wantErr { - diff, err := kmp.ShortDiff(tt.wantDefaults, actualDefaults) - if err != nil { - t.Fatalf("Diff failed: %s %q", err, diff) - } - if diff != "" { - t.Fatal("diff", diff) - } - } - }) - } -} +//func TestGetBrokerClass(t *testing.T) { +// _, example := ConfigMapsFromTestFile(t, DefaultsConfigName) +// defaults, err := NewDefaultsConfigFromConfigMap(example) +// if err != nil { +// t.Error("NewDefaultsConfigFromConfigMap(example) =", err) +// } +// c, err := defaults.GetBrokerClass("rando") +// if err != nil { +// t.Error("GetBrokerClass Failed =", err) +// } +// if c != "MTChannelBasedBroker" { +// t.Error("GetBrokerClass Failed, wanted MTChannelBasedBroker, got:", c) +// } +// c, err = defaults.GetBrokerClass("some-namespace") +// if err != nil { +// t.Error("GetBrokerClass Failed =", err) +// } +// if c != "someotherbrokerclass" { +// t.Error("GetBrokerClass Failed, wanted someothername, got:", c) +// } +// +// // Nil and empty tests +// var nilDefaults *Defaults +// _, err = nilDefaults.GetBrokerClass("rando") +// if err == nil { +// t.Errorf("GetBrokerClass did not fail with nil") +// } +// if err.Error() != "Defaults are nil" { +// t.Error("GetBrokerClass did not fail with nil msg, got", err) +// } +// emptyDefaults := Defaults{} +// _, err = emptyDefaults.GetBrokerClass("rando") +// if err == nil { +// t.Errorf("GetBrokerClass did not fail with empty") +// } +// if err.Error() != "Defaults for Broker Configurations have not been set up." { +// t.Error("GetBrokerClass did not fail with non-setup msg, got", err) +// } +//} +// +//func TestDefaultsConfiguration(t *testing.T) { +// configTests := []struct { +// name string +// wantErr bool +// wantDefaults interface{} +// config *corev1.ConfigMap +// }{{ +// name: "defaults configuration", +// wantErr: true, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{}, +// }, +// }, { +// name: "corrupt default-br-config", +// wantErr: true, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{ +// "default-br-config": ` +// broken YAML +//`, +// }, +// }, +// }, { +// name: "all specified values", +// wantErr: false, +// wantDefaults: &Defaults{ +// NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ +// "some-namespace": { +// BrokerClass: "somenamespaceclass", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothername", +// Namespace: "someothernamespace", +// }, +// Delivery: nil, +// }, +// }, +// "some-namespace-too": { +// BrokerClass: "somenamespaceclasstoo", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothernametoo", +// Namespace: "someothernamespacetoo", +// }, +// Delivery: nil, +// }, +// }, +// }, +// ClusterDefault: &ClassAndBrokerConfig{ +// BrokerClass: "clusterbrokerclass", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// Kind: "ConfigMap", +// APIVersion: "v1", +// Namespace: "knative-eventing", +// Name: "somename", +// }, +// Delivery: nil, +// }, +// }, +// }, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{ +// "default-br-config": ` +// clusterDefault: +// brokerClass: clusterbrokerclass +// apiVersion: v1 +// kind: ConfigMap +// name: somename +// namespace: knative-eventing +// namespaceDefaults: +// some-namespace: +// brokerClass: somenamespaceclass +// apiVersion: v1 +// kind: ConfigMap +// name: someothername +// namespace: someothernamespace +// some-namespace-too: +// brokerClass: somenamespaceclasstoo +// apiVersion: v1 +// kind: ConfigMap +// name: someothernametoo +// namespace: someothernamespacetoo +//`, +// }, +// }, +// }, { +// name: "only clusterdefault specified values", +// wantErr: false, +// wantDefaults: &Defaults{ +// // NamespaceDefaultsConfig: map[string]*duckv1.KReference{}, +// ClusterDefault: &ClassAndBrokerConfig{ +// BrokerClass: "clusterbrokerclass", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// Kind: "ConfigMap", +// APIVersion: "v1", +// Namespace: "knative-eventing", +// Name: "somename", +// }, +// Delivery: nil, +// }, +// }, +// }, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{ +// "default-br-config": ` +// clusterDefault: +// brokerClass: clusterbrokerclass +// apiVersion: v1 +// kind: ConfigMap +// name: somename +// namespace: knative-eventing +//`, +// }, +// }, +// }, { +// name: "only namespace defaults", +// wantErr: false, +// wantDefaults: &Defaults{ +// NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ +// "some-namespace": { +// BrokerClass: "brokerclassnamespace", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothername", +// Namespace: "someothernamespace", +// }, +// Delivery: nil, +// }, +// }, +// "some-namespace-too": { +// BrokerClass: "brokerclassnamespacetoo", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothernametoo", +// Namespace: "someothernamespacetoo", +// }, +// Delivery: nil, +// }, +// }, +// }, +// }, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{ +// "default-br-config": ` +// namespaceDefaults: +// some-namespace: +// brokerClass: brokerclassnamespace +// apiVersion: v1 +// kind: ConfigMap +// name: someothername +// namespace: someothernamespace +// some-namespace-too: +// brokerClass: brokerclassnamespacetoo +// apiVersion: v1 +// kind: ConfigMap +// name: someothernametoo +// namespace: someothernamespacetoo +//`, +// }, +// }, +// }, { +// name: "only namespace config default, cluster brokerclass", +// wantErr: false, +// wantDefaults: &Defaults{ +// ClusterDefault: &ClassAndBrokerConfig{ +// BrokerClass: "clusterbrokerclass", +// }, +// NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ +// "some-namespace": { +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothername", +// Namespace: "someothernamespace", +// }, +// Delivery: nil, +// }, +// }, +// "some-namespace-too": { +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothernametoo", +// Namespace: "someothernamespacetoo", +// }, +// Delivery: nil, +// }, +// }, +// }, +// }, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{ +// "default-br-config": ` +// clusterDefault: +// brokerClass: clusterbrokerclass +// namespaceDefaults: +// some-namespace: +// apiVersion: v1 +// kind: ConfigMap +// name: someothername +// namespace: someothernamespace +// some-namespace-too: +// apiVersion: v1 +// kind: ConfigMap +// name: someothernametoo +// namespace: someothernamespacetoo +//`, +// }, +// }, +// }, { +// name: "one namespace config default, namespace config default with class, cluster brokerclass", +// wantErr: false, +// wantDefaults: &Defaults{ +// ClusterDefault: &ClassAndBrokerConfig{ +// BrokerClass: "clusterbrokerclass", +// }, +// NamespaceDefaultsConfig: map[string]*ClassAndBrokerConfig{ +// "some-namespace": { +// BrokerClass: "namespacebrokerclass", +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothername", +// Namespace: "someothernamespace", +// }, +// Delivery: nil, +// }, +// }, +// "some-namespace-too": { +// BrokerConfig: &BrokerConfig{ +// KReference: &duckv1.KReference{ +// APIVersion: "v1", +// Kind: "ConfigMap", +// Name: "someothernametoo", +// Namespace: "someothernamespacetoo", +// }, +// Delivery: nil, +// }, +// }, +// }, +// }, +// config: &corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Namespace: system.Namespace(), +// Name: DefaultsConfigName, +// }, +// Data: map[string]string{ +// "default-br-config": ` +// clusterDefault: +// brokerClass: clusterbrokerclass +// namespaceDefaults: +// some-namespace: +// brokerClass: namespacebrokerclass +// apiVersion: v1 +// kind: ConfigMap +// name: someothername +// namespace: someothernamespace +// some-namespace-too: +// apiVersion: v1 +// kind: ConfigMap +// name: someothernametoo +// namespace: someothernamespacetoo +//`, +// }, +// }, +// }} +// +// for _, tt := range configTests { +// t.Run(tt.name, func(t *testing.T) { +// actualDefaults, err := NewDefaultsConfigFromConfigMap(tt.config) +// +// if (err != nil) != tt.wantErr { +// t.Fatalf("Test: %q; NewDefaultsConfigFromConfigMap() error = %v, WantErr %v", tt.name, err, tt.wantErr) +// } +// if !tt.wantErr { +// diff, err := kmp.ShortDiff(tt.wantDefaults, actualDefaults) +// if err != nil { +// t.Fatalf("Diff failed: %s %q", err, diff) +// } +// if diff != "" { +// t.Fatal("diff", diff) +// } +// } +// }) +// } +//} diff --git a/pkg/apis/config/testdata/config-br-defaults.yaml b/pkg/apis/config/testdata/config-br-defaults.yaml index 1e62c0fe5c3..2b4b74b5b6a 100644 --- a/pkg/apis/config/testdata/config-br-defaults.yaml +++ b/pkg/apis/config/testdata/config-br-defaults.yaml @@ -28,10 +28,10 @@ data: ################################ default-br-config: | clusterDefault: - brokerClass: MTChannelBasedBroker + brokerClass: default-cluster-class apiVersion: v1 kind: ConfigMap - name: somename + name: config-default-cluster-class namespace: knative-eventing delivery: retry: 3 @@ -39,18 +39,53 @@ data: ref: apiVersion: serving.knative.dev/v1 kind: Service - name: handle-error + name: mt-handle-error namespace: knative-eventing backoffPolicy: exponential backoffDelay: 3s + brokerClasses: + cluster-class-2: + delivery: + retry: 3 + deadLetterSink: + ref: + apiVersion: serving.knative.dev/v1 + kind: Service + name: mt-handle-error + namespace: knative-eventing + backoffPolicy: exponential + backoffDelay: 3s + + apiVersion: v1 + kind: ConfigMap + name: config-cluster-class-2 + namespace: knative-eventing + + shared-class: + delivery: + retry: 3 + deadLetterSink: + ref: + apiVersion: serving.knative.dev/v1 + kind: Service + name: kafka-handle-error + namespace: knative-eventing + backoffPolicy: exponential + backoffDelay: 3s + + apiVersion: v1 + kind: ConfigMap + name: config-shared-class + namespace: knative-eventing + namespaceDefaults: - some-namespace: - brokerClass: someotherbrokerclass + namespace-1: + brokerClass: default-namespace-1-class apiVersion: v1 kind: ConfigMap - name: someothername - namespace: someothernamespace + name: config-default-namespace-1-class + namespace: namespace-1 delivery: retry: 5 deadLetterSink: @@ -58,6 +93,46 @@ data: apiVersion: serving.knative.dev/v1 kind: Service name: someother-handle-error - namespace: someothernamespace + namespace: knative-eventing backoffPolicy: linear backoffDelay: 5s + brokerClasses: + default-namespace-1-class-2: + delivery: + retry: 3 + deadLetterSink: + ref: + apiVersion: serving.knative.dev/v1 + kind: Service + name: mt-handle-error + namespace: knative-eventing + backoffPolicy: exponential + backoffDelay: 3s + + apiVersion: v1 + kind: ConfigMap + name: config-default-namespace-1-class-2 + namespace: knative-eventing + + shared-class: + delivery: + retry: 3 + deadLetterSink: + ref: + apiVersion: serving.knative.dev/v1 + kind: Service + name: kafka-handle-error + namespace: knative-eventing + backoffPolicy: exponential + backoffDelay: 3s + + apiVersion: v1 + kind: ConfigMap + name: config-shared-class-in-namespace-1 + namespace: knative-eventing + + namespace-2: + brokerClass: default-namespace-2-class + + + namespace-3: \ No newline at end of file