Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create v1beta1 brokers, use context for defaulting #2651

Merged
merged 6 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
"knative.dev/pkg/webhook/resourcesemantics/validation"

defaultconfig "knative.dev/eventing/pkg/apis/config"
configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1"
"knative.dev/eventing/pkg/apis/eventing"
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
Expand Down Expand Up @@ -98,11 +99,15 @@ var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
}

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

logger := logging.FromContext(ctx)

// Decorate contexts with the current state of the config.
ctxFunc := func(ctx context.Context) context.Context {
return ctx
return store.ToContext(ctx)
}

// Watch the default-ch-webhook ConfigMap and dynamically update the default
Expand Down Expand Up @@ -134,6 +139,15 @@ func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher
}

func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
// Decorate contexts with the current state of the config.
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
store.WatchConfigs(cmw)

// Decorate contexts with the current state of the config.
ctxFunc := func(ctx context.Context) context.Context {
return store.ToContext(ctx)
}
Comment on lines +147 to +149
Copy link
Member

@dprotaso dprotaso Feb 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your APIs defaulting is now dependent on the store/config in the context - you'll need to update the conversion webhook context decorator - it will invoke defaulting on the way out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is good to know!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return validation.NewAdmissionController(ctx,

// Name of the resource webhook.
Expand All @@ -146,10 +160,7 @@ func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher
ourTypes,

// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
// return v1.WithUpgradeViaDefaulting(store.ToContext(ctx))
return ctx
},
ctxFunc,

// Whether to disallow unknown fields.
true,
Expand Down Expand Up @@ -214,6 +225,14 @@ func NewLegacySinkBindingWebhook(ctx context.Context, cmw configmap.Watcher) *co
}

func NewConversionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
// Decorate contexts with the current state of the config.
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
store.WatchConfigs(cmw)
// Decorate contexts with the current state of the config.
ctxFunc := func(ctx context.Context) context.Context {
return store.ToContext(ctx)
}

var (
eventingv1alpha1_ = eventingv1alpha1.SchemeGroupVersion.Version
eventingv1beta1_ = eventingv1beta1.SchemeGroupVersion.Version
Expand Down Expand Up @@ -318,9 +337,7 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
},

// A function that infuses the context passed to ConvertTo/ConvertFrom/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
return ctx
},
ctxFunc,
)
}

Expand Down
1 change: 1 addition & 0 deletions config/400-config-br-default-channel.yaml
1 change: 1 addition & 0 deletions config/400-config-br-defaults.yaml
27 changes: 27 additions & 0 deletions config/core/configmaps/default-broker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
# Configuration for defaulting channels that do not specify CRD implementations.
default-br-config: |
clusterDefault:
apiVersion: v1
kind: ConfigMap
name: config-br-default-channel
namespace: knative-eventing
22 changes: 22 additions & 0 deletions config/core/configmaps/default-imc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-default-channel
namespace: knative-eventing
data:
channelTemplateSpec.apiVersion: messaging.knative.dev/v1alpha1
channelTemplateSpec.kind: InMemoryChannel
6 changes: 6 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \
"legacysources:v1alpha1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

# Deep copy config
${GOPATH}/bin/deepcopy-gen \
-O zz_generated.deepcopy \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \
-i knative.dev/eventing/pkg/apis/config \

# Only deepcopy the Duck types, as they are not real resources.
${CODEGEN_PKG}/generate-groups.sh "deepcopy" \
knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \
Expand Down
95 changes: 95 additions & 0 deletions pkg/apis/config/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
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"

"github.com/ghodss/yaml"

corev1 "k8s.io/api/core/v1"

duckv1 "knative.dev/pkg/apis/duck/v1"
)

const (
// DefaultConfigName is the name of config map for the default
// configs that brokers should use
DefaultsConfigName = "config-br-defaults"

// BrokerDefaultsKey is the name of the key that's used for finding
// defaults for broker configs.
BrokerDefaultsKey = "default-br-config"
)

// NewDefaultsConfigFromMap creates a Defaults from the supplied Map
func NewDefaultsConfigFromMap(data map[string]string) (*Defaults, error) {
nc := &Defaults{}

// Parse out the Broker Configuration Cluster default section
value, present := data[BrokerDefaultsKey]
if !present || value == "" {
return nil, fmt.Errorf("ConfigMap is missing (or empty) key: %q : %v", BrokerDefaultsKey, 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([]byte(j), &out)
}

// NewDefaultsConfigFromConfigMap creates a Defaults from the supplied configMap
func NewDefaultsConfigFromConfigMap(config *corev1.ConfigMap) (*Defaults, error) {
return NewDefaultsConfigFromMap(config.Data)
}

// 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]*duckv1.KReference `json:"namespaceDefaults,omitempty"`

// ClusterDefaultBrokerConfig is the default broker config for all the namespaces that
// are not in NamespaceDefaultBrokerConfigs.
ClusterDefault *duckv1.KReference `json:"clusterDefault,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) (*duckv1.KReference, error) {
if d == nil {
return nil, errors.New("Defaults are nil")
}
value, present := d.NamespaceDefaultsConfig[ns]
if present {
return value, nil
}
if d.ClusterDefault != nil {
return d.ClusterDefault, nil
}
return nil, errors.New("Defaults for Broker Configurations have not been set up.")
}
Loading