From d7ee0ca94cd5ccabe170ff5af9f21a8f239aeeb6 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Fri, 26 Jan 2024 02:01:57 -0500 Subject: [PATCH] First draft of the default broker class code Signed-off-by: Leo Li --- pkg/apis/config/defaults.go | 122 +++++++++++++++++++++++++++++------- 1 file changed, 98 insertions(+), 24 deletions(-) diff --git a/pkg/apis/config/defaults.go b/pkg/apis/config/defaults.go index f12b83ab534..9f68edf86f8 100644 --- a/pkg/apis/config/defaults.go +++ b/pkg/apis/config/defaults.go @@ -71,20 +71,33 @@ func NewDefaultsConfigFromConfigMap(config *corev1.ConfigMap) (*Defaults, error) // Defaults includes the default values to be populated by the webhook. type Defaults struct { // NamespaceDefaultsConfig are the default Broker Configs for each namespace. - // Namespace is the key, the value is the KReference to the config. - NamespaceDefaultsConfig map[string]*ClassAndBrokerConfig `json:"namespaceDefaults,omitempty"` + // Namespace is the key, the value is the NamespaceDefaultConfig + NamespaceDefaultsConfig *NamespaceDefaultsConfig `json:"namespaceDefaults,omitempty"` - // ClusterDefaultBrokerConfig is the default broker config for all the namespaces that - // are not in NamespaceDefaultBrokerConfigs. - ClusterDefault *ClassAndBrokerConfig `json:"clusterDefault,omitempty"` + // ClusterDefaultConfig is the default broker config for all the namespaces that + // are not in NamespaceDefaultBrokerConfigs. Different broker class could have + // different default config. + ClusterDefaultConfig *DefaultConfig `json:"clusterDefault,omitempty"` } -// ClassAndBrokerConfig contains configuration for a given namespace for broker. Allows -// configuring the Class of the Broker, the reference to the -// config it should use and it's delivery. -type ClassAndBrokerConfig struct { - BrokerClass string `json:"brokerClass,omitempty"` - *BrokerConfig `json:",inline"` +// DefaultConfig struct contains the default configuration for the cluster and namespace. +type DefaultConfig struct { + // DefaultBrokerClass and DefaultBrokerClassConfig are the default broker class for the whole cluster + // Users have to specify both of them + DefaultBrokerClass string `json:"brokerClass,omitempty"` + DefaultBrokerClassConfig *BrokerConfig `json:",inline"` + + // BrokerClasses are the default broker classes' config for the whole cluster + BrokerClasses map[string]*BrokerConfig `json:"brokerClasses,omitempty"` + + DisallowDifferentNamespaceConfig *bool `json:"disallowDifferentNamespaceConfig,omitempty"` +} + +// NamespaceDefaultsConfig struct contains the default configuration for the namespace. +type NamespaceDefaultsConfig struct { + + // BrokerClasses are the default broker classes' config for the namespace + NameSpaces map[string]*DefaultConfig `json:"brokerClasses,omitempty"` DisallowDifferentNamespaceConfig *bool `json:"disallowDifferentNamespaceConfig,omitempty"` } @@ -100,33 +113,94 @@ type BrokerConfig struct { // GetBrokerConfig returns a namespace specific Broker Configuration, and if // that doesn't exist, return a Cluster Default and if that doesn't exist // return an error. -func (d *Defaults) GetBrokerConfig(ns string) (*BrokerConfig, error) { +func (d *Defaults) GetBrokerConfig(ns string, brokerClassName 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 + + value, present := d.NamespaceDefaultsConfig.NameSpaces[ns] + if present && value != nil { + // We have the namespace specific config + // Now check whether the brokerClassName is the default broker class for this namespace + if value.DefaultBrokerClass == brokerClassName { + // return the default broker class config for this namespace + return value.DefaultBrokerClassConfig, nil + } else { + // If the brokerClassName is not the default broker class for this namespace, check if we have the config for this brokerclass + if value.BrokerClasses != nil { + brokerConfig, present := value.BrokerClasses[brokerClassName] + if present && brokerConfig != nil { + // We have the config specifically for this broker class in this namespace, just return the config + return brokerConfig, nil + } else { + // We don't have the config specifically for this broker class in this namespace, check the default broker class config for this namespace + if value.DefaultBrokerClassConfig != nil { + // return the default broker class config for this namespace + return value.DefaultBrokerClassConfig, nil + } 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 { + return d.ClusterDefaultConfig.DefaultBrokerClassConfig, nil + } else { + return nil, errors.New("Defaults for Broker Configurations have not been set up.") + } + } + } + + } else { + // No brokerClasses config for this namespace, check the default broker class config for this namespace + if value.DefaultBrokerClassConfig != nil { + // return the default broker class config for this namespace + return value.DefaultBrokerClassConfig, nil + } 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 { + return d.ClusterDefaultConfig.DefaultBrokerClassConfig, nil + } else { + return nil, errors.New("Defaults for Broker Configurations have not been set up. Both default broker class config for this namespace and cluster default config are nil.") + } + } + } + } } - if d.ClusterDefault != nil && d.ClusterDefault.BrokerConfig != nil { - return d.ClusterDefault.BrokerConfig, nil + 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.") + } // GetBrokerClass returns a namespace specific Broker Class, and if // that doesn't exist, return a Cluster Default and if that doesn't exist // return an error. func (d *Defaults) GetBrokerClass(ns string) (string, error) { + if d == nil { return "", errors.New("Defaults are nil") } - value, present := d.NamespaceDefaultsConfig[ns] - if present && value.BrokerClass != "" { - return value.BrokerClass, nil - } - if d.ClusterDefault != nil && d.ClusterDefault.BrokerClass != "" { - return d.ClusterDefault.BrokerClass, nil + + value, present := d.NamespaceDefaultsConfig.NameSpaces[ns] + if present && value != nil { + // We have the namespace specific config + // Now check whether the brokerClassName is the default broker class for this namespace + if value.DefaultBrokerClass != "" { + // return the default broker class for this namespace + return value.DefaultBrokerClass, nil + } else { + // If the brokerClassName is not set for this namespace, we will check for the cluster default config + if d.ClusterDefaultConfig != nil && d.ClusterDefaultConfig.DefaultBrokerClass != "" { + return d.ClusterDefaultConfig.DefaultBrokerClass, nil + } else { + return "", errors.New("Defaults for Broker Configurations have not been set up.") + } + } + } else { + // We don't have the namespace specific config, check for the cluster default config + if d.ClusterDefaultConfig != nil && d.ClusterDefaultConfig.DefaultBrokerClass != "" { + return d.ClusterDefaultConfig.DefaultBrokerClass, nil + } else { + return "", errors.New("Defaults for Broker Configurations have not been set up.") + } } - return "", errors.New("Defaults for Broker Configurations have not been set up.") }