github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/node_test.go (about) 1 // Copyright 2016-2019 Authors of Cilium 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 // +build !privileged_tests 16 17 package k8s 18 19 import ( 20 "testing" 21 22 "github.com/cilium/cilium/pkg/annotation" 23 "github.com/cilium/cilium/pkg/checker" 24 "github.com/cilium/cilium/pkg/k8s/types" 25 nodeAddressing "github.com/cilium/cilium/pkg/node/addressing" 26 "github.com/cilium/cilium/pkg/source" 27 28 . "gopkg.in/check.v1" 29 "k8s.io/api/core/v1" 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 ) 32 33 func (s *K8sSuite) TestParseNode(c *C) { 34 // PodCIDR takes precedence over annotations 35 k8sNode := &types.Node{ 36 ObjectMeta: metav1.ObjectMeta{ 37 Name: "node1", 38 Annotations: map[string]string{ 39 annotation.V4CIDRName: "10.254.0.0/16", 40 annotation.V6CIDRName: "f00d:aaaa:bbbb:cccc:dddd:eeee::/112", 41 }, 42 }, 43 SpecPodCIDR: "10.1.0.0/16", 44 } 45 46 n := ParseNode(k8sNode, source.Local) 47 c.Assert(n.Name, Equals, "node1") 48 c.Assert(n.IPv4AllocCIDR, NotNil) 49 c.Assert(n.IPv4AllocCIDR.String(), Equals, "10.1.0.0/16") 50 c.Assert(n.IPv6AllocCIDR, NotNil) 51 c.Assert(n.IPv6AllocCIDR.String(), Equals, "f00d:aaaa:bbbb:cccc:dddd:eeee::/112") 52 53 // No IPv6 annotation 54 k8sNode = &types.Node{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Name: "node2", 57 Annotations: map[string]string{ 58 annotation.V4CIDRName: "10.254.0.0/16", 59 }, 60 }, 61 SpecPodCIDR: "10.1.0.0/16", 62 } 63 64 n = ParseNode(k8sNode, source.Local) 65 c.Assert(n.Name, Equals, "node2") 66 c.Assert(n.IPv4AllocCIDR, NotNil) 67 c.Assert(n.IPv4AllocCIDR.String(), Equals, "10.1.0.0/16") 68 c.Assert(n.IPv6AllocCIDR, IsNil) 69 70 // No IPv6 annotation but PodCIDR with v6 71 k8sNode = &types.Node{ 72 ObjectMeta: metav1.ObjectMeta{ 73 Name: "node2", 74 Annotations: map[string]string{ 75 annotation.V4CIDRName: "10.254.0.0/16", 76 }, 77 }, 78 SpecPodCIDR: "f00d:aaaa:bbbb:cccc:dddd:eeee::/112", 79 } 80 81 n = ParseNode(k8sNode, source.Local) 82 c.Assert(n.Name, Equals, "node2") 83 c.Assert(n.IPv4AllocCIDR, NotNil) 84 c.Assert(n.IPv4AllocCIDR.String(), Equals, "10.254.0.0/16") 85 c.Assert(n.IPv6AllocCIDR, NotNil) 86 c.Assert(n.IPv6AllocCIDR.String(), Equals, "f00d:aaaa:bbbb:cccc:dddd:eeee::/112") 87 } 88 89 func Test_ParseNodeAddressType(t *testing.T) { 90 type args struct { 91 k8sNodeType v1.NodeAddressType 92 } 93 94 type result struct { 95 ciliumNodeType nodeAddressing.AddressType 96 errExists bool 97 } 98 99 tests := []struct { 100 name string 101 args args 102 want result 103 }{ 104 { 105 name: "NodeExternalDNS", 106 args: args{ 107 k8sNodeType: v1.NodeExternalDNS, 108 }, 109 want: result{ 110 ciliumNodeType: nodeAddressing.NodeExternalDNS, 111 errExists: false, 112 }, 113 }, 114 { 115 name: "NodeExternalIP", 116 args: args{ 117 k8sNodeType: v1.NodeExternalIP, 118 }, 119 want: result{ 120 ciliumNodeType: nodeAddressing.NodeExternalIP, 121 errExists: false, 122 }, 123 }, 124 { 125 name: "NodeHostName", 126 args: args{ 127 k8sNodeType: v1.NodeHostName, 128 }, 129 want: result{ 130 ciliumNodeType: nodeAddressing.NodeHostName, 131 errExists: false, 132 }, 133 }, 134 { 135 name: "NodeInternalIP", 136 args: args{ 137 k8sNodeType: v1.NodeInternalIP, 138 }, 139 want: result{ 140 ciliumNodeType: nodeAddressing.NodeInternalIP, 141 errExists: false, 142 }, 143 }, 144 { 145 name: "NodeInternalDNS", 146 args: args{ 147 k8sNodeType: v1.NodeInternalDNS, 148 }, 149 want: result{ 150 ciliumNodeType: nodeAddressing.NodeInternalDNS, 151 errExists: false, 152 }, 153 }, 154 { 155 name: "invalid", 156 args: args{ 157 k8sNodeType: v1.NodeAddressType("lololol"), 158 }, 159 want: result{ 160 ciliumNodeType: nodeAddressing.AddressType("lololol"), 161 errExists: true, 162 }, 163 }, 164 } 165 166 for _, tt := range tests { 167 t.Run(tt.name, func(t *testing.T) { 168 gotNodeAddress, gotErr := ParseNodeAddressType(tt.args.k8sNodeType) 169 res := result{ 170 ciliumNodeType: gotNodeAddress, 171 errExists: gotErr != nil, 172 } 173 args := []interface{}{res, tt.want} 174 names := []string{"obtained", "expected"} 175 if equal, err := checker.DeepEquals.Check(args, names); !equal { 176 t.Errorf("Failed to ParseNodeAddressType():\n%s", err) 177 } 178 }) 179 } 180 }