k8s.io/apiserver@v0.31.1/pkg/endpoints/discovery/addresses_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package discovery
    18  
    19  import (
    20  	"net/http"
    21  	"reflect"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	utilnet "k8s.io/apimachinery/pkg/util/net"
    26  	netutils "k8s.io/utils/net"
    27  )
    28  
    29  func TestGetServerAddressByClientCIDRs(t *testing.T) {
    30  	publicAddressCIDRMap := []metav1.ServerAddressByClientCIDR{
    31  		{
    32  			ClientCIDR:    "0.0.0.0/0",
    33  			ServerAddress: "ExternalAddress",
    34  		},
    35  	}
    36  	internalAddressCIDRMap := []metav1.ServerAddressByClientCIDR{
    37  		publicAddressCIDRMap[0],
    38  		{
    39  			ClientCIDR:    "10.0.0.0/24",
    40  			ServerAddress: "serviceIP",
    41  		},
    42  	}
    43  	internalIP := "10.0.0.1"
    44  	publicIP := "1.1.1.1"
    45  	testCases := []struct {
    46  		Request     http.Request
    47  		ExpectedMap []metav1.ServerAddressByClientCIDR
    48  	}{
    49  		{
    50  			Request:     http.Request{},
    51  			ExpectedMap: publicAddressCIDRMap,
    52  		},
    53  		{
    54  			Request: http.Request{
    55  				Header: map[string][]string{
    56  					"X-Real-Ip": {internalIP},
    57  				},
    58  			},
    59  			ExpectedMap: internalAddressCIDRMap,
    60  		},
    61  		{
    62  			Request: http.Request{
    63  				Header: map[string][]string{
    64  					"X-Real-Ip": {publicIP},
    65  				},
    66  			},
    67  			ExpectedMap: publicAddressCIDRMap,
    68  		},
    69  		{
    70  			Request: http.Request{
    71  				Header: map[string][]string{
    72  					"X-Forwarded-For": {internalIP},
    73  				},
    74  			},
    75  			ExpectedMap: internalAddressCIDRMap,
    76  		},
    77  		{
    78  			Request: http.Request{
    79  				Header: map[string][]string{
    80  					"X-Forwarded-For": {publicIP},
    81  				},
    82  			},
    83  			ExpectedMap: publicAddressCIDRMap,
    84  		},
    85  
    86  		{
    87  			Request: http.Request{
    88  				RemoteAddr: internalIP,
    89  			},
    90  			ExpectedMap: internalAddressCIDRMap,
    91  		},
    92  		{
    93  			Request: http.Request{
    94  				RemoteAddr: publicIP,
    95  			},
    96  			ExpectedMap: publicAddressCIDRMap,
    97  		},
    98  		{
    99  			Request: http.Request{
   100  				RemoteAddr: "invalidIP",
   101  			},
   102  			ExpectedMap: publicAddressCIDRMap,
   103  		},
   104  	}
   105  
   106  	_, ipRange, _ := netutils.ParseCIDRSloppy("10.0.0.0/24")
   107  	discoveryAddresses := DefaultAddresses{DefaultAddress: "ExternalAddress"}
   108  	discoveryAddresses.CIDRRules = append(discoveryAddresses.CIDRRules,
   109  		CIDRRule{IPRange: *ipRange, Address: "serviceIP"})
   110  
   111  	for i, test := range testCases {
   112  		if a, e := discoveryAddresses.ServerAddressByClientCIDRs(utilnet.GetClientIP(&test.Request)), test.ExpectedMap; reflect.DeepEqual(e, a) != true {
   113  			t.Fatalf("test case %d failed. expected: %v, actual: %v", i+1, e, a)
   114  		}
   115  	}
   116  }