@@ -29,8 +29,7 @@ import (
2929 "github.com/kaasops/envoy-xds-controller/pkg/options"
3030 "github.com/kaasops/envoy-xds-controller/pkg/utils"
3131 "github.com/kaasops/envoy-xds-controller/pkg/utils/k8s"
32- corev1 "k8s.io/api/core/v1"
33- api_errors "k8s.io/apimachinery/pkg/api/errors"
32+
3433 "k8s.io/apimachinery/pkg/types"
3534 "k8s.io/client-go/discovery"
3635 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -193,7 +192,7 @@ func (vs *VirtualService) checkIfDomainAlredyExist(
193192 virtualServices := & VirtualServiceList {}
194193 listOpts := []client.ListOption {
195194 client .InNamespace (vs .Namespace ),
196- client.MatchingFields {options .VirtualServiceListenerFeild : vs .Spec .Listener .Name },
195+ client.MatchingFields {options .VirtualServiceListenerNameFeild : vs .Spec .Listener .Name },
197196 }
198197 if err := cl .List (ctx , virtualServices , listOpts ... ); err != nil {
199198 return err
@@ -236,11 +235,19 @@ func (tc *TlsConfig) Validate(
236235 return errors .Wrap (err , "cannot get TlsConfig Type" )
237236 }
238237
238+ // If Watch Namespaces set - try to found secret in all namespaces
239+ namespaces := cfg .GetWatchNamespaces ()
240+
239241 switch tlsType {
240242 case SecretRefType :
241- return validateSecretRef (ctx , client , vs .Namespace , tc .SecretRef )
243+ // If .Spec.TlsConfig.SecretRef.Namespace set - find secret only in this namespace
244+ if vs .Spec .TlsConfig .SecretRef .Namespace != nil {
245+ namespaces = []string {* vs .Spec .TlsConfig .SecretRef .Namespace }
246+ }
247+
248+ return validateSecretRef (ctx , client , namespaces , tc .SecretRef )
242249 case AutoDiscoveryType :
243- return validateAutoDiscovery (ctx , vs , client )
250+ return validateAutoDiscovery (ctx , vs , namespaces , client )
244251 }
245252
246253 return errors .New ("unexpected behavior when processing a TlsConfig Type" )
@@ -249,31 +256,37 @@ func (tc *TlsConfig) Validate(
249256func validateSecretRef (
250257 ctx context.Context ,
251258 client client.Client ,
252- namespace string ,
259+ namespaces [] string ,
253260 rr * ResourceRef ,
254261) error {
255- secret := & corev1.Secret {}
256-
257- err := client .Get (ctx , types.NamespacedName {Name : rr .Name , Namespace : namespace }, secret )
262+ secrets , err := k8s .GetCertificateSecrets (ctx , client , namespaces )
258263 if err != nil {
259- if api_errors .IsNotFound (err ) {
260- return errors .Wrap (err , fmt .Sprintf ("Secret %s from .Spec.TlsConfig.SecretRef.Name not found" , rr .Name ))
261- }
262264 return err
263265 }
264266
265- if secret .Type != corev1 .SecretTypeTLS {
266- return errors .Newf ("Secret %s is not a TLS secret" , rr .Name )
267+ for _ , secret := range secrets {
268+ if rr .Namespace == nil {
269+ if secret .Name == rr .Name {
270+ return nil
271+ }
272+ } else {
273+ if secret .Namespace == * rr .Namespace {
274+ if secret .Name == rr .Name {
275+ return nil
276+ }
277+ }
278+ }
267279 }
268280
269- // TODO (may be). Add check Certificate in secret have VS domain in DNS
281+ return errors . New ( fmt . Sprintf ( "Secret %s/%s from .Spec.TlsConfig.SecretRef not found" , * rr . Namespace , rr . Name ))
270282
271- return nil
283+ // TODO (may be). Add check Certificate in secret have VS domain in DNS
272284}
273285
274286func validateAutoDiscovery (
275287 ctx context.Context ,
276288 vs * VirtualService ,
289+ namespaces []string ,
277290 client client.Client ,
278291) error {
279292 // Get Virtual Host from Virtual Service
@@ -283,7 +296,7 @@ func validateAutoDiscovery(
283296 }
284297
285298 // Create index for fast search certificate for domain
286- index , err := k8s .IndexCertificateSecrets (ctx , client , vs . Namespace )
299+ index , err := k8s .IndexCertificateSecrets (ctx , client , namespaces )
287300 if err != nil {
288301 return errors .Wrap (err , "cannot generate TLS certificates index from Kubernetes secrets" )
289302 }
0 commit comments