Skip to content
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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 60 additions & 2 deletions pkg/apis/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ type Defaults struct {
type ClassAndBrokerConfig struct {
BrokerClass string `json:"brokerClass,omitempty"`
*BrokerConfig `json:",inline"`
// BrokerClassSpec set the multiple configurations of different brokerClass.
// Different brokerClass use corresponding brokerConfig for all the namespaces that
// are not in NamespaceDefaultBrokerConfigs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we leaving broker-class based defaults for namespace defaults out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no, we are not leaving, the annotation description is wrong

BrokerClassSpec map[string]*BrokerConfigSpec `json:"brokerClassSpec,omitempty"`
Copy link
Member

@pierDipi pierDipi Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call it brokerClasses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call it brokerClasses?

Sure

}

// 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
Expand All @@ -102,12 +119,30 @@ func (d *Defaults) GetBrokerConfig(ns string) (*BrokerConfig, error) {
if d == nil {
return nil, errors.New("Defaults are nil")
}

value, present := d.NamespaceDefaultsConfig[ns]
if present && value.BrokerConfig != nil {
return value.BrokerConfig, nil
}
if d.ClusterDefault != nil && d.ClusterDefault.BrokerConfig != nil {
return d.ClusterDefault.BrokerConfig, nil
if present && value.BrokerClass != "" {
bCSpec := getBrokerClassSpec(d)
bConfig := matchBrokerSpec(value.BrokerClass, bCSpec)
if bConfig != nil {
return bConfig, nil
}
}

if d.ClusterDefault != nil {
if d.ClusterDefault.BrokerClass != "" {
bCSpec := getBrokerClassSpec(d)
bConfig := matchBrokerSpec(d.ClusterDefault.BrokerClass, bCSpec)
if bConfig != nil {
return bConfig, nil
}
}
if d.ClusterDefault.BrokerConfig != nil {
return d.ClusterDefault.BrokerConfig, nil
}
}
return nil, errors.New("Defaults for Broker Configurations have not been set up.")
}
Expand All @@ -128,3 +163,26 @@ func (d *Defaults) GetBrokerClass(ns string) (string, error) {
}
return "", errors.New("Defaults for Broker Configurations have not been set up.")
}

// getBrokerClassSpec get the configurations of multiple brokerClass
func getBrokerClassSpec(d *Defaults) map[string]*BrokerConfigSpec {
if d.ClusterDefault != nil && d.ClusterDefault.BrokerClassSpec != nil {
return d.ClusterDefault.BrokerClassSpec
}
return nil
}

// matchBrokerSpec find the corresponding brokerConfig for a given brokerClass
func matchBrokerSpec(brokerClass string, brokerClassSpec map[string]*BrokerConfigSpec) *BrokerConfig {
if brokerClassSpec != nil {
for bClass, bCSpec := range brokerClassSpec {
if bClass == brokerClass {
var bConfig BrokerConfig
bConfig.KReference = bCSpec.Spec.Config
bConfig.Delivery = bCSpec.Spec.Delivery
return &bConfig
}
}
}
return nil
}
106 changes: 104 additions & 2 deletions pkg/apis/config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -117,6 +142,34 @@ func TestGetBrokerClass(t *testing.T) {
}

func TestDefaultsConfiguration(t *testing.T) {
brokerClassSpec := make(map[string]*BrokerConfigSpec)
brokerSpec1 := &BrokerSpec{
Copy link
Member

Choose a reason for hiding this comment

The 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,
}
brokerClassSpec["MTChannelBasedBroker"] = brokerConfigSpec1
brokerClassSpec["KafkaBroker"] = brokerConfigSpec2

configTests := []struct {
name string
wantErr bool
Expand Down Expand Up @@ -415,6 +468,55 @@ func TestDefaultsConfiguration(t *testing.T) {
kind: ConfigMap
name: someothernametoo
namespace: someothernamespacetoo
`,
},
},
}, {
name: "only clusterdefault specified values add brokerClassSpec",
wantErr: false,
wantDefaults: &Defaults{
ClusterDefault: &ClassAndBrokerConfig{
BrokerClass: "clusterbrokerclass",
BrokerConfig: &BrokerConfig{
KReference: &duckv1.KReference{
Kind: "ConfigMap",
APIVersion: "v1",
Namespace: "knative-eventing",
Name: "somename",
},
Delivery: nil,
},
BrokerClassSpec: brokerClassSpec,
},
},
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
brokerClassSpec:
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
`,
},
},
Expand Down
41 changes: 40 additions & 1 deletion pkg/apis/config/testdata/config-br-defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,45 @@ data:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: handle-error
name: cluster-handle-error
namespace: knative-eventing
backoffPolicy: exponential
backoffDelay: 3s
brokerClassSpec:
MTChannelBasedBroker:
spec:
delivery:
retry: 3
deadLetterSink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: mt-handle-error
namespace: knative-eventing
backoffPolicy: exponential
backoffDelay: 3s
config:
apiVersion: v1
kind: ConfigMap
name: mt-test
namespace: knative-eventing
KafkaBroker:
spec:
delivery:
retry: 3
deadLetterSink:
ref:
apiVersion: serving.knative.
kind: Service
name: kafka-handle-error
namespace: knative-eventing
backoffPolicy: exponential
backoffDelay: 3s
config:
apiVersion: v1
kind: ConfigMap
name: kafka-test
namespace: knative-eventing

namespaceDefaults:
some-namespace:
Expand All @@ -62,3 +97,7 @@ data:
namespace: someothernamespace
backoffPolicy: linear
backoffDelay: 5s
some-namespace-two:
brokerClass: KafkaBroker
some-namespace-three:
brokerClass: KafkaBroker