diff --git a/cmd/broker/config.go b/cmd/broker/config.go deleted file mode 100644 index 89d5454e1b2..00000000000 --- a/cmd/broker/config.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 broker - -import ( - "context" - - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - kubeclient "knative.dev/pkg/client/injection/kube/client" - "knative.dev/pkg/logging" -) - -// GetLoggingConfig will get config from a specific namespace -func GetLoggingConfig(ctx context.Context, namespace, loggingConfigMapName string) (*logging.Config, error) { - loggingConfigMap, err := kubeclient.Get(ctx).CoreV1().ConfigMaps(namespace).Get(loggingConfigMapName, metav1.GetOptions{}) - if apierrors.IsNotFound(err) { - return logging.NewConfigFromMap(nil) - } else if err != nil { - return nil, err - } - return logging.NewConfigFromConfigMap(loggingConfigMap) -} diff --git a/cmd/broker/filter/kodata/HEAD b/cmd/broker/filter/kodata/HEAD deleted file mode 120000 index 481bd4eff49..00000000000 --- a/cmd/broker/filter/kodata/HEAD +++ /dev/null @@ -1 +0,0 @@ -../../../../.git/HEAD \ No newline at end of file diff --git a/cmd/broker/filter/kodata/LICENSE b/cmd/broker/filter/kodata/LICENSE deleted file mode 120000 index 14776154326..00000000000 --- a/cmd/broker/filter/kodata/LICENSE +++ /dev/null @@ -1 +0,0 @@ -../../../../LICENSE \ No newline at end of file diff --git a/cmd/broker/filter/kodata/VENDOR-LICENSE b/cmd/broker/filter/kodata/VENDOR-LICENSE deleted file mode 120000 index 7322c09d957..00000000000 --- a/cmd/broker/filter/kodata/VENDOR-LICENSE +++ /dev/null @@ -1 +0,0 @@ -../../../../third_party/VENDOR-LICENSE \ No newline at end of file diff --git a/cmd/broker/filter/kodata/refs b/cmd/broker/filter/kodata/refs deleted file mode 120000 index fe164fe40f7..00000000000 --- a/cmd/broker/filter/kodata/refs +++ /dev/null @@ -1 +0,0 @@ -../../../../.git/refs \ No newline at end of file diff --git a/cmd/broker/filter/main.go b/cmd/broker/filter/main.go deleted file mode 100644 index b28cc2629db..00000000000 --- a/cmd/broker/filter/main.go +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 main - -import ( - "flag" - "log" - "time" - - "github.com/google/uuid" - "github.com/kelseyhightower/envconfig" - "go.opencensus.io/stats/view" - "go.uber.org/zap" - - "knative.dev/eventing/cmd/broker" - "knative.dev/eventing/pkg/broker/filter" - cmpresources "knative.dev/eventing/pkg/reconciler/configmappropagation/resources" - namespaceresources "knative.dev/eventing/pkg/reconciler/namespace/resources" - "knative.dev/eventing/pkg/tracing" - kubeclient "knative.dev/pkg/client/injection/kube/client" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - "knative.dev/pkg/injection" - "knative.dev/pkg/kmeta" - "knative.dev/pkg/logging" - "knative.dev/pkg/metrics" - "knative.dev/pkg/signals" - tracingconfig "knative.dev/pkg/tracing/config" - - "knative.dev/pkg/injection/sharedmain" - - eventingv1alpha1 "knative.dev/eventing/pkg/client/clientset/versioned" - eventinginformers "knative.dev/eventing/pkg/client/informers/externalversions" -) - -var ( - masterURL = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") - kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") -) - -const ( - defaultMetricsPort = 9092 - component = "broker_filter" -) - -type envConfig struct { - Broker string `envconfig:"BROKER" required:"true"` - Namespace string `envconfig:"NAMESPACE" required:"true"` - // TODO: change this environment variable to something like "PodGroupName". - PodName string `envconfig:"POD_NAME" required:"true"` - ContainerName string `envconfig:"CONTAINER_NAME" required:"true"` -} - -func main() { - flag.Parse() - - ctx := signals.NewContext() - - // Report stats on Go memory usage every 30 seconds. - msp := metrics.NewMemStatsAll() - msp.Start(ctx, 30*time.Second) - if err := view.Register(msp.DefaultViews()...); err != nil { - log.Fatalf("Error exporting go memstats view: %v", err) - } - - cfg, err := sharedmain.GetConfig(*masterURL, *kubeconfig) - if err != nil { - log.Fatal("Error building kubeconfig", err) - } - - var env envConfig - if err := envconfig.Process("", &env); err != nil { - log.Fatal("Failed to process env var", zap.Error(err)) - } - - ctx, _ = injection.Default.SetupInformers(ctx, cfg) - kubeClient := kubeclient.Get(ctx) - - loggingConfigMapName := cmpresources.MakeCopyConfigMapName(namespaceresources.DefaultConfigMapPropagationName, logging.ConfigMapName()) - metricsConfigMapName := cmpresources.MakeCopyConfigMapName(namespaceresources.DefaultConfigMapPropagationName, metrics.ConfigMapName()) - - loggingConfig, err := broker.GetLoggingConfig(ctx, env.Namespace, loggingConfigMapName) - if err != nil { - log.Fatal("Error loading/parsing logging configuration:", err) - } - sl, atomicLevel := logging.NewLoggerFromConfig(loggingConfig, component) - logger := sl.Desugar() - defer flush(sl) - - logger.Info("Starting the Broker Filter") - - eventingClient := eventingv1alpha1.NewForConfigOrDie(cfg) - eventingFactory := eventinginformers.NewSharedInformerFactoryWithOptions(eventingClient, - controller.GetResyncPeriod(ctx), - eventinginformers.WithNamespace(env.Namespace)) - triggerInformer := eventingFactory.Eventing().V1alpha1().Triggers() - - // Watch the logging config map and dynamically update logging levels. - configMapWatcher := configmap.NewInformedWatcher(kubeClient, env.Namespace) - // Watch the observability config map and dynamically update metrics exporter. - updateFunc, err := metrics.UpdateExporterFromConfigMapWithOpts(metrics.ExporterOptions{ - Component: component, - PrometheusPort: defaultMetricsPort, - }, sl) - if err != nil { - logger.Fatal("Failed to create metrics exporter update function", zap.Error(err)) - } - configMapWatcher.Watch(metricsConfigMapName, updateFunc) - // TODO change the component name to broker once Stackdriver metrics are approved. - // Watch the observability config map and dynamically update request logs. - configMapWatcher.Watch(loggingConfigMapName, logging.UpdateLevelFromConfigMap(sl, atomicLevel, component)) - - bin := tracing.BrokerFilterName(tracing.BrokerFilterNameArgs{ - Namespace: env.Namespace, - BrokerName: env.Broker, - }) - if err = tracing.SetupDynamicPublishing(sl, configMapWatcher, bin, - cmpresources.MakeCopyConfigMapName(namespaceresources.DefaultConfigMapPropagationName, tracingconfig.ConfigName)); err != nil { - logger.Fatal("Error setting up trace publishing", zap.Error(err)) - } - - reporter := filter.NewStatsReporter(env.ContainerName, kmeta.ChildName(env.PodName, uuid.New().String())) - - // We are running both the receiver (takes messages in from the Broker) and the dispatcher (send - // the messages to the triggers' subscribers) in this binary. - handler, err := filter.NewHandler(logger, triggerInformer.Lister().Triggers(env.Namespace), reporter) - if err != nil { - logger.Fatal("Error creating Handler", zap.Error(err)) - } - - // configMapWatcher does not block, so start it first. - if err = configMapWatcher.Start(ctx.Done()); err != nil { - logger.Warn("Failed to start ConfigMap watcher", zap.Error(err)) - } - - // Start all of the informers and wait for them to sync. - logger.Info("Starting informer.") - - go eventingFactory.Start(ctx.Done()) - eventingFactory.WaitForCacheSync(ctx.Done()) - - // Start blocks forever. - logger.Info("Filter starting...") - - err = handler.Start(ctx) - if err != nil { - logger.Fatal("handler.Start() returned an error", zap.Error(err)) - } - logger.Info("Exiting...") -} - -func flush(logger *zap.SugaredLogger) { - _ = logger.Sync() - metrics.FlushExporter() -} diff --git a/cmd/broker/ingress/kodata/HEAD b/cmd/broker/ingress/kodata/HEAD deleted file mode 120000 index 481bd4eff49..00000000000 --- a/cmd/broker/ingress/kodata/HEAD +++ /dev/null @@ -1 +0,0 @@ -../../../../.git/HEAD \ No newline at end of file diff --git a/cmd/broker/ingress/kodata/LICENSE b/cmd/broker/ingress/kodata/LICENSE deleted file mode 120000 index 14776154326..00000000000 --- a/cmd/broker/ingress/kodata/LICENSE +++ /dev/null @@ -1 +0,0 @@ -../../../../LICENSE \ No newline at end of file diff --git a/cmd/broker/ingress/kodata/VENDOR-LICENSE b/cmd/broker/ingress/kodata/VENDOR-LICENSE deleted file mode 120000 index 7322c09d957..00000000000 --- a/cmd/broker/ingress/kodata/VENDOR-LICENSE +++ /dev/null @@ -1 +0,0 @@ -../../../../third_party/VENDOR-LICENSE \ No newline at end of file diff --git a/cmd/broker/ingress/kodata/refs b/cmd/broker/ingress/kodata/refs deleted file mode 120000 index fe164fe40f7..00000000000 --- a/cmd/broker/ingress/kodata/refs +++ /dev/null @@ -1 +0,0 @@ -../../../../.git/refs \ No newline at end of file diff --git a/cmd/broker/ingress/main.go b/cmd/broker/ingress/main.go deleted file mode 100644 index 6e741057f3d..00000000000 --- a/cmd/broker/ingress/main.go +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 main - -import ( - "flag" - "log" - "net/http" - "net/url" - "time" - - // Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters). - // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" - - cloudevents "github.com/cloudevents/sdk-go" - "github.com/google/uuid" - "github.com/kelseyhightower/envconfig" - "go.opencensus.io/stats/view" - "go.uber.org/zap" - - kubeclient "knative.dev/pkg/client/injection/kube/client" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - "knative.dev/pkg/injection" - "knative.dev/pkg/injection/sharedmain" - "knative.dev/pkg/kmeta" - "knative.dev/pkg/logging" - "knative.dev/pkg/metrics" - "knative.dev/pkg/signals" - tracingconfig "knative.dev/pkg/tracing/config" - - cmdbroker "knative.dev/eventing/cmd/broker" - "knative.dev/eventing/pkg/broker" - "knative.dev/eventing/pkg/broker/ingress" - "knative.dev/eventing/pkg/kncloudevents" - cmpresources "knative.dev/eventing/pkg/reconciler/configmappropagation/resources" - namespaceresources "knative.dev/eventing/pkg/reconciler/namespace/resources" - "knative.dev/eventing/pkg/tracing" -) - -var ( - masterURL = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") - kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") -) - -// TODO make these constants configurable (either as env variables, config map, or part of broker spec). -// Issue: https://github.com/knative/eventing/issues/1777 -const ( - // Constants for the underlying HTTP Client transport. These would enable better connection reuse. - // Purposely set them to be equal, as the ingress only connects to its channel. - // These are magic numbers, partly set based on empirical evidence running performance workloads, and partly - // based on what serving is doing. See https://github.com/knative/serving/blob/master/pkg/network/transports.go. - defaultMaxIdleConnections = 1000 - defaultMaxIdleConnectionsPerHost = 1000 - defaultTTL int32 = 255 - defaultMetricsPort = 9092 - component = "broker_ingress" -) - -type envConfig struct { - Broker string `envconfig:"BROKER" required:"true"` - Channel string `envconfig:"CHANNEL" required:"true"` - Namespace string `envconfig:"NAMESPACE" required:"true"` - // TODO: change this environment variable to something like "PodGroupName". - PodName string `envconfig:"POD_NAME" required:"true"` - ContainerName string `envconfig:"CONTAINER_NAME" required:"true"` -} - -func main() { - flag.Parse() - - ctx := signals.NewContext() - - // Report stats on Go memory usage every 30 seconds. - msp := metrics.NewMemStatsAll() - msp.Start(ctx, 30*time.Second) - if err := view.Register(msp.DefaultViews()...); err != nil { - log.Fatalf("Error exporting go memstats view: %v", err) - } - - cfg, err := sharedmain.GetConfig(*masterURL, *kubeconfig) - if err != nil { - log.Fatal("Error building kubeconfig", err) - } - - var env envConfig - if err := envconfig.Process("", &env); err != nil { - log.Fatal("Failed to process env var", zap.Error(err)) - } - - log.Printf("Registering %d clients", len(injection.Default.GetClients())) - log.Printf("Registering %d informer factories", len(injection.Default.GetInformerFactories())) - log.Printf("Registering %d informers", len(injection.Default.GetInformers())) - - ctx, informers := injection.Default.SetupInformers(ctx, cfg) - - loggingConfigMapName := cmpresources.MakeCopyConfigMapName(namespaceresources.DefaultConfigMapPropagationName, logging.ConfigMapName()) - metricsConfigMapName := cmpresources.MakeCopyConfigMapName(namespaceresources.DefaultConfigMapPropagationName, metrics.ConfigMapName()) - - loggingConfig, err := cmdbroker.GetLoggingConfig(ctx, env.Namespace, loggingConfigMapName) - if err != nil { - log.Fatal("Error loading/parsing logging configuration:", err) - } - sl, atomicLevel := logging.NewLoggerFromConfig(loggingConfig, component) - logger := sl.Desugar() - defer flush(sl) - - logger.Info("Starting the Broker Ingress") - - channelURI := &url.URL{ - Scheme: "http", - Host: env.Channel, - Path: "/", - } - - // Watch the logging config map and dynamically update logging levels. - configMapWatcher := configmap.NewInformedWatcher(kubeclient.Get(ctx), env.Namespace) - // Watch the observability config map and dynamically update metrics exporter. - updateFunc, err := metrics.UpdateExporterFromConfigMapWithOpts(metrics.ExporterOptions{ - Component: component, - PrometheusPort: defaultMetricsPort, - }, sl) - if err != nil { - logger.Fatal("Failed to create metrics exporter update function", zap.Error(err)) - } - configMapWatcher.Watch(metricsConfigMapName, updateFunc) - // TODO change the component name to broker once Stackdriver metrics are approved. - // Watch the observability config map and dynamically update request logs. - configMapWatcher.Watch(loggingConfigMapName, logging.UpdateLevelFromConfigMap(sl, atomicLevel, component)) - - bin := tracing.BrokerIngressName(tracing.BrokerIngressNameArgs{ - Namespace: env.Namespace, - BrokerName: env.Broker, - }) - if err = tracing.SetupDynamicPublishing(sl, configMapWatcher, bin, - cmpresources.MakeCopyConfigMapName(namespaceresources.DefaultConfigMapPropagationName, tracingconfig.ConfigName)); err != nil { - logger.Fatal("Error setting up trace publishing", zap.Error(err)) - } - - connectionArgs := kncloudevents.ConnectionArgs{ - MaxIdleConns: defaultMaxIdleConnections, - MaxIdleConnsPerHost: defaultMaxIdleConnectionsPerHost, - } - httpTransport, err := cloudevents.NewHTTPTransport( - cloudevents.WithBinaryEncoding(), - cloudevents.WithHTTPTransport(connectionArgs.NewDefaultHTTPTransport()), - ) - if err != nil { - logger.Fatal("Unable to create CE transport", zap.Error(err)) - } - - // Liveness check. - httpTransport.Handler = http.NewServeMux() - httpTransport.Handler.HandleFunc("/healthz", func(writer http.ResponseWriter, _ *http.Request) { - writer.WriteHeader(http.StatusOK) - }) - - ceClient, err := kncloudevents.NewDefaultHTTPClient(httpTransport) - if err != nil { - logger.Fatal("Unable to create CE client", zap.Error(err)) - } - - reporter := ingress.NewStatsReporter(env.ContainerName, kmeta.ChildName(env.PodName, uuid.New().String())) - - h := &ingress.Handler{ - Logger: logger, - CeClient: ceClient, - ChannelURI: channelURI, - BrokerName: env.Broker, - Namespace: env.Namespace, - Reporter: reporter, - Defaulter: broker.TTLDefaulter(logger, defaultTTL), - } - - // configMapWatcher does not block, so start it first. - if err = configMapWatcher.Start(ctx.Done()); err != nil { - logger.Warn("Failed to start ConfigMap watcher", zap.Error(err)) - } - - // Start all of the informers and wait for them to sync. - logger.Info("Starting informers.") - if err := controller.StartInformers(ctx.Done(), informers...); err != nil { - logger.Fatal("Failed to start informers", zap.Error(err)) - } - - // Start blocks forever. - if err = h.Start(ctx); err != nil { - logger.Error("ingress.Start() returned an error", zap.Error(err)) - } - logger.Info("Exiting...") -} - -func flush(logger *zap.SugaredLogger) { - _ = logger.Sync() - metrics.FlushExporter() -} diff --git a/cmd/channel_broker/kodata/HEAD b/cmd/channel_broker/kodata/HEAD deleted file mode 120000 index 8f63681d362..00000000000 --- a/cmd/channel_broker/kodata/HEAD +++ /dev/null @@ -1 +0,0 @@ -../../../.git/HEAD \ No newline at end of file diff --git a/cmd/channel_broker/kodata/LICENSE b/cmd/channel_broker/kodata/LICENSE deleted file mode 120000 index 5853aaea53b..00000000000 --- a/cmd/channel_broker/kodata/LICENSE +++ /dev/null @@ -1 +0,0 @@ -../../../LICENSE \ No newline at end of file diff --git a/cmd/channel_broker/kodata/VENDOR-LICENSE b/cmd/channel_broker/kodata/VENDOR-LICENSE deleted file mode 120000 index 3cc89764519..00000000000 --- a/cmd/channel_broker/kodata/VENDOR-LICENSE +++ /dev/null @@ -1 +0,0 @@ -../../../third_party/VENDOR-LICENSE \ No newline at end of file diff --git a/cmd/channel_broker/kodata/refs b/cmd/channel_broker/kodata/refs deleted file mode 120000 index 5c9979c2618..00000000000 --- a/cmd/channel_broker/kodata/refs +++ /dev/null @@ -1 +0,0 @@ -../../../.git/refs/ \ No newline at end of file diff --git a/cmd/channel_broker/main.go b/cmd/channel_broker/main.go deleted file mode 100644 index dd1e53eb15f..00000000000 --- a/cmd/channel_broker/main.go +++ /dev/null @@ -1,39 +0,0 @@ -/* -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 - - http://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 main - -import ( - // Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters). - // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" - - "knative.dev/pkg/injection/sharedmain" - - "knative.dev/eventing/pkg/reconciler/broker" - "knative.dev/eventing/pkg/reconciler/configmappropagation" - "knative.dev/eventing/pkg/reconciler/namespace" -) - -func main() { - sharedmain.Main("broker-controller", - // Eventing - namespace.NewController, - broker.NewController, - - // Utility for sole-tenancy brokers. - configmappropagation.NewController, - ) -} diff --git a/config/brokers/channel-broker/200-channel-broker-binding.yaml b/config/brokers/channel-broker/200-channel-broker-binding.yaml deleted file mode 100644 index 8a61bd64fd3..00000000000 --- a/config/brokers/channel-broker/200-channel-broker-binding.yaml +++ /dev/null @@ -1,28 +0,0 @@ -# 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 -# -# http://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: rbac.authorization.k8s.io/v1 -kind: ClusterRoleBinding -metadata: - name: eventing-channel-broker-controller - labels: - eventing.knative.dev/release: devel -subjects: - - kind: ServiceAccount - name: eventing-controller - namespace: knative-eventing -roleRef: - kind: ClusterRole - name: knative-eventing-channel-broker-controller - apiGroup: rbac.authorization.k8s.io diff --git a/config/brokers/channel-broker/200-channel-broker-clusterrole.yaml b/config/brokers/channel-broker/200-channel-broker-clusterrole.yaml deleted file mode 120000 index 30646a228a5..00000000000 --- a/config/brokers/channel-broker/200-channel-broker-clusterrole.yaml +++ /dev/null @@ -1 +0,0 @@ -roles/controller-clusterroles.yaml \ No newline at end of file diff --git a/config/brokers/channel-broker/300-configmappropagation.yaml b/config/brokers/channel-broker/300-configmappropagation.yaml deleted file mode 120000 index 299cbf86a8c..00000000000 --- a/config/brokers/channel-broker/300-configmappropagation.yaml +++ /dev/null @@ -1 +0,0 @@ -resources/configmappropagation.yaml \ No newline at end of file diff --git a/config/brokers/channel-broker/500-broker-controller.yaml b/config/brokers/channel-broker/500-controller.yaml similarity index 100% rename from config/brokers/channel-broker/500-broker-controller.yaml rename to config/brokers/channel-broker/500-controller.yaml diff --git a/config/brokers/channel-broker/deployments/controller.yaml b/config/brokers/channel-broker/deployments/controller.yaml index 2e4a69f2870..52fb71f124e 100644 --- a/config/brokers/channel-broker/deployments/controller.yaml +++ b/config/brokers/channel-broker/deployments/controller.yaml @@ -20,7 +20,7 @@ metadata: labels: eventing.knative.dev/release: devel spec: - replicas: 1 + replicas: 0 selector: matchLabels: app: broker-controller @@ -31,11 +31,10 @@ spec: eventing.knative.dev/release: devel spec: serviceAccountName: eventing-controller - containers: - name: broker-controller terminationMessagePolicy: FallbackToLogsOnError - image: ko://knative.dev/eventing/cmd/channel_broker + image: gcr.io/knative-releases/knative.dev/eventing/cmd/channel_broker@sha256:7d3ae9a7139a846490869e58d9f4ddbe31b8a60df306c3636962609fbcca523d resources: requests: @@ -55,11 +54,11 @@ spec: value: knative.dev/eventing # Broker - name: BROKER_INGRESS_IMAGE - value: ko://knative.dev/eventing/cmd/broker/ingress + value: gcr.io/knative-releases/knative.dev/eventing/cmd/broker/ingress@sha256:a7a9dba94024ba961604f08be1bc49eddb40941dee4f291ed609149d899e7afa - name: BROKER_INGRESS_SERVICE_ACCOUNT value: eventing-broker-ingress - name: BROKER_FILTER_IMAGE - value: ko://knative.dev/eventing/cmd/broker/filter + value: gcr.io/knative-releases/knative.dev/eventing/cmd/broker/filter@sha256:fa50a25fd2db5ab09cc604d2b3d4ad36da3687882352c68ca11cb273e65111f7 - name: BROKER_FILTER_SERVICE_ACCOUNT value: eventing-broker-filter - name: BROKER_IMAGE_PULL_SECRET_NAME diff --git a/config/brokers/channel-broker/deployments/dummy.go b/config/brokers/channel-broker/deployments/dummy.go index a3bde1ba61c..16869185609 100644 --- a/config/brokers/channel-broker/deployments/dummy.go +++ b/config/brokers/channel-broker/deployments/dummy.go @@ -1,12 +1,9 @@ /* 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. diff --git a/config/brokers/channel-broker/dummy.go b/config/brokers/channel-broker/dummy.go deleted file mode 100644 index 0a1a23000b7..00000000000 --- a/config/brokers/channel-broker/dummy.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -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 broker is a placeholder that allows us to pull in config files -// via go mod vendor. -package broker diff --git a/config/brokers/channel-broker/resources/configmappropagation.yaml b/config/brokers/channel-broker/resources/configmappropagation.yaml deleted file mode 100644 index 09218308e32..00000000000 --- a/config/brokers/channel-broker/resources/configmappropagation.yaml +++ /dev/null @@ -1,59 +0,0 @@ -# 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 -# -# http://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: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: configmappropagations.configs.internal.knative.dev - labels: - eventing.knative.dev/release: devel - knative.dev/crd-install: "true" -spec: - group: configs.internal.knative.dev - versions: - - name: v1alpha1 - served: true - storage: true - names: - kind: ConfigMapPropagation - plural: configmappropagations - singular: configmappropagation - categories: - - knative-internal - shortNames: - - kcmp - - cmp - scope: Namespaced - subresources: - status: {} - additionalPrinterColumns: - - name: Ready - type: string - JSONPath: ".status.conditions[?(@.type==\"Ready\")].status" - - name: Reason - type: string - JSONPath: ".status.conditions[?(@.type==\"Ready\")].reason" - - name: OriginalNamespace - type: string - JSONPath: ".spec.originalNamespace" - validation: - openAPIV3Schema: - properties: - spec: - required: - - originalNamespace - properties: - originalNamespace: - type: string - description: "The namespace where original ConfigMaps exist in." diff --git a/config/brokers/channel-broker/resources/dummy.go b/config/brokers/channel-broker/resources/dummy.go deleted file mode 100644 index 229171cdcbf..00000000000 --- a/config/brokers/channel-broker/resources/dummy.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -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 resources is a placeholder that allows us to pull in config files -// via go mod vendor. -package resources diff --git a/config/brokers/channel-broker/roles/controller-clusterroles.yaml b/config/brokers/channel-broker/roles/controller-clusterroles.yaml deleted file mode 100644 index c4f0da7942a..00000000000 --- a/config/brokers/channel-broker/roles/controller-clusterroles.yaml +++ /dev/null @@ -1,59 +0,0 @@ -# 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 -# -# http://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: rbac.authorization.k8s.io/v1 -kind: ClusterRole -metadata: - name: knative-eventing-channel-broker-controller - labels: - eventing.knative.dev/release: devel -rules: - # Configs resources and status we care about. - - apiGroups: - - "configs.internal.knative.dev" - resources: - - "configmappropagations" - - "configmappropagations/status" - verbs: - - "get" - - "list" - - "create" - - "update" - - "delete" - - "patch" - - "watch" - - apiGroups: - - "configs.internal.knative.dev" - resources: - - "configmappropagations/finalizers" - verbs: - - "update" - - apiGroups: - - "" - resources: - - "namespaces/finalizers" - verbs: - - "update" - - apiGroups: - - coordination.k8s.io - resources: - - leases - verbs: - - "get" - - "list" - - "create" - - "update" - - "delete" - - "patch" - - "watch" diff --git a/config/brokers/channel-broker/roles/dummy.go b/config/brokers/channel-broker/roles/dummy.go deleted file mode 100644 index cddff2b7fd6..00000000000 --- a/config/brokers/channel-broker/roles/dummy.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -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 roles is a placeholder that allows us to pull in config files -// via go mod vendor. -package roles diff --git a/config/brokers/mt-channel-broker/deployments/broker-filter.yaml b/config/brokers/mt-channel-broker/deployments/broker-filter.yaml index 1b5c6ad0c9b..fcd266188ef 100644 --- a/config/brokers/mt-channel-broker/deployments/broker-filter.yaml +++ b/config/brokers/mt-channel-broker/deployments/broker-filter.yaml @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +# This one is a placeholder to bring the replicas down to zero. +# Remove this after .16 ships. apiVersion: apps/v1 kind: Deployment metadata: @@ -20,6 +22,7 @@ metadata: labels: eventing.knative.dev/release: devel spec: + replicas: 0 selector: matchLabels: eventing.knative.dev/brokerRole: filter @@ -85,6 +88,80 @@ spec: allowPrivilegeEscalation: false --- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mt-broker-filter + namespace: knative-eventing + labels: + eventing.knative.dev/release: devel +spec: + selector: + matchLabels: + eventing.knative.dev/brokerRole: filter + template: + metadata: + labels: + eventing.knative.dev/brokerRole: filter + eventing.knative.dev/release: devel + spec: + serviceAccountName: mt-broker-filter + containers: + - name: filter + terminationMessagePolicy: FallbackToLogsOnError + image: ko://knative.dev/eventing/cmd/mtbroker/filter + livenessProbe: + failureThreshold: 3 + httpGet: + path: /healthz + port: 8080 + scheme: HTTP + initialDelaySeconds: 5 + periodSeconds: 2 + successThreshold: 1 + timeoutSeconds: 1 + resources: + requests: + cpu: 100m + memory: 100Mi + ports: + - containerPort: 8080 + name: http + protocol: TCP + - containerPort: 9090 + name: metrics + protocol: TCP + terminationMessagePath: /dev/termination-log + env: + - name: SYSTEM_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: NAMESPACE + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.namespace + - name: POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CONTAINER_NAME + value: filter + - name: CONFIG_LOGGING_NAME + value: config-logging + - name: CONFIG_OBSERVABILITY_NAME + value: config-observability + - name: METRICS_DOMAIN + value: knative.dev/internal/eventing + - name: FILTER_PORT + value: "8080" + securityContext: + allowPrivilegeEscalation: false + +--- + apiVersion: v1 kind: Service metadata: diff --git a/config/brokers/mt-channel-broker/deployments/broker-ingress.yaml b/config/brokers/mt-channel-broker/deployments/broker-ingress.yaml index 738214be8b0..3710d18e7ed 100644 --- a/config/brokers/mt-channel-broker/deployments/broker-ingress.yaml +++ b/config/brokers/mt-channel-broker/deployments/broker-ingress.yaml @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +# This one is a placeholder to bring the replicas down to zero. +# Remove this after .16 ships. apiVersion: apps/v1 kind: Deployment metadata: @@ -20,6 +22,7 @@ metadata: labels: eventing.knative.dev/release: devel spec: + replicas: 0 selector: matchLabels: eventing.knative.dev/brokerRole: ingress @@ -85,6 +88,80 @@ spec: allowPrivilegeEscalation: false --- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mt-broker-ingress + namespace: knative-eventing + labels: + eventing.knative.dev/release: devel +spec: + selector: + matchLabels: + eventing.knative.dev/brokerRole: ingress + template: + metadata: + labels: + eventing.knative.dev/brokerRole: ingress + eventing.knative.dev/release: devel + spec: + serviceAccountName: mt-broker-ingress + containers: + - name: ingress + terminationMessagePolicy: FallbackToLogsOnError + image: ko://knative.dev/eventing/cmd/mtbroker/ingress + livenessProbe: + failureThreshold: 3 + httpGet: + path: /healthz + port: 8080 + scheme: HTTP + initialDelaySeconds: 5 + periodSeconds: 2 + successThreshold: 1 + timeoutSeconds: 1 + resources: + requests: + cpu: 100m + memory: 100Mi + ports: + - containerPort: 8080 + name: http + protocol: TCP + - containerPort: 9090 + name: metrics + protocol: TCP + terminationMessagePath: /dev/termination-log + env: + - name: SYSTEM_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: NAMESPACE + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.namespace + - name: POD_NAME + valueFrom: + fieldRef: + apiVersion: v1 + fieldPath: metadata.name + - name: CONTAINER_NAME + value: ingress + - name: CONFIG_LOGGING_NAME + value: config-logging + - name: CONFIG_OBSERVABILITY_NAME + value: config-observability + - name: METRICS_DOMAIN + value: knative.dev/internal/eventing + - name: INGRESS_PORT + value: "8080" + securityContext: + allowPrivilegeEscalation: false + +--- + apiVersion: v1 kind: Service metadata: diff --git a/config/brokers/mt-channel-broker/deployments/hpa.yaml b/config/brokers/mt-channel-broker/deployments/hpa.yaml index d24fd5a33d2..cbd419061cb 100644 --- a/config/brokers/mt-channel-broker/deployments/hpa.yaml +++ b/config/brokers/mt-channel-broker/deployments/hpa.yaml @@ -7,7 +7,7 @@ spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment - name: broker-ingress + name: mt-broker-ingress minReplicas: 1 maxReplicas: 10 metrics: @@ -27,7 +27,7 @@ spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment - name: broker-filter + name: mt-broker-filter minReplicas: 1 maxReplicas: 10 metrics: diff --git a/pkg/apis/config/defaults_test.go b/pkg/apis/config/defaults_test.go index 9b23cd0912a..22cd36052f6 100644 --- a/pkg/apis/config/defaults_test.go +++ b/pkg/apis/config/defaults_test.go @@ -86,8 +86,8 @@ func TestGetBrokerClass(t *testing.T) { if err != nil { t.Errorf("GetBrokerClass Failed = %v", err) } - if c != "ChannelBasedBroker" { - t.Errorf("GetBrokerClass Failed, wanted ChannelBasedBroker, got: %s", c) + if c != "MTChannelBasedBroker" { + t.Errorf("GetBrokerClass Failed, wanted MTChannelBasedBroker, got: %s", c) } c, err = defaults.GetBrokerClass("some-namespace") if err != nil { diff --git a/pkg/apis/config/testdata/config-br-defaults.yaml b/pkg/apis/config/testdata/config-br-defaults.yaml index e1adf63dce4..76ccf027b34 100644 --- a/pkg/apis/config/testdata/config-br-defaults.yaml +++ b/pkg/apis/config/testdata/config-br-defaults.yaml @@ -29,7 +29,7 @@ data: ################################ default-br-config: | clusterDefault: - brokerClass: ChannelBasedBroker + brokerClass: MTChannelBasedBroker apiVersion: v1 kind: ConfigMap name: somename diff --git a/pkg/apis/eventing/register.go b/pkg/apis/eventing/register.go index 802e5f02167..7aa995c784c 100644 --- a/pkg/apis/eventing/register.go +++ b/pkg/apis/eventing/register.go @@ -29,11 +29,6 @@ const ( // which Controller is responsible for them. BrokerClassKey = GroupName + "/broker.class" - // ChannelBrokerClassValue is the value we use to specify the - // Broker using channels. As in Broker from this repository - // pkg/reconciler/broker - ChannelBrokerClassValue = "ChannelBasedBroker" - // MTChannelBrokerClassValue is the value we use to specify the // Broker using channels, but the resources (ingress,filter) run // in the system namespace. As in Broker from this repository diff --git a/pkg/apis/eventing/v1alpha1/broker_defaults_test.go b/pkg/apis/eventing/v1alpha1/broker_defaults_test.go index c6eb403cde0..702166177e6 100644 --- a/pkg/apis/eventing/v1alpha1/broker_defaults_test.go +++ b/pkg/apis/eventing/v1alpha1/broker_defaults_test.go @@ -54,7 +54,7 @@ var ( }, }, ClusterDefault: &config.ClassAndKRef{ - BrokerClass: eventing.ChannelBrokerClassValue, + BrokerClass: eventing.MTChannelBrokerClassValue, }, }, } @@ -72,7 +72,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: v1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, }, @@ -81,7 +81,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: v1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, }, @@ -91,7 +91,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: v1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ @@ -114,7 +114,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: v1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ @@ -142,7 +142,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: v1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ @@ -177,7 +177,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: v1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, }, diff --git a/pkg/apis/eventing/v1beta1/broker_defaults_test.go b/pkg/apis/eventing/v1beta1/broker_defaults_test.go index 072b41a8c92..1ea5bb31df4 100644 --- a/pkg/apis/eventing/v1beta1/broker_defaults_test.go +++ b/pkg/apis/eventing/v1beta1/broker_defaults_test.go @@ -53,7 +53,7 @@ var ( }, }, ClusterDefault: &config.ClassAndKRef{ - BrokerClass: eventing.ChannelBrokerClassValue, + BrokerClass: eventing.MTChannelBrokerClassValue, KReference: &duckv1.KReference{ APIVersion: "v1", Kind: "ConfigMap", @@ -74,7 +74,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ @@ -91,7 +91,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ @@ -126,7 +126,7 @@ func TestBrokerSetDefaults(t *testing.T) { expected: Broker{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ @@ -145,7 +145,7 @@ func TestBrokerSetDefaults(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Namespace: "mynamespace", Annotations: map[string]string{ - eventing.BrokerClassKey: eventing.ChannelBrokerClassValue, + eventing.BrokerClassKey: eventing.MTChannelBrokerClassValue, }, }, Spec: BrokerSpec{ diff --git a/pkg/broker/filter/filter_handler.go b/pkg/broker/filter/filter_handler.go deleted file mode 100644 index f16bbec6a20..00000000000 --- a/pkg/broker/filter/filter_handler.go +++ /dev/null @@ -1,396 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 filter - -import ( - "context" - "errors" - "fmt" - "net/http" - "sync/atomic" - "time" - - cloudevents "github.com/cloudevents/sdk-go" - "go.opencensus.io/trace" - "go.uber.org/zap" - - eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/broker" - eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1" - "knative.dev/eventing/pkg/kncloudevents" - "knative.dev/eventing/pkg/logging" - "knative.dev/eventing/pkg/reconciler/trigger/path" - "knative.dev/eventing/pkg/tracing" - "knative.dev/eventing/pkg/utils" - "knative.dev/pkg/network" -) - -const ( - passFilter FilterResult = "pass" - failFilter FilterResult = "fail" - noFilter FilterResult = "no_filter" - - // readyz is the HTTP path that will be used for readiness checks. - readyz = "/readyz" - - // TODO make these constants configurable (either as env variables, config map, or part of broker spec). - // Issue: https://github.com/knative/eventing/issues/1777 - // Constants for the underlying HTTP Client transport. These would enable better connection reuse. - // Set them on a 10:1 ratio, but this would actually depend on the Triggers' subscribers and the workload itself. - // These are magic numbers, partly set based on empirical evidence running performance workloads, and partly - // based on what serving is doing. See https://github.com/knative/serving/blob/master/pkg/network/transports.go. - defaultMaxIdleConnections = 1000 - defaultMaxIdleConnectionsPerHost = 100 -) - -// Handler parses Cloud Events, determines if they pass a filter, and sends them to a subscriber. -type Handler struct { - logger *zap.Logger - triggerLister eventinglisters.TriggerNamespaceLister - ceClient cloudevents.Client - reporter StatsReporter - isReady *atomic.Value -} - -type sendError struct { - Err error - Status int -} - -func (e sendError) Error() string { - return e.Err.Error() -} - -func (e sendError) Unwrap() error { - return e.Err -} - -// FilterResult has the result of the filtering operation. -type FilterResult string - -// NewHandler creates a new Handler and its associated MessageReceiver. The caller is responsible for -// Start()ing the returned Handler. -func NewHandler(logger *zap.Logger, triggerLister eventinglisters.TriggerNamespaceLister, reporter StatsReporter) (*Handler, error) { - connectionArgs := kncloudevents.ConnectionArgs{ - MaxIdleConns: defaultMaxIdleConnections, - MaxIdleConnsPerHost: defaultMaxIdleConnectionsPerHost, - } - httpTransport, err := cloudevents.NewHTTPTransport( - cloudevents.WithBinaryEncoding(), - cloudevents.WithHTTPTransport(connectionArgs.NewDefaultHTTPTransport()), - ) - if err != nil { - return nil, err - } - - ceClient, err := kncloudevents.NewDefaultHTTPClient(httpTransport) - if err != nil { - return nil, err - } - - r := &Handler{ - logger: logger, - triggerLister: triggerLister, - ceClient: ceClient, - reporter: reporter, - isReady: &atomic.Value{}, - } - r.isReady.Store(false) - - httpTransport.Handler = http.NewServeMux() - httpTransport.Handler.HandleFunc("/healthz", r.healthZ) - httpTransport.Handler.HandleFunc(readyz, r.readyZ) - - return r, nil -} - -func (r *Handler) healthZ(writer http.ResponseWriter, _ *http.Request) { - writer.WriteHeader(http.StatusOK) -} - -func (r *Handler) readyZ(writer http.ResponseWriter, _ *http.Request) { - if r.isReady == nil || !r.isReady.Load().(bool) { - http.Error(writer, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable) - return - } - writer.WriteHeader(http.StatusOK) -} - -// Start begins to receive messages for the handler. -// -// Only HTTP POST requests to the root path (/) are accepted. If other paths or -// methods are needed, use the HandleRequest method directly with another HTTP -// server. -// -// This method will block until a message is received on the stop channel. -func (r *Handler) Start(ctx context.Context) error { - ctx, cancel := context.WithCancel(ctx) - defer cancel() - - errCh := make(chan error, 1) - go func() { - errCh <- r.ceClient.StartReceiver(ctx, r.serveHTTP) - }() - - // We are ready. - r.isReady.Store(true) - - // Stop either if the receiver stops (sending to errCh) or if stopCh is closed. - select { - case err := <-errCh: - return err - case <-ctx.Done(): - break - } - - // No longer ready. - r.isReady.Store(false) - - // stopCh has been closed, we need to gracefully shutdown h.ceClient. cancel() will start its - // shutdown, if it hasn't finished in a reasonable amount of time, just return an error. - cancel() - select { - case err := <-errCh: - return err - case <-time.After(network.DefaultDrainTimeout): - return errors.New("timeout shutting down ceClient") - } -} - -func (r *Handler) serveHTTP(ctx context.Context, event cloudevents.Event, resp *cloudevents.EventResponse) error { - tctx := cloudevents.HTTPTransportContextFrom(ctx) - if tctx.Method != http.MethodPost { - resp.Status = http.StatusMethodNotAllowed - return nil - } - - // tctx.URI is actually the path... - triggerRef, err := path.Parse(tctx.URI) - if err != nil { - r.logger.Info("Unable to parse path as a trigger", zap.Error(err), zap.String("path", tctx.URI)) - return errors.New("unable to parse path as a Trigger") - } - - ctx, span := trace.StartSpan(ctx, tracing.TriggerMessagingDestination(triggerRef.NamespacedName)) - defer span.End() - - if span.IsRecordingEvents() { - span.AddAttributes( - tracing.MessagingSystemAttribute, - tracing.MessagingProtocolHTTP, - tracing.TriggerMessagingDestinationAttribute(triggerRef.NamespacedName), - tracing.MessagingMessageIDAttribute(event.ID()), - ) - } - - // Remove the TTL attribute that is used by the Broker. - ttl, err := broker.GetTTL(event.Context) - if err != nil { - // Only messages sent by the Broker should be here. If the attribute isn't here, then the - // event wasn't sent by the Broker, so we can drop it. - r.logger.Warn("No TTL seen, dropping", zap.Any("triggerRef", triggerRef), zap.Any("event", event)) - // Return a BadRequest error, so the upstream can decide how to handle it, e.g. sending - // the message to a DLQ. - resp.Status = http.StatusBadRequest - return nil - } - if err := broker.DeleteTTL(event.Context); err != nil { - r.logger.Warn("Failed to delete TTL.", zap.Error(err)) - } - - r.logger.Debug("Received message", zap.Any("triggerRef", triggerRef)) - - responseEvent, err := r.sendEvent(ctx, tctx, triggerRef, &event) - if err != nil { - // Propagate any error codes from the invoke back upstram. - var httpError sendError - if errors.As(err, &httpError) { - resp.Status = httpError.Status - } - r.logger.Error("Error sending the event", zap.Error(err)) - return err - } - - resp.Status = http.StatusAccepted - if responseEvent == nil { - return nil - } - - // Reattach the TTL (with the same value) to the response event before sending it to the Broker. - - if err := broker.SetTTL(responseEvent.Context, ttl); err != nil { - return err - } - resp.Event = responseEvent - resp.Context = &cloudevents.HTTPTransportResponseContext{ - Header: utils.PassThroughHeaders(tctx.Header), - } - - return nil -} - -// sendEvent sends an event to a subscriber if the trigger filter passes. -func (r *Handler) sendEvent(ctx context.Context, tctx cloudevents.HTTPTransportContext, trigger path.NamespacedNameUID, event *cloudevents.Event) (*cloudevents.Event, error) { - t, err := r.getTrigger(ctx, trigger) - if err != nil { - r.logger.Info("Unable to get the Trigger", zap.Error(err), zap.Any("triggerRef", trigger)) - return nil, err - } - - reportArgs := &ReportArgs{ - Namespace: t.Namespace, - Trigger: t.Name, - Broker: t.Spec.Broker, - FilterType: triggerFilterAttribute(t.Spec.Filter, "type"), - } - - subscriberURI := t.Status.SubscriberURI - if subscriberURI == nil { - err = errors.New("unable to read subscriberURI") - // Record the event count. - r.reporter.ReportEventCount(reportArgs, http.StatusNotFound) - return nil, err - } - - // Check if the event should be sent. - filterResult := r.shouldSendEvent(ctx, &t.Spec, event) - - if filterResult == failFilter { - r.logger.Debug("Event did not pass filter", zap.Any("triggerRef", trigger)) - // We do not count the event. The event will be counted in the broker ingress. - // If the filter didn't pass, it means that the event wasn't meant for this Trigger. - return nil, nil - } - - // Record the event processing time. This might be off if the receiver and the filter pods are running in - // different nodes with different clocks. - var arrivalTimeStr string - if extErr := event.ExtensionAs(broker.EventArrivalTime, &arrivalTimeStr); extErr == nil { - arrivalTime, err := time.Parse(time.RFC3339, arrivalTimeStr) - if err == nil { - r.reporter.ReportEventProcessingTime(reportArgs, time.Since(arrivalTime)) - } - } - - sendingCTX := utils.SendingContextFrom(ctx, tctx, subscriberURI.URL()) - - start := time.Now() - rctx, replyEvent, err := r.ceClient.Send(sendingCTX, *event) - rtctx := cloudevents.HTTPTransportContextFrom(rctx) - // Record the dispatch time. - r.reporter.ReportEventDispatchTime(reportArgs, rtctx.StatusCode, time.Since(start)) - // Record the event count. - r.reporter.ReportEventCount(reportArgs, rtctx.StatusCode) - // Wrap any errors along with the response status code so that can be propagated upstream. - if err != nil { - err = sendError{err, rtctx.StatusCode} - } - return replyEvent, err -} - -func (r *Handler) getTrigger(ctx context.Context, ref path.NamespacedNameUID) (*eventingv1alpha1.Trigger, error) { - t, err := r.triggerLister.Get(ref.Name) - if err != nil { - return nil, err - } - if t.UID != ref.UID { - return nil, fmt.Errorf("trigger had a different UID. From ref '%s'. From Kubernetes '%s'", ref.UID, t.UID) - } - return t, nil -} - -// shouldSendEvent determines whether event 'event' should be sent based on the triggerSpec 'ts'. -// Currently it supports exact matching on event context attributes and extension attributes. -// If no filter is present, shouldSendEvent returns passFilter. -func (r *Handler) shouldSendEvent(ctx context.Context, ts *eventingv1alpha1.TriggerSpec, event *cloudevents.Event) FilterResult { - // No filter specified, default to passing everything. - if ts.Filter == nil || (ts.Filter.DeprecatedSourceAndType == nil && ts.Filter.Attributes == nil) { - return noFilter - } - - attrs := map[string]string{} - // Since the filters cannot distinguish presence, filtering for an empty - // string is impossible. - if ts.Filter.DeprecatedSourceAndType != nil { - attrs["type"] = ts.Filter.DeprecatedSourceAndType.Type - attrs["source"] = ts.Filter.DeprecatedSourceAndType.Source - } else if ts.Filter.Attributes != nil { - attrs = map[string]string(*ts.Filter.Attributes) - } - - return r.filterEventByAttributes(ctx, attrs, event) -} - -func (r *Handler) filterEventByAttributes(ctx context.Context, attrs map[string]string, event *cloudevents.Event) FilterResult { - // Set standard context attributes. The attributes available may not be - // exactly the same as the attributes defined in the current version of the - // CloudEvents spec. - ce := map[string]interface{}{ - "specversion": event.SpecVersion(), - "type": event.Type(), - "source": event.Source(), - "subject": event.Subject(), - "id": event.ID(), - "time": event.Time().String(), - "schemaurl": event.DataSchema(), - "datacontenttype": event.DataContentType(), - "datamediatype": event.DataMediaType(), - // TODO: use data_base64 when SDK supports it. - "datacontentencoding": event.DeprecatedDataContentEncoding(), - } - ext := event.Extensions() - for k, v := range ext { - ce[k] = v - } - - for k, v := range attrs { - var value interface{} - value, ok := ce[k] - // If the attribute does not exist in the event, return false. - if !ok { - logging.FromContext(ctx).Debug("Attribute not found", zap.String("attribute", k)) - return failFilter - } - // If the attribute is not set to any and is different than the one from the event, return false. - if v != eventingv1alpha1.TriggerAnyFilter && v != value { - logging.FromContext(ctx).Debug("Attribute had non-matching value", zap.String("attribute", k), zap.String("filter", v), zap.Any("received", value)) - return failFilter - } - } - return passFilter -} - -// triggerFilterAttribute returns the filter attribute value for a given `attributeName`. If it doesn't not exist, -// returns the any value filter. -func triggerFilterAttribute(filter *eventingv1alpha1.TriggerFilter, attributeName string) string { - attributeValue := eventingv1alpha1.TriggerAnyFilter - if filter != nil { - if filter.DeprecatedSourceAndType != nil { - if attributeName == "type" { - attributeValue = filter.DeprecatedSourceAndType.Type - } else if attributeName == "source" { - attributeValue = filter.DeprecatedSourceAndType.Source - } - } else if filter.Attributes != nil { - attrs := map[string]string(*filter.Attributes) - if v, ok := attrs[attributeName]; ok { - attributeValue = v - } - } - } - return attributeValue -} diff --git a/pkg/broker/filter/filter_handler_test.go b/pkg/broker/filter/filter_handler_test.go deleted file mode 100644 index 804e4b838c0..00000000000 --- a/pkg/broker/filter/filter_handler_test.go +++ /dev/null @@ -1,624 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 filter - -import ( - "context" - "fmt" - "net/http" - "net/http/httptest" - "net/url" - "strings" - "testing" - "time" - - "k8s.io/apimachinery/pkg/labels" - - cloudevents "github.com/cloudevents/sdk-go" - cepkg "github.com/cloudevents/sdk-go/pkg/cloudevents" - cehttp "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http" - "github.com/google/go-cmp/cmp" - "go.uber.org/zap" - "go.uber.org/zap/zaptest" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/kubernetes/scheme" - - "knative.dev/pkg/apis" - - eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/broker" - "knative.dev/eventing/pkg/client/clientset/versioned/fake" - "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1" - "knative.dev/eventing/pkg/utils" -) - -const ( - testNS = "test-namespace" - triggerName = "test-trigger" - triggerUID = "test-trigger-uid" - eventType = `com.example.someevent` - eventSource = `/mycontext` - extensionName = `my-extension` - extensionValue = `my-extension-value` - - // Because it's a URL we're comparing to, without protocol it looks like this. - toBeReplaced = "//toBeReplaced" -) - -var ( - host = fmt.Sprintf("%s.%s.triggers.%s", triggerName, testNS, utils.GetClusterDomainName()) - validPath = fmt.Sprintf("/triggers/%s/%s/%s", testNS, triggerName, triggerUID) -) - -func init() { - // Add types to scheme. - _ = eventingv1alpha1.AddToScheme(scheme.Scheme) -} - -func TestReceiver(t *testing.T) { - testCases := map[string]struct { - triggers []*eventingv1alpha1.Trigger - tctx *cloudevents.HTTPTransportContext - event *cloudevents.Event - requestFails bool - failureStatus int - returnedEvent *cloudevents.Event - expectNewToFail bool - expectedErr bool - expectedDispatch bool - expectedStatus int - expectedHeaders http.Header - expectedEventCount bool - expectedEventDispatchTime bool - expectedEventProcessingTime bool - }{ - "Not POST": { - tctx: &cloudevents.HTTPTransportContext{ - Method: "GET", - Host: host, - URI: validPath, - }, - expectedStatus: http.StatusMethodNotAllowed, - }, - "Path too short": { - tctx: &cloudevents.HTTPTransportContext{ - Method: "POST", - Host: host, - URI: "/test-namespace/test-trigger", - }, - expectedErr: true, - }, - "Path too long": { - tctx: &cloudevents.HTTPTransportContext{ - Method: "POST", - Host: host, - URI: "/triggers/test-namespace/test-trigger/extra", - }, - expectedErr: true, - }, - "Path without prefix": { - tctx: &cloudevents.HTTPTransportContext{ - Method: "POST", - Host: host, - URI: "/something/test-namespace/test-trigger", - }, - expectedErr: true, - }, - "Bad host": { - tctx: &cloudevents.HTTPTransportContext{ - Method: "POST", - Host: "badhost-cant-be-parsed-as-a-trigger-name-plus-namespace", - URI: validPath, - }, - expectedErr: true, - }, - "Trigger.Get fails": { - // No trigger exists, so the Get will fail. - expectedErr: true, - }, - "Trigger doesn't have SubscriberURI": { - triggers: []*eventingv1alpha1.Trigger{ - makeTriggerWithoutSubscriberURI(), - }, - expectedErr: true, - expectedEventCount: true, - }, - "Trigger without a Filter": { - triggers: []*eventingv1alpha1.Trigger{ - makeTriggerWithoutFilter(), - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "No TTL": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")), - }, - event: makeEventWithoutTTL(), - }, - "Wrong type": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("some-other-type", "")), - }, - expectedEventCount: false, - }, - "Wrong type with attribs": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes("some-other-type", "")), - }, - expectedEventCount: false, - }, - "Wrong source": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "some-other-source")), - }, - expectedEventCount: false, - }, - "Wrong source with attribs": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes("", "some-other-source")), - }, - expectedEventCount: false, - }, - "Wrong extension": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes("", "some-other-source")), - }, - expectedEventCount: false, - }, - "Dispatch failed": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")), - }, - requestFails: true, - expectedErr: true, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "Dispatch succeeded - Any": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")), - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "Dispatch succeeded - Any with attribs": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes("", "")), - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "Dispatch succeeded - Specific": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType(eventType, eventSource)), - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "Dispatch succeeded - Specific with attribs": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes(eventType, eventSource)), - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "Dispatch succeeded - Extension with attribs": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributesAndExtension(eventType, eventSource, extensionValue)), - }, - event: makeEventWithExtension(extensionName, extensionValue), - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - }, - "Dispatch succeeded - Any with attribs - Arrival extension": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes("", "")), - }, - event: makeEventWithExtension(broker.EventArrivalTime, "2019-08-26T23:38:17.834384404Z"), - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - expectedEventProcessingTime: true, - }, - "Wrong Extension with attribs": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributesAndExtension(eventType, eventSource, "some-other-extension-value")), - }, - event: makeEventWithExtension(extensionName, extensionValue), - expectedEventCount: false, - }, - "Returned Cloud Event": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")), - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - returnedEvent: makeDifferentEvent(), - }, - "Error From Trigger": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithAttributes("", "")), - }, - tctx: &cloudevents.HTTPTransportContext{ - Method: "POST", - Host: host, - URI: validPath, - StatusCode: http.StatusTooManyRequests, - }, - requestFails: true, - failureStatus: http.StatusTooManyRequests, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - expectedErr: true, - expectedStatus: http.StatusTooManyRequests, - }, - "Returned Cloud Event with custom headers": { - triggers: []*eventingv1alpha1.Trigger{ - makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")), - }, - tctx: &cloudevents.HTTPTransportContext{ - Method: "POST", - Host: host, - URI: validPath, - Header: http.Header{ - // foo won't pass filtering. - "foo": []string{"bar"}, - // traceparent will not pass filtering. - "Traceparent": []string{"0"}, - // Knative-Foo will pass as a prefix match. - "Knative-Foo": []string{"baz", "qux"}, - // X-Request-Id will pass as an exact header match. - "X-Request-Id": []string{"123"}, - }, - }, - expectedHeaders: http.Header{ - // X-Request-Id will pass as an exact header match. - "X-Request-Id": []string{"123"}, - // Knative-Foo will pass as a prefix match. - "Knative-Foo": []string{"baz", "qux"}, - }, - expectedDispatch: true, - expectedEventCount: true, - expectedEventDispatchTime: true, - returnedEvent: makeDifferentEvent(), - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - - fh := fakeHandler{ - failRequest: tc.requestFails, - failStatus: tc.failureStatus, - returnedEvent: tc.returnedEvent, - headers: tc.expectedHeaders, - t: t, - } - s := httptest.NewServer(&fh) - defer s.Client() - - // Replace the SubscriberURI to point at our fake server. - correctURI := make([]runtime.Object, 0, len(tc.triggers)) - for _, trig := range tc.triggers { - if trig.Status.SubscriberURI != nil && trig.Status.SubscriberURI.String() == toBeReplaced { - - url, err := apis.ParseURL(s.URL) - if err != nil { - t.Fatalf("Failed to parse URL %q : %s", s.URL, err) - } - trig.Status.SubscriberURI = url - } - correctURI = append(correctURI, trig) - } - reporter := &mockReporter{} - r, err := NewHandler( - zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())), - getFakeTriggerLister(correctURI), - reporter) - if tc.expectNewToFail { - if err == nil { - t.Fatal("Expected New to fail, it didn't") - } - return - } else if err != nil { - t.Fatalf("Unable to create receiver: %v", err) - } - - tctx := tc.tctx - if tctx == nil { - tctx = &cloudevents.HTTPTransportContext{ - Method: http.MethodPost, - Host: host, - URI: validPath, - } - } - ctx := cehttp.WithTransportContext(context.Background(), *tctx) - resp := &cloudevents.EventResponse{} - event := tc.event - if event == nil { - event = makeEvent() - } - err = r.serveHTTP(ctx, *event, resp) - - if tc.expectedErr && err == nil { - t.Errorf("Expected an error, received nil") - } else if !tc.expectedErr && err != nil { - t.Errorf("Expected no error, received %v", err) - } - - if tc.expectedStatus != 0 && tc.expectedStatus != resp.Status { - t.Errorf("Unexpected status. Expected %v. Actual %v.", tc.expectedStatus, resp.Status) - } - if tc.expectedDispatch != fh.requestReceived { - t.Errorf("Incorrect dispatch. Expected %v, Actual %v", tc.expectedDispatch, fh.requestReceived) - } - if tc.expectedEventCount != reporter.eventCountReported { - t.Errorf("Incorrect event count reported metric. Expected %v, Actual %v", tc.expectedEventCount, reporter.eventCountReported) - } - if tc.expectedEventDispatchTime != reporter.eventDispatchTimeReported { - t.Errorf("Incorrect event dispatch time reported metric. Expected %v, Actual %v", tc.expectedEventDispatchTime, reporter.eventDispatchTimeReported) - } - if tc.expectedEventProcessingTime != reporter.eventProcessingTimeReported { - t.Errorf("Incorrect event processing time reported metric. Expected %v, Actual %v", tc.expectedEventProcessingTime, reporter.eventProcessingTimeReported) - } - if tc.returnedEvent != nil { - if tc.returnedEvent.SpecVersion() != cepkg.CloudEventsVersionV1 { - t.Errorf("Incorrect event processing time reported metric. Expected %v, Actual %v", tc.expectedEventProcessingTime, reporter.eventProcessingTimeReported) - } - } - // Compare the returned event. - if tc.returnedEvent == nil { - if resp.Event != nil { - t.Fatalf("Unexpected response event: %v", resp.Event) - } - return - } else if resp.Event == nil { - t.Fatalf("Expected response event, actually nil") - } - - // The TTL will be added again. - expectedResponseEvent := addTTLToEvent(*tc.returnedEvent) - if diff := cmp.Diff(expectedResponseEvent.Context.AsV1(), resp.Event.Context.AsV1()); diff != "" { - t.Errorf("Incorrect response event context (-want +got): %s", diff) - } - if diff := cmp.Diff(expectedResponseEvent.Data, resp.Event.Data); diff != "" { - t.Errorf("Incorrect response event data (-want +got): %s", diff) - } - }) - } -} - -type mockReporter struct { - eventCountReported bool - eventDispatchTimeReported bool - eventProcessingTimeReported bool -} - -func (r *mockReporter) ReportEventCount(args *ReportArgs, responseCode int) error { - r.eventCountReported = true - return nil -} - -func (r *mockReporter) ReportEventDispatchTime(args *ReportArgs, responseCode int, d time.Duration) error { - r.eventDispatchTimeReported = true - return nil -} - -func (r *mockReporter) ReportEventProcessingTime(args *ReportArgs, d time.Duration) error { - r.eventProcessingTimeReported = true - return nil -} - -type fakeHandler struct { - failRequest bool - failStatus int - requestReceived bool - headers http.Header - returnedEvent *cloudevents.Event - t *testing.T -} - -func (h *fakeHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - h.requestReceived = true - - for n, v := range h.headers { - if strings.Contains(strings.ToLower(n), strings.ToLower(broker.TTLAttribute)) { - h.t.Errorf("Broker TTL should not be seen by the subscriber: %s", n) - } - if diff := cmp.Diff(v, req.Header[n]); diff != "" { - h.t.Errorf("Incorrect request header '%s' (-want +got): %s", n, diff) - } - } - - if h.failRequest { - if h.failStatus != 0 { - resp.WriteHeader(h.failStatus) - } else { - resp.WriteHeader(http.StatusBadRequest) - } - return - } - if h.returnedEvent == nil { - resp.WriteHeader(http.StatusAccepted) - return - } - - c := &cehttp.CodecV1{} - m, err := c.Encode(context.Background(), *h.returnedEvent) - if err != nil { - h.t.Fatalf("Could not encode message: %v", err) - } - msg := m.(*cehttp.Message) - for k, vs := range msg.Header { - resp.Header().Del(k) - for _, v := range vs { - resp.Header().Set(k, v) - } - } - _, err = resp.Write(msg.Body) - if err != nil { - h.t.Fatalf("Unable to write body: %v", err) - } -} - -type FakeBrokerNamespaceLister struct { - client *fake.Clientset -} - -func (f *FakeBrokerNamespaceLister) List(selector labels.Selector) (ret []*eventingv1alpha1.Trigger, err error) { - panic("implement me") -} - -func (f *FakeBrokerNamespaceLister) Get(name string) (*eventingv1alpha1.Trigger, error) { - return f.client.EventingV1alpha1().Triggers(testNS).Get(name, metav1.GetOptions{}) -} - -func getFakeTriggerLister(initial []runtime.Object) v1alpha1.TriggerNamespaceLister { - c := fake.NewSimpleClientset(initial...) - - return &FakeBrokerNamespaceLister{ - client: c, - } -} - -func makeTriggerFilterWithDeprecatedSourceAndType(t, s string) *eventingv1alpha1.TriggerFilter { - return &eventingv1alpha1.TriggerFilter{ - DeprecatedSourceAndType: &eventingv1alpha1.TriggerFilterSourceAndType{ - Type: t, - Source: s, - }, - } -} - -func makeTriggerFilterWithAttributes(t, s string) *eventingv1alpha1.TriggerFilter { - return &eventingv1alpha1.TriggerFilter{ - Attributes: &eventingv1alpha1.TriggerFilterAttributes{ - "type": t, - "source": s, - }, - } -} - -func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1alpha1.TriggerFilter { - return &eventingv1alpha1.TriggerFilter{ - Attributes: &eventingv1alpha1.TriggerFilterAttributes{ - "type": t, - "source": s, - extensionName: e, - }, - } -} - -func makeTrigger(filter *eventingv1alpha1.TriggerFilter) *eventingv1alpha1.Trigger { - return &eventingv1alpha1.Trigger{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "eventing.knative.dev/v1alpha1", - Kind: "Trigger", - }, - ObjectMeta: metav1.ObjectMeta{ - Namespace: testNS, - Name: triggerName, - UID: triggerUID, - }, - Spec: eventingv1alpha1.TriggerSpec{ - Filter: filter, - }, - Status: eventingv1alpha1.TriggerStatus{ - SubscriberURI: &apis.URL{Host: "toBeReplaced"}, - }, - } -} - -func makeTriggerWithoutFilter() *eventingv1alpha1.Trigger { - t := makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")) - t.Spec.Filter = nil - return t -} - -func makeTriggerWithoutSubscriberURI() *eventingv1alpha1.Trigger { - t := makeTrigger(makeTriggerFilterWithDeprecatedSourceAndType("", "")) - t.Status = eventingv1alpha1.TriggerStatus{} - return t -} - -func makeEventWithoutTTL() *cloudevents.Event { - return &cloudevents.Event{ - Context: cloudevents.EventContextV02{ - Type: eventType, - Source: cloudevents.URLRef{ - URL: url.URL{ - Path: eventSource, - }, - }, - ContentType: cloudevents.StringOfApplicationJSON(), - }.AsV1(), - } -} - -func makeEvent() *cloudevents.Event { - noTTL := makeEventWithoutTTL() - e := addTTLToEvent(*noTTL) - return &e -} - -func addTTLToEvent(e cloudevents.Event) cloudevents.Event { - broker.SetTTL(e.Context, 1) - return e -} - -func makeDifferentEvent() *cloudevents.Event { - return &cloudevents.Event{ - Context: cloudevents.EventContextV02{ - Type: "some-other-type", - Source: cloudevents.URLRef{ - URL: url.URL{ - Path: eventSource, - }, - }, - ContentType: cloudevents.StringOfApplicationJSON(), - }.AsV1(), - } -} - -func makeEventWithExtension(extName, extValue string) *cloudevents.Event { - noTTL := &cloudevents.Event{ - Context: cloudevents.EventContextV02{ - Type: eventType, - Source: cloudevents.URLRef{ - URL: url.URL{ - Path: eventSource, - }, - }, - ContentType: cloudevents.StringOfApplicationJSON(), - Extensions: map[string]interface{}{ - extName: extValue, - }, - }.AsV1(), - } - e := addTTLToEvent(*noTTL) - return &e -} diff --git a/pkg/broker/filter/stats_reporter.go b/pkg/broker/filter/stats_reporter.go deleted file mode 100644 index 2d8869c96e4..00000000000 --- a/pkg/broker/filter/stats_reporter.go +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 filter - -import ( - "context" - "log" - "strconv" - "time" - - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" - "go.opencensus.io/tag" - "knative.dev/eventing/pkg/broker" - "knative.dev/pkg/metrics" - "knative.dev/pkg/metrics/metricskey" -) - -const ( - // anyValue is the default value if the trigger filter attributes are empty. - anyValue = "any" -) - -var ( - // eventCountM is a counter which records the number of events received - // by a Trigger. - eventCountM = stats.Int64( - "event_count", - "Number of events received by a Trigger", - stats.UnitDimensionless, - ) - - // dispatchTimeInMsecM records the time spent dispatching an event to - // a Trigger subscriber, in milliseconds. - dispatchTimeInMsecM = stats.Float64( - "event_dispatch_latencies", - "The time spent dispatching an event to a Trigger subscriber", - stats.UnitMilliseconds, - ) - - // processingTimeInMsecM records the time spent between arrival at the Broker - // and the delivery to the Trigger subscriber. - processingTimeInMsecM = stats.Float64( - "event_processing_latencies", - "The time spent processing an event before it is dispatched to a Trigger subscriber", - stats.UnitMilliseconds, - ) - - // Create the tag keys that will be used to add tags to our measurements. - // Tag keys must conform to the restrictions described in - // go.opencensus.io/tag/validate.go. Currently those restrictions are: - // - length between 1 and 255 inclusive - // - characters are printable US-ASCII - namespaceKey = tag.MustNewKey(metricskey.LabelNamespaceName) - triggerKey = tag.MustNewKey(metricskey.LabelTriggerName) - brokerKey = tag.MustNewKey(metricskey.LabelBrokerName) - triggerFilterTypeKey = tag.MustNewKey(metricskey.LabelFilterType) - responseCodeKey = tag.MustNewKey(metricskey.LabelResponseCode) - responseCodeClassKey = tag.MustNewKey(metricskey.LabelResponseCodeClass) -) - -type ReportArgs struct { - Namespace string - Trigger string - Broker string - FilterType string -} - -func init() { - register() -} - -// StatsReporter defines the interface for sending filter metrics. -type StatsReporter interface { - ReportEventCount(args *ReportArgs, responseCode int) error - ReportEventDispatchTime(args *ReportArgs, responseCode int, d time.Duration) error - ReportEventProcessingTime(args *ReportArgs, d time.Duration) error -} - -var _ StatsReporter = (*reporter)(nil) -var emptyContext = context.Background() - -// reporter holds cached metric objects to report filter metrics. -type reporter struct { - container string - uniqueName string -} - -// NewStatsReporter creates a reporter that collects and reports filter metrics. -func NewStatsReporter(container, uniqueName string) StatsReporter { - return &reporter{ - container: container, - uniqueName: uniqueName, - } -} - -func register() { - // Create view to see our measurements. - err := view.Register( - &view.View{ - Description: eventCountM.Description(), - Measure: eventCountM, - Aggregation: view.Count(), - TagKeys: []tag.Key{namespaceKey, triggerKey, brokerKey, triggerFilterTypeKey, responseCodeKey, responseCodeClassKey, broker.UniqueTagKey, broker.ContainerTagKey}, - }, - &view.View{ - Description: dispatchTimeInMsecM.Description(), - Measure: dispatchTimeInMsecM, - Aggregation: view.Distribution(metrics.Buckets125(1, 10000)...), // 1, 2, 5, 10, 20, 50, 100, 1000, 5000, 10000 - TagKeys: []tag.Key{namespaceKey, triggerKey, brokerKey, triggerFilterTypeKey, responseCodeKey, responseCodeClassKey, broker.UniqueTagKey, broker.ContainerTagKey}, - }, - &view.View{ - Description: processingTimeInMsecM.Description(), - Measure: processingTimeInMsecM, - Aggregation: view.Distribution(metrics.Buckets125(1, 10000)...), // 1, 2, 5, 10, 20, 50, 100, 1000, 5000, 10000 - TagKeys: []tag.Key{namespaceKey, triggerKey, brokerKey, triggerFilterTypeKey, broker.UniqueTagKey, broker.ContainerTagKey}, - }, - ) - if err != nil { - log.Printf("failed to register opencensus views, %s", err) - } -} - -// ReportEventCount captures the event count. -func (r *reporter) ReportEventCount(args *ReportArgs, responseCode int) error { - ctx, err := r.generateTag(args, - tag.Insert(responseCodeKey, strconv.Itoa(responseCode)), - tag.Insert(responseCodeClassKey, metrics.ResponseCodeClass(responseCode))) - if err != nil { - return err - } - metrics.Record(ctx, eventCountM.M(1)) - return nil -} - -// ReportEventDispatchTime captures dispatch times. -func (r *reporter) ReportEventDispatchTime(args *ReportArgs, responseCode int, d time.Duration) error { - ctx, err := r.generateTag(args, - tag.Insert(responseCodeKey, strconv.Itoa(responseCode)), - tag.Insert(responseCodeClassKey, metrics.ResponseCodeClass(responseCode))) - if err != nil { - return err - } - // convert time.Duration in nanoseconds to milliseconds. - metrics.Record(ctx, dispatchTimeInMsecM.M(float64(d/time.Millisecond))) - return nil -} - -// ReportEventProcessingTime captures event processing times. -func (r *reporter) ReportEventProcessingTime(args *ReportArgs, d time.Duration) error { - ctx, err := r.generateTag(args) - if err != nil { - return err - } - - // convert time.Duration in nanoseconds to milliseconds. - metrics.Record(ctx, processingTimeInMsecM.M(float64(d/time.Millisecond))) - return nil -} - -func (r *reporter) generateTag(args *ReportArgs, tags ...tag.Mutator) (context.Context, error) { - // Note that FilterType and filterSource can be empty strings, so they need a special treatment. - ctx, err := tag.New( - emptyContext, - tag.Insert(broker.ContainerTagKey, r.container), - tag.Insert(broker.UniqueTagKey, r.uniqueName), - tag.Insert(namespaceKey, args.Namespace), - tag.Insert(triggerKey, args.Trigger), - tag.Insert(brokerKey, args.Broker), - tag.Insert(triggerFilterTypeKey, valueOrAny(args.FilterType))) - if err != nil { - return nil, err - } - for _, t := range tags { - ctx, err = tag.New(ctx, t) - if err != nil { - return nil, err - } - } - return ctx, err -} - -func valueOrAny(v string) string { - if v != "" { - return v - } - return anyValue -} diff --git a/pkg/broker/filter/stats_reporter_test.go b/pkg/broker/filter/stats_reporter_test.go deleted file mode 100644 index a6733ab3ce2..00000000000 --- a/pkg/broker/filter/stats_reporter_test.go +++ /dev/null @@ -1,142 +0,0 @@ -/* -Copyright 2019 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 - - http://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 filter - -import ( - "net/http" - "testing" - "time" - - "knative.dev/eventing/pkg/broker" - "knative.dev/pkg/metrics/metricskey" - "knative.dev/pkg/metrics/metricstest" - _ "knative.dev/pkg/metrics/testing" -) - -func TestStatsReporter(t *testing.T) { - setup() - args := &ReportArgs{ - Namespace: "testns", - Trigger: "testtrigger", - Broker: "testbroker", - FilterType: "testeventtype", - } - - r := NewStatsReporter("testcontainer", "testpod") - - wantTags := map[string]string{ - metricskey.LabelNamespaceName: "testns", - metricskey.LabelTriggerName: "testtrigger", - metricskey.LabelBrokerName: "testbroker", - metricskey.LabelFilterType: "testeventtype", - broker.LabelContainerName: "testcontainer", - broker.LabelUniqueName: "testpod", - } - - wantAllTags := map[string]string{} - for k, v := range wantTags { - wantAllTags[k] = v - } - wantAllTags[metricskey.LabelResponseCode] = "202" - wantAllTags[metricskey.LabelResponseCodeClass] = "2xx" - - // test ReportEventCount - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - metricstest.CheckCountData(t, "event_count", wantAllTags, 2) - - // test ReportEventDispatchTime - expectSuccess(t, func() error { - return r.ReportEventDispatchTime(args, http.StatusAccepted, 1100*time.Millisecond) - }) - expectSuccess(t, func() error { - return r.ReportEventDispatchTime(args, http.StatusAccepted, 9100*time.Millisecond) - }) - metricstest.CheckDistributionData(t, "event_dispatch_latencies", wantAllTags, 2, 1100.0, 9100.0) - - // test ReportEventProcessingTime - expectSuccess(t, func() error { - return r.ReportEventProcessingTime(args, 1000*time.Millisecond) - }) - expectSuccess(t, func() error { - return r.ReportEventProcessingTime(args, 8000*time.Millisecond) - }) - metricstest.CheckDistributionData(t, "event_processing_latencies", wantTags, 2, 1000.0, 8000.0) -} - -func TestReporterEmptySourceAndTypeFilter(t *testing.T) { - setup() - - args := &ReportArgs{ - Namespace: "testns", - Trigger: "testtrigger", - Broker: "testbroker", - FilterType: "", - } - - r := NewStatsReporter("testcontainer", "testpod") - - wantTags := map[string]string{ - metricskey.LabelNamespaceName: "testns", - metricskey.LabelTriggerName: "testtrigger", - metricskey.LabelBrokerName: "testbroker", - metricskey.LabelFilterType: anyValue, - metricskey.LabelResponseCode: "202", - metricskey.LabelResponseCodeClass: "2xx", - broker.LabelContainerName: "testcontainer", - broker.LabelUniqueName: "testpod", - } - - // test ReportEventCount - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - metricstest.CheckCountData(t, "event_count", wantTags, 4) -} - -func expectSuccess(t *testing.T, f func() error) { - t.Helper() - if err := f(); err != nil { - t.Errorf("Reporter expected success but got error: %v", err) - } -} - -func setup() { - resetMetrics() -} - -func resetMetrics() { - // OpenCensus metrics carry global state that need to be reset between unit tests. - metricstest.Unregister( - "event_count", - "event_dispatch_latencies", - "event_processing_latencies") - register() -} diff --git a/pkg/broker/ingress/ingress_handler.go b/pkg/broker/ingress/ingress_handler.go deleted file mode 100644 index 53a2fc02bf8..00000000000 --- a/pkg/broker/ingress/ingress_handler.go +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 ingress - -import ( - "context" - "errors" - "net/http" - "net/url" - "time" - - cloudevents "github.com/cloudevents/sdk-go" - "github.com/cloudevents/sdk-go/pkg/cloudevents/client" - "go.opencensus.io/trace" - "go.uber.org/zap" - "k8s.io/apimachinery/pkg/types" - - "knative.dev/eventing/pkg/broker" - "knative.dev/eventing/pkg/tracing" - "knative.dev/eventing/pkg/utils" - "knative.dev/pkg/network" -) - -type Handler struct { - Logger *zap.Logger - CeClient cloudevents.Client - ChannelURI *url.URL - BrokerName string - Namespace string - Reporter StatsReporter - - Defaulter client.EventDefaulter -} - -func (h *Handler) Start(ctx context.Context) error { - ctx, cancel := context.WithCancel(ctx) - defer cancel() - - errCh := make(chan error, 1) - go func() { - errCh <- h.CeClient.StartReceiver(ctx, h.receive) - }() - - // Stop either if the receiver stops (sending to errCh) or if stopCh is closed. - select { - case err := <-errCh: - return err - case <-ctx.Done(): - break - } - - // stopCh has been closed, we need to gracefully shutdown h.ceClient. cancel() will start its - // shutdown, if it hasn't finished in a reasonable amount of time, just return an error. - cancel() - select { - case err := <-errCh: - return err - case <-time.After(network.DefaultDrainTimeout): - return errors.New("timeout shutting down ceClient") - } -} - -func (h *Handler) receive(ctx context.Context, event cloudevents.Event, resp *cloudevents.EventResponse) error { - brokerName := types.NamespacedName{ - Name: h.BrokerName, - Namespace: h.Namespace, - } - ctx, span := trace.StartSpan(ctx, tracing.BrokerMessagingDestination(brokerName)) - defer span.End() - - if span.IsRecordingEvents() { - span.AddAttributes( - tracing.MessagingSystemAttribute, - tracing.MessagingProtocolHTTP, - tracing.BrokerMessagingDestinationAttribute(brokerName), - tracing.MessagingMessageIDAttribute(event.ID()), - ) - } - - // Setting the extension as a string as the CloudEvents sdk does not support non-string extensions. - event.SetExtension(broker.EventArrivalTime, cloudevents.Timestamp{Time: time.Now()}) - tctx := cloudevents.HTTPTransportContextFrom(ctx) - if tctx.Method != http.MethodPost { - resp.Status = http.StatusMethodNotAllowed - return nil - } - - // tctx.URI is actually the request uri... - if tctx.URI != "/" { - resp.Status = http.StatusNotFound - return nil - } - - reporterArgs := &ReportArgs{ - Namespace: h.Namespace, - Broker: h.BrokerName, - EventType: event.Type(), - } - - if h.Defaulter != nil { - event = h.Defaulter(ctx, event) - } - - if ttl, err := broker.GetTTL(event.Context); err != nil || ttl <= 0 { - h.Logger.Debug("dropping event based on TTL status.", - zap.Int32("TTL", ttl), - zap.String("event.id", event.ID()), - zap.Error(err)) - // Record the event count. - h.Reporter.ReportEventCount(reporterArgs, http.StatusBadRequest) - return nil - } - - start := time.Now() - sendingCTX := utils.SendingContextFrom(ctx, tctx, h.ChannelURI) - - rctx, _, err := h.CeClient.Send(sendingCTX, event) - rtctx := cloudevents.HTTPTransportContextFrom(rctx) - // Record the dispatch time. - h.Reporter.ReportEventDispatchTime(reporterArgs, rtctx.StatusCode, time.Since(start)) - // Record the event count. - h.Reporter.ReportEventCount(reporterArgs, rtctx.StatusCode) - return err -} diff --git a/pkg/broker/ingress/ingress_handler_test.go b/pkg/broker/ingress/ingress_handler_test.go deleted file mode 100644 index f1284da2566..00000000000 --- a/pkg/broker/ingress/ingress_handler_test.go +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 ingress - -import ( - "context" - nethttp "net/http" - "net/url" - "reflect" - "sync" - "testing" - "time" - - cloudevents "github.com/cloudevents/sdk-go" - "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http" - "go.uber.org/zap" - "go.uber.org/zap/zaptest" - - "knative.dev/eventing/pkg/broker" -) - -const ( - namespace = "testNamespace" - brokerName = "testBroker" - validURI = "/" - urlHost = "testHost" - urlPath = "/" - urlScheme = "http" - validHTTPMethod = nethttp.MethodPost -) - -type mockReporter struct { - eventCountReported bool - eventDispatchTimeReported bool -} - -func (r *mockReporter) ReportEventCount(args *ReportArgs, responseCode int) error { - r.eventCountReported = true - return nil -} - -func (r *mockReporter) ReportEventDispatchTime(args *ReportArgs, responseCode int, d time.Duration) error { - r.eventDispatchTimeReported = true - return nil -} - -type fakeClient struct { - sent bool - fn interface{} - mux sync.Mutex -} - -func (f *fakeClient) Send(ctx context.Context, event cloudevents.Event) (context.Context, *cloudevents.Event, error) { - f.sent = true - return ctx, &event, nil -} - -func (f *fakeClient) StartReceiver(ctx context.Context, fn interface{}) error { - f.mux.Lock() - f.fn = fn - f.mux.Unlock() - <-ctx.Done() - return nil -} - -func (f *fakeClient) ready() bool { - f.mux.Lock() - ready := f.fn != nil - f.mux.Unlock() - return ready -} - -func (f *fakeClient) fakeReceive(t *testing.T, event cloudevents.Event) { - // receive(ctx context.Context, event cloudevents.Event, resp *cloudevents.EventResponse) error - - resp := new(cloudevents.EventResponse) - tctx := http.TransportContext{Header: nethttp.Header{}, Method: validHTTPMethod, URI: validURI} - ctx := http.WithTransportContext(context.Background(), tctx) - - fnType := reflect.TypeOf(f.fn) - if fnType.Kind() != reflect.Func { - t.Fatal("wrong method type.", fnType.Kind()) - } - - fn := reflect.ValueOf(f.fn) - _ = fn.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(event), reflect.ValueOf(resp)}) -} - -func TestIngressHandler_Receive_FAIL(t *testing.T) { - testCases := map[string]struct { - httpmethod string - URI string - expectedStatus int - expectedEventCount bool - expectedEventDispatchTime bool - }{ - "method not allowed": { - httpmethod: nethttp.MethodGet, - URI: validURI, - expectedStatus: nethttp.StatusMethodNotAllowed, - }, - "invalid url": { - httpmethod: validHTTPMethod, - URI: "invalidURI", - expectedStatus: nethttp.StatusNotFound, - }, - } - - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - client, _ := cloudevents.NewDefaultClient() - reporter := &mockReporter{} - handler := Handler{ - Logger: zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())), - CeClient: client, - ChannelURI: &url.URL{ - Scheme: urlScheme, - Host: urlHost, - Path: urlPath, - }, - BrokerName: brokerName, - Namespace: namespace, - Reporter: reporter, - Defaulter: broker.TTLDefaulter(zap.NewNop(), 5), - } - event := cloudevents.NewEvent(cloudevents.VersionV1) - resp := new(cloudevents.EventResponse) - tctx := http.TransportContext{Header: nethttp.Header{}, Method: tc.httpmethod, URI: tc.URI} - ctx := http.WithTransportContext(context.Background(), tctx) - _ = handler.receive(ctx, event, resp) - if resp.Status != tc.expectedStatus { - t.Errorf("Unexpected status code. Expected %v, Actual %v", tc.expectedStatus, resp.Status) - } - if reporter.eventCountReported != tc.expectedEventCount { - t.Errorf("Unexpected event count reported. Expected %v, Actual %v", tc.expectedEventCount, reporter.eventCountReported) - } - if reporter.eventDispatchTimeReported != tc.expectedEventDispatchTime { - t.Errorf("Unexpected event dispatch time reported. Expected %v, Actual %v", tc.expectedEventDispatchTime, reporter.eventDispatchTimeReported) - } - }) - } -} - -func TestIngressHandler_Receive_Succeed(t *testing.T) { - client := &fakeClient{} - reporter := &mockReporter{} - handler := Handler{ - Logger: zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())), - CeClient: client, - ChannelURI: &url.URL{ - Scheme: urlScheme, - Host: urlHost, - Path: urlPath, - }, - BrokerName: brokerName, - Namespace: namespace, - Reporter: reporter, - Defaulter: broker.TTLDefaulter(zap.NewNop(), 5), - } - - event := cloudevents.NewEvent() - resp := new(cloudevents.EventResponse) - tctx := http.TransportContext{Header: nethttp.Header{}, Method: validHTTPMethod, URI: validURI} - ctx := http.WithTransportContext(context.Background(), tctx) - _ = handler.receive(ctx, event, resp) - - if !client.sent { - t.Errorf("client should invoke send function") - } - if !reporter.eventCountReported { - t.Errorf("event count should have been reported") - } - if !reporter.eventDispatchTimeReported { - t.Errorf("event dispatch time should have been reported") - } -} - -func TestIngressHandler_Receive_NoTTL(t *testing.T) { - client := &fakeClient{} - reporter := &mockReporter{} - handler := Handler{ - Logger: zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())), - CeClient: client, - ChannelURI: &url.URL{ - Scheme: urlScheme, - Host: urlHost, - Path: urlPath, - }, - BrokerName: brokerName, - Namespace: namespace, - Reporter: reporter, - } - event := cloudevents.NewEvent(cloudevents.VersionV1) - resp := new(cloudevents.EventResponse) - tctx := http.TransportContext{Header: nethttp.Header{}, Method: validHTTPMethod, URI: validURI} - ctx := http.WithTransportContext(context.Background(), tctx) - _ = handler.receive(ctx, event, resp) - if client.sent { - t.Errorf("client should NOT invoke send function") - } - if !reporter.eventCountReported { - t.Errorf("event count should have been reported") - } -} - -func TestIngressHandler_Start(t *testing.T) { - client := &fakeClient{} - reporter := &mockReporter{} - handler := Handler{ - Logger: zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller())), - CeClient: client, - ChannelURI: &url.URL{ - Scheme: urlScheme, - Host: urlHost, - Path: urlPath, - }, - BrokerName: brokerName, - Namespace: namespace, - Reporter: reporter, - Defaulter: broker.TTLDefaulter(zap.NewNop(), 5), - } - - ctx, cancel := context.WithCancel(context.Background()) - go func() { - if err := handler.Start(ctx); err != nil { - t.Error(err) - } - }() - // Need time for the handler to start up. Wait. - for !client.ready() { - time.Sleep(1 * time.Millisecond) - } - - event := cloudevents.NewEvent() - client.fakeReceive(t, event) - cancel() - - if !client.sent { - t.Errorf("client should invoke send function") - } - if !reporter.eventCountReported { - t.Errorf("event count should have been reported") - } - if !reporter.eventDispatchTimeReported { - t.Errorf("event dispatch time should have been reported") - } -} diff --git a/pkg/broker/ingress/stats_reporter.go b/pkg/broker/ingress/stats_reporter.go deleted file mode 100644 index 6a2b83b91fa..00000000000 --- a/pkg/broker/ingress/stats_reporter.go +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 ingress - -import ( - "context" - "log" - "strconv" - "time" - - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" - "go.opencensus.io/tag" - "knative.dev/eventing/pkg/broker" - "knative.dev/pkg/metrics" - "knative.dev/pkg/metrics/metricskey" -) - -var ( - // eventCountM is a counter which records the number of events received - // by the Broker. - eventCountM = stats.Int64( - "event_count", - "Number of events received by a Broker", - stats.UnitDimensionless, - ) - - // dispatchTimeInMsecM records the time spent dispatching an event to - // a Channel, in milliseconds. - dispatchTimeInMsecM = stats.Float64( - "event_dispatch_latencies", - "The time spent dispatching an event to a Channel", - stats.UnitMilliseconds, - ) - - // Create the tag keys that will be used to add tags to our measurements. - // Tag keys must conform to the restrictions described in - // go.opencensus.io/tag/validate.go. Currently those restrictions are: - // - length between 1 and 255 inclusive - // - characters are printable US-ASCII - namespaceKey = tag.MustNewKey(metricskey.LabelNamespaceName) - brokerKey = tag.MustNewKey(metricskey.LabelBrokerName) - eventTypeKey = tag.MustNewKey(metricskey.LabelEventType) - responseCodeKey = tag.MustNewKey(metricskey.LabelResponseCode) - responseCodeClassKey = tag.MustNewKey(metricskey.LabelResponseCodeClass) -) - -type ReportArgs struct { - Namespace string - Broker string - EventType string -} - -func init() { - register() -} - -// StatsReporter defines the interface for sending ingress metrics. -type StatsReporter interface { - ReportEventCount(args *ReportArgs, responseCode int) error - ReportEventDispatchTime(args *ReportArgs, responseCode int, d time.Duration) error -} - -var _ StatsReporter = (*reporter)(nil) -var emptyContext = context.Background() - -// Reporter holds cached metric objects to report ingress metrics. -type reporter struct { - container string - uniqueName string -} - -// NewStatsReporter creates a reporter that collects and reports ingress metrics. -func NewStatsReporter(container, uniqueName string) StatsReporter { - return &reporter{ - container: container, - uniqueName: uniqueName, - } -} - -func register() { - tagKeys := []tag.Key{ - namespaceKey, - brokerKey, - eventTypeKey, - responseCodeKey, - responseCodeClassKey, - broker.ContainerTagKey, - broker.UniqueTagKey} - - // Create view to see our measurements. - err := view.Register( - &view.View{ - Description: eventCountM.Description(), - Measure: eventCountM, - Aggregation: view.Count(), - TagKeys: tagKeys, - }, - &view.View{ - Description: dispatchTimeInMsecM.Description(), - Measure: dispatchTimeInMsecM, - Aggregation: view.Distribution(metrics.Buckets125(1, 10000)...), // 1, 2, 5, 10, 20, 50, 100, 500, 1000, 5000, 10000 - TagKeys: tagKeys, - }, - ) - if err != nil { - log.Printf("failed to register opencensus views, %s", err) - } -} - -// ReportEventCount captures the event count. -func (r *reporter) ReportEventCount(args *ReportArgs, responseCode int) error { - ctx, err := r.generateTag(args, responseCode) - if err != nil { - return err - } - metrics.Record(ctx, eventCountM.M(1)) - return nil -} - -// ReportEventDispatchTime captures dispatch times. -func (r *reporter) ReportEventDispatchTime(args *ReportArgs, responseCode int, d time.Duration) error { - ctx, err := r.generateTag(args, responseCode) - if err != nil { - return err - } - // convert time.Duration in nanoseconds to milliseconds. - metrics.Record(ctx, dispatchTimeInMsecM.M(float64(d/time.Millisecond))) - return nil -} - -func (r *reporter) generateTag(args *ReportArgs, responseCode int) (context.Context, error) { - return tag.New( - emptyContext, - tag.Insert(broker.ContainerTagKey, r.container), - tag.Insert(broker.UniqueTagKey, r.uniqueName), - tag.Insert(namespaceKey, args.Namespace), - tag.Insert(brokerKey, args.Broker), - tag.Insert(eventTypeKey, args.EventType), - tag.Insert(responseCodeKey, strconv.Itoa(responseCode)), - tag.Insert(responseCodeClassKey, metrics.ResponseCodeClass(responseCode))) -} diff --git a/pkg/broker/ingress/stats_reporter_test.go b/pkg/broker/ingress/stats_reporter_test.go deleted file mode 100644 index 2370426f0b9..00000000000 --- a/pkg/broker/ingress/stats_reporter_test.go +++ /dev/null @@ -1,87 +0,0 @@ -/* -Copyright 2019 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 - - http://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 ingress - -import ( - "net/http" - "testing" - "time" - - "knative.dev/eventing/pkg/broker" - "knative.dev/pkg/metrics/metricskey" - "knative.dev/pkg/metrics/metricstest" - _ "knative.dev/pkg/metrics/testing" -) - -func TestStatsReporter(t *testing.T) { - setup() - - args := &ReportArgs{ - Namespace: "testns", - Broker: "testbroker", - EventType: "testeventtype", - } - - r := NewStatsReporter("testcontainer", "testpod") - - wantTags := map[string]string{ - metricskey.LabelNamespaceName: "testns", - metricskey.LabelBrokerName: "testbroker", - metricskey.LabelEventType: "testeventtype", - metricskey.LabelResponseCode: "202", - metricskey.LabelResponseCodeClass: "2xx", - broker.LabelUniqueName: "testpod", - broker.LabelContainerName: "testcontainer", - } - - // test ReportEventCount - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - expectSuccess(t, func() error { - return r.ReportEventCount(args, http.StatusAccepted) - }) - metricstest.CheckCountData(t, "event_count", wantTags, 2) - - // test ReportDispatchTime - expectSuccess(t, func() error { - return r.ReportEventDispatchTime(args, http.StatusAccepted, 1100*time.Millisecond) - }) - expectSuccess(t, func() error { - return r.ReportEventDispatchTime(args, http.StatusAccepted, 9100*time.Millisecond) - }) - metricstest.CheckDistributionData(t, "event_dispatch_latencies", wantTags, 2, 1100.0, 9100.0) -} - -func expectSuccess(t *testing.T, f func() error) { - t.Helper() - if err := f(); err != nil { - t.Errorf("Reporter expected success but got error: %v", err) - } -} - -func setup() { - resetMetrics() -} - -func resetMetrics() { - // OpenCensus metrics carry global state that need to be reset between unit tests. - metricstest.Unregister( - "event_count", - "event_dispatch_latencies") - register() -} diff --git a/pkg/broker/metrics.go b/pkg/broker/metrics.go deleted file mode 100644 index a2056c74a66..00000000000 --- a/pkg/broker/metrics.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 broker - -import "go.opencensus.io/tag" - -const ( - // EventArrivalTime is used to access the metadata stored on a - // CloudEvent to measure the time difference between when an event is - // received on a broker and before it is dispatched to the trigger function. - // The format is an RFC3339 time in string format. For example: 2019-08-26T23:38:17.834384404Z. - EventArrivalTime = "knativearrivaltime" - - // LabelUniqueName is the label for the unique name per stats_reporter instance. - LabelUniqueName = "unique_name" - - // LabelContainerName is the label for the immutable name of the container. - LabelContainerName = "container_name" -) - -var ( - ContainerTagKey = tag.MustNewKey(LabelContainerName) - UniqueTagKey = tag.MustNewKey(LabelUniqueName) -) diff --git a/pkg/broker/ttl.go b/pkg/broker/ttl.go deleted file mode 100644 index 7adc78ec0ad..00000000000 --- a/pkg/broker/ttl.go +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 broker - -import ( - "context" - - cloudevents "github.com/cloudevents/sdk-go" - "github.com/cloudevents/sdk-go/pkg/cloudevents/client" - cetypes "github.com/cloudevents/sdk-go/pkg/cloudevents/types" - "go.uber.org/zap" -) - -const ( - // TTLAttribute is the name of the CloudEvents extension attribute used to store the - // Broker's TTL (number of times a single event can reply through a Broker continuously). All - // interactions with the attribute should be done through the GetTTL and SetTTL functions. - TTLAttribute = "knativebrokerttl" -) - -// GetTTL finds the TTL in the EventContext using a case insensitive comparison -// for the key. The second return param, is the case preserved key that matched. -// Depending on the encoding/transport, the extension case could be changed. -func GetTTL(ctx cloudevents.EventContext) (int32, error) { - ttl, err := ctx.GetExtension(TTLAttribute) - if err != nil { - return 0, err - } - return cetypes.ToInteger(ttl) -} - -// SetTTL sets the TTL into the EventContext. ttl should be a positive integer. -func SetTTL(ctx cloudevents.EventContext, ttl int32) error { - return ctx.SetExtension(TTLAttribute, ttl) -} - -// DeleteTTL removes the TTL CE extension attribute -func DeleteTTL(ctx cloudevents.EventContext) error { - return ctx.SetExtension(TTLAttribute, nil) -} - -// TTLDefaulter returns a cloudevents event defaulter that will manage the TTL -// for events with the following rules: -// If TTL is not found, it will set it to the default passed in. -// If TTL is <= 0, it will remain 0. -// If TTL is > 1, it will be reduced by one. -func TTLDefaulter(logger *zap.Logger, defaultTTL int32) client.EventDefaulter { - return func(ctx context.Context, event cloudevents.Event) cloudevents.Event { - // Get the current or default TTL from the event. - var ttl int32 - if ttlraw, err := event.Context.GetExtension(TTLAttribute); err != nil { - logger.Debug("TTL not found in outbound event, defaulting.", - zap.String("event.id", event.ID()), - zap.Int32(TTLAttribute, defaultTTL), - zap.Error(err), - ) - ttl = defaultTTL - } else if ttl, err = cetypes.ToInteger(ttlraw); err != nil { - logger.Warn("Failed to convert existing TTL into integer, defaulting.", - zap.String("event.id", event.ID()), - zap.Any(TTLAttribute, ttlraw), - zap.Error(err), - ) - ttl = defaultTTL - } else { - // Decrement TTL. - ttl = ttl - 1 - if ttl < 0 { - ttl = 0 - } - } - // Overwrite the TTL into the event. - if err := event.Context.SetExtension(TTLAttribute, ttl); err != nil { - logger.Error("Failed to set TTL on outbound event.", - zap.String("event.id", event.ID()), - zap.Int32(TTLAttribute, ttl), - zap.Error(err), - ) - } - - return event - } -} diff --git a/pkg/broker/ttl_test.go b/pkg/broker/ttl_test.go deleted file mode 100644 index 3f7bab1962b..00000000000 --- a/pkg/broker/ttl_test.go +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2019 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 - * - * http://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 broker - -import ( - "context" - "testing" - - cloudevents "github.com/cloudevents/sdk-go" - "go.uber.org/zap" -) - -func TestTTLDefaulter(t *testing.T) { - defaultTTL := int32(10) - - defaulter := TTLDefaulter(zap.NewNop(), defaultTTL) - ctx := context.TODO() - - tests := map[string]struct { - event cloudevents.Event - want int32 - }{ - // TODO: Add test cases. - "happy empty": { - event: cloudevents.NewEvent(), - want: defaultTTL, - }, - "existing ttl of 10": { - event: func() cloudevents.Event { - event := cloudevents.NewEvent() - _ = SetTTL(event.Context, 10) - return event - }(), - want: 9, - }, - "existing ttl of 1": { - event: func() cloudevents.Event { - event := cloudevents.NewEvent() - _ = SetTTL(event.Context, 1) - return event - }(), - want: 0, - }, - "existing invalid ttl of 'XYZ'": { - event: func() cloudevents.Event { - event := cloudevents.NewEvent() - event.SetExtension(TTLAttribute, "XYZ") - return event - }(), - want: defaultTTL, - }, - "existing ttl of 0": { - event: func() cloudevents.Event { - event := cloudevents.NewEvent() - _ = SetTTL(event.Context, 0) - return event - }(), - want: 0, - }, - } - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - event := defaulter(ctx, tc.event) - got, err := GetTTL(event.Context) - if err != nil { - t.Error(err) - } - if got != tc.want { - t.Errorf("Unexpected TTL, wanted %d, got %d", tc.want, got) - } - }) - } -} diff --git a/pkg/mtbroker/filter/filter_handler.go b/pkg/mtbroker/filter/filter_handler.go index 3d660a257fc..7f1166ff424 100644 --- a/pkg/mtbroker/filter/filter_handler.go +++ b/pkg/mtbroker/filter/filter_handler.go @@ -28,10 +28,10 @@ import ( "go.opencensus.io/trace" "go.uber.org/zap" eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/broker" eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1" "knative.dev/eventing/pkg/kncloudevents" "knative.dev/eventing/pkg/logging" + broker "knative.dev/eventing/pkg/mtbroker" "knative.dev/eventing/pkg/reconciler/trigger/path" "knative.dev/eventing/pkg/tracing" "knative.dev/eventing/pkg/utils" diff --git a/pkg/mtbroker/filter/filter_handler_test.go b/pkg/mtbroker/filter/filter_handler_test.go index 39d727dd7c3..40b47baf9f4 100644 --- a/pkg/mtbroker/filter/filter_handler_test.go +++ b/pkg/mtbroker/filter/filter_handler_test.go @@ -37,7 +37,7 @@ import ( "k8s.io/client-go/kubernetes/scheme" eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" reconcilertesting "knative.dev/eventing/pkg/reconciler/testing" "knative.dev/eventing/pkg/utils" "knative.dev/pkg/apis" diff --git a/pkg/mtbroker/filter/stats_reporter.go b/pkg/mtbroker/filter/stats_reporter.go index 04b40ad9cb9..4e9ca187027 100644 --- a/pkg/mtbroker/filter/stats_reporter.go +++ b/pkg/mtbroker/filter/stats_reporter.go @@ -25,7 +25,7 @@ import ( "go.opencensus.io/stats" "go.opencensus.io/stats/view" "go.opencensus.io/tag" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" "knative.dev/pkg/metrics" "knative.dev/pkg/metrics/metricskey" ) diff --git a/pkg/mtbroker/filter/stats_reporter_test.go b/pkg/mtbroker/filter/stats_reporter_test.go index 88bf9d3da18..f003d1024c6 100644 --- a/pkg/mtbroker/filter/stats_reporter_test.go +++ b/pkg/mtbroker/filter/stats_reporter_test.go @@ -21,7 +21,7 @@ import ( "testing" "time" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" "knative.dev/pkg/metrics/metricskey" "knative.dev/pkg/metrics/metricstest" _ "knative.dev/pkg/metrics/testing" diff --git a/pkg/mtbroker/ingress/ingress_handler.go b/pkg/mtbroker/ingress/ingress_handler.go index 617189b6037..726d66ff6ac 100644 --- a/pkg/mtbroker/ingress/ingress_handler.go +++ b/pkg/mtbroker/ingress/ingress_handler.go @@ -30,7 +30,7 @@ import ( "go.opencensus.io/trace" "go.uber.org/zap" "k8s.io/apimachinery/pkg/types" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" "knative.dev/eventing/pkg/tracing" "knative.dev/eventing/pkg/utils" "knative.dev/pkg/network" diff --git a/pkg/mtbroker/ingress/ingress_handler_test.go b/pkg/mtbroker/ingress/ingress_handler_test.go index fc9ce63ccfe..f751687c784 100644 --- a/pkg/mtbroker/ingress/ingress_handler_test.go +++ b/pkg/mtbroker/ingress/ingress_handler_test.go @@ -28,7 +28,7 @@ import ( "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http" "go.uber.org/zap" "go.uber.org/zap/zaptest" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" ) const ( diff --git a/pkg/mtbroker/ingress/stats_reporter.go b/pkg/mtbroker/ingress/stats_reporter.go index 75dc9ab7dbc..c521842e8b1 100644 --- a/pkg/mtbroker/ingress/stats_reporter.go +++ b/pkg/mtbroker/ingress/stats_reporter.go @@ -25,7 +25,7 @@ import ( "go.opencensus.io/stats" "go.opencensus.io/stats/view" "go.opencensus.io/tag" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" "knative.dev/pkg/metrics" "knative.dev/pkg/metrics/metricskey" ) diff --git a/pkg/mtbroker/ingress/stats_reporter_test.go b/pkg/mtbroker/ingress/stats_reporter_test.go index 35a1fbc76ff..ebbb832ab14 100644 --- a/pkg/mtbroker/ingress/stats_reporter_test.go +++ b/pkg/mtbroker/ingress/stats_reporter_test.go @@ -21,7 +21,7 @@ import ( "testing" "time" - "knative.dev/eventing/pkg/broker" + broker "knative.dev/eventing/pkg/mtbroker" "knative.dev/pkg/metrics/metricskey" "knative.dev/pkg/metrics/metricstest" _ "knative.dev/pkg/metrics/testing" diff --git a/pkg/reconciler/broker/broker.go b/pkg/reconciler/broker/broker.go deleted file mode 100644 index 65a3f764314..00000000000 --- a/pkg/reconciler/broker/broker.go +++ /dev/null @@ -1,506 +0,0 @@ -/* -Copyright 2018 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 - - http://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 broker - -import ( - "context" - "errors" - "fmt" - - "go.uber.org/zap" - v1 "k8s.io/api/apps/v1" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/equality" - apierrs "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/labels" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/kubernetes" - appsv1listers "k8s.io/client-go/listers/apps/v1" - corev1listers "k8s.io/client-go/listers/core/v1" - "knative.dev/pkg/apis" - "knative.dev/pkg/controller" - "knative.dev/pkg/kmeta" - "knative.dev/pkg/logging" - - duckv1alpha1 "knative.dev/eventing/pkg/apis/duck/v1alpha1" - duckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1" - "knative.dev/eventing/pkg/apis/eventing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - clientset "knative.dev/eventing/pkg/client/clientset/versioned" - brokerreconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1alpha1/broker" - duckv1 "knative.dev/pkg/apis/duck/v1" - pkgduckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1" - pkgduckv1beta1 "knative.dev/pkg/apis/duck/v1beta1" - - // Need this for Brokers since we reconcile them still as v1alpha1 - eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1" - eventingv1beta1listers "knative.dev/eventing/pkg/client/listers/eventing/v1beta1" - messaginglisters "knative.dev/eventing/pkg/client/listers/messaging/v1beta1" - "knative.dev/eventing/pkg/duck" - "knative.dev/eventing/pkg/reconciler/broker/resources" - "knative.dev/eventing/pkg/reconciler/names" - duckapis "knative.dev/pkg/apis/duck" - pkgreconciler "knative.dev/pkg/reconciler" - "knative.dev/pkg/resolver" -) - -const ( - // Name of the corev1.Events emitted from the Broker reconciliation process. - brokerReconciled = "BrokerReconciled" -) - -type Reconciler struct { - eventingClientSet clientset.Interface - dynamicClientSet dynamic.Interface - kubeClientSet kubernetes.Interface - - // listers index properties about resources - brokerLister eventinglisters.BrokerLister - serviceLister corev1listers.ServiceLister - endpointsLister corev1listers.EndpointsLister - deploymentLister appsv1listers.DeploymentLister - subscriptionLister messaginglisters.SubscriptionLister - triggerLister eventingv1beta1listers.TriggerLister - - channelableTracker duck.ListableTracker - - ingressImage string - ingressServiceAccountName string - filterImage string - filterServiceAccountName string - - // Dynamic tracker to track KResources. In particular, it tracks the dependency between Triggers and Sources. - kresourceTracker duck.ListableTracker - - // Dynamic tracker to track AddressableTypes. In particular, it tracks Trigger subscribers. - addressableTracker duck.ListableTracker - uriResolver *resolver.URIResolver - - // If specified, only reconcile brokers with these labels - brokerClass string -} - -// Check that our Reconciler implements Interface -var _ brokerreconciler.Interface = (*Reconciler)(nil) -var _ brokerreconciler.Finalizer = (*Reconciler)(nil) - -var brokerGVK = v1alpha1.SchemeGroupVersion.WithKind("Broker") - -// ReconcilerArgs are the arguments needed to create a broker.Reconciler. -type ReconcilerArgs struct { - IngressImage string - IngressServiceAccountName string - FilterImage string - FilterServiceAccountName string -} - -func newReconciledNormal(namespace, name string) pkgreconciler.Event { - return pkgreconciler.NewEvent(corev1.EventTypeNormal, brokerReconciled, "Broker reconciled: \"%s/%s\"", namespace, name) -} - -func (r *Reconciler) ReconcileKind(ctx context.Context, b *v1alpha1.Broker) pkgreconciler.Event { - filterSvc, err := r.reconcileKind(ctx, b) - - if b.Status.IsReady() { - // So, at this point the Broker is ready and everything should be solid - // for the triggers to act upon, so reconcile them. - te := r.reconcileTriggers(ctx, b, filterSvc) - if te != nil { - logging.FromContext(ctx).Error("Problem reconciling triggers", zap.Error(te)) - return fmt.Errorf("failed to reconcile triggers: %v", te) - } - } else { - // Broker is not ready, but propagate it's status to my triggers. - if te := r.propagateBrokerStatusToTriggers(ctx, b.Namespace, b.Name, &b.Status); te != nil { - return fmt.Errorf("Trigger reconcile failed: %v", te) - } - } - return err -} - -func (r *Reconciler) reconcileKind(ctx context.Context, b *v1alpha1.Broker) (kmeta.Accessor, pkgreconciler.Event) { - logging.FromContext(ctx).Debug("Reconciling", zap.Any("Broker", b)) - b.Status.InitializeConditions() - b.Status.ObservedGeneration = b.Generation - - // 1. Trigger Channel is created for all events. Triggers will Subscribe to this Channel. - // 2. Filter Deployment. - // 3. K8s Service that points to the Filter Deployment. - // 4. Ingress Deployment. - // 5. K8s Service that points to the Ingress Deployment. - chanMan, err := r.getChannelTemplate(ctx, b) - if err != nil { - b.Status.MarkTriggerChannelFailed("ChannelTemplateFailed", "Error on setting up the ChannelTemplate: %s", err) - return nil, err - } - - logging.FromContext(ctx).Info("Reconciling the trigger channel") - c, err := resources.NewChannel("trigger", b, &chanMan.template, TriggerChannelLabels(b.Name)) - if err != nil { - logging.FromContext(ctx).Error(fmt.Sprintf("Failed to create Trigger Channel object: %s/%s", chanMan.ref.Namespace, chanMan.ref.Name), zap.Error(err)) - return nil, err - } - - triggerChan, err := r.reconcileChannel(ctx, chanMan.inf, chanMan.ref, c, b) - if err != nil { - logging.FromContext(ctx).Error("Problem reconciling the trigger channel", zap.Error(err)) - b.Status.MarkTriggerChannelFailed("ChannelFailure", "%v", err) - return nil, fmt.Errorf("Failed to reconcile trigger channel: %v", err) - } - - if triggerChan.Status.Address == nil || triggerChan.Status.Address.URL == nil { - logging.FromContext(ctx).Debug("Trigger Channel does not have an address", zap.Any("triggerChan", triggerChan)) - b.Status.MarkTriggerChannelFailed("NoAddress", "Channel does not have an address.") - // Ok to return nil for error here, once channel address becomes available, this will get requeued. - return nil, nil - } - b.Status.TriggerChannel = &chanMan.ref - // We need to downconvert from v1beta1 -> v1alpha1 since broker only handles v1alpha1. - channelStatus := &duckv1alpha1.ChannelableStatus{AddressStatus: pkgduckv1alpha1.AddressStatus{Address: &pkgduckv1alpha1.Addressable{Addressable: pkgduckv1beta1.Addressable{URL: triggerChan.Status.Address.URL}}}} - b.Status.PropagateTriggerChannelReadiness(channelStatus) - - if err := r.reconcileFilterDeployment(ctx, b); err != nil { - logging.FromContext(ctx).Error("Problem reconciling filter Deployment", zap.Error(err)) - b.Status.MarkFilterFailed("DeploymentFailure", "%v", err) - return nil, err - } - filterEndpoints, err := r.reconcileFilterService(ctx, b) - if err != nil { - logging.FromContext(ctx).Error("Problem reconciling filter Service", zap.Error(err)) - b.Status.MarkFilterFailed("ServiceFailure", "%v", err) - return nil, err - } - b.Status.PropagateFilterAvailability(filterEndpoints) - - if err := r.reconcileIngressDeployment(ctx, b, triggerChan); err != nil { - logging.FromContext(ctx).Error("Problem reconciling ingress Deployment", zap.Error(err)) - b.Status.MarkIngressFailed("DeploymentFailure", "%v", err) - return nil, err - } - - ingressEndpoints, err := r.reconcileIngressService(ctx, b) - if err != nil { - logging.FromContext(ctx).Error("Problem reconciling ingress Service", zap.Error(err)) - b.Status.MarkIngressFailed("ServiceFailure", "%v", err) - return nil, err - } - b.Status.PropagateIngressAvailability(ingressEndpoints) - b.Status.SetAddress(&apis.URL{ - Scheme: "http", - Host: names.ServiceHostName(ingressEndpoints.GetName(), ingressEndpoints.GetNamespace()), - }) - - markDeprecated(&b.Status.Status, "SingleTenantChannelBrokerDeprecated", "Single Tenant Channel Brokers are deprecated and will be removed in release 0.16. Use Multi Tenant Channel Brokers instead.") - - // So, at this point the Broker is ready and everything should be solid - // for the triggers to act upon. - return filterEndpoints, nil -} - -func markDeprecated(s *duckv1.Status, reason, msg string) { - dc := apis.Condition{ - Type: "Deprecated", - Reason: reason, - Status: corev1.ConditionTrue, - Severity: apis.ConditionSeverityWarning, - Message: msg, - } - for i, c := range s.Conditions { - if c.Type == dc.Type { - s.Conditions[i] = dc - return - } - } - s.Conditions = append(s.Conditions, dc) -} - -type channelTemplate struct { - ref corev1.ObjectReference - inf dynamic.ResourceInterface - template messagingv1beta1.ChannelTemplateSpec -} - -func (r *Reconciler) getChannelTemplate(ctx context.Context, b *v1alpha1.Broker) (*channelTemplate, error) { - triggerChannelName := resources.BrokerChannelName(b.Name, "trigger") - ref := corev1.ObjectReference{ - Name: triggerChannelName, - Namespace: b.Namespace, - } - var template *messagingv1beta1.ChannelTemplateSpec - - if b.Spec.Config != nil { - if b.Spec.Config.Kind == "ConfigMap" && b.Spec.Config.APIVersion == "v1" { - if b.Spec.Config.Namespace == "" || b.Spec.Config.Name == "" { - logging.FromContext(ctx).Error("Broker.Spec.Config name and namespace are required", - zap.String("namespace", b.Namespace), zap.String("name", b.Name)) - return nil, errors.New("Broker.Spec.Config name and namespace are required") - } - cm, err := r.kubeClientSet.CoreV1().ConfigMaps(b.Spec.Config.Namespace).Get(b.Spec.Config.Name, metav1.GetOptions{}) - if err != nil { - return nil, err - } - // TODO: there are better ways to do this... - - if config, err := NewConfigFromConfigMapFunc(ctx)(cm); err != nil { - return nil, err - } else if config != nil { - template = &config.DefaultChannelTemplate - } - logging.FromContext(ctx).Info("Using channel template = ", template) - } else { - return nil, errors.New("Broker.Spec.Config configuration not supported, only [kind: ConfigMap, apiVersion: v1]") - } - } else if b.Spec.ChannelTemplate != nil { - template = b.Spec.ChannelTemplate - } else { - logging.FromContext(ctx).Error("Broker.Spec.ChannelTemplate is nil", - zap.String("namespace", b.Namespace), zap.String("name", b.Name)) - return nil, errors.New("Broker.Spec.ChannelTemplate is nil") - } - - if template == nil { - return nil, errors.New("failed to find channelTemplate") - } - ref.APIVersion = template.APIVersion - ref.Kind = template.Kind - - gvr, _ := meta.UnsafeGuessKindToResource(template.GetObjectKind().GroupVersionKind()) - - inf := r.dynamicClientSet.Resource(gvr).Namespace(b.Namespace) - if inf == nil { - return nil, fmt.Errorf("unable to create dynamic client for: %+v", template) - } - - track := r.channelableTracker.TrackInNamespace(b) - - // Start tracking the trigger channel. - if err := track(ref); err != nil { - return nil, fmt.Errorf("unable to track changes to the trigger Channel: %v", err) - } - return &channelTemplate{ - ref: ref, - inf: inf, - template: *template, - }, nil -} - -func (r *Reconciler) FinalizeKind(ctx context.Context, b *v1alpha1.Broker) pkgreconciler.Event { - if err := r.propagateBrokerStatusToTriggers(ctx, b.Namespace, b.Name, nil); err != nil { - return fmt.Errorf("Trigger reconcile failed: %v", err) - } - return newReconciledNormal(b.Namespace, b.Name) -} - -// reconcileFilterDeployment reconciles Broker's 'b' filter deployment. -func (r *Reconciler) reconcileFilterDeployment(ctx context.Context, b *v1alpha1.Broker) error { - expected := resources.MakeFilterDeployment(&resources.FilterArgs{ - Broker: b, - Image: r.filterImage, - ServiceAccountName: r.filterServiceAccountName, - }) - return r.reconcileDeployment(ctx, expected) -} - -// reconcileFilterService reconciles Broker's 'b' filter service. -func (r *Reconciler) reconcileFilterService(ctx context.Context, b *v1alpha1.Broker) (*corev1.Endpoints, error) { - expected := resources.MakeFilterService(b) - return r.reconcileService(ctx, expected) -} - -// reconcileChannel reconciles Broker's 'b' underlying channel. -func (r *Reconciler) reconcileChannel(ctx context.Context, channelResourceInterface dynamic.ResourceInterface, channelObjRef corev1.ObjectReference, newChannel *unstructured.Unstructured, b *v1alpha1.Broker) (*duckv1beta1.Channelable, error) { - lister, err := r.channelableTracker.ListerFor(channelObjRef) - if err != nil { - logging.FromContext(ctx).Error(fmt.Sprintf("Error getting lister for Channel: %s/%s", channelObjRef.Namespace, channelObjRef.Name), zap.Error(err)) - return nil, err - } - c, err := lister.ByNamespace(channelObjRef.Namespace).Get(channelObjRef.Name) - // If the resource doesn't exist, we'll create it - if err != nil { - if apierrs.IsNotFound(err) { - logging.FromContext(ctx).Debug(fmt.Sprintf("Creating Channel Object: %+v", newChannel)) - created, err := channelResourceInterface.Create(newChannel, metav1.CreateOptions{}) - if err != nil { - logging.FromContext(ctx).Error(fmt.Sprintf("Failed to create Channel: %s/%s", channelObjRef.Namespace, channelObjRef.Name), zap.Error(err)) - return nil, err - } - logging.FromContext(ctx).Info(fmt.Sprintf("Created Channel: %s/%s", channelObjRef.Namespace, channelObjRef.Name), zap.Any("NewChannel", newChannel)) - channelable := &duckv1beta1.Channelable{} - err = duckapis.FromUnstructured(created, channelable) - if err != nil { - logging.FromContext(ctx).Error(fmt.Sprintf("Failed to convert to Channelable Object: %s/%s", channelObjRef.Namespace, channelObjRef.Name), zap.Any("createdChannel", created), zap.Error(err)) - return nil, err - - } - return channelable, nil - } - logging.FromContext(ctx).Error(fmt.Sprintf("Failed to get Channel: %s/%s", channelObjRef.Namespace, channelObjRef.Name), zap.Error(err)) - return nil, err - } - logging.FromContext(ctx).Debug(fmt.Sprintf("Found Channel: %s/%s", channelObjRef.Namespace, channelObjRef.Name)) - channelable, ok := c.(*duckv1beta1.Channelable) - if !ok { - logging.FromContext(ctx).Error(fmt.Sprintf("Failed to convert to Channelable Object: %s/%s", channelObjRef.Namespace, channelObjRef.Name), zap.Error(err)) - return nil, err - } - return channelable, nil -} - -// TriggerChannelLabels are all the labels placed on the Trigger Channel for the given brokerName. This -// should only be used by Broker and Trigger code. -func TriggerChannelLabels(brokerName string) map[string]string { - return map[string]string{ - eventing.BrokerLabelKey: brokerName, - "eventing.knative.dev/brokerEverything": "true", - } -} - -// reconcileDeployment reconciles the K8s Deployment 'd'. -func (r *Reconciler) reconcileDeployment(ctx context.Context, d *v1.Deployment) error { - current, err := r.deploymentLister.Deployments(d.Namespace).Get(d.Name) - if apierrs.IsNotFound(err) { - _, err = r.kubeClientSet.AppsV1().Deployments(d.Namespace).Create(d) - if err != nil { - return err - } - } else if err != nil { - return err - } else if !equality.Semantic.DeepDerivative(d.Spec, current.Spec) { - // Don't modify the informers copy. - desired := current.DeepCopy() - desired.Spec = d.Spec - _, err = r.kubeClientSet.AppsV1().Deployments(desired.Namespace).Update(desired) - if err != nil { - return err - } - } - return nil -} - -// reconcileService reconciles the K8s Service 'svc'. -func (r *Reconciler) reconcileService(ctx context.Context, svc *corev1.Service) (*corev1.Endpoints, error) { - current, err := r.serviceLister.Services(svc.Namespace).Get(svc.Name) - if apierrs.IsNotFound(err) { - current, err = r.kubeClientSet.CoreV1().Services(svc.Namespace).Create(svc) - if err != nil { - return nil, err - } - } else if err != nil { - return nil, err - } - - // spec.clusterIP is immutable and is set on existing services. If we don't set this to the same value, we will - // encounter an error while updating. - svc.Spec.ClusterIP = current.Spec.ClusterIP - if !equality.Semantic.DeepDerivative(svc.Spec, current.Spec) { - // Don't modify the informers copy. - desired := current.DeepCopy() - desired.Spec = svc.Spec - _, err = r.kubeClientSet.CoreV1().Services(current.Namespace).Update(desired) - if err != nil { - return nil, err - } - } - - return r.endpointsLister.Endpoints(svc.Namespace).Get(svc.Name) -} - -// reconcileIngressDeploymentCRD reconciles the Ingress Deployment for a CRD backed channel. -func (r *Reconciler) reconcileIngressDeployment(ctx context.Context, b *v1alpha1.Broker, c *duckv1beta1.Channelable) error { - expected := resources.MakeIngressDeployment(&resources.IngressArgs{ - Broker: b, - Image: r.ingressImage, - ServiceAccountName: r.ingressServiceAccountName, - ChannelAddress: c.Status.Address.URL.Host, - }) - return r.reconcileDeployment(ctx, expected) -} - -// reconcileIngressService reconciles the Ingress Service. -func (r *Reconciler) reconcileIngressService(ctx context.Context, b *v1alpha1.Broker) (*corev1.Endpoints, error) { - expected := resources.MakeIngressService(b) - return r.reconcileService(ctx, expected) -} - -// reconcileTriggers reconciles the Triggers that are pointed to this broker -func (r *Reconciler) reconcileTriggers(ctx context.Context, b *v1alpha1.Broker, filterSvc kmeta.Accessor) error { - - // TODO: Figure out the labels stuff... If webhook does it, we can filter like this... - // Find all the Triggers that have been labeled as belonging to me - /* - triggers, err := r.triggerLister.Triggers(b.Namespace).List(labels.SelectorFromSet(brokerLabels(b.brokerClass))) - */ - triggers, err := r.triggerLister.Triggers(b.Namespace).List(labels.Everything()) - if err != nil { - return err - } - for _, t := range triggers { - if t.Spec.Broker == b.Name { - trigger := t.DeepCopy() - tErr := r.reconcileTrigger(ctx, b, trigger, filterSvc) - if tErr != nil { - logging.FromContext(ctx).Error("Reconciling trigger failed:", zap.String("name", t.Name), zap.Error(err)) - controller.GetEventRecorder(ctx).Eventf(trigger, corev1.EventTypeWarning, triggerReconcileFailed, "Trigger reconcile failed: %v", tErr) - } else { - controller.GetEventRecorder(ctx).Event(trigger, corev1.EventTypeNormal, triggerReconciled, "Trigger reconciled") - } - trigger.Status.ObservedGeneration = t.Generation - if _, updateStatusErr := r.updateTriggerStatus(ctx, trigger); updateStatusErr != nil { - logging.FromContext(ctx).Error("Failed to update Trigger status", zap.Error(updateStatusErr)) - controller.GetEventRecorder(ctx).Eventf(trigger, corev1.EventTypeWarning, triggerUpdateStatusFailed, "Failed to update Trigger's status: %v", updateStatusErr) - } - } - } - return nil -} - -/* TODO: Enable once we start filtering by classes of brokers -func brokerLabels(name string) map[string]string { - return map[string]string{ - brokerAnnotationKey: name, - } -} -*/ - -func (r *Reconciler) propagateBrokerStatusToTriggers(ctx context.Context, namespace, name string, bs *v1alpha1.BrokerStatus) error { - triggers, err := r.triggerLister.Triggers(namespace).List(labels.Everything()) - if err != nil { - return err - } - for _, t := range triggers { - if t.Spec.Broker == name { - // Don't modify informers copy - trigger := t.DeepCopy() - trigger.Status.InitializeConditions() - if bs == nil { - trigger.Status.MarkBrokerFailed("BrokerDoesNotExist", "Broker %q does not exist", name) - } else { - trigger.Status.PropagateBrokerCondition(bs.GetTopLevelCondition()) - } - if _, updateStatusErr := r.updateTriggerStatus(ctx, trigger); updateStatusErr != nil { - logging.FromContext(ctx).Error("Failed to update Trigger status", zap.Error(updateStatusErr)) - controller.GetEventRecorder(ctx).Eventf(trigger, corev1.EventTypeWarning, triggerUpdateStatusFailed, "Failed to update Trigger's status: %v", updateStatusErr) - return updateStatusErr - } - } - } - return nil -} diff --git a/pkg/reconciler/broker/broker_test.go b/pkg/reconciler/broker/broker_test.go deleted file mode 100644 index d8581aa0974..00000000000 --- a/pkg/reconciler/broker/broker_test.go +++ /dev/null @@ -1,1991 +0,0 @@ -/* -Copyright 2019 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 - - http://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 broker - -import ( - "context" - "fmt" - "testing" - - fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake" - fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" - fakedynamicclient "knative.dev/pkg/injection/clients/dynamicclient/fake" - - sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - "k8s.io/apimachinery/pkg/util/intstr" - "k8s.io/client-go/kubernetes/scheme" - - clientgotesting "k8s.io/client-go/testing" - eventingduckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1" - "knative.dev/eventing/pkg/apis/eventing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/eventing/pkg/client/injection/ducks/duck/v1beta1/channelable" - "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1alpha1/broker" - "knative.dev/eventing/pkg/duck" - "knative.dev/eventing/pkg/reconciler/broker/resources" - "knative.dev/eventing/pkg/utils" - "knative.dev/pkg/apis" - duckv1 "knative.dev/pkg/apis/duck/v1" - duckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1" - v1addr "knative.dev/pkg/client/injection/ducks/duck/v1/addressable" - "knative.dev/pkg/client/injection/ducks/duck/v1/conditions" - v1a1addr "knative.dev/pkg/client/injection/ducks/duck/v1alpha1/addressable" - v1b1addr "knative.dev/pkg/client/injection/ducks/duck/v1beta1/addressable" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - logtesting "knative.dev/pkg/logging/testing" - "knative.dev/pkg/resolver" - "knative.dev/pkg/system" - - _ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/trigger/fake" - . "knative.dev/eventing/pkg/reconciler/testing" - rtv1beta1 "knative.dev/eventing/pkg/reconciler/testing/v1beta1" - _ "knative.dev/pkg/client/injection/ducks/duck/v1/addressable/fake" - . "knative.dev/pkg/reconciler/testing" -) - -type channelType string - -const ( - testNS = "test-namespace" - brokerName = "test-broker" - - filterImage = "filter-image" - filterSA = "filter-SA" - ingressImage = "ingress-image" - ingressSA = "ingress-SA" - - filterContainerName = "filter" - ingressContainerName = "ingress" - - triggerChannel channelType = "TriggerChannel" - - triggerName = "test-trigger" - triggerUID = "test-trigger-uid" - triggerNameLong = "test-trigger-name-is-a-long-name" - triggerUIDLong = "cafed00d-cafed00d-cafed00d-cafed00d-cafed00d" - - subscriberURI = "http://example.com/subscriber/" - subscriberKind = "Service" - subscriberName = "subscriber-name" - subscriberGroup = "serving.knative.dev" - subscriberVersion = "v1" - - brokerGeneration = 79 - - pingSourceName = "test-ping-source" - testSchedule = "*/2 * * * *" - testData = "data" - sinkName = "testsink" - dependencyAnnotation = "{\"kind\":\"PingSource\",\"name\":\"test-ping-source\",\"apiVersion\":\"sources.knative.dev/v1alpha2\"}" - subscriberURIReference = "foo" - subscriberResolvedTargetURI = "http://example.com/subscriber/foo" - - k8sServiceResolvedURI = "http://subscriber-name.test-namespace.svc.cluster.local/" - currentGeneration = 1 - outdatedGeneration = 0 - - finalizerName = "brokers.eventing.knative.dev" -) - -var ( - trueVal = true - - testKey = fmt.Sprintf("%s/%s", testNS, brokerName) - - triggerChannelHostname = fmt.Sprintf("foo.bar.svc.%s", utils.GetClusterDomainName()) - - filterDeploymentName = fmt.Sprintf("%s-broker-filter", brokerName) - filterServiceName = fmt.Sprintf("%s-broker-filter", brokerName) - ingressDeploymentName = fmt.Sprintf("%s-broker-ingress", brokerName) - ingressServiceName = fmt.Sprintf("%s-broker", brokerName) - - subscriptionName = fmt.Sprintf("%s-%s-%s", brokerName, triggerName, triggerUID) - - subscriberAPIVersion = fmt.Sprintf("%s/%s", subscriberGroup, subscriberVersion) - subscriberGVK = metav1.GroupVersionKind{ - Group: subscriberGroup, - Version: subscriberVersion, - Kind: subscriberKind, - } - k8sServiceGVK = metav1.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service", - } - brokerDestv1 = duckv1.Destination{ - Ref: &duckv1.KReference{ - Name: sinkName, - Kind: "Broker", - APIVersion: "eventing.knative.dev/v1alpha1", - }, - } - sinkDNS = "sink.mynamespace.svc." + utils.GetClusterDomainName() - sinkURI = "http://" + sinkDNS - finalizerUpdatedEvent = Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "test-broker" finalizers`) -) - -func init() { - // Add types to scheme - _ = v1alpha1.AddToScheme(scheme.Scheme) - _ = v1beta1.AddToScheme(scheme.Scheme) - _ = duckv1alpha1.AddToScheme(scheme.Scheme) -} - -func TestReconcile(t *testing.T) { - table := TableTest{ - { - Name: "bad workqueue key", - // Make sure Reconcile handles bad keys. - Key: "too/many/parts", - }, { - Name: "key not found", - // Make sure Reconcile handles good keys that don't exist. - Key: "foo/not-found", - }, { - Name: "Broker not found", - Key: testKey, - }, { - Name: "Broker is being deleted", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerDeletionTimestamp), - }, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "BrokerReconciled", `Broker reconciled: "test-namespace/test-broker"`), - }, - }, { - Name: "nil channeltemplatespec", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithInitBrokerConditions), - }, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "Broker.Spec.ChannelTemplate is nil"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithInitBrokerConditions, - WithTriggerChannelFailed("ChannelTemplateFailed", "Error on setting up the ChannelTemplate: Broker.Spec.ChannelTemplate is nil")), - }}, - // This returns an internal error, so it emits an Error - WantErr: true, - }, { - Name: "Trigger Channel.Create error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - }, - WantCreates: []runtime.Object{ - createChannel(testNS, triggerChannel, false), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithInitBrokerConditions, - WithBrokerChannel(channel()), - WithTriggerChannelFailed("ChannelFailure", "inducing failure for create inmemorychannels")), - }}, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "inmemorychannels"), - }, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "Failed to reconcile trigger channel: %v", "inducing failure for create inmemorychannels"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantErr: true, - }, { - Name: "Trigger Channel.Create no address", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - }, - WantCreates: []runtime.Object{ - createChannel(testNS, triggerChannel, false), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithInitBrokerConditions, - WithBrokerChannel(channel()), - WithTriggerChannelFailed("NoAddress", "Channel does not have an address.")), - }}, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantEvents: []string{ - finalizerUpdatedEvent, - }, - }, { - Name: "Trigger Channel is not yet Addressable", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, false), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelFailed("NoAddress", "Channel does not have an address.")), - }}, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantEvents: []string{ - finalizerUpdatedEvent, - }, - }, { - Name: "Filter Deployment.Create error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "deployments"), - }, - WantCreates: []runtime.Object{ - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithFilterFailed("DeploymentFailure", "inducing failure for create deployments")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for create deployments"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantErr: true, - }, { - Name: "Filter Deployment.Update error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, "some-other-image", livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "deployments"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithFilterFailed("DeploymentFailure", "inducing failure for update deployments")), - }}, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for update deployments"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantErr: true, - }, { - Name: "Filter Service.Create error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "services"), - }, - WantCreates: []runtime.Object{ - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithFilterFailed("ServiceFailure", "inducing failure for create services")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for create services"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantErr: true, - }, { - Name: "Filter Service.Update error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(9090))), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "services"), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithFilterFailed("ServiceFailure", "inducing failure for update services")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for update services"), - }, - WantErr: true, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Ingress Deployment.Create error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "deployments"), - }, - WantCreates: []runtime.Object{ - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080)), - ), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithFilterAvailable(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithIngressFailed("DeploymentFailure", "inducing failure for create deployments")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for create deployments"), - }, - WantErr: true, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Ingress Deployment.Update error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerGeneration(brokerGeneration), - ), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(9090))), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "deployments"), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerGeneration(brokerGeneration), - WithBrokerStatusObservedGeneration(brokerGeneration), - WithTriggerChannelReady(), - WithFilterAvailable(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithIngressFailed("DeploymentFailure", "inducing failure for update deployments")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for update deployments"), - }, - WantErr: true, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Ingress Service.Create error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "services"), - }, - WantCreates: []runtime.Object{ - NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithFilterAvailable(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithIngressFailed("ServiceFailure", "inducing failure for create services")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for create services"), - }, - WantErr: true, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Ingress Service.Update error", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(9090))), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "services"), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithTriggerChannelReady(), - WithFilterAvailable(), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithIngressFailed("ServiceFailure", "inducing failure for update services")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "inducing failure for update services"), - }, - WantErr: true, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Successful Reconciliation", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(ingressServiceName, testNS, - WithEndpointsLabels(resources.IngressLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithBrokerReady, - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithDeprecatedStatus, - WithBrokerAddress(fmt.Sprintf("%s.%s.svc.%s", ingressServiceName, testNS, utils.GetClusterDomainName())), - ), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Successful Reconciliation, broker ignored because mismatching BrokerClass", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerClass("broker-class-mismatch")), - }, - }, { - Name: "Successful Reconciliation, status update fails", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(ingressServiceName, testNS, - WithEndpointsLabels(resources.IngressLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "brokers"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithBrokerReady, - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithDeprecatedStatus, - WithBrokerAddress(fmt.Sprintf("%s.%s.svc.%s", ingressServiceName, testNS, utils.GetClusterDomainName())), - ), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "UpdateFailed", `Failed to update status for "test-broker": inducing failure for update brokers`), - }, - WantErr: true, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Successful Reconciliation, with single trigger", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(ingressServiceName, testNS, - WithEndpointsLabels(resources.IngressLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - }, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerDependencyReady(), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerSubscribedUnknown("SubscriptionNotConfigured", "Subscription has not yet been reconciled."), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI)), - }, { - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithBrokerReady, - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithDeprecatedStatus, - WithBrokerAddress(fmt.Sprintf("%s.%s.svc.%s", ingressServiceName, testNS, utils.GetClusterDomainName())))}, - }, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - }, { - Name: "Fail Reconciliation, with single trigger, trigger status updated", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithInitBrokerConditions), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerDependencyReady(), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerSubscribedUnknown("SubscriptionNotConfigured", "Subscription has not yet been reconciled."), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI)), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerDependencyReady(), - rtv1beta1.WithTriggerSubscribedUnknown("SubscriptionNotConfigured", "Subscription has not yet been reconciled."), - rtv1beta1.WithTriggerBrokerFailed("ChannelTemplateFailed", "Error on setting up the ChannelTemplate: Broker.Spec.ChannelTemplate is nil"), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI)), - }, { - Object: NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithInitBrokerConditions, - WithTriggerChannelFailed("ChannelTemplateFailed", "Error on setting up the ChannelTemplate: Broker.Spec.ChannelTemplate is nil")), - }}, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeWarning, "InternalError", "Broker.Spec.ChannelTemplate is nil"), - }, - WantPatches: []clientgotesting.PatchActionImpl{ - patchFinalizers(testNS, brokerName), - }, - WantErr: true, - }, { - Name: "Broker being deleted, marks trigger as not ready due to broker missing", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerFinalizers("brokers.eventing.knative.dev"), - WithBrokerResourceVersion(""), - WithBrokerDeletionTimestamp), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "test-broker" does not exist`)), - }}, - WantPatches: []clientgotesting.PatchActionImpl{ - patchRemoveFinalizers(testNS, brokerName), - }, - WantEvents: []string{ - finalizerUpdatedEvent, - Eventf(corev1.EventTypeNormal, "BrokerReconciled", `Broker reconciled: "test-namespace/test-broker"`), - }, - }, { - Name: "Broker being deleted, marks trigger as not ready due to broker missing, fails", - Key: testKey, - Objects: []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerFinalizers("brokers.eventing.knative.dev"), - WithBrokerResourceVersion(""), - WithBrokerDeletionTimestamp), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "triggers"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerFailed("BrokerDoesNotExist", `Broker "test-broker" does not exist`)), - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "TriggerUpdateStatusFailed", `Failed to update Trigger's status: inducing failure for update triggers`), - Eventf(corev1.EventTypeWarning, "InternalError", "Trigger reconcile failed: inducing failure for update triggers"), - }, - WantErr: true, - }, { - Name: "Trigger being deleted", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerDeleted, - rtv1beta1.WithTriggerSubscriberURI(subscriberURI))}...), - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerDeleted, - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - }, { - Name: "Trigger subscription create fails", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI))}...), - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "subscriptions"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerNotSubscribed("NotSubscribed", "inducing failure for create subscriptions")), - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "SubscriptionCreateFailed", "Create Trigger's subscription failed: inducing failure for create subscriptions"), - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", "Trigger reconcile failed: inducing failure for create subscriptions"), - }, - }, { - Name: "Trigger subscription create fails, update status fails", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI))}...), - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "subscriptions"), - InduceFailure("update", "triggers"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerNotSubscribed("NotSubscribed", "inducing failure for create subscriptions")), - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "SubscriptionCreateFailed", "Create Trigger's subscription failed: inducing failure for create subscriptions"), - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", "Trigger reconcile failed: inducing failure for create subscriptions"), - Eventf(corev1.EventTypeWarning, "TriggerUpdateStatusFailed", "Failed to update Trigger's status: inducing failure for update triggers"), - }, - }, { - Name: "Trigger subscription delete fails", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - makeDifferentReadySubscription()}...), - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("delete", "subscriptions"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerNotSubscribed("NotSubscribed", "inducing failure for delete subscriptions"))}, - }, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: subscriptionName, - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "SubscriptionDeleteFailed", "Delete Trigger's subscription failed: inducing failure for delete subscriptions"), - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", "Trigger reconcile failed: inducing failure for delete subscriptions"), - }, - }, { - Name: "Trigger subscription create after delete fails", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - makeDifferentReadySubscription()}...), - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "subscriptions"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerNotSubscribed("NotSubscribed", "inducing failure for create subscriptions")), - }}, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: subscriptionName, - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "SubscriptionCreateFailed", "Create Trigger's subscription failed: inducing failure for create subscriptions"), - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", "Trigger reconcile failed: inducing failure for create subscriptions"), - }, - }, { - Name: "Trigger subscription not owned by Trigger", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - makeIngressSubscriptionNotOwnedByTrigger()}...), - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerNotSubscribed("NotSubscribed", `trigger "test-trigger" does not own subscription "test-broker-test-trigger-test-trigger-uid"`), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI)), - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", `Trigger reconcile failed: trigger "test-trigger" does not own subscription "test-broker-test-trigger-test-trigger-uid"`), - }, - }, { - Name: "Trigger subscription update works", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI)), - makeDifferentReadySubscription()}...), - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscriptionNotConfigured(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady()), - }}, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: subscriptionName, - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - }, { - Name: "Trigger has subscriber ref exists", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeSubscriberAddressableAsUnstructured(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), - rtv1beta1.WithInitTriggerConditions)}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscriptionNotConfigured(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - }, { - Name: "Trigger has subscriber ref exists and URI", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeSubscriberAddressableAsUnstructured(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRefAndURIReference(subscriberGVK, subscriberName, testNS, subscriberURIReference), - rtv1beta1.WithInitTriggerConditions, - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRefAndURIReference(subscriberGVK, subscriberName, testNS, subscriberURIReference), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscriptionNotConfigured(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberResolvedTargetURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - }, { - Name: "Trigger has subscriber ref exists kubernetes Service", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeSubscriberKubernetesServiceAsUnstructured(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRef(k8sServiceGVK, subscriberName, testNS), - rtv1beta1.WithInitTriggerConditions, - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRef(k8sServiceGVK, subscriberName, testNS), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscriptionNotConfigured(), - rtv1beta1.WithTriggerStatusSubscriberURI(k8sServiceResolvedURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - WantCreates: []runtime.Object{ - makeIngressSubscription(), - }, - }, { - Name: "Trigger has subscriber ref doesn't exist", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), - rtv1beta1.WithInitTriggerConditions, - )}...), - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", `Trigger reconcile failed: failed to get ref &ObjectReference{Kind:Service,Namespace:test-namespace,Name:subscriber-name,UID:,APIVersion:serving.knative.dev/v1,ResourceVersion:,FieldPath:,}: services.serving.knative.dev "subscriber-name" not found`), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberRef(subscriberGVK, subscriberName, testNS), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscriberResolvedFailed("Unable to get the Subscriber's URI", `failed to get ref &ObjectReference{Kind:Service,Namespace:test-namespace,Name:subscriber-name,UID:,APIVersion:serving.knative.dev/v1,ResourceVersion:,FieldPath:,}: services.serving.knative.dev "subscriber-name" not found`), - ), - }}, - }, { - Name: "Subscription not ready, trigger marked not ready", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeFalseStatusSubscription(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerNotSubscribed("testInducedError", "test induced error"), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - }, { - Name: "Subscription ready, trigger marked ready", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscription(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribed(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - }, { - Name: "Dependency doesn't exist", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscription(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - )}...), - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "TriggerReconcileFailed", "Trigger reconcile failed: propagating dependency readiness: getting the dependency: pingsources.sources.knative.dev \"test-ping-source\" not found"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribed(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyFailed("DependencyDoesNotExist", "Dependency does not exist: pingsources.sources.knative.dev \"test-ping-source\" not found"), - ), - }}, - }, { - Name: "The status of Dependency is False", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscription(), - makeFalseStatusPingSource(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled")}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribed(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyFailed("NotFound", ""), - ), - }}, - }, { - Name: "The status of Dependency is Unknown", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscription(), - makeUnknownStatusCronJobSource(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled")}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribed(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyUnknown("", ""), - ), - }}, - }, - { - Name: "Dependency generation not equal", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscription(), - makeGenerationNotEqualPingSource(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled")}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribed(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyUnknown("GenerationNotEqual", fmt.Sprintf("The dependency's metadata.generation, %q, is not equal to its status.observedGeneration, %q.", currentGeneration, outdatedGeneration))), - }}, - }, - { - Name: "Dependency ready", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscription(), - makeReadyPingSource(), - rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerName, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUID), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithDependencyAnnotation(dependencyAnnotation), - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribed(), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - }, { - Name: "Trigger has deprecated named subscriber", - Key: testKey, - Objects: allBrokerObjectsReadyPlus([]runtime.Object{ - makeReadySubscriptionDeprecatedName(triggerNameLong, triggerUIDLong), - rtv1beta1.NewTrigger(triggerNameLong, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUIDLong), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - rtv1beta1.WithInitTriggerConditions, - )}...), - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, subscriptionDeleted, `Deprecated subscription removed: "%s/%s"`, testNS, makeReadySubscriptionDeprecatedName(triggerNameLong, triggerUIDLong).Name), - Eventf(corev1.EventTypeNormal, "TriggerReconciled", "Trigger reconciled"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: rtv1beta1.NewTrigger(triggerNameLong, testNS, brokerName, - rtv1beta1.WithTriggerUID(triggerUIDLong), - rtv1beta1.WithTriggerSubscriberURI(subscriberURI), - // The first reconciliation will initialize the status conditions. - rtv1beta1.WithInitTriggerConditions, - rtv1beta1.WithTriggerBrokerReady(), - rtv1beta1.WithTriggerSubscribedUnknown("SubscriptionNotConfigured", "Subscription has not yet been reconciled."), - rtv1beta1.WithTriggerStatusSubscriberURI(subscriberURI), - rtv1beta1.WithTriggerSubscriberResolvedSucceeded(), - rtv1beta1.WithTriggerDependencyReady(), - ), - }}, - WantCreates: []runtime.Object{ - makeIngressSubscriptionWithCustomData(triggerNameLong, triggerUIDLong), - }, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: makeReadySubscriptionDeprecatedName(triggerNameLong, triggerUIDLong).Name, - }}, - }, - } - - logger := logtesting.TestLogger(t) - table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler { - ctx = channelable.WithDuck(ctx) - ctx = v1a1addr.WithDuck(ctx) - ctx = v1b1addr.WithDuck(ctx) - ctx = v1addr.WithDuck(ctx) - ctx = conditions.WithDuck(ctx) - r := &Reconciler{ - eventingClientSet: fakeeventingclient.Get(ctx), - dynamicClientSet: fakedynamicclient.Get(ctx), - kubeClientSet: fakekubeclient.Get(ctx), - subscriptionLister: listers.GetV1Beta1SubscriptionLister(), - triggerLister: listers.GetV1Beta1TriggerLister(), - brokerLister: listers.GetBrokerLister(), - serviceLister: listers.GetK8sServiceLister(), - endpointsLister: listers.GetEndpointsLister(), - deploymentLister: listers.GetDeploymentLister(), - filterImage: filterImage, - filterServiceAccountName: filterSA, - ingressImage: ingressImage, - ingressServiceAccountName: ingressSA, - kresourceTracker: duck.NewListableTracker(ctx, conditions.Get, func(types.NamespacedName) {}, 0), - channelableTracker: duck.NewListableTracker(ctx, channelable.Get, func(types.NamespacedName) {}, 0), - addressableTracker: duck.NewListableTracker(ctx, v1a1addr.Get, func(types.NamespacedName) {}, 0), - uriResolver: resolver.NewURIResolver(ctx, func(types.NamespacedName) {}), - brokerClass: eventing.ChannelBrokerClassValue, - } - return broker.NewReconciler(ctx, logger, - fakeeventingclient.Get(ctx), listers.GetBrokerLister(), - controller.GetEventRecorder(ctx), r, eventing.ChannelBrokerClassValue) - - }, - false, - logger, - )) -} - -func ownerReferences() []metav1.OwnerReference { - return []metav1.OwnerReference{{ - APIVersion: v1alpha1.SchemeGroupVersion.String(), - Kind: "Broker", - Name: brokerName, - Controller: &trueVal, - BlockOwnerDeletion: &trueVal, - }} -} - -func channel() metav1.TypeMeta { - return metav1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1alpha1", - Kind: "InMemoryChannel", - } -} - -func livenessProbe() *corev1.Probe { - return &corev1.Probe{ - Handler: corev1.Handler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/healthz", - Port: intstr.IntOrString{Type: intstr.Int, IntVal: 8080}, - }, - }, - InitialDelaySeconds: 5, - PeriodSeconds: 2, - } -} - -func readinessProbe() *corev1.Probe { - return &corev1.Probe{ - Handler: corev1.Handler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/readyz", - Port: intstr.IntOrString{Type: intstr.Int, IntVal: 8080}, - }, - }, - InitialDelaySeconds: 5, - PeriodSeconds: 2, - } -} - -func envVars(containerName string) []corev1.EnvVar { - switch containerName { - case filterContainerName: - return []corev1.EnvVar{ - { - Name: system.NamespaceEnvKey, - Value: system.Namespace(), - }, - { - Name: "NAMESPACE", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.namespace", - }, - }, - }, - { - Name: "POD_NAME", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.name", - }, - }, - }, - { - Name: "CONTAINER_NAME", - Value: filterContainerName, - }, - { - Name: "BROKER", - Value: brokerName, - }, - { - Name: "METRICS_DOMAIN", - Value: "knative.dev/internal/eventing", - }, - } - case ingressContainerName: - return []corev1.EnvVar{ - { - Name: system.NamespaceEnvKey, - Value: system.Namespace(), - }, - { - Name: "NAMESPACE", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.namespace", - }, - }, - }, - { - Name: "POD_NAME", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.name", - }, - }, - }, - { - Name: "CONTAINER_NAME", - Value: ingressContainerName, - }, - { - Name: "FILTER", - Value: "", - }, - { - Name: "CHANNEL", - Value: triggerChannelHostname, - }, - { - Name: "BROKER", - Value: brokerName, - }, - { - Name: "METRICS_DOMAIN", - Value: "knative.dev/internal/eventing", - }, - } - } - return []corev1.EnvVar{} -} - -func containerPorts(httpInternal int32) []corev1.ContainerPort { - return []corev1.ContainerPort{ - { - Name: "http", - ContainerPort: httpInternal, - }, - { - Name: "metrics", - ContainerPort: 9092, - }, - } -} - -func servicePorts(httpInternal int) []corev1.ServicePort { - svcPorts := []corev1.ServicePort{ - { - Name: "http", - Port: 80, - TargetPort: intstr.FromInt(httpInternal), - }, { - Name: "http-metrics", - Port: 9090, - }, - } - return svcPorts -} - -func createChannel(namespace string, t channelType, ready bool) *unstructured.Unstructured { - var labels map[string]interface{} - var name string - var hostname string - var url string - if t == triggerChannel { - name = fmt.Sprintf("%s-kne-trigger", brokerName) - labels = map[string]interface{}{ - eventing.BrokerLabelKey: brokerName, - "eventing.knative.dev/brokerEverything": "true", - } - hostname = triggerChannelHostname - url = fmt.Sprintf("http://%s", triggerChannelHostname) - } - if ready { - return &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "messaging.knative.dev/v1alpha1", - "kind": "InMemoryChannel", - "metadata": map[string]interface{}{ - "creationTimestamp": nil, - "namespace": namespace, - "name": name, - "ownerReferences": []interface{}{ - map[string]interface{}{ - "apiVersion": "eventing.knative.dev/v1alpha1", - "blockOwnerDeletion": true, - "controller": true, - "kind": "Broker", - "name": brokerName, - "uid": "", - }, - }, - "labels": labels, - }, - "status": map[string]interface{}{ - "address": map[string]interface{}{ - "hostname": hostname, - "url": url, - }, - }, - }, - } - } - - return &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "messaging.knative.dev/v1alpha1", - "kind": "InMemoryChannel", - "metadata": map[string]interface{}{ - "creationTimestamp": nil, - "namespace": namespace, - "name": name, - "ownerReferences": []interface{}{ - map[string]interface{}{ - "apiVersion": "eventing.knative.dev/v1alpha1", - "blockOwnerDeletion": true, - "controller": true, - "kind": "Broker", - "name": brokerName, - "uid": "", - }, - }, - "labels": labels, - }, - }, - } -} - -func createTriggerChannelRef() *corev1.ObjectReference { - return &corev1.ObjectReference{ - APIVersion: "messaging.knative.dev/v1alpha1", - Kind: "InMemoryChannel", - Namespace: testNS, - Name: fmt.Sprintf("%s-kne-trigger", brokerName), - } -} - -func makeIngressSubscription() *messagingv1beta1.Subscription { - return resources.NewSubscription(makeTrigger(), createTriggerChannelRef(), makeBrokerRef(), makeServiceURI(), makeEmptyDelivery()) -} - -func makeIngressSubscriptionWithCustomData(triggerName, triggerUID string) *messagingv1beta1.Subscription { - t := makeTrigger() - t.Name = triggerName - t.UID = types.UID(triggerUID) - - uri := makeServiceURI() - uri.Path = fmt.Sprintf("/triggers/%s/%s/%s", testNS, triggerName, triggerUID) - - return resources.NewSubscription(t, createTriggerChannelRef(), makeBrokerRef(), uri, makeEmptyDelivery()) -} - -func makeTrigger() *v1beta1.Trigger { - return &v1beta1.Trigger{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "eventing.knative.dev/v1beta1", - Kind: "Trigger", - }, - ObjectMeta: metav1.ObjectMeta{ - Namespace: testNS, - Name: triggerName, - UID: triggerUID, - }, - Spec: v1beta1.TriggerSpec{ - Broker: brokerName, - Filter: &v1beta1.TriggerFilter{ - Attributes: map[string]string{"Source": "Any", "Type": "Any"}, - }, - Subscriber: duckv1.Destination{ - Ref: &duckv1.KReference{ - Name: subscriberName, - Namespace: testNS, - Kind: subscriberKind, - APIVersion: subscriberAPIVersion, - }, - }, - }, - } -} - -func makeBrokerRef() *corev1.ObjectReference { - return &corev1.ObjectReference{ - APIVersion: "eventing.knative.dev/v1alpha1", - Kind: "Broker", - Namespace: testNS, - Name: brokerName, - } -} -func makeServiceURI() *apis.URL { - return &apis.URL{ - Scheme: "http", - Host: fmt.Sprintf("%s.%s.svc.%s", makeBrokerFilterService().Name, testNS, utils.GetClusterDomainName()), - Path: fmt.Sprintf("/triggers/%s/%s/%s", testNS, triggerName, triggerUID), - } -} -func makeEmptyDelivery() *eventingduckv1beta1.DeliverySpec { - return nil -} -func makeBrokerFilterService() *corev1.Service { - return resources.MakeFilterService(makeBroker()) -} - -func makeBroker() *v1alpha1.Broker { - return &v1alpha1.Broker{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "eventing.knative.dev/v1alpha1", - Kind: "Broker", - }, - ObjectMeta: metav1.ObjectMeta{ - Namespace: testNS, - Name: brokerName, - }, - Spec: v1alpha1.BrokerSpec{}, - } -} - -func allBrokerObjectsReadyPlus(objs ...runtime.Object) []runtime.Object { - brokerObjs := []runtime.Object{ - NewBroker(brokerName, testNS, - WithBrokerClass(eventing.ChannelBrokerClassValue), - WithBrokerChannel(channel()), - WithInitBrokerConditions, - WithBrokerReady, - WithBrokerFinalizers("brokers.eventing.knative.dev"), - WithBrokerResourceVersion(""), - WithBrokerTriggerChannel(createTriggerChannelRef()), - WithDeprecatedStatus, - WithBrokerAddress(fmt.Sprintf("%s.%s.svc.%s", ingressServiceName, testNS, utils.GetClusterDomainName()))), - createChannel(testNS, triggerChannel, true), - NewDeployment(filterDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.FilterLabels(brokerName)), - WithDeploymentServiceAccount(filterSA), - WithDeploymentContainer(filterContainerName, filterImage, livenessProbe(), readinessProbe(), envVars(filterContainerName), containerPorts(8080))), - NewService(filterServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.FilterLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(filterServiceName, testNS, - WithEndpointsLabels(resources.FilterLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - NewDeployment(ingressDeploymentName, testNS, - WithDeploymentOwnerReferences(ownerReferences()), - WithDeploymentLabels(resources.IngressLabels(brokerName)), - WithDeploymentServiceAccount(ingressSA), - WithDeploymentContainer(ingressContainerName, ingressImage, livenessProbe(), nil, envVars(ingressContainerName), containerPorts(8080))), - NewService(ingressServiceName, testNS, - WithServiceOwnerReferences(ownerReferences()), - WithServiceLabels(resources.IngressLabels(brokerName)), - WithServicePorts(servicePorts(8080))), - NewEndpoints(ingressServiceName, testNS, - WithEndpointsLabels(resources.IngressLabels(brokerName)), - WithEndpointsAddresses(corev1.EndpointAddress{IP: "127.0.0.1"})), - } - return append(brokerObjs[:], objs...) -} - -// Just so we can test subscription updates -func makeDifferentReadySubscription() *messagingv1beta1.Subscription { - s := makeIngressSubscription() - s.Spec.Subscriber.URI = apis.HTTP("different.example.com") - s.Status = *v1beta1.TestHelper.ReadySubscriptionStatus() - return s -} - -func makeIngressSubscriptionNotOwnedByTrigger() *messagingv1beta1.Subscription { - sub := makeIngressSubscription() - sub.OwnerReferences = []metav1.OwnerReference{} - return sub -} - -func makeReadySubscription() *messagingv1beta1.Subscription { - s := makeIngressSubscription() - s.Status = *v1beta1.TestHelper.ReadySubscriptionStatus() - return s -} - -func makeReadySubscriptionDeprecatedName(triggerName, triggerUID string) *messagingv1beta1.Subscription { - s := makeIngressSubscription() - t := rtv1beta1.NewTrigger(triggerName, testNS, brokerName) - t.UID = types.UID(triggerUID) - s.Name = utils.GenerateFixedName(t, fmt.Sprintf("%s-%s", brokerName, triggerName)) - s.Status = *v1beta1.TestHelper.ReadySubscriptionStatus() - return s -} - -func makeSubscriberAddressableAsUnstructured() *unstructured.Unstructured { - return &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": subscriberAPIVersion, - "kind": subscriberKind, - "metadata": map[string]interface{}{ - "namespace": testNS, - "name": subscriberName, - }, - "status": map[string]interface{}{ - "address": map[string]interface{}{ - "url": subscriberURI, - }, - }, - }, - } -} - -func makeFalseStatusSubscription() *messagingv1beta1.Subscription { - s := makeIngressSubscription() - s.Status.MarkReferencesNotResolved("testInducedError", "test induced error") - return s -} - -func makeFalseStatusPingSource() *sourcesv1alpha2.PingSource { - return NewPingSourceV1Alpha2(pingSourceName, testNS, WithPingSourceV1A2SinkNotFound) -} - -func makeUnknownStatusCronJobSource() *sourcesv1alpha2.PingSource { - cjs := NewPingSourceV1Alpha2(pingSourceName, testNS) - cjs.Status.InitializeConditions() - return cjs -} - -func makeGenerationNotEqualPingSource() *sourcesv1alpha2.PingSource { - c := makeFalseStatusPingSource() - c.Generation = currentGeneration - c.Status.ObservedGeneration = outdatedGeneration - return c -} - -func makeReadyPingSource() *sourcesv1alpha2.PingSource { - u, _ := apis.ParseURL(sinkURI) - return NewPingSourceV1Alpha2(pingSourceName, testNS, - WithPingSourceV1A2Spec(sourcesv1alpha2.PingSourceSpec{ - Schedule: testSchedule, - JsonData: testData, - SourceSpec: duckv1.SourceSpec{ - Sink: brokerDestv1, - }, - }), - WithInitPingSourceV1A2Conditions, - WithValidPingSourceV1A2Schedule, - WithValidPingSourceV1A2Resources, - WithPingSourceV1A2Deployed, - WithPingSourceV1A2EventType, - WithPingSourceV1A2Sink(u), - ) -} -func makeSubscriberKubernetesServiceAsUnstructured() *unstructured.Unstructured { - return &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "v1", - "kind": "Service", - "metadata": map[string]interface{}{ - "namespace": testNS, - "name": subscriberName, - }, - }, - } -} - -func patchFinalizers(namespace, name string) clientgotesting.PatchActionImpl { - action := clientgotesting.PatchActionImpl{} - action.Name = name - action.Namespace = namespace - patch := `{"metadata":{"finalizers":["` + finalizerName + `"],"resourceVersion":""}}` - action.Patch = []byte(patch) - return action -} - -func patchRemoveFinalizers(namespace, name string) clientgotesting.PatchActionImpl { - action := clientgotesting.PatchActionImpl{} - action.Name = name - action.Namespace = namespace - patch := `{"metadata":{"finalizers":[],"resourceVersion":""}}` - action.Patch = []byte(patch) - return action -} diff --git a/pkg/reconciler/broker/config.go b/pkg/reconciler/broker/config.go deleted file mode 100644 index 5c6c84175a1..00000000000 --- a/pkg/reconciler/broker/config.go +++ /dev/null @@ -1,69 +0,0 @@ -/* -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 broker - -import ( - "context" - "encoding/json" - "errors" - "fmt" - - "github.com/ghodss/yaml" - "go.uber.org/zap" - - corev1 "k8s.io/api/core/v1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/eventing/pkg/logging" -) - -type Config struct { - DefaultChannelTemplate messagingv1beta1.ChannelTemplateSpec -} - -const ( - channelTemplateSpec = "channelTemplateSpec" -) - -func NewConfigFromConfigMapFunc(ctx context.Context) func(configMap *corev1.ConfigMap) (*Config, error) { - return func(configMap *corev1.ConfigMap) (*Config, error) { - config := &Config{ - DefaultChannelTemplate: messagingv1beta1.ChannelTemplateSpec{}, - } - - temp, present := configMap.Data[channelTemplateSpec] - if !present { - logging.FromContext(ctx).Info("ConfigMap is missing key", zap.String("key", channelTemplateSpec), zap.Any("configMap", configMap)) - return nil, errors.New("not found") - } - - if temp == "" { - logging.FromContext(ctx).Info("ConfigMap's value was the empty string, ignoring it.", zap.Any("configMap", configMap)) - return nil, errors.New("not found") - } - - j, err := yaml.YAMLToJSON([]byte(temp)) - if err != nil { - return nil, fmt.Errorf("ConfigMap's value could not be converted to JSON. %w, %s", err, temp) - } - - if err := json.Unmarshal(j, &config.DefaultChannelTemplate); err != nil { - return nil, fmt.Errorf("ConfigMap's value could not be unmarshaled. %w, %s", err, string(j)) - } - - return config, nil - } -} diff --git a/pkg/reconciler/broker/config_test.go b/pkg/reconciler/broker/config_test.go deleted file mode 100644 index 544f2e1bf87..00000000000 --- a/pkg/reconciler/broker/config_test.go +++ /dev/null @@ -1,93 +0,0 @@ -/* -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 broker - -import ( - "testing" - - "github.com/google/go-cmp/cmp" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - logtesting "knative.dev/pkg/logging/testing" - - . "knative.dev/pkg/configmap/testing" -) - -func TestOurConfig(t *testing.T) { - actual, example := ConfigMapsFromTestFile(t, "config-broker") - exampleSpec := runtime.RawExtension{Raw: []byte(`"customValue: foo\n"`)} - - for _, tt := range []struct { - name string - fail bool - want *Config - data *corev1.ConfigMap - }{{ - name: "Actual config, no defaults.", - fail: true, - want: nil, - data: actual, - }, { - name: "Example config", - fail: false, - want: &Config{ - DefaultChannelTemplate: messagingv1beta1.ChannelTemplateSpec{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1alpha1", - Kind: "InMemoryChannel", - }, - Spec: &exampleSpec, - }}, - data: example, - }, { - name: "With values", - want: &Config{ - DefaultChannelTemplate: messagingv1beta1.ChannelTemplateSpec{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "Foo/v1", - Kind: "Bar", - }, - }, - }, - data: &corev1.ConfigMap{ - Data: map[string]string{ - "channelTemplateSpec": ` - apiVersion: Foo/v1 - kind: Bar -`, - }, - }, - }} { - t.Run(tt.name, func(t *testing.T) { - testConfig, err := NewConfigFromConfigMapFunc(logtesting.TestContextWithLogger(t))(tt.data) - if tt.fail != (err != nil) { - t.Fatalf("Unexpected error value: %v", err) - } - - t.Log(actual) - - if diff := cmp.Diff(tt.want, testConfig); diff != "" { - if testConfig != nil && testConfig.DefaultChannelTemplate.Spec != nil { - t.Log(string(testConfig.DefaultChannelTemplate.Spec.Raw)) - } - t.Errorf("Unexpected controller config (-want, +got): %s", diff) - } - }) - } -} diff --git a/pkg/reconciler/broker/controller.go b/pkg/reconciler/broker/controller.go deleted file mode 100644 index 76dd879b8f9..00000000000 --- a/pkg/reconciler/broker/controller.go +++ /dev/null @@ -1,148 +0,0 @@ -/* -Copyright 2019 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 - - http://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 broker - -import ( - "context" - "log" - - eventingclient "knative.dev/eventing/pkg/client/injection/client" - kubeclient "knative.dev/pkg/client/injection/kube/client" - "knative.dev/pkg/injection/clients/dynamicclient" - - "github.com/kelseyhightower/envconfig" - "go.uber.org/zap" - "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/tools/cache" - - "knative.dev/eventing/pkg/apis/eventing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" - "knative.dev/eventing/pkg/client/injection/ducks/duck/v1beta1/channelable" - brokerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/broker" - triggerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/trigger" - subscriptioninformer "knative.dev/eventing/pkg/client/injection/informers/messaging/v1beta1/subscription" - brokerreconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1alpha1/broker" - "knative.dev/eventing/pkg/duck" - "knative.dev/pkg/client/injection/ducks/duck/v1/addressable" - "knative.dev/pkg/client/injection/ducks/duck/v1/conditions" - deploymentinformer "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment" - endpointsinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/endpoints" - serviceinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/service" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - "knative.dev/pkg/logging" - pkgreconciler "knative.dev/pkg/reconciler" - "knative.dev/pkg/resolver" -) - -type envConfig struct { - IngressImage string `envconfig:"BROKER_INGRESS_IMAGE" required:"true"` - IngressServiceAccount string `envconfig:"BROKER_INGRESS_SERVICE_ACCOUNT" required:"true"` - FilterImage string `envconfig:"BROKER_FILTER_IMAGE" required:"true"` - FilterServiceAccount string `envconfig:"BROKER_FILTER_SERVICE_ACCOUNT" required:"true"` -} - -// NewController initializes the controller and is called by the generated code -// Registers event handlers to enqueue events -func NewController( - ctx context.Context, - cmw configmap.Watcher, -) *controller.Impl { - - var env envConfig - if err := envconfig.Process("", &env); err != nil { - log.Fatal("Failed to process env var", zap.Error(err)) - } - - deploymentInformer := deploymentinformer.Get(ctx) - brokerInformer := brokerinformer.Get(ctx) - triggerInformer := triggerinformer.Get(ctx) - subscriptionInformer := subscriptioninformer.Get(ctx) - serviceInformer := serviceinformer.Get(ctx) - endpointsInformer := endpointsinformer.Get(ctx) - - r := &Reconciler{ - eventingClientSet: eventingclient.Get(ctx), - dynamicClientSet: dynamicclient.Get(ctx), - kubeClientSet: kubeclient.Get(ctx), - brokerLister: brokerInformer.Lister(), - serviceLister: serviceInformer.Lister(), - endpointsLister: endpointsInformer.Lister(), - deploymentLister: deploymentInformer.Lister(), - subscriptionLister: subscriptionInformer.Lister(), - triggerLister: triggerInformer.Lister(), - ingressImage: env.IngressImage, - ingressServiceAccountName: env.IngressServiceAccount, - filterImage: env.FilterImage, - filterServiceAccountName: env.FilterServiceAccount, - brokerClass: eventing.ChannelBrokerClassValue, - } - - impl := brokerreconciler.NewImpl(ctx, r, eventing.ChannelBrokerClassValue) - - logging.FromContext(ctx).Info("Setting up event handlers") - - r.kresourceTracker = duck.NewListableTracker(ctx, conditions.Get, impl.EnqueueKey, controller.GetTrackerLease(ctx)) - r.channelableTracker = duck.NewListableTracker(ctx, channelable.Get, impl.EnqueueKey, controller.GetTrackerLease(ctx)) - r.addressableTracker = duck.NewListableTracker(ctx, addressable.Get, impl.EnqueueKey, controller.GetTrackerLease(ctx)) - r.uriResolver = resolver.NewURIResolver(ctx, impl.EnqueueKey) - - brokerInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: pkgreconciler.AnnotationFilterFunc(brokerreconciler.ClassAnnotationKey, eventing.ChannelBrokerClassValue, false /*allowUnset*/), - Handler: controller.HandleAll(impl.Enqueue), - }) - - serviceInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGK(v1alpha1.Kind("Broker")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - - endpointsInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: pkgreconciler.LabelExistsFilterFunc(eventing.BrokerLabelKey), - Handler: controller.HandleAll(impl.EnqueueLabelOfNamespaceScopedResource("" /*any namespace*/, eventing.BrokerLabelKey)), - }) - - deploymentInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGK(v1alpha1.Kind("Broker")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - - // Reconcile Broker (which transitively reconciles the triggers), when Subscriptions - // that I own are changed. - subscriptionInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGK(v1alpha1.Kind("Broker")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - - // Reconcile trigger (by enqueuing the broker specified in the label) when subscriptions - // of triggers change. - subscriptionInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: pkgreconciler.LabelExistsFilterFunc(eventing.BrokerLabelKey), - Handler: controller.HandleAll(impl.EnqueueLabelOfNamespaceScopedResource("" /*any namespace*/, eventing.BrokerLabelKey)), - }) - - triggerInformer.Informer().AddEventHandler(controller.HandleAll( - func(obj interface{}) { - if trigger, ok := obj.(*v1beta1.Trigger); ok { - impl.EnqueueKey(types.NamespacedName{Namespace: trigger.Namespace, Name: trigger.Spec.Broker}) - } - }, - )) - - return impl -} diff --git a/pkg/reconciler/broker/controller_test.go b/pkg/reconciler/broker/controller_test.go deleted file mode 100644 index 6a13179746d..00000000000 --- a/pkg/reconciler/broker/controller_test.go +++ /dev/null @@ -1,51 +0,0 @@ -/* -Copyright 2019 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 - - http://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 broker - -import ( - "os" - "testing" - - "knative.dev/pkg/configmap" - . "knative.dev/pkg/reconciler/testing" - - // Fake injection informers - _ "knative.dev/eventing/pkg/client/injection/ducks/duck/v1beta1/channelable/fake" - _ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/broker/fake" - _ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/trigger/fake" - _ "knative.dev/eventing/pkg/client/injection/informers/messaging/v1beta1/subscription/fake" - _ "knative.dev/pkg/client/injection/ducks/duck/v1/addressable/fake" - _ "knative.dev/pkg/client/injection/ducks/duck/v1/conditions/fake" - _ "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment/fake" - _ "knative.dev/pkg/client/injection/kube/informers/core/v1/endpoints/fake" - _ "knative.dev/pkg/client/injection/kube/informers/core/v1/service/fake" -) - -func TestNew(t *testing.T) { - ctx, _ := SetupFakeContext(t) - - _ = os.Setenv("BROKER_INGRESS_IMAGE", "INGRESS_IMAGE") - _ = os.Setenv("BROKER_INGRESS_SERVICE_ACCOUNT", "INGRESS_SERVICE_ACCOUNT") - _ = os.Setenv("BROKER_FILTER_IMAGE", "FILTER_IMAGE") - _ = os.Setenv("BROKER_FILTER_SERVICE_ACCOUNT", "FILTER_SERVICE_ACCOUNT") - - c := NewController(ctx, configmap.NewStaticWatcher()) - - if c == nil { - t.Fatal("Expected NewController to return a non-nil value") - } -} diff --git a/pkg/reconciler/broker/resources/channel.go b/pkg/reconciler/broker/resources/channel.go deleted file mode 100644 index 601489df097..00000000000 --- a/pkg/reconciler/broker/resources/channel.go +++ /dev/null @@ -1,65 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "encoding/json" - "fmt" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/pkg/kmeta" -) - -// BrokerChannelName creates a name for the Channel for a Broker for a given -// Channel type. -func BrokerChannelName(brokerName, channelType string) string { - return fmt.Sprintf("%s-kne-%s", brokerName, channelType) -} - -// test -// NewChannel returns an unstructured.Unstructured based on the ChannelTemplateSpec -// for a given Broker. -func NewChannel(channelType string, owner kmeta.OwnerRefable, channelTemplate *messagingv1beta1.ChannelTemplateSpec, l map[string]string) (*unstructured.Unstructured, error) { - // Set the name of the resource we're creating as well as the namespace, etc. - template := messagingv1beta1.ChannelTemplateSpecInternal{ - TypeMeta: metav1.TypeMeta{ - Kind: channelTemplate.Kind, - APIVersion: channelTemplate.APIVersion, - }, - ObjectMeta: metav1.ObjectMeta{ - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(owner), - }, - Name: BrokerChannelName(owner.GetObjectMeta().GetName(), channelType), - Namespace: owner.GetObjectMeta().GetNamespace(), - Labels: l, - }, - Spec: channelTemplate.Spec, - } - raw, err := json.Marshal(template) - if err != nil { - return nil, err - } - u := &unstructured.Unstructured{} - err = json.Unmarshal(raw, u) - if err != nil { - return nil, err - } - return u, nil -} diff --git a/pkg/reconciler/broker/resources/channel_test.go b/pkg/reconciler/broker/resources/channel_test.go deleted file mode 100644 index ae1fcdaf9c9..00000000000 --- a/pkg/reconciler/broker/resources/channel_test.go +++ /dev/null @@ -1,139 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "testing" - - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" -) - -func TestBrokerChannelName(t *testing.T) { - // Any changes to this name are breaking changes, this test is here so that changes can't be - // made by accident. - expected := "default-kne-ingress" - if actual := BrokerChannelName("default", "ingress"); actual != expected { - t.Errorf("expected %q, actual %q", expected, actual) - } -} - -func TestNewChannel(t *testing.T) { - testCases := map[string]struct { - channelTemplate messagingv1beta1.ChannelTemplateSpec - expectError bool - }{ - "InMemoryChannel": { - channelTemplate: messagingv1beta1.ChannelTemplateSpec{ - TypeMeta: v1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1alpha1", - Kind: "InMemoryChannel", - }, - }, - }, - "KafkaChannel": { - channelTemplate: messagingv1beta1.ChannelTemplateSpec{ - TypeMeta: v1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1alpha1", - Kind: "KafkaChannel", - }, - }, - }, - "Bad raw extension": { - channelTemplate: messagingv1beta1.ChannelTemplateSpec{ - TypeMeta: v1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1alpha1", - Kind: "InMemoryChannel", - }, - Spec: &runtime.RawExtension{ - Raw: []byte("hello world"), - }, - }, - expectError: true, - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - b := &v1alpha1.Broker{ - ObjectMeta: v1.ObjectMeta{ - Namespace: "brokers-namespace", - Name: "my-broker", - UID: "1234", - }, - Spec: v1alpha1.BrokerSpec{ - ChannelTemplate: &tc.channelTemplate, - }, - } - labels := map[string]string{"key": "value"} - c, err := NewChannel("ingress", b, b.Spec.ChannelTemplate, labels) - if err != nil { - if !tc.expectError { - t.Fatalf("Unexpected error calling NewChannel: %v", err) - } - return - } else if tc.expectError { - t.Fatalf("Expected an error calling NewChannel, actually nil") - } - - if api := c.Object["apiVersion"]; api != tc.channelTemplate.APIVersion { - t.Errorf("Expected APIVersion %q, actually %q", tc.channelTemplate.APIVersion, api) - } - if kind := c.Object["kind"]; kind != tc.channelTemplate.Kind { - t.Errorf("Expected Kind %q, actually %q", tc.channelTemplate.Kind, kind) - } - - md := c.Object["metadata"].(map[string]interface{}) - assertSoleOwner(t, b, c) - if md["namespace"] != b.Namespace { - t.Errorf("expected namespace %q, actually %q", b.Namespace, md["namespace"]) - } - if name := md["name"]; name != "my-broker-kne-ingress" { - t.Errorf("Expected name %q, actually %q", "my-broker-kn-ingress", name) - } - if l := md["labels"].(map[string]interface{}); len(l) != len(labels) { - t.Errorf("Expected labels %q, actually %q", labels, l) - } else { - for k, v := range labels { - if l[k] != v { - t.Errorf("Expected labels %q, actually %q", labels, l) - } - } - } - }) - } -} - -func assertSoleOwner(t *testing.T, owner v1.Object, owned *unstructured.Unstructured) { - md := owned.Object["metadata"].(map[string]interface{}) - owners := md["ownerReferences"].([]interface{}) - if len(owners) != 1 { - t.Errorf("Expected 1 owner, actually %d", len(owners)) - } - o := owners[0].(map[string]interface{}) - if uid := o["uid"]; uid != string(owner.GetUID()) { - t.Errorf("Expected UID %q, actually %q", owner.GetUID(), uid) - } - if name := o["name"]; name != owner.GetName() { - t.Errorf("Expected name %q, actually %q", owner.GetName(), name) - } - if !o["controller"].(bool) { - t.Error("Expected controller true, actually false") - } -} diff --git a/pkg/reconciler/broker/resources/filter.go b/pkg/reconciler/broker/resources/filter.go deleted file mode 100644 index dee9db48b3b..00000000000 --- a/pkg/reconciler/broker/resources/filter.go +++ /dev/null @@ -1,162 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "fmt" - - appsv1 "k8s.io/api/apps/v1" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/intstr" - - "knative.dev/eventing/pkg/apis/eventing" - eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/pkg/kmeta" - "knative.dev/pkg/system" -) - -const ( - filterContainerName = "filter" -) - -// FilterArgs are the arguments to create a Broker's filter Deployment. -type FilterArgs struct { - Broker *eventingv1alpha1.Broker - Image string - ServiceAccountName string -} - -// MakeFilterDeployment creates the in-memory representation of the Broker's filter Deployment. -func MakeFilterDeployment(args *FilterArgs) *appsv1.Deployment { - return &appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: args.Broker.Namespace, - Name: fmt.Sprintf("%s-broker-filter", args.Broker.Name), - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(args.Broker), - }, - Labels: FilterLabels(args.Broker.Name), - }, - Spec: appsv1.DeploymentSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: FilterLabels(args.Broker.Name), - }, - Template: corev1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: FilterLabels(args.Broker.Name), - }, - Spec: corev1.PodSpec{ - ServiceAccountName: args.ServiceAccountName, - Containers: []corev1.Container{{ - Name: filterContainerName, - Image: args.Image, - LivenessProbe: &corev1.Probe{ - Handler: corev1.Handler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/healthz", - Port: intstr.IntOrString{Type: intstr.Int, IntVal: 8080}, - }, - }, - InitialDelaySeconds: 5, - PeriodSeconds: 2, - }, - ReadinessProbe: &corev1.Probe{ - Handler: corev1.Handler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/readyz", - Port: intstr.IntOrString{Type: intstr.Int, IntVal: 8080}, - }, - }, - InitialDelaySeconds: 5, - PeriodSeconds: 2, - }, - Env: []corev1.EnvVar{{ - Name: system.NamespaceEnvKey, - Value: system.Namespace(), - }, { - Name: "NAMESPACE", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.namespace", - }, - }, - }, { - Name: "POD_NAME", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.name", - }, - }, - }, { - Name: "CONTAINER_NAME", - Value: filterContainerName, - }, { - Name: "BROKER", - Value: args.Broker.Name, - }, { - // Used for StackDriver only. - Name: "METRICS_DOMAIN", - Value: "knative.dev/internal/eventing", - }}, - Ports: []corev1.ContainerPort{{ - ContainerPort: 8080, - Name: "http", - }, { - ContainerPort: 9092, - Name: "metrics", - }}, - }}, - }, - }, - }, - } -} - -// MakeFilterService creates the in-memory representation of the Broker's filter Service. -func MakeFilterService(b *eventingv1alpha1.Broker) *corev1.Service { - return &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: b.Namespace, - Name: fmt.Sprintf("%s-broker-filter", b.Name), - Labels: FilterLabels(b.Name), - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(b), - }, - }, - Spec: corev1.ServiceSpec{ - Selector: FilterLabels(b.Name), - Ports: []corev1.ServicePort{{ - Name: "http", - Port: 80, - TargetPort: intstr.FromInt(8080), - }, { - Name: "http-metrics", - Port: 9090, - }}, - }, - } -} - -// FilterLabels generates the labels present on all resources representing the filter of the given -// Broker. -func FilterLabels(brokerName string) map[string]string { - return map[string]string{ - eventing.BrokerLabelKey: brokerName, - "eventing.knative.dev/brokerRole": "filter", - } -} diff --git a/pkg/reconciler/broker/resources/filter_test.go b/pkg/reconciler/broker/resources/filter_test.go deleted file mode 100644 index 94d0306ab00..00000000000 --- a/pkg/reconciler/broker/resources/filter_test.go +++ /dev/null @@ -1,277 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "encoding/json" - "testing" - - "github.com/google/go-cmp/cmp" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - - _ "knative.dev/pkg/system/testing" -) - -func TestMakeFilterDeployment(t *testing.T) { - testCases := map[string]struct { - args FilterArgs - want []byte - }{ - "happy": { - args: FilterArgs{ - Broker: &v1alpha1.Broker{ - ObjectMeta: v1.ObjectMeta{ - Name: "happy", - }, - Spec: v1alpha1.BrokerSpec{}, - Status: v1alpha1.BrokerStatus{}, - }, - Image: "image-uri", - ServiceAccountName: "service-account-name", - }, - want: []byte(`{ - "metadata": { - "name": "happy-broker-filter", - "creationTimestamp": null, - "labels": { - "eventing.knative.dev/broker": "happy", - "eventing.knative.dev/brokerRole": "filter" - }, - "ownerReferences": [ - { - "apiVersion": "eventing.knative.dev/v1alpha1", - "kind": "Broker", - "name": "happy", - "uid": "", - "controller": true, - "blockOwnerDeletion": true - } - ] - }, - "spec": { - "selector": { - "matchLabels": { - "eventing.knative.dev/broker": "happy", - "eventing.knative.dev/brokerRole": "filter" - } - }, - "template": { - "metadata": { - "creationTimestamp": null, - "labels": { - "eventing.knative.dev/broker": "happy", - "eventing.knative.dev/brokerRole": "filter" - } - }, - "spec": { - "containers": [ - { - "name": "filter", - "image": "image-uri", - "ports": [ - { - "name": "http", - "containerPort": 8080 - }, - { - "name": "metrics", - "containerPort": 9092 - } - ], - "env": [ - { - "name": "SYSTEM_NAMESPACE", - "value": "knative-testing" - }, - { - "name": "NAMESPACE", - "valueFrom": { - "fieldRef": { - "fieldPath": "metadata.namespace" - } - } - }, - { - "name": "POD_NAME", - "valueFrom": { - "fieldRef": { - "fieldPath": "metadata.name" - } - } - }, - { - "name": "CONTAINER_NAME", - "value": "filter" - }, - { - "name": "BROKER", - "value": "happy" - }, - { - "name": "METRICS_DOMAIN", - "value": "knative.dev/internal/eventing" - } - ], - "resources": {}, - "livenessProbe": { - "httpGet": { - "path": "/healthz", - "port": 8080 - }, - "initialDelaySeconds": 5, - "periodSeconds": 2 - }, - "readinessProbe": { - "httpGet": { - "path": "/readyz", - "port": 8080 - }, - "initialDelaySeconds": 5, - "periodSeconds": 2 - } - } - ], - "serviceAccountName": "service-account-name" - } - }, - "strategy": {} - }, - "status": {} -}`), - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - dep := MakeFilterDeployment(&tc.args) - - got, err := json.MarshalIndent(dep, "", " ") - if err != nil { - t.Errorf("failed to marshal deployment, %s", err) - } - - if diff := cmp.Diff(tc.want, got); diff != "" { - t.Log(string(got)) - t.Errorf("unexpected deployment (-want, +got) = %v", diff) - } - }) - } -} - -func TestMakeFilterService(t *testing.T) { - testCases := map[string]struct { - broker v1alpha1.Broker - want []byte - }{ - "happy": { - broker: v1alpha1.Broker{ - ObjectMeta: v1.ObjectMeta{ - Name: "happy", - }, - Spec: v1alpha1.BrokerSpec{}, - Status: v1alpha1.BrokerStatus{}, - }, - want: []byte(`{ - "metadata": { - "name": "happy-broker-filter", - "creationTimestamp": null, - "labels": { - "eventing.knative.dev/broker": "happy", - "eventing.knative.dev/brokerRole": "filter" - }, - "ownerReferences": [ - { - "apiVersion": "eventing.knative.dev/v1alpha1", - "kind": "Broker", - "name": "happy", - "uid": "", - "controller": true, - "blockOwnerDeletion": true - } - ] - }, - "spec": { - "ports": [ - { - "name": "http", - "port": 80, - "targetPort": 8080 - }, - { - "name": "http-metrics", - "port": 9090, - "targetPort": 0 - } - ], - "selector": { - "eventing.knative.dev/broker": "happy", - "eventing.knative.dev/brokerRole": "filter" - } - }, - "status": { - "loadBalancer": {} - } -}`), - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - dep := MakeFilterService(&tc.broker) - - got, err := json.MarshalIndent(dep, "", " ") - if err != nil { - t.Errorf("failed to marshal deployment, %s", err) - } - - if diff := cmp.Diff(tc.want, got); diff != "" { - t.Log(string(got)) - t.Errorf("unexpected deployment (-want, +got) = %v", diff) - } - }) - } -} - -func TestMakeFilterLabels(t *testing.T) { - testCases := map[string]struct { - name string - want map[string]string - }{ - "with name": { - name: "brokerName", - want: map[string]string{ - "eventing.knative.dev/broker": "brokerName", - "eventing.knative.dev/brokerRole": "filter", - }, - }, - "name empty": { - name: "", - want: map[string]string{ - "eventing.knative.dev/broker": "", - "eventing.knative.dev/brokerRole": "filter", - }, - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - got := FilterLabels(tc.name) - - if diff := cmp.Diff(tc.want, got); diff != "" { - t.Errorf("unexpected labels (-want, +got) = %v", diff) - } - }) - } -} diff --git a/pkg/reconciler/broker/resources/ingress.go b/pkg/reconciler/broker/resources/ingress.go deleted file mode 100644 index 69a20383864..00000000000 --- a/pkg/reconciler/broker/resources/ingress.go +++ /dev/null @@ -1,160 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "fmt" - - appsv1 "k8s.io/api/apps/v1" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/intstr" - - "knative.dev/eventing/pkg/apis/eventing" - eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/pkg/kmeta" - "knative.dev/pkg/system" -) - -const ( - ingressContainerName = "ingress" -) - -// IngressArgs are the arguments to create a Broker's ingress Deployment. -type IngressArgs struct { - Broker *eventingv1alpha1.Broker - Image string - ServiceAccountName string - ChannelAddress string -} - -// MakeIngress creates the in-memory representation of the Broker's ingress Deployment. -func MakeIngressDeployment(args *IngressArgs) *appsv1.Deployment { - return &appsv1.Deployment{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: args.Broker.Namespace, - Name: fmt.Sprintf("%s-broker-ingress", args.Broker.Name), - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(args.Broker), - }, - Labels: IngressLabels(args.Broker.Name), - }, - Spec: appsv1.DeploymentSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: IngressLabels(args.Broker.Name), - }, - Template: corev1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: IngressLabels(args.Broker.Name), - }, - Spec: corev1.PodSpec{ - ServiceAccountName: args.ServiceAccountName, - Containers: []corev1.Container{{ - Image: args.Image, - Name: ingressContainerName, - LivenessProbe: &corev1.Probe{ - Handler: corev1.Handler{ - HTTPGet: &corev1.HTTPGetAction{ - Path: "/healthz", - Port: intstr.IntOrString{Type: intstr.Int, IntVal: 8080}, - }, - }, - InitialDelaySeconds: 5, - PeriodSeconds: 2, - }, - Env: []corev1.EnvVar{{ - Name: system.NamespaceEnvKey, - Value: system.Namespace(), - }, { - Name: "NAMESPACE", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.namespace", - }, - }, - }, { - Name: "POD_NAME", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.name", - }, - }, - }, { - Name: "CONTAINER_NAME", - Value: ingressContainerName, - }, { - Name: "FILTER", - Value: "", // TODO Add one. - }, { - Name: "CHANNEL", - Value: args.ChannelAddress, - }, { - Name: "BROKER", - Value: args.Broker.Name, - }, { - // Used for StackDriver only. - Name: "METRICS_DOMAIN", - Value: "knative.dev/internal/eventing", - }}, - Ports: []corev1.ContainerPort{{ - ContainerPort: 8080, - Name: "http", - }, { - ContainerPort: 9092, - Name: "metrics", - }}, - }}, - }, - }, - }, - } -} - -// MakeIngressService creates the in-memory representation of the Broker's ingress Service. -func MakeIngressService(b *eventingv1alpha1.Broker) *corev1.Service { - return &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: b.Namespace, - // TODO add -ingress to the name to be consistent with the filter service naming. - Name: fmt.Sprintf("%s-broker", b.Name), - Labels: IngressLabels(b.Name), - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(b), - }, - }, - Spec: corev1.ServiceSpec{ - Selector: IngressLabels(b.Name), - Ports: []corev1.ServicePort{{ - Name: "http", - Port: 80, - TargetPort: intstr.FromInt(8080), - }, { - Name: "http-metrics", - Port: 9090, - }}, - }, - } -} - -// IngressLabels generates the labels present on all resources representing the ingress of the given -// Broker. -func IngressLabels(brokerName string) map[string]string { - return map[string]string{ - eventing.BrokerLabelKey: brokerName, - "eventing.knative.dev/brokerRole": "ingress", - } -} diff --git a/pkg/reconciler/broker/resources/subscription.go b/pkg/reconciler/broker/resources/subscription.go deleted file mode 100644 index 09dd2dfc775..00000000000 --- a/pkg/reconciler/broker/resources/subscription.go +++ /dev/null @@ -1,112 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "fmt" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/pkg/apis" - "knative.dev/pkg/kmeta" - - duckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1" - "knative.dev/eventing/pkg/apis/eventing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - duckv1 "knative.dev/pkg/apis/duck/v1" -) - -// MakeSubscription returns a placeholder subscription for broker 'b', channelable 'c', and service 'svc'. -func MakeSubscription(b *v1alpha1.Broker, c *duckv1beta1.Channelable, svc *corev1.Service) *messagingv1beta1.Subscription { - return &messagingv1beta1.Subscription{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: b.Namespace, - Name: kmeta.ChildName(fmt.Sprintf("internal-ingress-%s", b.Name), string(b.GetUID())), - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(b), - }, - Labels: ingressSubscriptionLabels(b.Name), - }, - Spec: messagingv1beta1.SubscriptionSpec{ - Channel: corev1.ObjectReference{ - APIVersion: c.APIVersion, - Kind: c.Kind, - Name: c.Name, - }, - Subscriber: &duckv1.Destination{ - Ref: &duckv1.KReference{ - APIVersion: "v1", - Kind: "Service", - Name: svc.Name, - Namespace: svc.Namespace, - }, - }, - }, - } -} - -func ingressSubscriptionLabels(brokerName string) map[string]string { - return map[string]string{ - eventing.BrokerLabelKey: brokerName, - "eventing.knative.dev/brokerIngress": "true", - } -} - -// NewSubscription returns a placeholder subscription for trigger 't', from brokerTrigger to 'uri' -// replying to brokerIngress. -func NewSubscription(t *v1beta1.Trigger, brokerTrigger, brokerRef *corev1.ObjectReference, uri *apis.URL, delivery *duckv1beta1.DeliverySpec) *messagingv1beta1.Subscription { - return &messagingv1beta1.Subscription{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: t.Namespace, - Name: kmeta.ChildName(fmt.Sprintf("%s-%s-", t.Spec.Broker, t.Name), string(t.GetUID())), - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(t), - }, - Labels: SubscriptionLabels(t), - }, - Spec: messagingv1beta1.SubscriptionSpec{ - Channel: corev1.ObjectReference{ - APIVersion: brokerTrigger.APIVersion, - Kind: brokerTrigger.Kind, - Name: brokerTrigger.Name, - }, - Subscriber: &duckv1.Destination{ - URI: uri, - }, - Reply: &duckv1.Destination{ - Ref: &duckv1.KReference{ - APIVersion: brokerRef.APIVersion, - Kind: brokerRef.Kind, - Name: brokerRef.Name, - Namespace: brokerRef.Namespace, - }, - }, - Delivery: delivery, - }, - } -} - -// SubscriptionLabels generates the labels present on the Subscription linking this Trigger to the -// Broker's Channels. -func SubscriptionLabels(t *v1beta1.Trigger) map[string]string { - return map[string]string{ - eventing.BrokerLabelKey: t.Spec.Broker, - "eventing.knative.dev/trigger": t.Name, - } -} diff --git a/pkg/reconciler/broker/resources/subscription_test.go b/pkg/reconciler/broker/resources/subscription_test.go deleted file mode 100644 index 8180da84c72..00000000000 --- a/pkg/reconciler/broker/resources/subscription_test.go +++ /dev/null @@ -1,180 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - "fmt" - "testing" - - "github.com/google/go-cmp/cmp" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - duckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1" - "knative.dev/eventing/pkg/apis/eventing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/pkg/apis" - duckv1 "knative.dev/pkg/apis/duck/v1" - "knative.dev/pkg/kmeta" -) - -func TestMakeSubscription(t *testing.T) { - testCases := map[string]struct { - channelable duckv1beta1.Channelable - }{ - "InMemoryChannel": { - channelable: duckv1beta1.Channelable{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1beta1", - Kind: "InMemoryChannel", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "in-memory-channel", - }, - }, - }, - "KafkaChannel": { - channelable: duckv1beta1.Channelable{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "messaging.knative.dev/v1beta1", - Kind: "KafkaChannel", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "kafka-channel", - }, - }, - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - b := &v1alpha1.Broker{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "brokers-namespace", - Name: "my-broker", - UID: "1234", - }, - } - svc := &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-svc", - }, - } - sub := MakeSubscription(b, &tc.channelable, svc) - - if ns := sub.Namespace; ns != b.Namespace { - t.Errorf("Expected namespace %q, actually %q", b.Namespace, ns) - } - if !metav1.IsControlledBy(sub, b) { - t.Errorf("Expected sub to be controlled by the broker") - } - expectedChannel := corev1.ObjectReference{ - APIVersion: tc.channelable.APIVersion, - Kind: tc.channelable.Kind, - Name: tc.channelable.Name, - } - if ch := sub.Spec.Channel; ch != expectedChannel { - t.Errorf("Expected spec.channel %q, actually %q", expectedChannel, ch) - } - expectedSubscriber := duckv1.KReference{ - APIVersion: "v1", - Kind: "Service", - Name: svc.Name, - Namespace: svc.Namespace, - } - if subscriber := *sub.Spec.Subscriber.Ref; subscriber != expectedSubscriber { - t.Errorf("Expected spec.subscriber.ref %q, actually %q", expectedSubscriber, subscriber) - } - }) - } -} - -func TestNewSubscription(t *testing.T) { - var TrueValue = true - trigger := &v1beta1.Trigger{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "t-namespace", - Name: "t-name-is-a-long-name", - UID: "cafed00d-cafed00d-cafed00d-cafed00d", - }, - Spec: v1beta1.TriggerSpec{ - Broker: "broker-name", - }, - } - triggerChannelRef := &corev1.ObjectReference{ - Name: "tc-name", - Kind: "tc-kind", - APIVersion: "tc-apiVersion", - } - brokerRef := &corev1.ObjectReference{ - Name: "broker-name", - Namespace: "t-namespace", - Kind: "broker-kind", - APIVersion: "broker-apiVersion", - } - delivery := &duckv1beta1.DeliverySpec{ - DeadLetterSink: &duckv1.Destination{ - URI: apis.HTTP("dlc.example.com"), - }, - } - got := NewSubscription(trigger, triggerChannelRef, brokerRef, apis.HTTP("example.com"), delivery) - want := &messagingv1beta1.Subscription{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: "t-namespace", - Name: kmeta.ChildName(fmt.Sprintf("%s-%s-", "broker-name", "t-name-is-a-long-name"), "cafed00d-cafed00d-cafed00d-cafed00d"), - OwnerReferences: []metav1.OwnerReference{{ - APIVersion: "eventing.knative.dev/v1beta1", - Kind: "Trigger", - Name: "t-name-is-a-long-name", - UID: "cafed00d-cafed00d-cafed00d-cafed00d", - Controller: &TrueValue, - BlockOwnerDeletion: &TrueValue, - }}, - Labels: map[string]string{ - eventing.BrokerLabelKey: "broker-name", - "eventing.knative.dev/trigger": "t-name-is-a-long-name", - }, - }, - Spec: messagingv1beta1.SubscriptionSpec{ - Channel: corev1.ObjectReference{ - Name: "tc-name", - Kind: "tc-kind", - APIVersion: "tc-apiVersion", - }, - Subscriber: &duckv1.Destination{ - URI: apis.HTTP("example.com"), - }, - Reply: &duckv1.Destination{ - Ref: &duckv1.KReference{ - Name: "broker-name", - Namespace: "t-namespace", - Kind: "broker-kind", - APIVersion: "broker-apiVersion", - }, - }, - Delivery: &duckv1beta1.DeliverySpec{ - DeadLetterSink: &duckv1.Destination{ - URI: apis.HTTP("dlc.example.com"), - }, - }, - }, - } - - if diff := cmp.Diff(want, got); diff != "" { - t.Errorf("unexpected diff (-want, +got) = %v", diff) - } -} diff --git a/pkg/reconciler/broker/testdata/config-broker.yaml b/pkg/reconciler/broker/testdata/config-broker.yaml deleted file mode 100644 index 8e5d290e426..00000000000 --- a/pkg/reconciler/broker/testdata/config-broker.yaml +++ /dev/null @@ -1,51 +0,0 @@ -# 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-broker - namespace: default - -data: - _example: | - ################################ - # # - # EXAMPLE CONFIGURATION # - # # - ################################ - - # This block is not actually functional configuration, - # but serves to illustrate the available configuration - # options and document them in a way that is accessible - # to users that `kubectl edit` this config map. - # - # These sample configuration options may be copied out of - # this example block and unindented to be in the data block - # to actually change the configuration. - - # This defines the default channel template to use for the broker. - channelTemplateSpec: | - # The api and version of the kind of channel to use inthe broker. - # This field required. - apiVersion: messaging.knative.dev/v1alpha1 - - # The api and version of the kind of channel to use inthe broker. - # This field required. - kind: InMemoryChannel - - # The custom spec that should be used for channel templates. - #This field is optional. - spec: | - customValue: foo diff --git a/pkg/reconciler/broker/trigger.go b/pkg/reconciler/broker/trigger.go deleted file mode 100644 index c5a5fddd576..00000000000 --- a/pkg/reconciler/broker/trigger.go +++ /dev/null @@ -1,250 +0,0 @@ -/* -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 - - http://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 broker - -import ( - "context" - "errors" - "fmt" - "reflect" - - "go.uber.org/zap" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/equality" - apierrs "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/pkg/controller" - - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" - messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/eventing/pkg/logging" - "knative.dev/eventing/pkg/reconciler/broker/resources" - "knative.dev/eventing/pkg/reconciler/names" - "knative.dev/eventing/pkg/reconciler/trigger/path" - "knative.dev/eventing/pkg/utils" - "knative.dev/pkg/apis" - duckv1 "knative.dev/pkg/apis/duck/v1" - "knative.dev/pkg/kmeta" -) - -const ( - // Name of the corev1.Events emitted from the Trigger reconciliation process. - triggerReconciled = "TriggerReconciled" - triggerReconcileFailed = "TriggerReconcileFailed" - triggerUpdateStatusFailed = "TriggerUpdateStatusFailed" - subscriptionDeleteFailed = "SubscriptionDeleteFailed" - subscriptionCreateFailed = "SubscriptionCreateFailed" - subscriptionGetFailed = "SubscriptionGetFailed" - subscriptionDeleted = "SubscriptionDeleted" -) - -func (r *Reconciler) reconcileTrigger(ctx context.Context, b *v1alpha1.Broker, t *v1beta1.Trigger, filterSvc kmeta.Accessor) error { - t.Status.InitializeConditions() - - if t.DeletionTimestamp != nil { - // Everything is cleaned up by the garbage collector. - return nil - } - - t.Status.PropagateBrokerCondition(b.Status.GetTopLevelCondition()) - - brokerTrigger := b.Status.TriggerChannel - if brokerTrigger == nil { - // Should not happen because Broker is ready to go if we get here - return errors.New("failed to find Broker's Trigger channel") - } - - if t.Spec.Subscriber.Ref != nil { - // To call URIFromDestination(dest apisv1alpha1.Destination, parent interface{}), dest.Ref must have a Namespace - // We will use the Namespace of Trigger as the Namespace of dest.Ref - t.Spec.Subscriber.Ref.Namespace = t.GetNamespace() - } - - subscriberURI, err := r.uriResolver.URIFromDestinationV1(t.Spec.Subscriber, b) - if err != nil { - logging.FromContext(ctx).Error("Unable to get the Subscriber's URI", zap.Error(err)) - t.Status.MarkSubscriberResolvedFailed("Unable to get the Subscriber's URI", "%v", err) - t.Status.SubscriberURI = nil - return err - } - t.Status.SubscriberURI = subscriberURI - t.Status.MarkSubscriberResolvedSucceeded() - - sub, err := r.subscribeToBrokerChannel(ctx, b, t, brokerTrigger, filterSvc) - if err != nil { - logging.FromContext(ctx).Error("Unable to Subscribe", zap.Error(err)) - t.Status.MarkNotSubscribed("NotSubscribed", "%v", err) - return err - } - t.Status.PropagateSubscriptionCondition(sub.Status.GetTopLevelCondition()) - - if err := r.checkDependencyAnnotation(ctx, t, b); err != nil { - return err - } - - return nil -} - -// subscribeToBrokerChannel subscribes service 'svc' to the Broker's channels. -func (r *Reconciler) subscribeToBrokerChannel(ctx context.Context, b *v1alpha1.Broker, t *v1beta1.Trigger, brokerTrigger *corev1.ObjectReference, svc kmeta.Accessor) (*messagingv1beta1.Subscription, error) { - if svc == nil { - return nil, fmt.Errorf("service for broker is nil") - } - uri := &apis.URL{ - Scheme: "http", - Host: names.ServiceHostName(svc.GetName(), svc.GetNamespace()), - Path: path.Generate(t), - } - // Note that we have to hard code the brokerGKV stuff as sometimes typemeta is not - // filled in. So instead of b.TypeMeta.Kind and b.TypeMeta.APIVersion, we have to - // do it this way. - brokerObjRef := &corev1.ObjectReference{ - Kind: brokerGVK.Kind, - APIVersion: brokerGVK.GroupVersion().String(), - Name: b.Name, - Namespace: b.Namespace, - } - expected := resources.NewSubscription(t, brokerTrigger, brokerObjRef, uri, b.Spec.Delivery) - - sub, err := r.subscriptionLister.Subscriptions(t.Namespace).Get(expected.Name) - // If the resource doesn't exist, we'll create it. - if apierrs.IsNotFound(err) { - // Issue #2842: Subscription name uses kmeta.ChildName. If a subscription by the previous name pattern is found, it should - // be deleted. This might cause temporary downtime. - if deprecatedName := utils.GenerateFixedName(t, fmt.Sprintf("%s-%s", t.Spec.Broker, t.Name)); deprecatedName != expected.Name { - if err := r.eventingClientSet.MessagingV1beta1().Subscriptions(t.Namespace).Delete(deprecatedName, &metav1.DeleteOptions{}); err != nil && !apierrs.IsNotFound(err) { - return nil, fmt.Errorf("error deleting deprecated named subscription: %v", err) - } - controller.GetEventRecorder(ctx).Eventf(t, corev1.EventTypeNormal, subscriptionDeleted, "Deprecated subscription removed: \"%s/%s\"", t.Namespace, deprecatedName) - } - - logging.FromContext(ctx).Info("Creating subscription") - sub, err = r.eventingClientSet.MessagingV1beta1().Subscriptions(t.Namespace).Create(expected) - if err != nil { - controller.GetEventRecorder(ctx).Eventf(t, corev1.EventTypeWarning, subscriptionCreateFailed, "Create Trigger's subscription failed: %v", err) - return nil, err - } - return sub, nil - } else if err != nil { - logging.FromContext(ctx).Error("Failed to get subscription", zap.Error(err)) - controller.GetEventRecorder(ctx).Eventf(t, corev1.EventTypeWarning, subscriptionGetFailed, "Getting the Trigger's Subscription failed: %v", err) - return nil, err - } else if !metav1.IsControlledBy(sub, t) { - t.Status.MarkNotSubscribed("SubscriptionNotOwnedByTrigger", "trigger %q does not own subscription %q", t.Name, sub.Name) - return nil, fmt.Errorf("trigger %q does not own subscription %q", t.Name, sub.Name) - } else if sub, err = r.reconcileSubscription(ctx, t, expected, sub); err != nil { - logging.FromContext(ctx).Error("Failed to reconcile subscription", zap.Error(err)) - return sub, err - } - - return sub, nil -} - -func (r *Reconciler) reconcileSubscription(ctx context.Context, t *v1beta1.Trigger, expected, actual *messagingv1beta1.Subscription) (*messagingv1beta1.Subscription, error) { - // Update Subscription if it has changed. Ignore the generation. - if equality.Semantic.DeepDerivative(expected.Spec, actual.Spec) { - return actual, nil - } - logging.FromContext(ctx).Info("Differing Subscription", zap.Any("expected", expected.Spec), zap.Any("actual", actual.Spec)) - - // Given that spec.channel is immutable, we cannot just update the Subscription. We delete - // it and re-create it instead. - logging.FromContext(ctx).Info("Deleting subscription", zap.String("namespace", actual.Namespace), zap.String("name", actual.Name)) - err := r.eventingClientSet.MessagingV1beta1().Subscriptions(t.Namespace).Delete(actual.Name, &metav1.DeleteOptions{}) - if err != nil { - logging.FromContext(ctx).Info("Cannot delete subscription", zap.Error(err)) - controller.GetEventRecorder(ctx).Eventf(t, corev1.EventTypeWarning, subscriptionDeleteFailed, "Delete Trigger's subscription failed: %v", err) - return nil, err - } - logging.FromContext(ctx).Info("Creating subscription") - newSub, err := r.eventingClientSet.MessagingV1beta1().Subscriptions(t.Namespace).Create(expected) - if err != nil { - logging.FromContext(ctx).Info("Cannot create subscription", zap.Error(err)) - controller.GetEventRecorder(ctx).Eventf(t, corev1.EventTypeWarning, subscriptionCreateFailed, "Create Trigger's subscription failed: %v", err) - return nil, err - } - return newSub, nil -} - -func (r *Reconciler) updateTriggerStatus(ctx context.Context, desired *v1beta1.Trigger) (*v1beta1.Trigger, error) { - trigger, err := r.triggerLister.Triggers(desired.Namespace).Get(desired.Name) - if err != nil { - return nil, err - } - - if reflect.DeepEqual(trigger.Status, desired.Status) { - return trigger, nil - } - - // Don't modify the informers copy. - existing := trigger.DeepCopy() - existing.Status = desired.Status - - return r.eventingClientSet.EventingV1beta1().Triggers(desired.Namespace).UpdateStatus(existing) -} - -func (r *Reconciler) checkDependencyAnnotation(ctx context.Context, t *v1beta1.Trigger, b *v1alpha1.Broker) error { - if dependencyAnnotation, ok := t.GetAnnotations()[v1beta1.DependencyAnnotation]; ok { - dependencyObjRef, err := v1beta1.GetObjRefFromDependencyAnnotation(dependencyAnnotation) - if err != nil { - t.Status.MarkDependencyFailed("ReferenceError", "Unable to unmarshal objectReference from dependency annotation of trigger: %v", err) - return fmt.Errorf("getting object ref from dependency annotation %q: %v", dependencyAnnotation, err) - } - trackKResource := r.kresourceTracker.TrackInNamespace(b) - // Trigger and its dependent source are in the same namespace, we already did the validation in the webhook. - if err := trackKResource(dependencyObjRef); err != nil { - return fmt.Errorf("tracking dependency: %v", err) - } - if err := r.propagateDependencyReadiness(ctx, t, dependencyObjRef); err != nil { - return fmt.Errorf("propagating dependency readiness: %v", err) - } - } else { - t.Status.MarkDependencySucceeded() - } - return nil -} - -func (r *Reconciler) propagateDependencyReadiness(ctx context.Context, t *v1beta1.Trigger, dependencyObjRef corev1.ObjectReference) error { - lister, err := r.kresourceTracker.ListerFor(dependencyObjRef) - if err != nil { - t.Status.MarkDependencyUnknown("ListerDoesNotExist", "Failed to retrieve lister: %v", err) - return fmt.Errorf("retrieving lister: %v", err) - } - dependencyObj, err := lister.ByNamespace(t.GetNamespace()).Get(dependencyObjRef.Name) - if err != nil { - if apierrs.IsNotFound(err) { - t.Status.MarkDependencyFailed("DependencyDoesNotExist", "Dependency does not exist: %v", err) - } else { - t.Status.MarkDependencyUnknown("DependencyGetFailed", "Failed to get dependency: %v", err) - } - return fmt.Errorf("getting the dependency: %v", err) - } - dependency := dependencyObj.(*duckv1.KResource) - - // The dependency hasn't yet reconciled our latest changes to - // its desired state, so its conditions are outdated. - if dependency.GetGeneration() != dependency.Status.ObservedGeneration { - logging.FromContext(ctx).Info("The ObjectMeta Generation of dependency is not equal to the observedGeneration of status", - zap.Any("objectMetaGeneration", dependency.GetGeneration()), - zap.Any("statusObservedGeneration", dependency.Status.ObservedGeneration)) - t.Status.MarkDependencyUnknown("GenerationNotEqual", "The dependency's metadata.generation, %q, is not equal to its status.observedGeneration, %q.", dependency.GetGeneration(), dependency.Status.ObservedGeneration) - return nil - } - t.Status.PropagateDependencyStatus(dependency) - return nil -} diff --git a/pkg/reconciler/configmappropagation/configmappropagation.go b/pkg/reconciler/configmappropagation/configmappropagation.go deleted file mode 100644 index 7bc8d2bdbac..00000000000 --- a/pkg/reconciler/configmappropagation/configmappropagation.go +++ /dev/null @@ -1,276 +0,0 @@ -/* -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 - - http://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 configmappropagation - -import ( - "context" - "fmt" - "strings" - - "go.uber.org/zap" - corev1 "k8s.io/api/core/v1" - apierrs "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/kubernetes" - corev1listers "k8s.io/client-go/listers/core/v1" - "knative.dev/pkg/reconciler" - - "knative.dev/eventing/pkg/apis/configs/v1alpha1" - cmpreconciler "knative.dev/eventing/pkg/client/injection/reconciler/configs/v1alpha1/configmappropagation" - configslisters "knative.dev/eventing/pkg/client/listers/configs/v1alpha1" - "knative.dev/eventing/pkg/logging" - "knative.dev/eventing/pkg/reconciler/configmappropagation/resources" - "knative.dev/pkg/controller" - "knative.dev/pkg/tracker" -) - -const ( - // Name of the corev1.Events emitted from the reconciliation process. - configMapPropagationPropagateSingleConfigMapFailed = "ConfigMapPropagationPropagateSingleConfigMapFailed" - configMapPropagationPropagateSingleConfigMapSucceed = "ConfigMapPropagationPropagateSingleConfigMapSucceed" - - // Name of operation to propagate single configmap. - copyConfigMap = "Copy" - deleteConfigMap = "Delete" - // stopConfigMap indicates a configmap stop propagating. - stopConfigMap = "Stop" -) - -type Reconciler struct { - kubeClientSet kubernetes.Interface - - // Listers index properties about resources - configMapPropagationLister configslisters.ConfigMapPropagationLister - configMapLister corev1listers.ConfigMapLister - - tracker tracker.Interface -} - -var configMapGVK = corev1.SchemeGroupVersion.WithKind("ConfigMap") - -// Check that our Reconciler implements cmpreconciler.Interface. -var _ cmpreconciler.Interface = (*Reconciler)(nil) - -func (r *Reconciler) ReconcileKind(ctx context.Context, cmp *v1alpha1.ConfigMapPropagation) reconciler.Event { - logging.FromContext(ctx).Debug("Reconciling", zap.Any("ConfigMapPropagation", cmp)) - cmp.Status.InitializeConditions() - cmp.Status.CopyConfigMaps = []v1alpha1.ConfigMapPropagationStatusCopyConfigMap{} - cmp.Status.ObservedGeneration = cmp.Generation - - // 1. Create/update ConfigMaps from original namespace to current namespace. - // 2. Track changes of original ConfigMaps as well as copy ConfigMaps. - - // No need to reconcile if the ConfigMapPropagation has been marked for deletion. - if cmp.DeletionTimestamp != nil { - return nil - } - - // Tell tracker to reconcile this ConfigMapPropagation whenever an original ConfigMap changes. - // Note: Temporarily set Selector to match all objects. - // If Selector is set to match specific labels (the required labels from ConfigMapPropagation), - // the tracker can't track changes when an original ConfigMap no longer has required labels. - // One alternative way is to track the name of every qualified original ConfigMap, - // but this requires to track specific Selector as well, in order to notice newly qualified original ConfigMaps. - originalConfigMapObjRef := tracker.Reference{ - Kind: configMapGVK.Kind, - APIVersion: configMapGVK.GroupVersion().String(), - Namespace: cmp.Spec.OriginalNamespace, - Selector: metav1.SetAsLabelSelector(map[string]string{}), - } - if err := r.tracker.TrackReference(originalConfigMapObjRef, cmp); err != nil { - return err - } - - // Tell tracker to reconcile this ConfigMapPropagation whenever the a copy ConfigMap changes. - // Note: Temporarily set Selector to match all objects. - // If Selector is set to match specific labels (the required labels from creation), - // the tracker can't track changes when a copy ConfigMap no longer has required labels. - // One alternative way is to track the name of every qualified copy ConfigMap. - copyConfigMapObjRef := tracker.Reference{ - Kind: configMapGVK.Kind, - APIVersion: configMapGVK.GroupVersion().String(), - Namespace: cmp.Namespace, - Selector: metav1.SetAsLabelSelector(map[string]string{}), - } - if err := r.tracker.TrackReference(copyConfigMapObjRef, cmp); err != nil { - logging.FromContext(ctx).Error("Unable to track changes to ConfigMap", zap.Error(err)) - return err - } - - if err := r.reconcileConfigMap(ctx, cmp); err != nil { - cmp.Status.MarkNotPropagated() - return err - } - - cmp.Status.MarkPropagated() - return nil -} - -func (r *Reconciler) reconcileConfigMap(ctx context.Context, cmp *v1alpha1.ConfigMapPropagation) error { - // List ConfigMaps in original namespace and create/update copy ConfigMap in current namespace. - var errs error - originalConfigMapList, err := r.configMapLister.ConfigMaps(cmp.Spec.OriginalNamespace).List(resources.ExpectedOriginalSelector(cmp.Spec.Selector)) - if err != nil { - logging.FromContext(ctx).Error("Unable to get the ConfigMap list in original namespace", zap.Error(err)) - return err - } - for _, configMap := range originalConfigMapList { - name := resources.MakeCopyConfigMapName(cmp.Name, configMap.Name) - source := types.NamespacedName{Namespace: cmp.Spec.OriginalNamespace, Name: configMap.Name}.String() - resourceVersion := configMap.ResourceVersion - var expectedStatus v1alpha1.ConfigMapPropagationStatusCopyConfigMap - // Variable "succeed" represents whether a create/update action is successful or not. - if err, succeed := r.createOrUpdateConfigMaps(ctx, cmp, configMap); err != nil { - logging.FromContext(ctx).Warn("Failed to propagate ConfigMap: ", zap.Error(err)) - controller.GetEventRecorder(ctx).Eventf(cmp, corev1.EventTypeWarning, configMapPropagationPropagateSingleConfigMapFailed, - fmt.Sprintf("Failed to propagate ConfigMap %v: %v", configMap.Name, err)) - if errs == nil { - errs = fmt.Errorf("one or more ConfigMap propagation failed") - } - expectedStatus.SetCopyConfigMapStatus(name, source, copyConfigMap, "False", err.Error(), resourceVersion) - } else if !succeed { - // If there is no error, but the create/update action is not successful, - // this indicates the copy configmap's copy label is removed. - logging.FromContext(ctx).Debug("Stop propagating ConfigMap " + configMap.Name) - controller.GetEventRecorder(ctx).Eventf(cmp, corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, - fmt.Sprintf("Stop propagating ConfigMap: %s", configMap.Name)) - expectedStatus.SetCopyConfigMapStatus(name, source, stopConfigMap, "True", - `copy ConfigMap doesn't have copy label, stop propagating this ConfigMap`, resourceVersion) - } else { - logging.FromContext(ctx).Debug("Propagate ConfigMap " + configMap.Name + " succeed") - controller.GetEventRecorder(ctx).Eventf(cmp, corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, - fmt.Sprintf("Propagate ConfigMap %v succeed", configMap.Name)) - expectedStatus.SetCopyConfigMapStatus(name, source, copyConfigMap, "True", "", resourceVersion) - } - // Update current copy configmap's status. - cmp.Status.CopyConfigMaps = append(cmp.Status.CopyConfigMaps, expectedStatus) - } - // List ConfigMaps in current namespace and delete copy ConfigMap if the corresponding original ConfigMap no longer exists or no longer has the required label. - copyConfigMapList, err := r.configMapLister.ConfigMaps(cmp.Namespace).List(labels.SelectorFromSet(map[string]string{resources.PropagationLabelKey: resources.PropagationLabelValueCopy})) - if err != nil { - logging.FromContext(ctx).Error("Unable to get the ConfigMap list in current namespace", zap.Error(err)) - return err - } - - for _, copyConfigMap := range copyConfigMapList { - // Select copy ConfigMap which is controlled by current ConfigMapPropagation. - if metav1.IsControlledBy(copyConfigMap, cmp) { - // Get the name of original ConfigMap. - // The name of Copy ConfigMap is followed by -. - originalConfigMapName := strings.TrimPrefix(copyConfigMap.Name, cmp.Name+"-") - source := types.NamespacedName{Namespace: cmp.Spec.OriginalNamespace, Name: originalConfigMapName}.String() - var expectedStatus v1alpha1.ConfigMapPropagationStatusCopyConfigMap - // Variable "succeed" represents whether a delete action is successful or not. - if err, succeed := r.deleteOrKeepConfigMap(ctx, cmp, copyConfigMap, originalConfigMapName, originalConfigMapList); err != nil { - logging.FromContext(ctx).Warn("Failed to propagate ConfigMap: ", zap.Error(err)) - controller.GetEventRecorder(ctx).Eventf(cmp, corev1.EventTypeWarning, configMapPropagationPropagateSingleConfigMapFailed, - fmt.Sprintf("Failed to propagate ConfigMap %v: %v", originalConfigMapName, err)) - if errs == nil { - errs = fmt.Errorf("one or more ConfigMap propagation failed") - } - expectedStatus.SetCopyConfigMapStatus(copyConfigMap.Name, source, deleteConfigMap, "False", err.Error(), "") - } else if succeed { - expectedStatus.SetCopyConfigMapStatus(copyConfigMap.Name, source, deleteConfigMap, "True", "", "") - } - if expectedStatus.Name != "" { - // expectedStatus with same name will be merged and updated. - cmp.Status.CopyConfigMaps = append(cmp.Status.CopyConfigMaps, expectedStatus) - } - } - } - - return errs -} - -// createOrUpdateConfigMaps will return error and bool (represents whether a create/update action is successful or not). -func (r *Reconciler) createOrUpdateConfigMaps(ctx context.Context, cmp *v1alpha1.ConfigMapPropagation, configMap *corev1.ConfigMap) (error, bool) { - expected := resources.MakeConfigMap(resources.ConfigMapArgs{ - Original: configMap, - ConfigMapPropagation: cmp, - }) - current, err := r.configMapLister.ConfigMaps(cmp.Namespace).Get(expected.Name) - if err != nil && !apierrs.IsNotFound(err) { - logging.FromContext(ctx).Error("Unable to get ConfigMap: "+current.Name+" in current namespace", zap.Error(err)) - return fmt.Errorf("error getting ConfigMap in current namespace: %w", err), false - } - - // Only update ConfigMap with knative.dev/config-propagation:copy label. - // If the ConfigMap does not have this label, the controller must not update the ConfigMap. - if current != nil { - label := current.GetLabels() - succeed := true - if label[resources.PropagationLabelKey] != resources.PropagationLabelValueCopy { - // OwnerReference will be removed when the knative.dev/config-propagation:copy label is not set in copy configmap - // so that this copy configmap will not be deleted if cmp is deleted. - expected = current.DeepCopy() - // Delete the CMP owner reference, and keep all other owner references (if any). - expected.OwnerReferences = removeOwnerReference(expected.OwnerReferences, cmp.UID) - // It will return false for the create/update action is not successful, due to removed copy label. - // But it is not an error for ConfigMapPropagation for not propagating successfully. - succeed = false - } - if _, err = r.kubeClientSet.CoreV1().ConfigMaps(expected.Namespace).Update(expected); err != nil { - return fmt.Errorf("error updating ConfigMap in current namespace: %w", err), false - } - return nil, succeed - } - - if _, err = r.kubeClientSet.CoreV1().ConfigMaps(expected.Namespace).Create(expected); err != nil { - return fmt.Errorf("error creating ConfigMap in current namespace: %w", err), false - } - return nil, true -} - -// deleteOrKeepConfigMap will return error and bool (represents whether a delete action is successful or not). -func (r *Reconciler) deleteOrKeepConfigMap(ctx context.Context, cmp *v1alpha1.ConfigMapPropagation, copyConfigMap *corev1.ConfigMap, originalConfigMapName string, originalConfigMapList []*corev1.ConfigMap) (error, bool) { - originalConfigMap, contains := contains(originalConfigMapName, originalConfigMapList) - expectedSelector := resources.ExpectedOriginalSelector(cmp.Spec.Selector) - if !contains || !expectedSelector.Matches(labels.Set(originalConfigMap.Labels)) { - // If Original ConfigMap no longer exists or no longer has the required label, delete copy ConfigMap. - logging.FromContext(ctx).Info("Original ConfigMap " + originalConfigMapName + - ` no longer exists/no longer has "knative.dev/eventing/config-propagation:original" label, delete corresponding copy ConfigMap ` + copyConfigMap.Name) - if err := r.kubeClientSet.CoreV1().ConfigMaps(cmp.Namespace).Delete(copyConfigMap.Name, &metav1.DeleteOptions{}); err != nil { - logging.FromContext(ctx).Error("error deleting ConfigMap in current namespace", zap.Error(err)) - return err, false - } - return nil, true - } - return nil, false -} - -// contains returns a configmap object if its name is in a configmaplist. -func contains(name string, list []*corev1.ConfigMap) (*corev1.ConfigMap, bool) { - for _, configMap := range list { - if configMap.Name == name { - return configMap, true - } - } - return nil, false -} - -// removeOwnerReference removes the target ownerReference and returns a new slice of ownerReferences. -func removeOwnerReference(ownerReferences []metav1.OwnerReference, uid types.UID) []metav1.OwnerReference { - var expected []metav1.OwnerReference - for _, owner := range ownerReferences { - if owner.UID != uid { - expected = append(expected, owner) - } - } - return expected -} diff --git a/pkg/reconciler/configmappropagation/configmappropagation_test.go b/pkg/reconciler/configmappropagation/configmappropagation_test.go deleted file mode 100644 index d586ba754a6..00000000000 --- a/pkg/reconciler/configmappropagation/configmappropagation_test.go +++ /dev/null @@ -1,537 +0,0 @@ -/* -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 - - http://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 configmappropagation - -import ( - "context" - "testing" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - clientgotesting "k8s.io/client-go/testing" - - "knative.dev/eventing/pkg/apis/configs/v1alpha1" - "knative.dev/eventing/pkg/client/clientset/versioned/scheme" - fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake" - "knative.dev/eventing/pkg/client/injection/reconciler/configs/v1alpha1/configmappropagation" - "knative.dev/eventing/pkg/reconciler/configmappropagation/resources" - fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - logtesting "knative.dev/pkg/logging/testing" - "knative.dev/pkg/tracker" - - . "knative.dev/eventing/pkg/reconciler/testing" - . "knative.dev/pkg/reconciler/testing" -) - -const ( - currentNS = "test-current-ns" - configMapPropagationName = "test-cmp" - originalConfigMapName = "test-original-cm" - originalNS = "knative-eventing" - - configMapPropagationGeneration = 7 -) - -var ( - selector = metav1.LabelSelector{ - MatchLabels: map[string]string{"testings": "testing"}, - } - originalSelector = metav1.LabelSelector{ - MatchLabels: map[string]string{ - "testings": "testing", - resources.PropagationLabelKey: resources.PropagationLabelValueOriginal, - }, - } - copySelector = metav1.LabelSelector{ - MatchLabels: map[string]string{ - resources.PropagationLabelKey: resources.PropagationLabelValueCopy, - resources.CopyLabelKey: resources.MakeCopyConfigMapLabel(originalNS, originalConfigMapName), - }, - } - copyConfigMapName = resources.MakeCopyConfigMapName(configMapPropagationName, originalConfigMapName) - originalData = map[string]string{"data": "original"} - copyData = map[string]string{"data": "copy"} -) - -func init() { - // Add types to scheme - _ = v1alpha1.AddToScheme(scheme.Scheme) -} - -func TestAllCase(t *testing.T) { - testKey := currentNS + "/" + configMapPropagationName - table := TableTest{ - { - Name: "bad workqueue key", - // Make sure Reconcile handles bad keys. - Key: "too/many/parts", - }, { - Name: "key not found", - // Make sure Reconcile handles good keys that don't exist. - Key: "foo/not-found", - }, { - Name: "ConfigMapPropagation not found", - Key: testKey, - }, { - Name: "ConfigMapPropagation is being deleted", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationDeletionTimestamp, - WithInitConfigMapStatus(), - ), - }, - }, { - Name: "Original ConfigMap no longer has required labels", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(metav1.LabelSelector{ - MatchLabels: map[string]string{}, - }), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: copyConfigMapName, - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Delete", "True", ""), - ), - }}, - }, { - Name: "Original ConfigMap no longer exists, delete copy configMap succeeded", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: copyConfigMapName, - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Delete", "True", ""), - ), - }}, - }, { - Name: "Original ConfigMap no longer exists, delete copy configMap failed", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("delete", "configmaps"), - }, - WantDeletes: []clientgotesting.DeleteActionImpl{{ - Name: copyConfigMapName, - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationNotPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Delete", "False", "inducing failure for delete configmaps"), - ), - }}, - WantErr: true, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, configMapPropagationPropagateSingleConfigMapFailed, - "Failed to propagate ConfigMap %v: inducing failure for delete configmaps", originalConfigMapName), - Eventf(corev1.EventTypeWarning, "InternalError", - "one or more ConfigMap propagation failed"), - }, - }, { - Name: "Original ConfigMap has changed, update copy ConfigMap failed", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - WithConfigMapData(originalData), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapData(originalData), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }}, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("update", "configmaps"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationNotPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Copy", "False", "error updating ConfigMap in current namespace: inducing failure for update configmaps"), - ), - }}, - WantErr: true, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, configMapPropagationPropagateSingleConfigMapFailed, - "Failed to propagate ConfigMap %v: error updating ConfigMap in current namespace: inducing failure for update configmaps", originalConfigMapName), - Eventf(corev1.EventTypeWarning, "InternalError", - "one or more ConfigMap propagation failed"), - }, - }, { - Name: "Original ConfigMap has changed, update copy ConfigMap succeeded", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - WithConfigMapData(originalData), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapData(originalData), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Copy", "True", ""), - ), - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, "Propagate ConfigMap test-original-cm succeed"), - }, - }, { - Name: "Copy ConfigMap has changed", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - WithConfigMapData(originalData), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapData(copyData), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapData(originalData), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Copy", "True", ""), - ), - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, "Propagate ConfigMap test-original-cm succeed"), - }, - }, { - Name: "Copy ConfigMap no longer has required labels, with only one owner reference", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(metav1.LabelSelector{ - MatchLabels: map[string]string{}, - }), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(metav1.LabelSelector{ - MatchLabels: map[string]string{}, - }), - ), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Stop", "True", `copy ConfigMap doesn't have copy label, stop propagating this ConfigMap`), - ), - }}, - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, - `Stop propagating ConfigMap: test-original-cm`), - }, - }, { - Name: "Copy ConfigMap no longer has required labels, with multiple owner references", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - ), - NewConfigMapPropagation("default", currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationUID("1234"), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - ), - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(metav1.LabelSelector{ - MatchLabels: map[string]string{}, - }), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - WithConfigMapOwnerReference(NewConfigMapPropagation("additional", currentNS, WithInitConfigMapPropagationConditions, WithConfigMapPropagationUID("1234"))), - ), - }, - WantUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(metav1.LabelSelector{ - MatchLabels: map[string]string{}, - }), - WithConfigMapOwnerReference(NewConfigMapPropagation("additional", currentNS, WithInitConfigMapPropagationConditions, WithConfigMapPropagationUID("1234"))), - ), - }}, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Stop", "True", `copy ConfigMap doesn't have copy label, stop propagating this ConfigMap`), - ), - }}, - WantErr: false, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, - `Stop propagating ConfigMap: test-original-cm`), - }, - }, { - Name: "Create new ConfigMap failed", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithInitConfigMapStatus(), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - ), - }, - WantCreates: []runtime.Object{ - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "configmaps"), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationNotPropagated, - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Copy", "False", "error creating ConfigMap in current namespace: inducing failure for create configmaps"), - ), - }}, - WantErr: true, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, configMapPropagationPropagateSingleConfigMapFailed, - "Failed to propagate ConfigMap %v: error creating ConfigMap in current namespace: inducing failure for create configmaps", originalConfigMapName), - Eventf(corev1.EventTypeWarning, "InternalError", - "one or more ConfigMap propagation failed"), - }, - }, { - Name: "Successfully reconcile, became ready", - Key: testKey, - Objects: []runtime.Object{ - NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationGeneration(configMapPropagationGeneration), - WithInitConfigMapStatus(), - ), - NewConfigMap(originalConfigMapName, originalNS, - WithConfigMapLabels(originalSelector), - ), - }, - WantCreates: []runtime.Object{ - NewConfigMap(copyConfigMapName, currentNS, - WithConfigMapLabels(copySelector), - WithConfigMapOwnerReference(NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - )), - ), - }, - WantStatusUpdates: []clientgotesting.UpdateActionImpl{{ - Object: NewConfigMapPropagation(configMapPropagationName, currentNS, - WithInitConfigMapPropagationConditions, - WithConfigMapPropagationSelector(selector), - WithConfigMapPropagationPropagated, - WithConfigMapPropagationGeneration(configMapPropagationGeneration), - WithConfigMapPropagationStatusObservedGeneration(configMapPropagationGeneration), - WithInitConfigMapStatus(), - WithCopyConfigMapStatus("test-cmp-test-original-cm", "knative-eventing/test-original-cm", - "Copy", "True", ""), - ), - }}, - WantEvents: []string{ - Eventf(corev1.EventTypeNormal, configMapPropagationPropagateSingleConfigMapSucceed, "Propagate ConfigMap test-original-cm succeed"), - }, - }, - } - logger := logtesting.TestLogger(t) - table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler { - r := &Reconciler{ - kubeClientSet: fakekubeclient.Get(ctx), - configMapPropagationLister: listers.GetConfigMapPropagationLister(), - configMapLister: listers.GetConfigMapLister(), - tracker: tracker.New(func(types.NamespacedName) {}, 0), - } - return configmappropagation.NewReconciler(ctx, logger, - fakeeventingclient.Get(ctx), listers.GetConfigMapPropagationLister(), - controller.GetEventRecorder(ctx), r) - }, - false, - logger, - )) -} diff --git a/pkg/reconciler/configmappropagation/controller.go b/pkg/reconciler/configmappropagation/controller.go deleted file mode 100644 index 3f33a2657c4..00000000000 --- a/pkg/reconciler/configmappropagation/controller.go +++ /dev/null @@ -1,67 +0,0 @@ -/* -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 - - http://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 configmappropagation - -import ( - "context" - - corev1 "k8s.io/api/core/v1" - kubeclient "knative.dev/pkg/client/injection/kube/client" - - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - "knative.dev/pkg/logging" - "knative.dev/pkg/tracker" - - configmappropagationinformer "knative.dev/eventing/pkg/client/injection/informers/configs/v1alpha1/configmappropagation" - "knative.dev/eventing/pkg/client/injection/reconciler/configs/v1alpha1/configmappropagation" - configmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap" -) - -// NewController initializes the controller and is called by the generated code -// Registers event handlers to enqueue events -func NewController( - ctx context.Context, - cmw configmap.Watcher, -) *controller.Impl { - - configMapPropagationInformer := configmappropagationinformer.Get(ctx) - configMapInformer := configmapinformer.Get(ctx) - - r := &Reconciler{ - kubeClientSet: kubeclient.Get(ctx), - configMapPropagationLister: configMapPropagationInformer.Lister(), - configMapLister: configMapInformer.Lister(), - } - - impl := configmappropagation.NewImpl(ctx, r) - - logging.FromContext(ctx).Info("Setting up event handlers") - configMapPropagationInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue)) - - // Tracker is used to notify us that a ConfigMap has changed so that - // we can reconcile. - r.tracker = tracker.New(impl.EnqueueKey, controller.GetTrackerLease(ctx)) - configMapInformer.Informer().AddEventHandler(controller.HandleAll( - controller.EnsureTypeMeta( - r.tracker.OnChanged, - corev1.SchemeGroupVersion.WithKind("ConfigMap"), - ), - )) - - return impl -} diff --git a/pkg/reconciler/configmappropagation/controller_test.go b/pkg/reconciler/configmappropagation/controller_test.go deleted file mode 100644 index f74d38f89e1..00000000000 --- a/pkg/reconciler/configmappropagation/controller_test.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -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 - - http://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 configmappropagation - -import ( - "testing" - - "knative.dev/pkg/configmap" - - . "knative.dev/pkg/reconciler/testing" - - // Fake injection informers - _ "knative.dev/eventing/pkg/client/injection/informers/configs/v1alpha1/configmappropagation/fake" - _ "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/fake" - _ "knative.dev/pkg/injection/clients/dynamicclient/fake" -) - -func TestNew(t *testing.T) { - ctx, _ := SetupFakeContext(t) - - c := NewController(ctx, configmap.NewStaticWatcher()) - - if c == nil { - t.Fatal("Expected NewController to return a non-nil value") - } -} diff --git a/pkg/reconciler/configmappropagation/resources/configmap.go b/pkg/reconciler/configmappropagation/resources/configmap.go deleted file mode 100644 index 3c1bbacf9ba..00000000000 --- a/pkg/reconciler/configmappropagation/resources/configmap.go +++ /dev/null @@ -1,57 +0,0 @@ -/* -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 - - http://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 resources - -import ( - configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1" - "knative.dev/pkg/kmeta" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -type ConfigMapArgs struct { - Original *corev1.ConfigMap - ConfigMapPropagation *configsv1alpha1.ConfigMapPropagation -} - -func MakeConfigMap(args ConfigMapArgs) *corev1.ConfigMap { - return &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: MakeCopyConfigMapName(args.ConfigMapPropagation.Name, args.Original.Name), - Namespace: args.ConfigMapPropagation.Namespace, - Labels: map[string]string{ - PropagationLabelKey: PropagationLabelValueCopy, - CopyLabelKey: MakeCopyConfigMapLabel(args.Original.Namespace, args.Original.Name), - }, - OwnerReferences: []metav1.OwnerReference{ - *kmeta.NewControllerRef(args.ConfigMapPropagation), - }, - }, - Data: args.Original.Data, - } -} - -func MakeCopyConfigMapName(configMapPropagationName, configMapName string) string { - return configMapPropagationName + "-" + configMapName -} - -// MakeCopyCOnfigMapLabel uses '-' to separate namespace and configmap name instead of '/', -// for label values only accept '-', '.', '_'. -func MakeCopyConfigMapLabel(originalNamespace, originalName string) string { - return originalNamespace + "-" + originalName -} diff --git a/pkg/reconciler/configmappropagation/resources/configmap_test.go b/pkg/reconciler/configmappropagation/resources/configmap_test.go deleted file mode 100644 index c09aeca6a87..00000000000 --- a/pkg/reconciler/configmappropagation/resources/configmap_test.go +++ /dev/null @@ -1,78 +0,0 @@ -/* -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 - - http://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 resources - -import ( - "reflect" - "testing" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1" -) - -func TestMakeConfigMap(t *testing.T) { - testCases := map[string]struct { - original *corev1.ConfigMap - configmappropagation *configsv1alpha1.ConfigMapPropagation - }{ - "general configmap": { - original: &corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: "original-config-map", - Namespace: "system", - }, - }, - configmappropagation: &configsv1alpha1.ConfigMapPropagation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "cmp", - Namespace: "default", - }, - }, - }, - } - for n, tc := range testCases { - t.Run(n, func(t *testing.T) { - configmap := MakeConfigMap(ConfigMapArgs{ - tc.original, tc.configmappropagation, - }) - // If name and namespace are correct. - if name := configmap.Name; name != "cmp-original-config-map" { - t.Errorf("want %v, got %v", "cmp-original-config-map", name) - } - - if ns := configmap.Namespace; ns != tc.configmappropagation.Namespace { - t.Errorf("want %v, got %v", tc.configmappropagation.Namespace, ns) - } - - // If OwnerReferences is correct. - if !metav1.IsControlledBy(configmap, tc.configmappropagation) { - t.Errorf("Expected configmap to be controlled by the configmappropagation") - } - - // If labels are correct. - expectedLabels := map[string]string{ - PropagationLabelKey: PropagationLabelValueCopy, - CopyLabelKey: "system-original-config-map", - } - if labels := configmap.Labels; !reflect.DeepEqual(labels, expectedLabels) { - t.Errorf("want %v, got %q", expectedLabels, labels) - } - }) - } -} diff --git a/pkg/reconciler/configmappropagation/resources/labels.go b/pkg/reconciler/configmappropagation/resources/labels.go deleted file mode 100644 index 4f092750a63..00000000000 --- a/pkg/reconciler/configmappropagation/resources/labels.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -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 - - http://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 resources - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" -) - -const ( - PropagationLabelKey = "knative.dev/config-propagation" - PropagationLabelValueOriginal = "original" - PropagationLabelValueCopy = "copy" - CopyLabelKey = "knative.dev/config-original" -) - -// ExpectedOriginalSelector with return a selector which matches the input set with an additional label "knative.dev/config-propagation: original" -// This is the expected selector to select original configmaps -func ExpectedOriginalSelector(selector *metav1.LabelSelector) labels.Selector { - expectedOriginalSelector := selector.DeepCopy() - // Add original label if it doesn't exist - if expectedOriginalSelector.MatchLabels[PropagationLabelKey] == "" { - metav1.AddLabelToSelector(expectedOriginalSelector, PropagationLabelKey, PropagationLabelValueOriginal) - } - return labels.SelectorFromSet(expectedOriginalSelector.MatchLabels) -} diff --git a/pkg/reconciler/configmappropagation/resources/labels_test.go b/pkg/reconciler/configmappropagation/resources/labels_test.go deleted file mode 100644 index 0e561c0ee66..00000000000 --- a/pkg/reconciler/configmappropagation/resources/labels_test.go +++ /dev/null @@ -1,78 +0,0 @@ -/* -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 - - http://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 resources - -import ( - "reflect" - "testing" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" -) - -func TestOriginalLabels(t *testing.T) { - testCases := []struct { - Name string - F func() labels.Selector - Want labels.Selector - }{{ - Name: "empty map", - F: func() labels.Selector { - return ExpectedOriginalSelector(&metav1.LabelSelector{ - MatchLabels: map[string]string{}, - }) - }, - Want: labels.SelectorFromSet(map[string]string{ - PropagationLabelKey: PropagationLabelValueOriginal, - }), - }, { - Name: "map with existing keys", - F: func() labels.Selector { - return ExpectedOriginalSelector(&metav1.LabelSelector{ - MatchLabels: map[string]string{ - "testing": "testing", - }, - }) - }, - Want: labels.SelectorFromSet(map[string]string{ - "testing": "testing", - PropagationLabelKey: PropagationLabelValueOriginal, - }), - }, { - Name: "map with original key", - F: func() labels.Selector { - return ExpectedOriginalSelector(&metav1.LabelSelector{ - MatchLabels: map[string]string{ - "testing": "testing", - PropagationLabelKey: PropagationLabelValueOriginal, - }, - }) - }, - Want: labels.SelectorFromSet(map[string]string{ - "testing": "testing", - PropagationLabelKey: PropagationLabelValueOriginal, - }), - }} - - for _, tc := range testCases { - t.Run(tc.Name, func(t *testing.T) { - if got := tc.F(); !reflect.DeepEqual(got, tc.Want) { - t.Errorf("want %v, got %v", tc.Want, got) - } - }) - } -} diff --git a/pkg/reconciler/mtbroker/trigger.go b/pkg/reconciler/mtbroker/trigger.go index ceaf230f1f1..2682f07e8b8 100644 --- a/pkg/reconciler/mtbroker/trigger.go +++ b/pkg/reconciler/mtbroker/trigger.go @@ -31,7 +31,7 @@ import ( "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/pkg/apis/eventing/v1beta1" messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/eventing/pkg/reconciler/broker/resources" + "knative.dev/eventing/pkg/reconciler/mtbroker/resources" "knative.dev/eventing/pkg/reconciler/names" "knative.dev/eventing/pkg/reconciler/trigger/path" "knative.dev/eventing/pkg/utils" diff --git a/pkg/reconciler/namespace/controller.go b/pkg/reconciler/namespace/controller.go deleted file mode 100644 index 5a56ece34bf..00000000000 --- a/pkg/reconciler/namespace/controller.go +++ /dev/null @@ -1,101 +0,0 @@ -/* -Copyright 2019 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 - - http://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 namespace - -import ( - "context" - - "github.com/kelseyhightower/envconfig" - corev1 "k8s.io/api/core/v1" - "k8s.io/client-go/tools/cache" - kubeclient "knative.dev/pkg/client/injection/kube/client" - namespacereconciler "knative.dev/pkg/client/injection/kube/reconciler/core/v1/namespace" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - "knative.dev/pkg/logging" - - eventingclient "knative.dev/eventing/pkg/client/injection/client" - "knative.dev/eventing/pkg/client/injection/informers/configs/v1alpha1/configmappropagation" - "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/broker" - "knative.dev/pkg/client/injection/kube/informers/core/v1/namespace" - "knative.dev/pkg/client/injection/kube/informers/core/v1/serviceaccount" - "knative.dev/pkg/client/injection/kube/informers/rbac/v1/rolebinding" -) - -type envConfig struct { - BrokerPullSecretName string `envconfig:"BROKER_IMAGE_PULL_SECRET_NAME" required:"false"` -} - -// NewController initializes the controller and is called by the generated code -// Registers event handlers to enqueue events -func NewController( - ctx context.Context, - cmw configmap.Watcher, -) *controller.Impl { - logger := logging.FromContext(ctx) - namespaceInformer := namespace.Get(ctx) - serviceAccountInformer := serviceaccount.Get(ctx) - roleBindingInformer := rolebinding.Get(ctx) - brokerInformer := broker.Get(ctx) - configMapPropagationInformer := configmappropagation.Get(ctx) - - r := &Reconciler{ - eventingClientSet: eventingclient.Get(ctx), - kubeClientSet: kubeclient.Get(ctx), - namespaceLister: namespaceInformer.Lister(), - serviceAccountLister: serviceAccountInformer.Lister(), - roleBindingLister: roleBindingInformer.Lister(), - brokerLister: brokerInformer.Lister(), - configMapPropagationLister: configMapPropagationInformer.Lister(), - } - - var env envConfig - if err := envconfig.Process("", &env); err != nil { - logger.Info("no broker image pull secret name defined") - } - r.brokerPullSecretName = env.BrokerPullSecretName - - impl := namespacereconciler.NewImpl(ctx, r) - - // TODO: filter label selector: on InjectionEnabledLabels() - - logger.Info("Setting up event handlers") - namespaceInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue)) - - // Watch all the resources that this reconciler reconciles. - serviceAccountInformer.Informer().AddEventHandler( - cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGVK(corev1.SchemeGroupVersion.WithKind("Namespace")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - roleBindingInformer.Informer().AddEventHandler( - cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGVK(corev1.SchemeGroupVersion.WithKind("Namespace")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - brokerInformer.Informer().AddEventHandler( - cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGVK(corev1.SchemeGroupVersion.WithKind("Namespace")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - configMapPropagationInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{ - FilterFunc: controller.FilterControllerGVK(corev1.SchemeGroupVersion.WithKind("Namespace")), - Handler: controller.HandleAll(impl.EnqueueControllerOf), - }) - - return impl -} diff --git a/pkg/reconciler/namespace/controller_test.go b/pkg/reconciler/namespace/controller_test.go deleted file mode 100644 index 5b4e3eae421..00000000000 --- a/pkg/reconciler/namespace/controller_test.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright 2019 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 - - http://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 namespace - -import ( - "testing" - - "knative.dev/pkg/configmap" - . "knative.dev/pkg/reconciler/testing" - - // Fake injection informers - _ "knative.dev/eventing/pkg/client/injection/informers/configs/v1alpha1/configmappropagation/fake" - _ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/broker/fake" - _ "knative.dev/pkg/client/injection/kube/informers/core/v1/namespace/fake" - _ "knative.dev/pkg/client/injection/kube/informers/core/v1/serviceaccount/fake" - _ "knative.dev/pkg/client/injection/kube/informers/rbac/v1/rolebinding/fake" -) - -func TestNew(t *testing.T) { - ctx, _ := SetupFakeContext(t) - - c := NewController(ctx, configmap.NewStaticWatcher()) - - if c == nil { - t.Fatal("Expected NewController to return a non-nil value") - } -} diff --git a/pkg/reconciler/namespace/namespace.go b/pkg/reconciler/namespace/namespace.go deleted file mode 100644 index 1f92d960702..00000000000 --- a/pkg/reconciler/namespace/namespace.go +++ /dev/null @@ -1,215 +0,0 @@ -/* -Copyright 2019 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 - - http://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 namespace - -import ( - "context" - "fmt" - - corev1 "k8s.io/api/core/v1" - rbacv1 "k8s.io/api/rbac/v1" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/client-go/kubernetes" - corev1listers "k8s.io/client-go/listers/core/v1" - rbacv1listers "k8s.io/client-go/listers/rbac/v1" - "knative.dev/pkg/controller" - "knative.dev/pkg/logging" - "knative.dev/pkg/reconciler" - "knative.dev/pkg/system" - - configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" - clientset "knative.dev/eventing/pkg/client/clientset/versioned" - configslisters "knative.dev/eventing/pkg/client/listers/configs/v1alpha1" - eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1beta1" - "knative.dev/eventing/pkg/reconciler/namespace/resources" - "knative.dev/eventing/pkg/utils" - namespacereconciler "knative.dev/pkg/client/injection/kube/reconciler/core/v1/namespace" -) - -const ( - // Name of the corev1.Events emitted from the reconciliation process. - configMapPropagationCreated = "ConfigMapPropagationCreated" - brokerCreated = "BrokerCreated" - serviceAccountCreated = "BrokerServiceAccountCreated" - serviceAccountRBACCreated = "BrokerServiceAccountRBACCreated" - secretCopied = "SecretCopied" - secretCopyFailure = "SecretCopyFailure" -) - -type Reconciler struct { - brokerPullSecretName string - - eventingClientSet clientset.Interface - kubeClientSet kubernetes.Interface - - // listers index properties about resources - namespaceLister corev1listers.NamespaceLister - serviceAccountLister corev1listers.ServiceAccountLister - roleBindingLister rbacv1listers.RoleBindingLister - brokerLister eventinglisters.BrokerLister - configMapPropagationLister configslisters.ConfigMapPropagationLister -} - -// Check that our Reconciler implements namespacereconciler.Interface -var _ namespacereconciler.Interface = (*Reconciler)(nil) - -func (r *Reconciler) ReconcileKind(ctx context.Context, ns *corev1.Namespace) reconciler.Event { - if ns.Labels[resources.InjectionLabelKey] != resources.InjectionEnabledLabelValue { - logging.FromContext(ctx).Debug("Not reconciling Namespace") - return nil - } - - if _, err := r.reconcileConfigMapPropagation(ctx, ns); err != nil { - return fmt.Errorf("configMapPropagation: %w", err) - } - - if err := r.reconcileServiceAccountAndRoleBindings(ctx, ns, resources.IngressServiceAccountName, resources.IngressRoleBindingName, resources.IngressClusterRoleName); err != nil { - return fmt.Errorf("broker ingress: %w", err) - } - - if err := r.reconcileServiceAccountAndRoleBindings(ctx, ns, resources.FilterServiceAccountName, resources.FilterRoleBindingName, resources.FilterClusterRoleName); err != nil { - return fmt.Errorf("broker filter: %w", err) - } - - if _, err := r.reconcileBroker(ctx, ns); err != nil { - return fmt.Errorf("broker: %v", err) - } - - return nil -} - -// reconcileConfigMapPropagation reconciles the default ConfigMapPropagation for the Namespace 'ns'. -func (r *Reconciler) reconcileConfigMapPropagation(ctx context.Context, ns *corev1.Namespace) (*configsv1alpha1.ConfigMapPropagation, error) { - current, err := r.configMapPropagationLister.ConfigMapPropagations(ns.Name).Get(resources.DefaultConfigMapPropagationName) - - // If the resource doesn't exist, we'll create it. - if k8serrors.IsNotFound(err) { - cmp := resources.MakeConfigMapPropagation(ns) - cmp, err = r.eventingClientSet.ConfigsV1alpha1().ConfigMapPropagations(ns.Name).Create(cmp) - if err != nil { - return nil, err - } - controller.GetEventRecorder(ctx).Event(ns, corev1.EventTypeNormal, configMapPropagationCreated, - "Default ConfigMapPropagation: "+cmp.Name+" created") - return cmp, nil - } else if err != nil { - return nil, err - } - // Don't update anything that is already present. - return current, nil -} - -// reconcileServiceAccountAndRoleBinding reconciles the service account and role binding for -// Namespace 'ns'. -func (r *Reconciler) reconcileServiceAccountAndRoleBindings(ctx context.Context, ns *corev1.Namespace, saName, rbName, clusterRoleName string) error { - sa, err := r.reconcileBrokerServiceAccount(ctx, ns, resources.MakeServiceAccount(ns, saName)) - if err != nil { - return fmt.Errorf("service account '%s': %w", saName, err) - } - - _, err = r.reconcileBrokerRBAC(ctx, ns, sa, resources.MakeRoleBinding(rbName, ns, ns.Name, sa, clusterRoleName)) - if err != nil { - return fmt.Errorf("role binding '%s': %w", rbName, err) - } - - // If the Broker pull secret has not been specified, then nothing to copy. - if r.brokerPullSecretName == "" { - return nil - } - - if sa.Name == resources.IngressServiceAccountName || sa.Name == resources.FilterServiceAccountName { - // check for existence of brokerPullSecret, and skip copy if it already exists - for _, v := range sa.ImagePullSecrets { - if fmt.Sprintf("%s", v) == ("{" + r.brokerPullSecretName + "}") { - return nil - } - } - _, err := utils.CopySecret(r.kubeClientSet.CoreV1(), system.Namespace(), r.brokerPullSecretName, ns.Name, sa.Name) - if err != nil { - controller.GetEventRecorder(ctx).Event(ns, corev1.EventTypeWarning, secretCopyFailure, - fmt.Sprintf("Error copying secret %s/%s => %s/%s : %v", system.Namespace(), r.brokerPullSecretName, ns.Name, sa.Name, err)) - return fmt.Errorf("Error copying secret %s/%s => %s/%s : %w", system.Namespace(), r.brokerPullSecretName, ns.Name, sa.Name, err) - } else { - controller.GetEventRecorder(ctx).Event(ns, corev1.EventTypeNormal, secretCopied, - fmt.Sprintf("Secret copied into namespace %s/%s => %s/%s", system.Namespace(), r.brokerPullSecretName, ns.Name, sa.Name)) - } - } - - return nil -} - -// reconcileBrokerServiceAccount reconciles the Broker's service account for Namespace 'ns'. -func (r *Reconciler) reconcileBrokerServiceAccount(ctx context.Context, ns *corev1.Namespace, sa *corev1.ServiceAccount) (*corev1.ServiceAccount, error) { - current, err := r.serviceAccountLister.ServiceAccounts(ns.Name).Get(sa.Name) - - // If the resource doesn't exist, we'll create it. - if k8serrors.IsNotFound(err) { - sa, err = r.kubeClientSet.CoreV1().ServiceAccounts(ns.Name).Create(sa) - if err != nil { - return nil, err - } - controller.GetEventRecorder(ctx).Event(ns, corev1.EventTypeNormal, serviceAccountCreated, - fmt.Sprintf("ServiceAccount '%s' created for the Broker", sa.Name)) - return sa, nil - } else if err != nil { - return nil, err - } - // Don't update anything that is already present. - return current, nil -} - -// reconcileBrokerRBAC reconciles the Broker's service account RBAC for the Namespace 'ns'. -func (r *Reconciler) reconcileBrokerRBAC(ctx context.Context, ns *corev1.Namespace, sa *corev1.ServiceAccount, rb *rbacv1.RoleBinding) (*rbacv1.RoleBinding, error) { - current, err := r.roleBindingLister.RoleBindings(rb.Namespace).Get(rb.Name) - - // If the resource doesn't exist, we'll create it. - if k8serrors.IsNotFound(err) { - rb, err = r.kubeClientSet.RbacV1().RoleBindings(rb.Namespace).Create(rb) - if err != nil { - return nil, err - } - controller.GetEventRecorder(ctx).Event(ns, corev1.EventTypeNormal, serviceAccountRBACCreated, - fmt.Sprintf("RoleBinding '%s/%s' created for the Broker", rb.Namespace, rb.Name)) - return rb, nil - } else if err != nil { - return nil, err - } - // Don't update anything that is already present. - return current, nil -} - -// reconcileBroker reconciles the default Broker for the Namespace 'ns'. -func (r *Reconciler) reconcileBroker(ctx context.Context, ns *corev1.Namespace) (*v1beta1.Broker, error) { - current, err := r.brokerLister.Brokers(ns.Name).Get(resources.DefaultBrokerName) - - // If the resource doesn't exist, we'll create it. - if k8serrors.IsNotFound(err) { - b := resources.MakeBroker(ns) - b, err = r.eventingClientSet.EventingV1beta1().Brokers(ns.Name).Create(b) - if err != nil { - return nil, err - } - controller.GetEventRecorder(ctx).Event(ns, corev1.EventTypeNormal, brokerCreated, - "Default eventing.knative.dev Broker created.") - return b, nil - } else if err != nil { - return nil, err - } - // Don't update anything that is already present. - return current, nil -} diff --git a/pkg/reconciler/namespace/namespace_test.go b/pkg/reconciler/namespace/namespace_test.go deleted file mode 100644 index 29470c15952..00000000000 --- a/pkg/reconciler/namespace/namespace_test.go +++ /dev/null @@ -1,426 +0,0 @@ -/* -Copyright 2019 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 - - http://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 namespace - -import ( - "context" - "testing" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/kubernetes/scheme" - clientgotesting "k8s.io/client-go/testing" - "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" - fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake" - "knative.dev/eventing/pkg/reconciler/namespace/resources" - fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" - namespacereconciler "knative.dev/pkg/client/injection/kube/reconciler/core/v1/namespace" - "knative.dev/pkg/configmap" - "knative.dev/pkg/controller" - logtesting "knative.dev/pkg/logging/testing" - "knative.dev/pkg/system" - - . "knative.dev/eventing/pkg/reconciler/testing" - . "knative.dev/pkg/reconciler/testing" -) - -const ( - testNS = "test-namespace" - brokerImagePullSecretName = "broker-image-pull-secret" -) - -func init() { - // Add types to scheme - _ = eventingv1alpha1.AddToScheme(scheme.Scheme) -} - -func TestAllCases(t *testing.T) { - // Events - cmpEvent := Eventf(corev1.EventTypeNormal, "ConfigMapPropagationCreated", "Default ConfigMapPropagation: eventing created") - saIngressEvent := Eventf(corev1.EventTypeNormal, "BrokerServiceAccountCreated", "ServiceAccount 'eventing-broker-ingress' created for the Broker") - rbIngressEvent := Eventf(corev1.EventTypeNormal, "BrokerServiceAccountRBACCreated", "RoleBinding 'test-namespace/eventing-broker-ingress' created for the Broker") - saFilterEvent := Eventf(corev1.EventTypeNormal, "BrokerServiceAccountCreated", "ServiceAccount 'eventing-broker-filter' created for the Broker") - rbFilterEvent := Eventf(corev1.EventTypeNormal, "BrokerServiceAccountRBACCreated", "RoleBinding 'test-namespace/eventing-broker-filter' created for the Broker") - brokerEvent := Eventf(corev1.EventTypeNormal, "BrokerCreated", "Default eventing.knative.dev Broker created.") - nsEventFailure := Eventf(corev1.EventTypeWarning, "InternalError", "broker ingress: Error copying secret knative-testing/broker-image-pull-secret => test-namespace/eventing-broker-ingress : secrets \"broker-image-pull-secret\" not found") - - secretEventFilter := Eventf(corev1.EventTypeNormal, "SecretCopied", "Secret copied into namespace knative-testing/broker-image-pull-secret => test-namespace/eventing-broker-filter") - secretEventIngress := Eventf(corev1.EventTypeNormal, "SecretCopied", "Secret copied into namespace knative-testing/broker-image-pull-secret => test-namespace/eventing-broker-ingress") - secretEventFailure := Eventf(corev1.EventTypeWarning, "SecretCopyFailure", "Error copying secret knative-testing/broker-image-pull-secret => test-namespace/eventing-broker-ingress : secrets \"broker-image-pull-secret\" not found") - - // Patches - ingressPatch := createPatch(testNS, "eventing-broker-ingress") - filterPatch := createPatch(testNS, "eventing-broker-filter") - - // Object - namespace := NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ) - secret := resources.MakeSecret(brokerImagePullSecretName) - broker := resources.MakeBroker(namespace) - saIngress := resources.MakeServiceAccount(namespace, resources.IngressServiceAccountName) - rbIngress := resources.MakeRoleBinding(resources.IngressRoleBindingName, namespace, testNS, resources.MakeServiceAccount(namespace, resources.IngressServiceAccountName), resources.IngressClusterRoleName) - saFilter := resources.MakeServiceAccount(namespace, resources.FilterServiceAccountName) - rbFilter := resources.MakeRoleBinding(resources.FilterRoleBindingName, namespace, testNS, resources.MakeServiceAccount(namespace, resources.FilterServiceAccountName), resources.FilterClusterRoleName) - configMapPropagation := resources.MakeConfigMapPropagation(namespace) - - table := TableTest{{ - Name: "bad workqueue key", - // Make sure Reconcile handles bad keys. - Key: "too/many/parts", - }, { - Name: "key not found", - // Make sure Reconcile handles good keys that don't exist. - Key: "foo/not-found", - }, { - Name: "Namespace is not labeled", - Objects: []runtime.Object{ - NewNamespace(testNS), - }, - Key: testNS, - }, { - Name: "Namespace is labeled disabled", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionDisabledLabels())), - }, - Key: testNS, - }, { - Name: "Namespace is deleted no resources", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - WithNamespaceDeleted, - ), - }, - Key: testNS, - }, { - Name: "Namespace enabled", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - WantEvents: []string{ - cmpEvent, - saIngressEvent, - rbIngressEvent, - secretEventIngress, - saFilterEvent, - rbFilterEvent, - secretEventFilter, - brokerEvent, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - broker, - secret, - saIngress, - rbIngress, - secret, - saFilter, - rbFilter, - secret, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - ingressPatch, - filterPatch, - }, - }, { - Name: "Namespace enabled - secret copy fails", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: true, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "secrets"), - }, - WantEvents: []string{ - cmpEvent, - saIngressEvent, - rbIngressEvent, - secretEventFailure, - nsEventFailure, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - saIngress, - rbIngress, - }, - }, { - Name: "Namespace enabled - configmappropagation fails", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: true, - WithReactors: []clientgotesting.ReactionFunc{ - InduceFailure("create", "configmappropagations"), - }, - WantEvents: []string{ - Eventf(corev1.EventTypeWarning, "InternalError", "configMapPropagation: inducing failure for create configmappropagations"), - }, - WantCreates: []runtime.Object{ - configMapPropagation, - }, - }, { - Name: "Namespace enabled, broker exists", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - resources.MakeBroker(NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - )), - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - WantEvents: []string{ - cmpEvent, - saIngressEvent, - rbIngressEvent, - secretEventIngress, - saFilterEvent, - rbFilterEvent, - secretEventFilter, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - secret, - saIngress, - rbIngress, - secret, - saFilter, - rbFilter, - secret, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - ingressPatch, - filterPatch, - }, - }, { - Name: "Namespace enabled, broker exists with no label", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionDisabledLabels()), - ), - &v1alpha1.Broker{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: testNS, - Name: resources.DefaultBrokerName, - }, - }, - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - }, { - Name: "Namespace enabled, ingress service account exists", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - saIngress, - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - WantEvents: []string{ - cmpEvent, - rbIngressEvent, - secretEventIngress, - saFilterEvent, - rbFilterEvent, - secretEventFilter, - brokerEvent, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - broker, - secret, - rbIngress, - secret, - saFilter, - rbFilter, - secret, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - ingressPatch, - filterPatch, - }, - }, { - Name: "Namespace enabled, ingress role binding exists", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - rbIngress, - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - WantEvents: []string{ - cmpEvent, - saIngressEvent, - secretEventIngress, - saFilterEvent, - rbFilterEvent, - secretEventFilter, - brokerEvent, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - broker, - secret, - saIngress, - secret, - saFilter, - rbFilter, - secret, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - ingressPatch, - filterPatch, - }, - }, { - Name: "Namespace enabled, filter service account exists", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - saFilter, - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - WantEvents: []string{ - cmpEvent, - saIngressEvent, - rbIngressEvent, - secretEventIngress, - rbFilterEvent, - secretEventFilter, - brokerEvent, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - broker, - secret, - saIngress, - rbIngress, - secret, - rbFilter, - secret, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - ingressPatch, - filterPatch, - }, - }, { - Name: "Namespace enabled, filter role binding exists", - Objects: []runtime.Object{ - NewNamespace(testNS, - WithNamespaceLabeled(resources.InjectionEnabledLabels()), - ), - rbFilter, - }, - Key: testNS, - SkipNamespaceValidation: true, - WantErr: false, - WantEvents: []string{ - cmpEvent, - saIngressEvent, - rbIngressEvent, - secretEventIngress, - saFilterEvent, - secretEventFilter, - brokerEvent, - }, - WantCreates: []runtime.Object{ - configMapPropagation, - broker, - secret, - saIngress, - rbIngress, - secret, - saFilter, - secret, - }, - WantPatches: []clientgotesting.PatchActionImpl{ - ingressPatch, - filterPatch, - }, - }, - // TODO: we need a existing default un-owned test. - } - - logger := logtesting.TestLogger(t) - // used to determine which test we are on - testNum := 0 - table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler { - - r := &Reconciler{ - eventingClientSet: fakeeventingclient.Get(ctx), - kubeClientSet: fakekubeclient.Get(ctx), - namespaceLister: listers.GetNamespaceLister(), - brokerLister: listers.GetV1Beta1BrokerLister(), - serviceAccountLister: listers.GetServiceAccountLister(), - roleBindingLister: listers.GetRoleBindingLister(), - configMapPropagationLister: listers.GetConfigMapPropagationLister(), - brokerPullSecretName: brokerImagePullSecretName, - } - - // only create secret in required tests - createSecretTests := []string{"Namespace enabled", "Namespace enabled, broker exists", - "Namespace enabled, ingress service account exists", "Namespace enabled, ingress role binding exists", - "Namespace enabled, filter service account exists", "Namespace enabled, filter role binding exists"} - - for _, theTest := range createSecretTests { - if theTest == table[testNum].Name { - // create the required secret in knative-eventing to be copied into required namespaces - tgtNSSecrets := fakekubeclient.Get(ctx).CoreV1().Secrets(system.Namespace()) - tgtNSSecrets.Create(resources.MakeSecret(brokerImagePullSecretName)) - break - } - } - testNum++ - return namespacereconciler.NewReconciler(ctx, logger, - fakekubeclient.Get(ctx), listers.GetNamespaceLister(), - controller.GetEventRecorder(ctx), r) - }, false, logger)) -} - -func createPatch(namespace string, name string) clientgotesting.PatchActionImpl { - patch := clientgotesting.PatchActionImpl{} - patch.Namespace = namespace - patch.Name = name - patch.Patch = []byte(`{"imagePullSecrets":[{"name":"` + brokerImagePullSecretName + `"}]}`) - return patch -} diff --git a/pkg/reconciler/namespace/resources/broker.go b/pkg/reconciler/namespace/resources/broker.go deleted file mode 100644 index 4e9a2291965..00000000000 --- a/pkg/reconciler/namespace/resources/broker.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" - "knative.dev/eventing/pkg/apis/eventing/v1beta1" -) - -// MakeBroker creates a default Broker object for Namespace 'namespace'. -func MakeBroker(ns *corev1.Namespace) *v1beta1.Broker { - return &v1beta1.Broker{ - ObjectMeta: metav1.ObjectMeta{ - OwnerReferences: []metav1.OwnerReference{ - *metav1.NewControllerRef(ns.GetObjectMeta(), schema.GroupVersionKind{ - Group: corev1.SchemeGroupVersion.Group, - Version: corev1.SchemeGroupVersion.Version, - Kind: "Namespace", - }), - }, - Namespace: ns.Name, - Name: DefaultBrokerName, - Labels: OwnedLabels(), - }, - } -} diff --git a/pkg/reconciler/namespace/resources/configmappropagation.go b/pkg/reconciler/namespace/resources/configmappropagation.go deleted file mode 100644 index e33cbab930e..00000000000 --- a/pkg/reconciler/namespace/resources/configmappropagation.go +++ /dev/null @@ -1,46 +0,0 @@ -/* -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 - - http://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 resources - -import ( - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" - "knative.dev/eventing/pkg/apis/configs/v1alpha1" - "knative.dev/pkg/system" -) - -// MakeConfigMapPropagation creates a default ConfigMapPropagation object for Namespace 'namespace'. -func MakeConfigMapPropagation(namespace *corev1.Namespace) *v1alpha1.ConfigMapPropagation { - return &v1alpha1.ConfigMapPropagation{ - ObjectMeta: metav1.ObjectMeta{ - OwnerReferences: []metav1.OwnerReference{ - *metav1.NewControllerRef(namespace.GetObjectMeta(), schema.GroupVersionKind{ - Group: corev1.SchemeGroupVersion.Group, - Version: corev1.SchemeGroupVersion.Version, - Kind: "Namespace", - }), - }, - Namespace: namespace.Name, - Name: DefaultConfigMapPropagationName, - }, - Spec: v1alpha1.ConfigMapPropagationSpec{ - OriginalNamespace: system.Namespace(), - Selector: &metav1.LabelSelector{MatchLabels: ConfigMapPropagationOwnedLabels()}, - }, - } -} diff --git a/pkg/reconciler/namespace/resources/labels.go b/pkg/reconciler/namespace/resources/labels.go deleted file mode 100644 index 4512b5e2b04..00000000000 --- a/pkg/reconciler/namespace/resources/labels.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -const ( - // Label to enable knative-eventing in a namespace. - InjectionLabelKey = "knative-eventing-injection" - InjectionEnabledLabelValue = "enabled" - InjectionDisabledLabelValue = "disabled" - InjectedResourceLabel = "eventing.knative.dev/namespaceInjected" - CmpDefaultLabelKey = "knative.dev/config-category" - CmpDefaultLabelValue = "eventing" -) - -// OwnedLabels generates the labels present on injected broker resources. -func OwnedLabels() map[string]string { - return map[string]string{ - InjectedResourceLabel: "true", - } -} - -// ConfigMapPropagationOwnedLabels generates the labels present on injected broker resources. -func ConfigMapPropagationOwnedLabels() map[string]string { - return map[string]string{ - CmpDefaultLabelKey: CmpDefaultLabelValue, - } -} - -func InjectionEnabledLabels() map[string]string { - return map[string]string{ - InjectionLabelKey: InjectionEnabledLabelValue, - } -} - -func InjectionDisabledLabels() map[string]string { - return map[string]string{ - InjectionLabelKey: InjectionDisabledLabelValue, - } -} diff --git a/pkg/reconciler/namespace/resources/names.go b/pkg/reconciler/namespace/resources/names.go deleted file mode 100644 index 5f7e36a04ec..00000000000 --- a/pkg/reconciler/namespace/resources/names.go +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -const ( - DefaultBrokerName = "default" - - FilterServiceAccountName = "eventing-broker-filter" - FilterRoleBindingName = "eventing-broker-filter" - FilterClusterRoleName = "eventing-broker-filter" - - IngressServiceAccountName = "eventing-broker-ingress" - IngressRoleBindingName = "eventing-broker-ingress" - IngressClusterRoleName = "eventing-broker-ingress" - - DefaultConfigMapPropagationName = "eventing" -) diff --git a/pkg/reconciler/namespace/resources/role_binding.go b/pkg/reconciler/namespace/resources/role_binding.go deleted file mode 100644 index 776d857191f..00000000000 --- a/pkg/reconciler/namespace/resources/role_binding.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - corev1 "k8s.io/api/core/v1" - rbacv1 "k8s.io/api/rbac/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -// MakeRoleBinding creates a RoleBinding object for the Broker's filter -// service account 'sa' in the Namespace 'ns'. -func MakeRoleBinding(name string, ns *corev1.Namespace, nsName string, sa *corev1.ServiceAccount, clusterRoleName string) *rbacv1.RoleBinding { - return &rbacv1.RoleBinding{ - ObjectMeta: metav1.ObjectMeta{ - OwnerReferences: []metav1.OwnerReference{ - *metav1.NewControllerRef(ns.GetObjectMeta(), schema.GroupVersionKind{ - Group: corev1.SchemeGroupVersion.Group, - Version: corev1.SchemeGroupVersion.Version, - Kind: "Namespace", - }), - }, - Name: name, - Namespace: nsName, - Labels: OwnedLabels(), - }, - RoleRef: rbacv1.RoleRef{ - APIGroup: "rbac.authorization.k8s.io", - Kind: "ClusterRole", - Name: clusterRoleName, - }, - Subjects: []rbacv1.Subject{ - { - Kind: "ServiceAccount", - Namespace: sa.Namespace, - Name: sa.Name, - }, - }, - } -} diff --git a/pkg/reconciler/namespace/resources/secret.go b/pkg/reconciler/namespace/resources/secret.go deleted file mode 100644 index d1fbb14f3fa..00000000000 --- a/pkg/reconciler/namespace/resources/secret.go +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// MakeSecret creates a Secret object. -func MakeSecret(name string) *corev1.Secret { - return &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - }, - } -} diff --git a/pkg/reconciler/namespace/resources/service_account.go b/pkg/reconciler/namespace/resources/service_account.go deleted file mode 100644 index bbcaed2bae2..00000000000 --- a/pkg/reconciler/namespace/resources/service_account.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright 2019 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 - - http://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 resources - -import ( - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -// MakeServiceAccount creates a ServiceAccount object for the Namespace 'ns'. -func MakeServiceAccount(namespace *corev1.Namespace, name string) *corev1.ServiceAccount { - return &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{ - OwnerReferences: []metav1.OwnerReference{ - *metav1.NewControllerRef(namespace.GetObjectMeta(), schema.GroupVersionKind{ - Group: corev1.SchemeGroupVersion.Group, - Version: corev1.SchemeGroupVersion.Version, - Kind: "Namespace", - }), - }, - Namespace: namespace.Name, - Name: name, - Labels: OwnedLabels(), - }, - } -} diff --git a/pkg/upgrader/broker/v0.14.0/upgrader.go b/pkg/upgrader/broker/v0.14.0/upgrader.go index 1cd00ed55e4..34fd31683dc 100644 --- a/pkg/upgrader/broker/v0.14.0/upgrader.go +++ b/pkg/upgrader/broker/v0.14.0/upgrader.go @@ -100,7 +100,7 @@ func processBroker(ctx context.Context, broker v1alpha1.Broker) ([]byte, error) return []byte{}, nil } if _, present := annotations[eventing.BrokerClassKey]; !present { - annotations[eventing.BrokerClassKey] = eventing.ChannelBrokerClassValue + annotations[eventing.BrokerClassKey] = "ChannelBasedBroker" modified.ObjectMeta.SetAnnotations(annotations) } patch, err := duck.CreateMergePatch(broker, modified) diff --git a/pkg/utils/secret_test.go b/pkg/utils/secret_test.go index 91fa6323424..78e97555a6f 100644 --- a/pkg/utils/secret_test.go +++ b/pkg/utils/secret_test.go @@ -21,10 +21,12 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1" - "knative.dev/eventing/pkg/reconciler/namespace/resources" + + "knative.dev/eventing/pkg/reconciler/mtnamespace/resources" ) const ( @@ -73,7 +75,9 @@ func TestCopySecret(t *testing.T) { t.Run(n, func(t *testing.T) { // set up src namespace secret srcNamespaceSecrets := tc.corev1Input.Secrets(srcNamespace) - _, secretCreateError := srcNamespaceSecrets.Create(resources.MakeSecret(pullSecretName)) + _, secretCreateError := srcNamespaceSecrets.Create(&corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{Name: pullSecretName}, + }) if secretCreateError != nil { t.Errorf("error creating secret resources for test case: %s", secretCreateError) @@ -85,7 +89,7 @@ func TestCopySecret(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: tgtNamespace, }} - _, saCreateError := tgtNamespaceServiceAccts.Create(resources.MakeServiceAccount(namespace, svcAccountName)) + _, saCreateError := tgtNamespaceServiceAccts.Create(makeServiceAccount(namespace, svcAccountName)) if saCreateError != nil { t.Errorf("error creating service account resources for test case %s", saCreateError) } @@ -115,3 +119,21 @@ func TestCopySecret(t *testing.T) { }) } } + +// makeServiceAccount creates a ServiceAccount object for the Namespace 'ns'. +func makeServiceAccount(namespace *corev1.Namespace, name string) *corev1.ServiceAccount { + return &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + OwnerReferences: []metav1.OwnerReference{ + *metav1.NewControllerRef(namespace.GetObjectMeta(), schema.GroupVersionKind{ + Group: corev1.SchemeGroupVersion.Group, + Version: corev1.SchemeGroupVersion.Version, + Kind: "Namespace", + }), + }, + Namespace: namespace.Name, + Name: name, + Labels: resources.OwnedLabels(), + }, + } +} diff --git a/test/conformance/helpers/broker_tracing_test_helper.go b/test/conformance/helpers/broker_tracing_test_helper.go index 292115496d5..abcd3b132ff 100644 --- a/test/conformance/helpers/broker_tracing_test_helper.go +++ b/test/conformance/helpers/broker_tracing_test_helper.go @@ -26,7 +26,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" - "knative.dev/eventing/pkg/apis/eventing" "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/pkg/apis/eventing/v1beta1" tracinghelper "knative.dev/eventing/test/conformance/helpers/tracing" @@ -83,12 +82,6 @@ func setupBrokerTracing(brokerClass string) SetupInfrastructureFunc { loggerPodName string, tc TracingTestCase, ) (tracinghelper.TestSpanTree, lib.EventMatchFunc) { - // Create the Broker. - if brokerClass == eventing.ChannelBrokerClassValue { - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers - client.CreateConfigMapPropagationOrFail(defaultCMPName) - client.CreateRBACResourcesForBrokers() - } // Create a configmap used by the broker. client.CreateBrokerConfigMapOrFail("br", channel) diff --git a/test/e2e-common.sh b/test/e2e-common.sh index 12772e17a8f..f4569e9edf0 100755 --- a/test/e2e-common.sh +++ b/test/e2e-common.sh @@ -111,12 +111,6 @@ function install_latest_release() { fail_test "Knative latest release installation failed" } -function install_broker() { - ko apply --strict -f ${CHANNEL_BASED_BROKER_DEFAULT_CONFIG} || return 1 - ko apply --strict -f ${CHANNEL_BASED_BROKER_CONTROLLER} || return 1 - wait_until_pods_running knative-eventing || fail_test "Knative Eventing with Broker did not come up" -} - function run_postinstall() { ko apply --strict -f ${POST_INSTALL_V015} || return 1 wait_until_batch_job_complete knative-eventing || return 1 @@ -130,10 +124,6 @@ function install_mt_broker() { wait_until_pods_running knative-eventing || fail_test "Knative Eventing with MT Broker did not come up" } -function uninstall_broker() { - ko delete -f ${CHANNEL_BASED_BROKER_CONTROLLER} || return 1 -} - # Teardown the Knative environment after tests finish. function knative_teardown() { echo ">> Stopping Knative Eventing" diff --git a/test/e2e-tests.sh b/test/e2e-tests.sh index 00ac825e3a3..231a56f8cf5 100755 --- a/test/e2e-tests.sh +++ b/test/e2e-tests.sh @@ -32,13 +32,6 @@ source "$(dirname "$0")/e2e-common.sh" initialize $@ --skip-istio-addon -install_broker || fail_test "Could not install Channel Based Broker" - -echo "Running tests with Channel Based Broker" -go_test_e2e -timeout=20m -parallel=12 ./test/e2e ./test/conformance -brokerclass=ChannelBasedBroker -channels=messaging.knative.dev/v1alpha1:InMemoryChannel,messaging.knative.dev/v1alpha1:Channel,messaging.knative.dev/v1beta1:InMemoryChannel || fail_test - -uninstall_broker || fail_test "Could not uninstall Channel Based Broker" - install_mt_broker || fail_test "Could not uninstall MT Channel Based Broker" echo "Running tests with Multi Tenant Channel Based Broker" diff --git a/test/e2e-upgrade-tests.sh b/test/e2e-upgrade-tests.sh index 50865996c1a..1600a2c4593 100755 --- a/test/e2e-upgrade-tests.sh +++ b/test/e2e-upgrade-tests.sh @@ -57,7 +57,7 @@ wait_for_file /tmp/prober-ready || fail_test header "Performing upgrade to HEAD" install_head || fail_test 'Installing HEAD version of eventing failed' install_channel_crds || fail_test 'Installing HEAD channel CRDs failed' -install_broker || fail_test 'Installing HEAD Broker failed' +install_mt_broker || fail_test 'Installing HEAD Broker failed' run_postinstall || fail_test 'Running postinstall failed' diff --git a/test/e2e/broker_default_test.go b/test/e2e/broker_default_test.go index 6ea24f244a3..b9d1ffde9be 100644 --- a/test/e2e/broker_default_test.go +++ b/test/e2e/broker_default_test.go @@ -23,7 +23,7 @@ import ( "testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - pkgResources "knative.dev/eventing/pkg/reconciler/namespace/resources" + pkgResources "knative.dev/eventing/pkg/reconciler/mtnamespace/resources" "knative.dev/eventing/test/e2e/helpers" "knative.dev/eventing/test/lib" ) diff --git a/test/e2e/channel_defaulter_test.go b/test/e2e/channel_defaulter_test.go index 12b3b406d0f..a636135ed2f 100644 --- a/test/e2e/channel_defaulter_test.go +++ b/test/e2e/channel_defaulter_test.go @@ -21,30 +21,15 @@ package e2e import ( "testing" - "knative.dev/eventing/pkg/apis/eventing" "knative.dev/eventing/test/e2e/helpers" ) // TestChannelClusterDefaulter tests a cluster defaulted channel can be created with the template specified through configmap. func TestChannelClusterDefaulter(t *testing.T) { - // Since these tests already get run as part of the ChannelBasedBroker, there's no point - // rerunning them again for MT broker. Or if want to, we then have to create the namespace - // with a label that prevents the Broker from (and hence a trigger channel for it being - // created). Either case, seems silly to run the tests twice. - if brokerClass == eventing.MTChannelBrokerClassValue { - t.Skip("Not double running tests for MT Broker") - } helpers.ChannelClusterDefaulterTestHelper(t, channelTestRunner) } // TestChannelNamespaceDefaulter tests a namespace defaulted channel can be created with the template specified through configmap. func TestChannelNamespaceDefaulter(t *testing.T) { - // Since these tests already get run as part of the ChannelBasedBroker, there's no point - // rerunning them again for MT broker. Or if want to, we then have to create the namespace - // with a label that prevents the Broker from (and hence a trigger channel for it being - // created). Either case, seems silly to run the tests twice. - if brokerClass == eventing.MTChannelBrokerClassValue { - t.Skip("Not double running tests for MT Broker") - } helpers.ChannelNamespaceDefaulterTestHelper(t, channelTestRunner) } diff --git a/test/e2e/configmappropagation_default_test.go b/test/e2e/configmappropagation_default_test.go deleted file mode 100644 index 489feb77156..00000000000 --- a/test/e2e/configmappropagation_default_test.go +++ /dev/null @@ -1,93 +0,0 @@ -// +build e2e - -/* -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 - - http://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 e2e - -import ( - "encoding/json" - "testing" - - "k8s.io/apimachinery/pkg/types" - "k8s.io/apiserver/pkg/storage/names" - - "knative.dev/eventing/pkg/apis/eventing" -) - -func TestDefaultConfigMapPropagation(t *testing.T) { - // Since MT Channel Based Broker does not use ConfigMap propagation, skip these tests. - if brokerClass == eventing.MTChannelBrokerClassValue { - t.Skip("Not double running tests for MT Broker") - } - - const ( - defaultCMP = "eventing" - eventingNamespace = "knative-eventing" - ) - testingCM1 := names.SimpleNameGenerator.GenerateName("config-testing1-") - testingCM2 := names.SimpleNameGenerator.GenerateName("config-testing2-") - - client := setup(t, true) - defer tearDown(client) - - // Create two new configmaps - client.CreateConfigMapOrFail(testingCM1, eventingNamespace, map[string]string{ - "firstdata": "data1", - "seconddata": "data2", - }) - client.CreateConfigMapOrFail(testingCM2, eventingNamespace, map[string]string{ - "thirddata": "data3", - "fourthdata": "data4", - }) - - // CMP copies all required configmaps from 'eventingNamespace' to current client namespace. - client.CreateConfigMapPropagationOrFail(defaultCMP) - - // Check if copy configmaps exist. - if err := client.CheckConfigMapsExist(client.Namespace, defaultCMP+"-"+testingCM1, defaultCMP+"-"+testingCM2); err != nil { - t.Fatalf("Failed to check the existence for all copied configmaps: %v", err) - } - // Check if copy configmaps contain the same data as original configmap. - if err := client.CheckConfigMapsEqual(eventingNamespace, defaultCMP, testingCM1, testingCM2); err != nil { - t.Fatalf("Failed to check copy configamp contains the same data as original configmap: %v", err) - } - - payload := []struct { - Op string `json:"op"` - Path string `json:"path"` - }{ - {"remove", "/data/firstdata"}, - } - payloadBytes, _ := json.Marshal(payload) - - // Remove one data key from copy configmap testingCM1. - if _, err := client.Kube.Kube.CoreV1().ConfigMaps(client.Namespace).Patch(defaultCMP+"-"+testingCM1, types.JSONPatchType, payloadBytes); err != nil { - t.Fatalf("Failed to patch copy configmap: %v", err) - } - // Check if copy configmap will revert back. - if err := client.CheckConfigMapsEqual(eventingNamespace, defaultCMP, testingCM1); err != nil { - t.Fatalf("Failed to check copy configmap will revert back: %v", err) - } - - // Remove one data key from original configmap. - if _, err := client.Kube.Kube.CoreV1().ConfigMaps(eventingNamespace).Patch(testingCM1, types.JSONPatchType, payloadBytes); err != nil { - t.Fatalf("Failed to patch original configmap: %v", err) - } - // Check if copy configmap will update after original configmap changes. - if err := client.CheckConfigMapsEqual(eventingNamespace, defaultCMP, testingCM1); err != nil { - t.Fatalf("Failed to check if copy configmap will update after original configmap changes: %v", err) - } -} diff --git a/test/e2e/helpers/broker_channel_flow_test_helper.go b/test/e2e/helpers/broker_channel_flow_test_helper.go index c3b3ca600fe..738ad1b752f 100644 --- a/test/e2e/helpers/broker_channel_flow_test_helper.go +++ b/test/e2e/helpers/broker_channel_flow_test_helper.go @@ -22,7 +22,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" - "knative.dev/eventing/pkg/apis/eventing" "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/cloudevents" @@ -61,11 +60,6 @@ func BrokerChannelFlowTestHelper(t *testing.T, client := lib.Setup(st, true, options...) defer lib.TearDown(client) - if brokerClass == eventing.ChannelBrokerClassValue { - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers - client.CreateRBACResourcesForBrokers() - } - // Create a configmap used by the broker. client.CreateBrokerConfigMapOrFail(brokerName, &channel) diff --git a/test/e2e/helpers/broker_dls_test_helper.go b/test/e2e/helpers/broker_dls_test_helper.go index 1419c409ee8..c3c652b24e7 100644 --- a/test/e2e/helpers/broker_dls_test_helper.go +++ b/test/e2e/helpers/broker_dls_test_helper.go @@ -21,7 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/eventing/pkg/apis/eventing" "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/cloudevents" "knative.dev/eventing/test/lib/resources" @@ -49,12 +48,6 @@ func BrokerDeadLetterSinkTestHelper(t *testing.T, client := lib.Setup(st, true, options...) defer lib.TearDown(client) - if brokerClass == eventing.ChannelBrokerClassValue { - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers - client.CreateRBACResourcesForBrokers() - } - // create logger pod and service for deadlettersink loggerPod := resources.EventLoggerPod(loggerPodName) client.CreatePodOrFail(loggerPod, lib.WithService(loggerPodName)) diff --git a/test/e2e/helpers/broker_event_transformation_test_helper.go b/test/e2e/helpers/broker_event_transformation_test_helper.go index 555c5195b7f..44c5003228b 100644 --- a/test/e2e/helpers/broker_event_transformation_test_helper.go +++ b/test/e2e/helpers/broker_event_transformation_test_helper.go @@ -22,7 +22,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" - "knative.dev/eventing/pkg/apis/eventing" "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/cloudevents" @@ -56,11 +55,6 @@ func EventTransformationForTriggerTestHelper(t *testing.T, client := lib.Setup(st, true, options...) defer lib.TearDown(client) - if brokerClass == eventing.ChannelBrokerClassValue { - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers - client.CreateRBACResourcesForBrokers() - } - // Create a configmap used by the broker. config := client.CreateBrokerConfigMapOrFail(brokerName, &channel) diff --git a/test/e2e/helpers/broker_test_helper.go b/test/e2e/helpers/broker_test_helper.go index 7d044b85c5c..e52212fb192 100644 --- a/test/e2e/helpers/broker_test_helper.go +++ b/test/e2e/helpers/broker_test_helper.go @@ -22,8 +22,6 @@ import ( "strings" "testing" - "knative.dev/eventing/pkg/apis/eventing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/test/lib" @@ -67,12 +65,6 @@ type BrokerCreator func(client *lib.Client) string // ChannelBasedBrokerCreator creates a BrokerCreator that creates a broker based on the channel parameter. func ChannelBasedBrokerCreator(channel metav1.TypeMeta, brokerClass string) BrokerCreator { return func(client *lib.Client) string { - - if brokerClass == eventing.ChannelBrokerClassValue { - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers. - client.CreateRBACResourcesForBrokers() - } - brokerName := strings.ToLower(channel.Kind) // create a ConfigMap used by the broker. diff --git a/test/e2e/helpers/broker_with_config_helper.go b/test/e2e/helpers/broker_with_config_helper.go index 56f56edc8d3..02ffbf1d0c3 100644 --- a/test/e2e/helpers/broker_with_config_helper.go +++ b/test/e2e/helpers/broker_with_config_helper.go @@ -22,7 +22,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" - "knative.dev/eventing/pkg/apis/eventing" "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/cloudevents" @@ -61,11 +60,6 @@ func TestBrokerWithConfig(t *testing.T, client := lib.Setup(st, true, options...) defer lib.TearDown(client) - if brokerClass == eventing.ChannelBrokerClassValue { - // create required RBAC resources including ServiceAccounts and ClusterRoleBindings for Brokers - client.CreateRBACResourcesForBrokers() - } - config := client.CreateBrokerConfigMapOrFail(brokerName, &channel) //&channel diff --git a/test/e2e/helpers/channel_defaulter_test_helper.go b/test/e2e/helpers/channel_defaulter_test_helper.go index 2effd5f63ab..8473514868a 100644 --- a/test/e2e/helpers/channel_defaulter_test_helper.go +++ b/test/e2e/helpers/channel_defaulter_test_helper.go @@ -23,6 +23,7 @@ import ( "github.com/ghodss/yaml" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/uuid" eventingduck "knative.dev/eventing/pkg/apis/duck/v1alpha1" @@ -114,8 +115,24 @@ func defaultChannelTestHelper(t *testing.T, client *lib.Client, expectedChannel if err != nil { t.Fatalf("Failed to list the underlying channels: %v", err) } - if len(objs) != 1 { - t.Fatalf("The defaultchannel is expected to create 1 underlying channel, but got %d", len(objs)) + + // Note that since by default MT ChannelBroker creates a Broker in each namespace, there's + // actually two channels. + // https://github.com/knative/eventing/issues/3138 + // So, filter out the broker channel from the list before checking that there's only one. + filteredObjs := make([]runtime.Object, 0) + for _, o := range objs { + if o.(*eventingduck.SubscribableType).Name != "default-kne-trigger" { + filteredObjs = append(filteredObjs, o) + } + } + + if len(filteredObjs) != 1 { + t.Logf("Got unexpected channels:") + for i, ec := range filteredObjs { + t.Logf("Extra channels: %d : %+v", i, ec) + } + t.Fatalf("The defaultchannel is expected to create 1 underlying channel, but got %d", len(filteredObjs)) } // send fake CloudEvent to the channel diff --git a/test/e2e/source_api_server_test.go b/test/e2e/source_api_server_test.go index 5a779023b13..381212ec9e3 100644 --- a/test/e2e/source_api_server_test.go +++ b/test/e2e/source_api_server_test.go @@ -28,7 +28,7 @@ import ( "k8s.io/apimachinery/pkg/util/sets" sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" - pkgResources "knative.dev/eventing/pkg/reconciler/namespace/resources" + pkgResources "knative.dev/eventing/pkg/reconciler/mtnamespace/resources" eventingtesting "knative.dev/eventing/pkg/reconciler/testing" "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/resources" diff --git a/test/e2e/source_ping_test.go b/test/e2e/source_ping_test.go index f714e6388b0..5834a59841b 100644 --- a/test/e2e/source_ping_test.go +++ b/test/e2e/source_ping_test.go @@ -22,7 +22,7 @@ import ( "testing" sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" - pkgResources "knative.dev/eventing/pkg/reconciler/namespace/resources" + pkgResources "knative.dev/eventing/pkg/reconciler/mtnamespace/resources" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" diff --git a/test/e2e/trigger_dependency_annotation_test.go b/test/e2e/trigger_dependency_annotation_test.go index c2332727da6..5b97f468e75 100644 --- a/test/e2e/trigger_dependency_annotation_test.go +++ b/test/e2e/trigger_dependency_annotation_test.go @@ -26,7 +26,7 @@ import ( "knative.dev/eventing/pkg/apis/eventing" sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2" - pkgResources "knative.dev/eventing/pkg/reconciler/namespace/resources" + pkgResources "knative.dev/eventing/pkg/reconciler/mtnamespace/resources" eventingtesting "knative.dev/eventing/pkg/reconciler/testing" "knative.dev/eventing/test/lib" "knative.dev/eventing/test/lib/resources" @@ -82,9 +82,6 @@ func TestTriggerDependencyAnnotation(t *testing.T) { }, }), ) - if brokerClass == eventing.ChannelBrokerClassValue { - pingSource.Spec.SourceSpec.Sink.Ref = resources.KnativeRefForService(defaultBrokerName+"-broker", client.Namespace) - } if brokerClass == eventing.MTChannelBrokerClassValue { pingSource.Spec.SourceSpec.Sink.URI = &apis.URL{ Scheme: "http", diff --git a/test/e2e_flags.go b/test/e2e_flags.go index 6af8e9d1604..9397a67d5b2 100644 --- a/test/e2e_flags.go +++ b/test/e2e_flags.go @@ -77,7 +77,7 @@ type EventingEnvironmentFlags struct { // go1.13+, see https://github.com/knative/test-infra/issues/1329 for details func InitializeEventingFlags() { flag.Var(&EventingFlags.Channels, "channels", "The names of the channel type metas, separated by comma. Example: \"messaging.knative.dev/v1alpha1:InMemoryChannel,messaging.cloud.google.com/v1alpha1:Channel,messaging.knative.dev/v1alpha1:KafkaChannel\".") - flag.StringVar(&EventingFlags.BrokerClass, "brokerclass", "ChannelBasedBroker", "Which brokerclass to test, requires the proper Broker implementation to have been installed, and only one value. brokerclass can be one of 'ChannelBasedBroker' or 'MTChannelBasedBroker'.") + flag.StringVar(&EventingFlags.BrokerClass, "brokerclass", "MTChannelBasedBroker", "Which brokerclass to test, requires the proper Broker implementation to have been installed, and only one value. brokerclass must be (for now) 'MTChannelBasedBroker'.") flag.Parse() // If no channel is passed through the flag, initialize it as the DefaultChannel. @@ -89,7 +89,7 @@ func InitializeEventingFlags() { log.Fatalf("Brokerclass not specified") } - if EventingFlags.BrokerClass != "ChannelBasedBroker" && EventingFlags.BrokerClass != "MTChannelBasedBroker" { - log.Fatalf("Invalid Brokerclass specified, got %q must be either %q or %q", EventingFlags.BrokerClass, "ChannelBasedBroker", "MTChannelBasedBroker") + if EventingFlags.BrokerClass != "MTChannelBasedBroker" { + log.Fatalf("Invalid Brokerclass specified, got %q must be %q", EventingFlags.BrokerClass, "MTChannelBasedBroker") } } diff --git a/test/lib/creation.go b/test/lib/creation.go index b823624d0f1..a046213f810 100644 --- a/test/lib/creation.go +++ b/test/lib/creation.go @@ -28,7 +28,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1" "knative.dev/eventing/pkg/apis/eventing/v1alpha1" "knative.dev/eventing/pkg/apis/eventing/v1beta1" flowsv1alpha1 "knative.dev/eventing/pkg/apis/flows/v1alpha1" @@ -129,19 +128,6 @@ func (c *Client) CreateSubscriptionsOrFail( } } -func (c *Client) CreateConfigMapPropagationOrFail(name string) *configsv1alpha1.ConfigMapPropagation { - c.T.Logf("Creating configMapPropagation %s", name) - namespace := c.Namespace - configMapPropagation := resources.ConfigMapPropagation(name, namespace) - configMapPropagations := c.Eventing.ConfigsV1alpha1().ConfigMapPropagations(namespace) - configMapPropagation, err := configMapPropagations.Create(configMapPropagation) - if err != nil { - c.T.Fatalf("Failed to create configMapPropagation %q: %v", name, err) - } - c.Tracker.AddObj(configMapPropagation) - return configMapPropagation -} - // CreateConfigMapOrFail will create a configmap or fail the test if there is an error. func (c *Client) CreateConfigMapOrFail(name, namespace string, data map[string]string) *corev1.ConfigMap { c.T.Logf("Creating configmap %s", name) diff --git a/test/lib/resources/eventing.go b/test/lib/resources/eventing.go index 6d1d24899eb..cf35b67a938 100644 --- a/test/lib/resources/eventing.go +++ b/test/lib/resources/eventing.go @@ -28,13 +28,11 @@ import ( duckv1 "knative.dev/pkg/apis/duck/v1" pkgTest "knative.dev/pkg/test" - configsv1alpha1 "knative.dev/eventing/pkg/apis/configs/v1alpha1" eventingduckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1" eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1" messagingv1alpha1 "knative.dev/eventing/pkg/apis/messaging/v1alpha1" messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1" - "knative.dev/eventing/pkg/reconciler/namespace/resources" ) // BrokerOption enables further configuration of a Broker. @@ -251,30 +249,12 @@ func WithBrokerClassForBroker(brokerClass string) BrokerOption { } } -// ConfigMapPropagation returns a ConfigMapPropagation. -func ConfigMapPropagation(name, namespace string) *configsv1alpha1.ConfigMapPropagation { - return &configsv1alpha1.ConfigMapPropagation{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: namespace, - Name: name, - }, - Spec: configsv1alpha1.ConfigMapPropagationSpec{ - OriginalNamespace: "knative-eventing", - Selector: &metav1.LabelSelector{ - MatchLabels: resources.ConfigMapPropagationOwnedLabels(), - }, - }, - } -} - // ConfigMap returns a ConfigMap. func ConfigMap(name string, data map[string]string) *corev1.ConfigMap { return &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: name, Labels: map[string]string{ - // Filter label for a configmap being picked to be propagated by current configmappropagation. - resources.CmpDefaultLabelKey: resources.CmpDefaultLabelValue, // Default label for a configmap being eligible to be propagated. "knative.dev/config-propagation": "original", },