istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/model/cluster_local_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package model_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  
    22  	meshconfig "istio.io/api/mesh/v1alpha1"
    23  	"istio.io/istio/pilot/pkg/model"
    24  	"istio.io/istio/pkg/config/host"
    25  	"istio.io/istio/pkg/config/mesh"
    26  )
    27  
    28  func TestIsClusterLocal(t *testing.T) {
    29  	cases := []struct {
    30  		name     string
    31  		m        *meshconfig.MeshConfig
    32  		host     string
    33  		expected bool
    34  	}{
    35  		{
    36  			name:     "kube-system is local",
    37  			m:        mesh.DefaultMeshConfig(),
    38  			host:     "s.kube-system.svc.cluster.local",
    39  			expected: true,
    40  		},
    41  		{
    42  			name:     "api server local is local",
    43  			m:        mesh.DefaultMeshConfig(),
    44  			host:     "kubernetes.default.svc.cluster.local",
    45  			expected: true,
    46  		},
    47  		{
    48  			name:     "discovery server is local",
    49  			m:        mesh.DefaultMeshConfig(),
    50  			host:     "istiod.istio-system.svc.cluster.local",
    51  			expected: true,
    52  		},
    53  		{
    54  			name:     "not local by default",
    55  			m:        mesh.DefaultMeshConfig(),
    56  			host:     "not.cluster.local",
    57  			expected: false,
    58  		},
    59  		{
    60  			name: "override default namespace",
    61  			m: &meshconfig.MeshConfig{
    62  				// Remove the cluster-local setting for kube-system.
    63  				ServiceSettings: []*meshconfig.MeshConfig_ServiceSettings{
    64  					{
    65  						Settings: &meshconfig.MeshConfig_ServiceSettings_Settings{
    66  							ClusterLocal: false,
    67  						},
    68  						Hosts: []string{"*.kube-system.svc.cluster.local"},
    69  					},
    70  				},
    71  			},
    72  			host:     "s.kube-system.svc.cluster.local",
    73  			expected: false,
    74  		},
    75  		{
    76  			name: "override default service",
    77  			m: &meshconfig.MeshConfig{
    78  				// Remove the cluster-local setting for kube-system.
    79  				ServiceSettings: []*meshconfig.MeshConfig_ServiceSettings{
    80  					{
    81  						Settings: &meshconfig.MeshConfig_ServiceSettings_Settings{
    82  							ClusterLocal: false,
    83  						},
    84  						Hosts: []string{"kubernetes.default.svc.cluster.local"},
    85  					},
    86  				},
    87  			},
    88  			host:     "kubernetes.default.svc.cluster.local",
    89  			expected: false,
    90  		},
    91  		{
    92  			name: "local 1",
    93  			m: &meshconfig.MeshConfig{
    94  				ServiceSettings: []*meshconfig.MeshConfig_ServiceSettings{
    95  					{
    96  						Settings: &meshconfig.MeshConfig_ServiceSettings_Settings{
    97  							ClusterLocal: true,
    98  						},
    99  						Hosts: []string{
   100  							"*.ns1.svc.cluster.local",
   101  							"*.ns2.svc.cluster.local",
   102  						},
   103  					},
   104  				},
   105  			},
   106  			host:     "s.ns1.svc.cluster.local",
   107  			expected: true,
   108  		},
   109  		{
   110  			name: "local 2",
   111  			m: &meshconfig.MeshConfig{
   112  				ServiceSettings: []*meshconfig.MeshConfig_ServiceSettings{
   113  					{
   114  						Settings: &meshconfig.MeshConfig_ServiceSettings_Settings{
   115  							ClusterLocal: true,
   116  						},
   117  						Hosts: []string{
   118  							"*.ns1.svc.cluster.local",
   119  							"*.ns2.svc.cluster.local",
   120  						},
   121  					},
   122  				},
   123  			},
   124  			host:     "s.ns2.svc.cluster.local",
   125  			expected: true,
   126  		},
   127  		{
   128  			name: "not local",
   129  			m: &meshconfig.MeshConfig{
   130  				ServiceSettings: []*meshconfig.MeshConfig_ServiceSettings{
   131  					{
   132  						Settings: &meshconfig.MeshConfig_ServiceSettings_Settings{
   133  							ClusterLocal: true,
   134  						},
   135  						Hosts: []string{
   136  							"*.ns1.svc.cluster.local",
   137  							"*.ns2.svc.cluster.local",
   138  						},
   139  					},
   140  				},
   141  			},
   142  			host:     "s.ns3.svc.cluster.local",
   143  			expected: false,
   144  		},
   145  	}
   146  
   147  	for _, c := range cases {
   148  		t.Run(c.name, func(t *testing.T) {
   149  			g := NewWithT(t)
   150  
   151  			env := &model.Environment{Watcher: mesh.NewFixedWatcher(c.m)}
   152  			env.Init()
   153  
   154  			clusterLocal := env.ClusterLocal().GetClusterLocalHosts().IsClusterLocal(host.Name(c.host))
   155  			g.Expect(clusterLocal).To(Equal(c.expected))
   156  		})
   157  	}
   158  }