Skip to content

Commit 5fd326d

Browse files
authored
create v1beta1 brokers, use context for defaulting (knative#2651)
* create v1beta1 brokers, use context for defaulting * add debug stuff * OMG... really??? * add default imc channel configmap for backwards compatibility * cleanup + unit tests * fix year
1 parent de8d802 commit 5fd326d

17 files changed

Lines changed: 693 additions & 17 deletions

cmd/webhook/main.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
4343
"knative.dev/pkg/webhook/resourcesemantics/validation"
4444

45+
defaultconfig "knative.dev/eventing/pkg/apis/config"
4546
configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1"
4647
"knative.dev/eventing/pkg/apis/eventing"
4748
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
@@ -109,11 +110,15 @@ var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
109110
}
110111

111112
func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
113+
// Decorate contexts with the current state of the config.
114+
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
115+
store.WatchConfigs(cmw)
116+
112117
logger := logging.FromContext(ctx)
113118

114119
// Decorate contexts with the current state of the config.
115120
ctxFunc := func(ctx context.Context) context.Context {
116-
return ctx
121+
return store.ToContext(ctx)
117122
}
118123

119124
// Watch the default-ch-webhook ConfigMap and dynamically update the default
@@ -145,6 +150,15 @@ func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher
145150
}
146151

147152
func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
153+
// Decorate contexts with the current state of the config.
154+
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
155+
store.WatchConfigs(cmw)
156+
157+
// Decorate contexts with the current state of the config.
158+
ctxFunc := func(ctx context.Context) context.Context {
159+
return store.ToContext(ctx)
160+
}
161+
148162
return validation.NewAdmissionController(ctx,
149163

150164
// Name of the resource webhook.
@@ -157,10 +171,7 @@ func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher
157171
ourTypes,
158172

159173
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
160-
func(ctx context.Context) context.Context {
161-
// return v1.WithUpgradeViaDefaulting(store.ToContext(ctx))
162-
return ctx
163-
},
174+
ctxFunc,
164175

165176
// Whether to disallow unknown fields.
166177
true,
@@ -231,6 +242,14 @@ func NewLegacySinkBindingWebhook(opts ...psbinding.ReconcilerOption) injection.C
231242
}
232243

233244
func NewConversionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
245+
// Decorate contexts with the current state of the config.
246+
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
247+
store.WatchConfigs(cmw)
248+
// Decorate contexts with the current state of the config.
249+
ctxFunc := func(ctx context.Context) context.Context {
250+
return store.ToContext(ctx)
251+
}
252+
234253
var (
235254
eventingv1alpha1_ = eventingv1alpha1.SchemeGroupVersion.Version
236255
eventingv1beta1_ = eventingv1beta1.SchemeGroupVersion.Version
@@ -328,9 +347,7 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
328347
},
329348

330349
// A function that infuses the context passed to ConvertTo/ConvertFrom/SetDefaults with custom metadata.
331-
func(ctx context.Context) context.Context {
332-
return ctx
333-
},
350+
ctxFunc,
334351
)
335352
}
336353

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
core/configmaps/default-imc.yaml

config/400-config-br-defaults.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
core/configmaps/default-broker.yaml
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2020 The Knative Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: v1
16+
kind: ConfigMap
17+
metadata:
18+
name: config-br-defaults
19+
namespace: knative-eventing
20+
data:
21+
# Configuration for defaulting channels that do not specify CRD implementations.
22+
default-br-config: |
23+
clusterDefault:
24+
apiVersion: v1
25+
kind: ConfigMap
26+
name: config-br-default-channel
27+
namespace: knative-eventing
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2020 The Knative Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: v1
16+
kind: ConfigMap
17+
metadata:
18+
name: config-br-default-channel
19+
namespace: knative-eventing
20+
data:
21+
channelTemplateSpec.apiVersion: messaging.knative.dev/v1alpha1
22+
channelTemplateSpec.kind: InMemoryChannel

hack/update-codegen.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \
3939
"legacysources:v1alpha1" \
4040
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt
4141

42+
# Deep copy config
43+
${GOPATH}/bin/deepcopy-gen \
44+
-O zz_generated.deepcopy \
45+
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \
46+
-i knative.dev/eventing/pkg/apis/config \
47+
4248
# Only deepcopy the Duck types, as they are not real resources.
4349
${CODEGEN_PKG}/generate-groups.sh "deepcopy" \
4450
knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \

pkg/apis/config/defaults.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)