Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Initial Kafka channel reconciler tests #1447

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions control-plane/pkg/reconciler/broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func brokerReconciliation(t *testing.T, format string, env config.Env) {
Name: "Reconciled normal - no DLS",
Objects: []runtime.Object{
NewBroker(),
NewConfigMap(&env, nil),
NewConfigMapWithBinaryData(&env, nil),
NewService(),
BrokerReceiverPod(env.SystemNamespace, map[string]string{
base.VolumeGenerationAnnotationKey: "0",
Expand Down Expand Up @@ -345,7 +345,7 @@ func brokerReconciliation(t *testing.T, format string, env config.Env) {
},
SkipNamespaceValidation: true, // WantCreates compare the broker namespace with configmap namespace, so skip it
WantCreates: []runtime.Object{
NewConfigMap(&env, nil),
NewConfigMapWithBinaryData(&env, nil),
},
WantUpdates: []clientgotesting.UpdateActionImpl{
ConfigMapUpdate(&env, &contract.Contract{
Expand Down Expand Up @@ -392,7 +392,7 @@ func brokerReconciliation(t *testing.T, format string, env config.Env) {
Name: "Reconciled normal - config map not readable",
Objects: []runtime.Object{
NewBroker(),
NewConfigMap(&env, []byte(`{"hello": "world"}`)),
NewConfigMapWithBinaryData(&env, []byte(`{"hello": "world"}`)),
NewService(),
BrokerReceiverPod(env.SystemNamespace, nil),
BrokerDispatcherPod(env.SystemNamespace, map[string]string{
Expand Down Expand Up @@ -887,7 +887,7 @@ func brokerReconciliation(t *testing.T, format string, env config.Env) {
),
),
BrokerConfig(bootstrapServers, 20, 5),
NewConfigMap(&env, nil),
NewConfigMapWithBinaryData(&env, nil),
NewService(),
BrokerReceiverPod(env.SystemNamespace, map[string]string{
base.VolumeGenerationAnnotationKey: "0",
Expand Down Expand Up @@ -959,7 +959,7 @@ func brokerReconciliation(t *testing.T, format string, env config.Env) {
),
NewSSLSecret(ConfigMapNamespace, "secret-1"),
BrokerConfig(bootstrapServers, 20, 5, BrokerAuthConfig("secret-1")),
NewConfigMap(&env, nil),
NewConfigMapWithBinaryData(&env, nil),
NewService(),
BrokerReceiverPod(env.SystemNamespace, map[string]string{
"annotation_to_preserve": "value_to_preserve",
Expand Down Expand Up @@ -1741,7 +1741,7 @@ func brokerFinalization(t *testing.T, format string, env config.Env) {
},
Key: testKey,
WantCreates: []runtime.Object{
NewConfigMap(&env, nil),
NewConfigMapWithBinaryData(&env, nil),
},
SkipNamespaceValidation: true, // WantCreates compare the broker namespace with configmap namespace, so skip it
OtherTestData: map[string]interface{}{
Expand Down
31 changes: 20 additions & 11 deletions control-plane/pkg/reconciler/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,35 @@ import (
"strings"

"github.com/Shopify/sarama"

"go.uber.org/multierr"
"go.uber.org/zap"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/util/retry"
"knative.dev/eventing-kafka-broker/control-plane/pkg/config"
"knative.dev/eventing-kafka-broker/control-plane/pkg/contract"
"knative.dev/eventing-kafka-broker/control-plane/pkg/receiver"
"knative.dev/eventing-kafka-broker/control-plane/pkg/security"
messagingv1beta1 "knative.dev/eventing-kafka/pkg/apis/messaging/v1beta1"
commonconfig "knative.dev/eventing-kafka/pkg/common/config"
"knative.dev/eventing-kafka/pkg/common/constants"
"knative.dev/eventing-kafka/pkg/common/kafka/offset"
commonsarama "knative.dev/eventing-kafka/pkg/common/kafka/sarama"
v1 "knative.dev/eventing/pkg/apis/duck/v1"

"knative.dev/pkg/controller"
"knative.dev/pkg/reconciler"
"knative.dev/pkg/resolver"
"knative.dev/pkg/system"

v1 "knative.dev/eventing/pkg/apis/duck/v1"

messagingv1beta1 "knative.dev/eventing-kafka/pkg/apis/messaging/v1beta1"
commonconfig "knative.dev/eventing-kafka/pkg/common/config"
"knative.dev/eventing-kafka/pkg/common/constants"
commonsarama "knative.dev/eventing-kafka/pkg/common/kafka/sarama"

"knative.dev/eventing-kafka-broker/control-plane/pkg/config"
"knative.dev/eventing-kafka-broker/control-plane/pkg/contract"
coreconfig "knative.dev/eventing-kafka-broker/control-plane/pkg/core/config"
kafkalogging "knative.dev/eventing-kafka-broker/control-plane/pkg/logging"
"knative.dev/eventing-kafka-broker/control-plane/pkg/receiver"
"knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/base"
"knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/kafka"
"knative.dev/eventing-kafka-broker/control-plane/pkg/security"
)

const (
Expand All @@ -69,6 +73,11 @@ type Reconciler struct {
// mock the function used during the reconciliation loop.
NewKafkaClient kafka.NewClientFunc

// InitOffsetsFunc initialize offsets for a provided set of topics and a provided consumer group id.
// It's convenient to add this as Reconciler field so that we can mock the function used during the
// reconciliation loop.
InitOffsetsFunc kafka.InitOffsetsFunc

ConfigMapLister corelisters.ConfigMapLister
}

Expand Down Expand Up @@ -439,7 +448,7 @@ func (r *Reconciler) reconcileInitialOffset(ctx context.Context, channel *messag

topicName := kafka.ChannelTopic(TopicPrefix, channel)
groupID := consumerGroup(channel, sub)
_, err := offset.InitOffsets(ctx, kafkaClient, kafkaClusterAdmin, []string{topicName}, groupID)
_, err := r.InitOffsetsFunc(ctx, kafkaClient, kafkaClusterAdmin, []string{topicName}, groupID)
return err
}

Expand Down
242 changes: 242 additions & 0 deletions control-plane/pkg/reconciler/channel/channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
* Copyright 2021 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 channel_test

import (
"context"
"fmt"
"testing"

"github.com/Shopify/sarama"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgotesting "k8s.io/client-go/testing"

kubeclient "knative.dev/pkg/client/injection/kube/client/fake"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
. "knative.dev/pkg/reconciler/testing"
"knative.dev/pkg/resolver"
"knative.dev/pkg/system"
"knative.dev/pkg/tracker"

"knative.dev/eventing-kafka-broker/control-plane/pkg/config"
"knative.dev/eventing-kafka-broker/control-plane/pkg/contract"
"knative.dev/eventing-kafka-broker/control-plane/pkg/receiver"
"knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/base"
. "knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/channel"
kafkatesting "knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/kafka/testing"
. "knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/testing"

messagingv1beta "knative.dev/eventing-kafka/pkg/apis/messaging/v1beta1"
fakeeventingkafkaclient "knative.dev/eventing-kafka/pkg/client/injection/client/fake"
messagingv1beta1kafkachannelreconciler "knative.dev/eventing-kafka/pkg/client/injection/reconciler/messaging/v1beta1/kafkachannel"
"knative.dev/eventing-kafka/pkg/common/constants"
)

// TODO: are we using all of these?
var configKafka = map[string]string{
"version": "1.0.0",
"sarama": `
enableLogging: false
config: |
Version: 2.0.0 # Kafka Version Compatibility From Sarama's Supported List (Major.Minor.Patch)
Admin:
Timeout: 10000000000 # 10 seconds
Net:
KeepAlive: 30000000000 # 30 seconds
MaxOpenRequests: 1 # Set to 1 for use with Idempotent Producer
TLS:
Enable: true
SASL:
Enable: true
Version: 1
Metadata:
RefreshFrequency: 300000000000 # 5 minutes
`,
"eventing-kafka": `
kafka:
authSecretName: kafka-cluster
authSecretNamespace: knative-eventing
brokers: kafka:9092
`,
}

const (
finalizerName = "kafkachannels.messaging.knative.dev"
)

var finalizerUpdatedEvent = Eventf(
corev1.EventTypeNormal,
"FinalizerUpdate",
fmt.Sprintf(`Updated %q finalizers`, ChannelName),
)

var DefaultEnv = &config.Env{
DataPlaneConfigMapNamespace: "knative-eventing",
DataPlaneConfigMapName: "kafka-channel-channels-subscriptions",
GeneralConfigMapName: "kafka-broker-config",
IngressName: "kafka-channel-ingress",
SystemNamespace: "knative-eventing",
DataPlaneConfigFormat: base.Json,
}

// TODO: tests with and without subscriptions
// TODO: tests for things from config-kafka
// TODO: are we setting a InitialOffsetsCommitted status?
// TODO: test if things from spec is used properly?

func TestReconcileKind(t *testing.T) {

messagingv1beta.RegisterAlternateKafkaChannelConditionSet(base.IngressConditionSet)

env := *DefaultEnv
testKey := fmt.Sprintf("%s/%s", ChannelNamespace, ChannelName)

table := TableTest{
{
Name: "Reconciled normal - no subscription - no auth",
Objects: []runtime.Object{
NewChannel(),
NewService(),
ChannelReceiverPod(env.SystemNamespace, map[string]string{
base.VolumeGenerationAnnotationKey: "0",
"annotation_to_preserve": "value_to_preserve",
}),
ChannelDispatcherPod(env.SystemNamespace, map[string]string{
base.VolumeGenerationAnnotationKey: "0",
"annotation_to_preserve": "value_to_preserve",
}),
NewConfigMapWithTextData(system.Namespace(), constants.SettingsConfigMapName, configKafka),
},
Key: testKey,
WantUpdates: []clientgotesting.UpdateActionImpl{
ConfigMapUpdate(&env, &contract.Contract{
Generation: 1,
Resources: []*contract.Resource{
{
Uid: ChannelUUID,
Topics: []string{ChannelTopic()},
BootstrapServers: ChannelBootstrapServers,
Ingress: &contract.Ingress{
IngressType: &contract.Ingress_Path{
Path: receiver.Path(ChannelNamespace, ChannelName),
},
},
},
},
}),
ChannelReceiverPodUpdate(env.SystemNamespace, map[string]string{
"annotation_to_preserve": "value_to_preserve",
base.VolumeGenerationAnnotationKey: "1",
}),
ChannelDispatcherPodUpdate(env.SystemNamespace, map[string]string{
"annotation_to_preserve": "value_to_preserve",
base.VolumeGenerationAnnotationKey: "1",
}),
},
SkipNamespaceValidation: true, // WantCreates compare the channel namespace with configmap namespace, so skip it
WantCreates: []runtime.Object{
NewConfigMapWithBinaryData(&env, nil),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewChannel(
WithInitKafkaChannelConditions,
StatusConfigParsed,
StatusConfigMapUpdatedReady(&env),
StatusTopicReadyWithName(ChannelTopic()),
StatusDataPlaneAvailable,
//StatusInitialOffsetsCommitted,
ChannelAddressable(&env),
),
},
},
WantPatches: []clientgotesting.PatchActionImpl{
patchFinalizers(),
},
WantEvents: []string{
finalizerUpdatedEvent,
},
},
}

useTable(t, table, env)
}

func useTable(t *testing.T, table TableTest, env config.Env) {
table.Test(t, NewFactory(&env, func(ctx context.Context, listers *Listers, env *config.Env, row *TableRow) controller.Reconciler {

reconciler := &Reconciler{
Reconciler: &base.Reconciler{
KubeClient: kubeclient.Get(ctx),
PodLister: listers.GetPodLister(),
SecretLister: listers.GetSecretLister(),
DataPlaneConfigMapNamespace: env.DataPlaneConfigMapNamespace,
DataPlaneConfigMapName: env.DataPlaneConfigMapName,
DataPlaneConfigFormat: env.DataPlaneConfigFormat,
SystemNamespace: env.SystemNamespace,
DispatcherLabel: base.ChannelDispatcherLabel,
ReceiverLabel: base.ChannelReceiverLabel,
},
Env: env,
ConfigMapLister: listers.GetConfigMapLister(),
InitOffsetsFunc: func(ctx context.Context, kafkaClient sarama.Client, kafkaAdminClient sarama.ClusterAdmin, topics []string, consumerGroup string) (int32, error) {
return 1, nil
},
NewKafkaClient: func(addrs []string, config *sarama.Config) (sarama.Client, error) {
return &kafkatesting.MockKafkaClient{}, nil
},
NewKafkaClusterAdminClient: func(_ []string, _ *sarama.Config) (sarama.ClusterAdmin, error) {
return &kafkatesting.MockKafkaClusterAdmin{
ExpectedTopicName: ChannelTopic(),
ExpectedTopicDetail: sarama.TopicDetail{
NumPartitions: 1,
ReplicationFactor: 1,
},
T: t,
}, nil
},
}

reconciler.ConfigMapTracker = &FakeTracker{}
reconciler.SecretTracker = &FakeTracker{}

reconciler.Resolver = resolver.NewURIResolverFromTracker(ctx, tracker.New(func(name types.NamespacedName) {}, 0))

r := messagingv1beta1kafkachannelreconciler.NewReconciler(
ctx,
logging.FromContext(ctx),
fakeeventingkafkaclient.Get(ctx),
listers.GetKafkaChannelLister(),
controller.GetEventRecorder(ctx),
reconciler,
)
return r
}))
}

func patchFinalizers() clientgotesting.PatchActionImpl {
action := clientgotesting.PatchActionImpl{}
action.Name = ChannelName
action.Namespace = ChannelNamespace
patch := `{"metadata":{"finalizers":["` + finalizerName + `"],"resourceVersion":""}}`
action.Patch = []byte(patch)
return action
}
3 changes: 3 additions & 0 deletions control-plane/pkg/reconciler/channel/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
"knative.dev/eventing-kafka/pkg/common/kafka/offset"

messagingv1beta "knative.dev/eventing-kafka/pkg/apis/messaging/v1beta1"
kafkachannelinformer "knative.dev/eventing-kafka/pkg/client/injection/informers/messaging/v1beta1/kafkachannel"
Expand All @@ -42,6 +43,7 @@ import (
"knative.dev/eventing-kafka-broker/control-plane/pkg/reconciler/base"
)

// TODO: no need for a configmap watcher?
func NewController(ctx context.Context, watcher configmap.Watcher, configs *config.Env) *controller.Impl {

messagingv1beta.RegisterAlternateKafkaChannelConditionSet(base.IngressConditionSet)
Expand All @@ -62,6 +64,7 @@ func NewController(ctx context.Context, watcher configmap.Watcher, configs *conf
},
NewKafkaClient: sarama.NewClient,
NewKafkaClusterAdminClient: sarama.NewClusterAdmin,
InitOffsetsFunc: offset.InitOffsets,
Env: configs,
ConfigMapLister: configmapInformer.Lister(),
}
Expand Down
Loading