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