-
Notifications
You must be signed in to change notification settings - Fork 200
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
Add validation webhook for KafkaUser #968
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc33950
Add validation webhook for KafkaUser
panyuenlau beb55e9
Merge branch 'master' into kafkauser
panyuenlau adad7a0
Add if check back to avoid hitting edge case
panyuenlau 475cc1f
Merge branch 'master' into kafkauser
panyuenlau 7a083eb
Update comment
panyuenlau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and/or its affiliates | ||
// | ||
// 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 webhooks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"emperror.dev/errors" | ||
"github.com/go-logr/logr" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
banzaicloudv1alpha1 "github.com/banzaicloud/koperator/api/v1alpha1" | ||
"github.com/banzaicloud/koperator/api/v1beta1" | ||
"github.com/banzaicloud/koperator/pkg/k8sutil" | ||
"github.com/banzaicloud/koperator/pkg/util" | ||
) | ||
|
||
type KafkaUserValidator struct { | ||
Client client.Client | ||
Log logr.Logger | ||
} | ||
|
||
// ValidateCreate validates if the user-provided KafkaUser specifications valid for creation | ||
func (validator KafkaUserValidator) ValidateCreate(ctx context.Context, obj runtime.Object) error { | ||
return validator.validate(ctx, obj) | ||
} | ||
|
||
// ValidateUpdate validates if the user-provided KafkaUser specifications valid for update | ||
func (validator KafkaUserValidator) ValidateUpdate(ctx context.Context, _, newObj runtime.Object) error { | ||
return validator.validate(ctx, newObj) | ||
} | ||
|
||
// ValidateDeleet validates if the user-selctd KafkaUser specifications valid for deletion | ||
func (validator KafkaUserValidator) ValidateDelete(_ context.Context, _ runtime.Object) error { | ||
// nothing to validate on deletion | ||
return nil | ||
} | ||
|
||
func (validator KafkaUserValidator) validate(ctx context.Context, obj runtime.Object) error { | ||
var ( | ||
kafkaUser = obj.(*banzaicloudv1alpha1.KafkaUser) | ||
log = validator.Log.WithValues("name", kafkaUser.GetName(), "namespace", kafkaUser.GetNamespace()) | ||
) | ||
|
||
fieldErrs, err := validator.validateKafkaUser(ctx, kafkaUser) | ||
if err != nil { | ||
log.Error(err, errorDuringValidationMsg) | ||
return apierrors.NewInternalError(errors.WithMessage(err, errorDuringValidationMsg)) | ||
} | ||
|
||
if len(fieldErrs) == 0 { | ||
return nil | ||
} | ||
|
||
log.Info("rejected", "invalid field(s)", fieldErrs.ToAggregate().Error()) | ||
|
||
return apierrors.NewInvalid(kafkaUser.GetObjectKind().GroupVersionKind().GroupKind(), kafkaUser.Name, fieldErrs) | ||
} | ||
|
||
func (validator KafkaUserValidator) validateKafkaUser(ctx context.Context, kafkaUser *banzaicloudv1alpha1.KafkaUser) (field.ErrorList, error) { | ||
var ( | ||
err error | ||
allErrs field.ErrorList | ||
logMsg string | ||
|
||
kafkaCluster *v1beta1.KafkaCluster | ||
clusterNamespace = util.GetClusterRefNamespace(kafkaUser.GetNamespace(), kafkaUser.Spec.ClusterRef) | ||
clusterName = kafkaUser.Spec.ClusterRef.Name | ||
) | ||
|
||
if kafkaCluster, err = k8sutil.LookupKafkaCluster(ctx, validator.Client, clusterName, clusterNamespace); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
logMsg = fmt.Sprintf("KafkaCluster CR '%s' in the namespace '%s' does not exist", clusterName, clusterNamespace) | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("clusterRef"), kafkaUser.Spec.ClusterRef, logMsg)) | ||
return allErrs, nil | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
if k8sutil.IsMarkedForDeletion(kafkaCluster.ObjectMeta) { | ||
return nil, errors.New("referenced KafkaCluster CR is being deleted") | ||
} | ||
|
||
if util.ObjectManagedByClusterRegistry(kafkaCluster) { | ||
// referencing remote Kafka clusters is not allowed | ||
logMsg = fmt.Sprintf("KafkaCluster CR '%s' in the namespace '%s' is a remote resource", clusterName, clusterNamespace) | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("clusterRef"), kafkaUser.Spec.ClusterRef, logMsg)) | ||
} | ||
|
||
if kafkaUser.Spec.GetIfCertShouldBeCreated() { | ||
// avoid panic if the user wants to create a kafka user but the cluster is in plaintext mode | ||
if kafkaCluster.Spec.ListenersConfig.SSLSecrets == nil && kafkaUser.Spec.PKIBackendSpec == nil { | ||
logMsg = fmt.Sprintf("KafkaCluster CR '%s' in namespace '%s' is in plaintext mode, "+ | ||
"therefore 'spec.pkiBackendSpec' must be provided to create certificate", clusterName, clusterNamespace) | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("pkiBackendSpec"), kafkaUser.Spec.PKIBackendSpec, logMsg)) | ||
} | ||
} | ||
Comment on lines
+108
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to myself: This check is not validate against a Kafka cluster within a Istio mesh since the broker certs are issued by Istio, not Koperator |
||
|
||
return allErrs, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Won't this cause issues with clreg replicated KafkaUsers from remote K8s clusters and Kafka clusters?
I didn't see any guard against validating KafkaUsers replicated by clreg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm yea you are right, we probably shouldn't have this restriction here