Skip to content

Commit

Permalink
First draft of the default broker class code
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Li <leoli@redhat.com>
  • Loading branch information
Leo6Leo committed Jan 26, 2024
1 parent bcba98a commit d7ee0ca
Showing 1 changed file with 98 additions and 24 deletions.
122 changes: 98 additions & 24 deletions pkg/apis/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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.")
}

0 comments on commit d7ee0ca

Please sign in to comment.