AlinsRan commented on code in PR #2603: URL: https://github.com/apache/apisix-ingress-controller/pull/2603#discussion_r2434446815
########## internal/webhook/v1/ssl/conflict_detector.go: ########## @@ -0,0 +1,475 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 ssl + +import ( + "context" + "fmt" + "sort" + "strings" + + corev1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" + + v1alpha1 "github.com/apache/apisix-ingress-controller/api/v1alpha1" + apiv2 "github.com/apache/apisix-ingress-controller/api/v2" + "github.com/apache/apisix-ingress-controller/internal/controller" + "github.com/apache/apisix-ingress-controller/internal/controller/indexer" + sslutil "github.com/apache/apisix-ingress-controller/internal/ssl" + internaltypes "github.com/apache/apisix-ingress-controller/internal/types" +) + +var logger = log.Log.WithName("ssl-conflict-detector") + +// HostCertMapping represents the relationship between a host and its certificate hash. +type HostCertMapping struct { + Host string + CertificateHash string + ResourceRef string +} + +// SSLConflict exposes the conflict details to the admission webhook for reporting. +type SSLConflict struct { + Host string + ConflictingResource string + CertificateHash string +} + +// ConflictDetector detects SSL conflicts among Gateway, Ingress, and ApisixTls resources. +type ConflictDetector struct { + client client.Client + secretCache map[types.NamespacedName]*secretInfo +} + +type secretInfo struct { + hash string + hosts []string +} + +// NewConflictDetector creates a detector backed by the provided client. +func NewConflictDetector(c client.Client) *ConflictDetector { + return &ConflictDetector{ + client: c, + secretCache: make(map[types.NamespacedName]*secretInfo), + } +} + +// DetectConflicts returns the list of conflicts between the provided mappings and +// existing resources that are associated with the same GatewayProxy. Best-effort: +// failures while enumerating existing resources or reading Secrets will be logged +// and result in no conflicts instead of blocking the admission. +func (d *ConflictDetector) DetectConflicts(ctx context.Context, obj client.Object, newMappings []HostCertMapping) ([]SSLConflict, error) { Review Comment: ```go if len(newMappings) == 0 { return } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
