-
Notifications
You must be signed in to change notification settings - Fork 597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Broker class spec based default #6621
Changes from 7 commits
520c7cb
3ef5c30
ca0f0e7
2ad62d2
e942ada
1fa7225
d247b0f
375ef8e
3ba0387
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,22 @@ type Defaults struct { | |
type ClassAndBrokerConfig struct { | ||
BrokerClass string `json:"brokerClass,omitempty"` | ||
*BrokerConfig `json:",inline"` | ||
// BrokerClasses set the multiple configurations of different brokerClass. | ||
// Different brokerClass use corresponding brokerConfig for all the namespaces. | ||
BrokerClasses map[string]*BrokerConfigSpec `json:"brokerClasses,omitempty"` | ||
} | ||
|
||
// BrokerConfigSpec contains concrete spec for broker. | ||
type BrokerConfigSpec struct { | ||
Spec *BrokerSpec `json:"spec,omitempty"` | ||
} | ||
|
||
// BrokerSpec contains the Config and Delivery for broker. Allows configuring the Config | ||
// which is a KReference that specifies configuration options for this Broker. | ||
// Delivery contains the delivery spec for each trigger to this Broker. | ||
type BrokerSpec struct { | ||
Config *duckv1.KReference `json:"config,omitempty"` | ||
Delivery *eventingduckv1.DeliverySpec `json:"delivery,omitempty"` | ||
} | ||
|
||
// BrokerConfig contains configuration for a given namespace for broker. Allows | ||
|
@@ -103,12 +119,24 @@ func (d *Defaults) GetBrokerConfig(ns string) (*BrokerConfig, error) { | |
return nil, errors.New("Defaults are nil") | ||
} | ||
value, present := d.NamespaceDefaultsConfig[ns] | ||
// get namespace default config | ||
if present && value.BrokerConfig != nil { | ||
return value.BrokerConfig, nil | ||
} | ||
ndConfig := getClassConfig(d, value) | ||
if ndConfig != nil { | ||
return ndConfig, nil | ||
} | ||
|
||
// get cluster default config | ||
cdConfig := getClassConfig(d, d.ClusterDefault) | ||
if cdConfig != nil { | ||
return cdConfig, nil | ||
} | ||
if d.ClusterDefault != nil && d.ClusterDefault.BrokerConfig != nil { | ||
return d.ClusterDefault.BrokerConfig, nil | ||
} | ||
|
||
return nil, errors.New("Defaults for Broker Configurations have not been set up.") | ||
} | ||
|
||
|
@@ -128,3 +156,36 @@ func (d *Defaults) GetBrokerClass(ns string) (string, error) { | |
} | ||
return "", errors.New("Defaults for Broker Configurations have not been set up.") | ||
} | ||
|
||
// getClassConfig get the corresponding class Config in multiple classes | ||
func getClassConfig(d *Defaults, cb *ClassAndBrokerConfig) *BrokerConfig { | ||
if cb == nil || cb.BrokerClass == "" { | ||
return nil | ||
} | ||
if bCSpec := getBrokerClasses(d); bCSpec != nil { | ||
bConfig := matchBrokerClass(cb.BrokerClass, bCSpec) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here, we are always using cb.BrokerClass which is equal to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the brokerclass we set is in cluster level right? We always need to set a |
||
if bConfig != nil { | ||
return bConfig | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// getBrokerClasses get the configurations of multiple brokerClass | ||
func getBrokerClasses(d *Defaults) map[string]*BrokerConfigSpec { | ||
if d.ClusterDefault != nil && d.ClusterDefault.BrokerClasses != nil { | ||
return d.ClusterDefault.BrokerClasses | ||
} | ||
return nil | ||
} | ||
|
||
// matchBrokerClass find the corresponding brokerConfig for a given brokerClass | ||
func matchBrokerClass(brokerClass string, brokerClasses map[string]*BrokerConfigSpec) *BrokerConfig { | ||
var bConfig BrokerConfig | ||
if bCSpec, ok := brokerClasses[brokerClass]; ok { | ||
bConfig.KReference = bCSpec.Spec.Config | ||
bConfig.Delivery = bCSpec.Spec.Delivery | ||
return &bConfig | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,8 +46,11 @@ func TestGetBrokerConfig(t *testing.T) { | |
if err != nil { | ||
t.Error("GetBrokerConfig Failed =", err) | ||
} | ||
if c.Name != "somename" { | ||
t.Error("GetBrokerConfig Failed, wanted somename, got:", c.Name) | ||
if c.Name != "mt-test" { | ||
t.Error("GetBrokerConfig Failed, wanted mt-test, got:", c.Name) | ||
} | ||
if c.Delivery.DeadLetterSink.Ref.Name != "mt-handle-error" { | ||
t.Error("GetBrokerConfig Failed, wanted mt-handle-error, got:", c.Delivery.DeadLetterSink.Ref.Name) | ||
} | ||
c, err = defaults.GetBrokerConfig("some-namespace") | ||
if err != nil { | ||
|
@@ -56,6 +59,28 @@ func TestGetBrokerConfig(t *testing.T) { | |
if c.Name != "someothername" { | ||
t.Error("GetBrokerConfig Failed, wanted someothername, got:", c.Name) | ||
} | ||
// Test GetBrokerConfig in different namespace | ||
c, err = defaults.GetBrokerConfig("some-namespace-two") | ||
if err != nil { | ||
t.Error("GetBrokerConfig Failed =", err) | ||
} | ||
if c.Name != "kafka-test" { | ||
t.Error("GetBrokerConfig Failed, wanted kafka-test, got:", c.Name) | ||
} | ||
if c.Delivery.DeadLetterSink.Ref.Name != "kafka-handle-error" { | ||
t.Error("GetBrokerConfig Failed, wanted kafka-handle-error, got:", c.Delivery.DeadLetterSink.Ref.Name) | ||
} | ||
|
||
c, err = defaults.GetBrokerConfig("some-namespace-three") | ||
if err != nil { | ||
t.Error("GetBrokerConfig Failed =", err) | ||
} | ||
if c.Name != "kafka-test" { | ||
t.Error("GetBrokerConfig Failed, wanted kafka-test, got:", c.Name) | ||
} | ||
if c.Delivery.DeadLetterSink.Ref.Name != "kafka-handle-error" { | ||
t.Error("GetBrokerConfig Failed, wanted kafka-handle-error, got:", c.Delivery.DeadLetterSink.Ref.Name) | ||
} | ||
|
||
// Nil and empty tests | ||
var nilDefaults *Defaults | ||
|
@@ -117,6 +142,34 @@ func TestGetBrokerClass(t *testing.T) { | |
} | ||
|
||
func TestDefaultsConfiguration(t *testing.T) { | ||
brokerClasses := make(map[string]*BrokerConfigSpec) | ||
brokerSpec1 := &BrokerSpec{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here on the naming |
||
Config: &duckv1.KReference{ | ||
Kind: "ConfigMap", | ||
APIVersion: "v1", | ||
Namespace: "knative-eventing", | ||
Name: "mt-test", | ||
}, | ||
Delivery: nil, | ||
} | ||
brokerConfigSpec1 := &BrokerConfigSpec{ | ||
Spec: brokerSpec1, | ||
} | ||
brokerSpec2 := &BrokerSpec{ | ||
Config: &duckv1.KReference{ | ||
Kind: "ConfigMap", | ||
APIVersion: "v1", | ||
Namespace: "knative-eventing", | ||
Name: "kafka-test", | ||
}, | ||
Delivery: nil, | ||
} | ||
brokerConfigSpec2 := &BrokerConfigSpec{ | ||
Spec: brokerSpec2, | ||
} | ||
brokerClasses["MTChannelBasedBroker"] = brokerConfigSpec1 | ||
brokerClasses["KafkaBroker"] = brokerConfigSpec2 | ||
|
||
configTests := []struct { | ||
name string | ||
wantErr bool | ||
|
@@ -415,6 +468,55 @@ func TestDefaultsConfiguration(t *testing.T) { | |
kind: ConfigMap | ||
name: someothernametoo | ||
namespace: someothernamespacetoo | ||
`, | ||
}, | ||
}, | ||
}, { | ||
name: "only clusterdefault specified values add brokerClasses", | ||
wantErr: false, | ||
wantDefaults: &Defaults{ | ||
ClusterDefault: &ClassAndBrokerConfig{ | ||
BrokerClass: "clusterbrokerclass", | ||
BrokerConfig: &BrokerConfig{ | ||
KReference: &duckv1.KReference{ | ||
Kind: "ConfigMap", | ||
APIVersion: "v1", | ||
Namespace: "knative-eventing", | ||
Name: "somename", | ||
}, | ||
Delivery: nil, | ||
}, | ||
BrokerClasses: brokerClasses, | ||
}, | ||
}, | ||
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 | ||
brokerClasses: | ||
MTChannelBasedBroker: | ||
spec: | ||
config: | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
name: mt-test | ||
namespace: knative-eventing | ||
KafkaBroker: | ||
spec: | ||
config: | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
name: kafka-test | ||
namespace: knative-eventing | ||
`, | ||
}, | ||
}, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I specify broker-class based default at the namespace level
value.BrokerClasses
I don't think this would work because we returning always the singlevalue.BrokerConfig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and that makes me think we need a test for this case as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, I think the broker-class based default at the namespace level we may not use it , but once we set the the single value.BrokerConfig we surely want to use it. And is it necessary to set broker-class based default at the namespace level ? Because we already set it at the cluster level, and in namespace we use the config of cluster is ok, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it's a valid use case and also for consistency with the cluster-level defaults