From 6f2b5272d753fcac28dc400ffbbeeaa0499c0fd2 Mon Sep 17 00:00:00 2001 From: marbarta Date: Fri, 21 Jan 2022 17:09:55 +0100 Subject: [PATCH 1/2] Fix multiple KafkaTopic creation --- pkg/webhook/topic_validator.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pkg/webhook/topic_validator.go b/pkg/webhook/topic_validator.go index 48bf63974..755920b67 100644 --- a/pkg/webhook/topic_validator.go +++ b/pkg/webhook/topic_validator.go @@ -21,7 +21,6 @@ import ( admissionv1beta1 "k8s.io/api/admission/v1beta1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -154,16 +153,9 @@ func (s *webhookServer) checkExistingKafkaTopicCRs(ctx context.Context, clusterNamespace string, topic *banzaicloudv1alpha1.KafkaTopic) *admissionv1beta1.AdmissionResponse { // check KafkaTopic in the referred KafkaCluster's namespace kafkaTopicList := banzaicloudv1alpha1.KafkaTopicList{} - err := s.client.List(ctx, &kafkaTopicList, - client.MatchingFieldsSelector{ - Selector: fields.OneTermEqualSelector("spec.name", topic.Spec.Name), - }, - client.MatchingFieldsSelector{ - Selector: fields.OneTermEqualSelector("spec.clusterRef.name", topic.Spec.ClusterRef.Name), - }, - ) + err := s.client.List(ctx, &kafkaTopicList, client.MatchingFields{"spec.name": topic.Spec.Name}) if err != nil { - log.Info("failed to list KafkaTopics") + log.Info(fmt.Sprintf("Failed to list KafkaTopics. Error message: %s", err)) return notAllowed("API failure while retrieving KafkaTopic list, please try again", metav1.StatusReasonServiceUnavailable) } @@ -175,9 +167,12 @@ func (s *webhookServer) checkExistingKafkaTopicCRs(ctx context.Context, } referredNamespace := kafkaTopic.Spec.ClusterRef.Namespace - if (kafkaTopic.GetNamespace() == clusterNamespace && referredNamespace == "") || referredNamespace == clusterNamespace { - foundKafkaTopic = &kafkaTopicList.Items[i] - break + referredName := kafkaTopic.Spec.ClusterRef.Name + if referredName == topic.Spec.ClusterRef.Name { + if (kafkaTopic.GetNamespace() == clusterNamespace && referredNamespace == "") || referredNamespace == clusterNamespace { + foundKafkaTopic = &kafkaTopicList.Items[i] + break + } } } if foundKafkaTopic != nil { From 92543b6f6c96f3842e7610ba30fc089c72f5b9cf Mon Sep 17 00:00:00 2001 From: marbarta Date: Fri, 21 Jan 2022 17:30:44 +0100 Subject: [PATCH 2/2] Fix log error list --- pkg/webhook/topic_validator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/webhook/topic_validator.go b/pkg/webhook/topic_validator.go index 755920b67..f4244bffc 100644 --- a/pkg/webhook/topic_validator.go +++ b/pkg/webhook/topic_validator.go @@ -155,7 +155,7 @@ func (s *webhookServer) checkExistingKafkaTopicCRs(ctx context.Context, kafkaTopicList := banzaicloudv1alpha1.KafkaTopicList{} err := s.client.List(ctx, &kafkaTopicList, client.MatchingFields{"spec.name": topic.Spec.Name}) if err != nil { - log.Info(fmt.Sprintf("Failed to list KafkaTopics. Error message: %s", err)) + log.Error(err, "couldn't list KafkaTopic custom resources") return notAllowed("API failure while retrieving KafkaTopic list, please try again", metav1.StatusReasonServiceUnavailable) }