|
| 1 | +/* |
| 2 | +Copyright 2020 The Knative Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/ghodss/yaml" |
| 25 | + |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + |
| 28 | + duckv1 "knative.dev/pkg/apis/duck/v1" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + // DefaultConfigName is the name of config map for the default |
| 33 | + // configs that brokers should use |
| 34 | + DefaultsConfigName = "config-br-defaults" |
| 35 | + |
| 36 | + // BrokerDefaultsKey is the name of the key that's used for finding |
| 37 | + // defaults for broker configs. |
| 38 | + BrokerDefaultsKey = "default-br-config" |
| 39 | +) |
| 40 | + |
| 41 | +// NewDefaultsConfigFromMap creates a Defaults from the supplied Map |
| 42 | +func NewDefaultsConfigFromMap(data map[string]string) (*Defaults, error) { |
| 43 | + nc := &Defaults{} |
| 44 | + |
| 45 | + // Parse out the Broker Configuration Cluster default section |
| 46 | + value, present := data[BrokerDefaultsKey] |
| 47 | + if !present || value == "" { |
| 48 | + return nil, fmt.Errorf("ConfigMap is missing (or empty) key: %q : %v", BrokerDefaultsKey, data) |
| 49 | + } |
| 50 | + if err := parseEntry(value, nc); err != nil { |
| 51 | + return nil, fmt.Errorf("Failed to parse the entry: %s", err) |
| 52 | + } |
| 53 | + return nc, nil |
| 54 | +} |
| 55 | + |
| 56 | +func parseEntry(entry string, out interface{}) error { |
| 57 | + j, err := yaml.YAMLToJSON([]byte(entry)) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("ConfigMap's value could not be converted to JSON: %s : %v", err, entry) |
| 60 | + } |
| 61 | + return json.Unmarshal([]byte(j), &out) |
| 62 | +} |
| 63 | + |
| 64 | +// NewDefaultsConfigFromConfigMap creates a Defaults from the supplied configMap |
| 65 | +func NewDefaultsConfigFromConfigMap(config *corev1.ConfigMap) (*Defaults, error) { |
| 66 | + return NewDefaultsConfigFromMap(config.Data) |
| 67 | +} |
| 68 | + |
| 69 | +// Defaults includes the default values to be populated by the webhook. |
| 70 | +type Defaults struct { |
| 71 | + // NamespaceDefaultsConfig are the default Broker Configs for each namespace. |
| 72 | + // Namespace is the key, the value is the KReference to the config. |
| 73 | + NamespaceDefaultsConfig map[string]*duckv1.KReference `json:"namespaceDefaults,omitempty"` |
| 74 | + |
| 75 | + // ClusterDefaultBrokerConfig is the default broker config for all the namespaces that |
| 76 | + // are not in NamespaceDefaultBrokerConfigs. |
| 77 | + ClusterDefault *duckv1.KReference `json:"clusterDefault,omitempty"` |
| 78 | +} |
| 79 | + |
| 80 | +// GetBrokerConfig returns a namespace specific Broker Configuration, and if |
| 81 | +// that doesn't exist, return a Cluster Default and if that doesn't exist |
| 82 | +// return an error. |
| 83 | +func (d *Defaults) GetBrokerConfig(ns string) (*duckv1.KReference, error) { |
| 84 | + if d == nil { |
| 85 | + return nil, errors.New("Defaults are nil") |
| 86 | + } |
| 87 | + value, present := d.NamespaceDefaultsConfig[ns] |
| 88 | + if present { |
| 89 | + return value, nil |
| 90 | + } |
| 91 | + if d.ClusterDefault != nil { |
| 92 | + return d.ClusterDefault, nil |
| 93 | + } |
| 94 | + return nil, errors.New("Defaults for Broker Configurations have not been set up.") |
| 95 | +} |
0 commit comments