Skip to content

Commit

Permalink
upgrade to latest dependencies
Browse files Browse the repository at this point in the history
bumping knative.dev/eventing 2b92299...36e0721:
  > 36e0721 Broker class based defaults (# 7631)
  > e4b6d68 [main] Upgrade to latest dependencies (# 8172)
  > 24a43e8 chore: refactor cross namespace test to make it reusable (# 8157)
  > da10b71 Support dispatcher format for triggers (# 8151)
  > 477588c JobSink: Reject unauthorized requests (# 8169)
  > 5fc4c0f Fix JobSink to have ObservedGeneration in status (# 8168)
  > ed71838 Fix: Use correct readyness check in AuthZ conformance tests (# 8167)
  > bf945f9 Support authorization in Channel ingress (# 8162)
  > b57ac3a Fix: propagate EventPolicy filter to underlying Channels EventPolicy (# 8163)

Signed-off-by: Knative Automation <[email protected]>
  • Loading branch information
knative-automation committed Aug 23, 2024
1 parent 00f77a8 commit b475175
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 61 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
k8s.io/api v0.30.3
k8s.io/apimachinery v0.30.3
k8s.io/client-go v0.30.3
knative.dev/eventing v0.42.1-0.20240816055941-2b922992c8f4
knative.dev/eventing v0.42.1-0.20240822171311-36e0721b3852
knative.dev/hack v0.0.0-20240814130635-06f7aff93954
knative.dev/pkg v0.0.0-20240815051656-89743d9bbf7c
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,8 @@ k8s.io/kube-openapi v0.0.0-20240808142205-8e686545bdb8 h1:1Wof1cGQgA5pqgo8MxKPtf
k8s.io/kube-openapi v0.0.0-20240808142205-8e686545bdb8/go.mod h1:Os6V6dZwLNii3vxFpxcNaTmH8LJJBkOTg1N0tOA0fvA=
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A=
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
knative.dev/eventing v0.42.1-0.20240816055941-2b922992c8f4 h1:phPrPjJo+shjObPBF0Qzxd9kfghUA00UFQp/72ONvBE=
knative.dev/eventing v0.42.1-0.20240816055941-2b922992c8f4/go.mod h1:eTJLSCpHchscm2VV/e10w3HcGIB7dOYdGAzeBIRmJ08=
knative.dev/eventing v0.42.1-0.20240822171311-36e0721b3852 h1:Ulgi6VO5b0/VAvSNvHIgls0GBiXlb9+S51KbtOzO4LU=
knative.dev/eventing v0.42.1-0.20240822171311-36e0721b3852/go.mod h1:Clx8z37Nwg321H9+vGNxp5C6bVdo4l4XM5g6T5CgZVI=
knative.dev/hack v0.0.0-20240814130635-06f7aff93954 h1:dGMK5VoL75szvrYQTL9NqhPYHu1f5dGaXx1hJI8fAFM=
knative.dev/hack v0.0.0-20240814130635-06f7aff93954/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY=
knative.dev/pkg v0.0.0-20240815051656-89743d9bbf7c h1:2crXVk4FG0dSG6WHaIT+WKbUzn7qG2wn0AfYmvA22zs=
Expand Down
199 changes: 169 additions & 30 deletions vendor/knative.dev/eventing/pkg/apis/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,65 +68,204 @@ func NewDefaultsConfigFromConfigMap(config *corev1.ConfigMap) (*Defaults, error)
return NewDefaultsConfigFromMap(config.Data)
}

/*
The priority precedence for determining the broker class and configuration is as follows:
1. If a specific broker class is provided:
a. Check namespace-specific configuration for the given broker class
b. If not found, check cluster-wide configuration for the given broker class
c. If still not found, use the cluster-wide default configuration
2. If no specific broker class is provided:
a. Check namespace-specific default broker class
b. If found, use its configuration (following step 1)
c. If not found, use cluster-wide default broker class and configuration
3. If no default cluster configuration is set, return an error
4. If no default namespace configuration is set, will use the cluster-wide default configuration.
This can be represented as a flow chart:
Start
|
v
Is broker class provided?
/ \
Yes No
/ \
Check namespace config Check namespace
for provided class default class
| |
v v
Found? Found?
/ \ / \
Yes No Yes No
| | | |
| | | |
| v | v
| Check cluster | Use cluster
| config for class | default class
| | | and config
| v |
| Found? |
| / \ |
| Yes No |
| | | |
| | v |
| | Use cluster |
| | default config |
v v v
Use found configuration
The system prioritizes namespace-specific configurations over cluster-wide defaults,
and explicitly provided broker classes over default classes.
*/

// 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 map[string]*DefaultConfig `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"`
// 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/namespace.
// Users have to specify both of them
DefaultBrokerClass string `json:"brokerClass,omitempty"`

//Deprecated: Use the config in BrokerClasses instead
*BrokerConfig `json:",inline"`

// Optional: BrokerClasses are the default broker classes' config. The key is the broker class name, and the value is the config for that broker class.
BrokerClasses map[string]*BrokerConfig `json:"brokerClasses,omitempty"`

DisallowDifferentNamespaceConfig *bool `json:"disallowDifferentNamespaceConfig,omitempty"`
}

// BrokerConfig contains configuration for a given namespace for broker. Allows
// BrokerConfig contains configuration for a given broker. Allows
// configuring the reference to the
// config it should use and it's delivery.
type BrokerConfig struct {
*duckv1.KReference `json:",inline"`
Delivery *eventingduckv1.DeliverySpec `json:"delivery,omitempty"`
}

// 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) {
// GetBrokerConfig returns a namespace specific Broker Config, 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, brokerClassName *string) (*BrokerConfig, error) {
if d == nil {
return nil, errors.New("Defaults are nil")
return nil, errors.New("Defaults for Broker Configurations for cluster have not been set up. You can set them via ConfigMap config-br-defaults.")
}

// Early return if brokerClassName is provided and valid
if brokerClassName != nil && *brokerClassName != "" {
return d.getBrokerConfigByClassName(ns, *brokerClassName)
}

// Handling empty brokerClassName
return d.getBrokerConfigForEmptyClassName(ns)
}

// getBrokerConfigByClassName returns the BrokerConfig for the given brokerClassName.
// It first checks the namespace specific configuration, then the cluster default configuration.
func (d *Defaults) getBrokerConfigByClassName(ns string, brokerClassName string) (*BrokerConfig, error) {
// Check namespace specific configuration
if nsConfig, ok := d.NamespaceDefaultsConfig[ns]; ok && nsConfig != nil {
// check if the brokerClassName is the default broker class for this namespace
if nsConfig.DefaultBrokerClass == brokerClassName {
if nsConfig.BrokerConfig == nil {
// as no default broker class config is set for this namespace, check whether nsconfig's brokerClasses map has the config for this broker class
if config, ok := nsConfig.BrokerClasses[brokerClassName]; ok && config != nil {
return config, nil
}
// if not found, return the cluster default config
return d.getClusterDefaultBrokerConfig(brokerClassName)
} else {
// If the brokerClassName exists in the BrokerClasses, return the config in the BrokerClasses
if config, ok := nsConfig.BrokerClasses[brokerClassName]; ok && config != nil {
return config, nil
} else {
// If the brokerClassName is the default broker class for the namespace, return the BrokerConfig
return nsConfig.BrokerConfig, nil
}
}
} else {
// if the brokerClassName is not the default broker class for the namespace, check whether nsconfig's brokerClasses map has the config for this broker class
if config, ok := nsConfig.BrokerClasses[brokerClassName]; ok && config != nil {
return config, nil
}
}
}
value, present := d.NamespaceDefaultsConfig[ns]
if present && value.BrokerConfig != nil {
return value.BrokerConfig, nil

// Check cluster default configuration
return d.getClusterDefaultBrokerConfig(brokerClassName)
}

// getBrokerConfigForEmptyClassName returns the BrokerConfig for the given namespace when brokerClassName is empty.
// It first checks the namespace specific configuration, then the cluster default configuration.
func (d *Defaults) getBrokerConfigForEmptyClassName(ns string) (*BrokerConfig, error) {
// Check if namespace has a default broker class
if nsConfig, ok := d.NamespaceDefaultsConfig[ns]; ok && nsConfig != nil {
if nsConfig.DefaultBrokerClass != "" {
return d.getBrokerConfigByClassName(ns, nsConfig.DefaultBrokerClass)
}
}
if d.ClusterDefault != nil && d.ClusterDefault.BrokerConfig != nil {
return d.ClusterDefault.BrokerConfig, nil

// Fallback to cluster default configuration
return d.getClusterDefaultBrokerConfig("")
}

// getClusterDefaultBrokerConfig returns the BrokerConfig for the given brokerClassName.
func (d *Defaults) getClusterDefaultBrokerConfig(brokerClassName string) (*BrokerConfig, error) {
if d.ClusterDefaultConfig == nil || d.ClusterDefaultConfig.BrokerConfig == nil {
return nil, errors.New("Defaults for Broker Configurations for cluster have not been set up. You can set them via ConfigMap config-br-defaults.")
}
return nil, errors.New("Defaults for Broker Configurations have not been set up.")

// Check if the brokerClassName is the default broker class for the whole cluster
if brokerClassName == "" || d.ClusterDefaultConfig.DefaultBrokerClass == brokerClassName {
// If the brokerClassName exists in the BrokerClasses, return the config in the BrokerClasses
if config, ok := d.ClusterDefaultConfig.BrokerClasses[brokerClassName]; ok && config != nil {
return config, nil
} else {
// If the brokerClassName is the default broker class for the cluster, return the BrokerConfig
return d.ClusterDefaultConfig.BrokerConfig, nil
}
}

if config, ok := d.ClusterDefaultConfig.BrokerClasses[brokerClassName]; ok && config != nil {
return config, nil
}

return d.ClusterDefaultConfig.BrokerConfig, nil
}

// GetBrokerClass returns a namespace specific Broker Class, and if
// that doesn't exist, return a Cluster Default and if that doesn't exist
// that doesn't exist, return a Cluster Default and if the defaults doesn't exist
// return an error.
func (d *Defaults) GetBrokerClass(ns string) (string, error) {
if d == nil {
return "", errors.New("Defaults are nil")
return "", errors.New("Defaults for Broker Configurations for cluster have not been set up. You can set them via ConfigMap config-br-defaults.")
}
value, present := d.NamespaceDefaultsConfig[ns]
if present && value.BrokerClass != "" {
return value.BrokerClass, nil

// Check if the namespace has a specific configuration
if nsConfig, ok := d.NamespaceDefaultsConfig[ns]; ok && nsConfig != nil {
if nsConfig.DefaultBrokerClass != "" {
return nsConfig.DefaultBrokerClass, nil
}
}
if d.ClusterDefault != nil && d.ClusterDefault.BrokerClass != "" {
return d.ClusterDefault.BrokerClass, nil

// Fallback to cluster default configuration if namespace specific configuration is not set
if d.ClusterDefaultConfig != nil && d.ClusterDefaultConfig.DefaultBrokerClass != "" {
return d.ClusterDefaultConfig.DefaultBrokerClass, nil
}
return "", errors.New("Defaults for Broker Configurations have not been set up.")

// If neither namespace specific nor cluster default broker class is found
return "", fmt.Errorf("Neither namespace specific nor cluster default broker class is found for namespace %q, please set them via ConfigMap config-br-defaults", ns)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ import (
func (b *Broker) SetDefaults(ctx context.Context) {
// Default Spec fields.
withNS := apis.WithinParent(ctx, b.ObjectMeta)
b.Spec.SetDefaults(withNS)
b.Spec.SetDefaults(withNS, b.Annotations["eventing.knative.dev/broker.class"])
eventing.DefaultBrokerClassIfUnset(withNS, &b.ObjectMeta)
}

func (bs *BrokerSpec) SetDefaults(ctx context.Context) {
func (bs *BrokerSpec) SetDefaults(ctx context.Context, brokerClass string) {
cfg := config.FromContextOrDefaults(ctx)
c, err := cfg.Defaults.GetBrokerConfig(apis.ParentMeta(ctx).Namespace)
c, err := cfg.Defaults.GetBrokerConfig(apis.ParentMeta(ctx).Namespace, &brokerClass)

if bs.Config != nil {
c, err = cfg.Defaults.GetBrokerConfig(apis.ParentMeta(ctx).Namespace, &bs.Config.Kind)
}

if err == nil {
if bs.Config == nil {
bs.Config = c.KReference
Expand All @@ -49,10 +54,14 @@ func (bs *BrokerSpec) SetDefaults(ctx context.Context) {
BackoffDelay: c.Delivery.BackoffDelay,
}
}

}

// Default the namespace if not given
if bs.Config != nil {
bs.Config.SetDefaults(ctx)
}

bs.Delivery.SetDefaults(ctx)

}
Loading

0 comments on commit b475175

Please sign in to comment.