From 23d9ffd4c8fb8c11354352fc68ec20f929e17d41 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Sat, 7 Mar 2020 00:33:55 +0100 Subject: [PATCH 1/2] Add metaproxier unit tests --- pkg/proxy/metaproxier/meta_proxier_test.go | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 pkg/proxy/metaproxier/meta_proxier_test.go diff --git a/pkg/proxy/metaproxier/meta_proxier_test.go b/pkg/proxy/metaproxier/meta_proxier_test.go new file mode 100644 index 00000000000..3b922abbe86 --- /dev/null +++ b/pkg/proxy/metaproxier/meta_proxier_test.go @@ -0,0 +1,89 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed 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 metaproxier + +import ( + "reflect" + "testing" + + v1 "k8s.io/api/core/v1" +) + +func Test_endpointsIPFamily(t *testing.T) { + + ipv4 := v1.IPv4Protocol + ipv6 := v1.IPv6Protocol + + tests := []struct { + name string + endpoints *v1.Endpoints + want *v1.IPFamily + wantErr bool + errorMsg string + }{ + { + name: "Endpoints No Subsets", + endpoints: &v1.Endpoints{}, + want: nil, + wantErr: true, + errorMsg: "failed to identify ipfamily for endpoints (no subsets)", + }, + { + name: "Endpoints No Addresses", + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{NotReadyAddresses: []v1.EndpointAddress{}}}}, + want: nil, + wantErr: true, + errorMsg: "failed to identify ipfamily for endpoints (no addresses)", + }, + { + name: "Endpoints Address Has No IP", + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{Addresses: []v1.EndpointAddress{{Hostname: "testhost", IP: ""}}}}}, + want: nil, + wantErr: true, + errorMsg: "failed to identify ipfamily for endpoints (address has no ip)", + }, + { + name: "Endpoints Address IPv4", + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{Addresses: []v1.EndpointAddress{{IP: "1.2.3.4"}}}}}, + want: &ipv4, + wantErr: false, + }, + { + name: "Endpoints Address IPv6", + endpoints: &v1.Endpoints{Subsets: []v1.EndpointSubset{{Addresses: []v1.EndpointAddress{{IP: "2001:db9::2"}}}}}, + want: &ipv6, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := endpointsIPFamily(tt.endpoints) + if (err != nil) != tt.wantErr { + t.Errorf("endpointsIPFamily() error = %v, wantErr %v", err, tt.wantErr) + return + } + if err != nil && err.Error() != tt.errorMsg { + t.Errorf("endpointsIPFamily() error = %v, wantErr %v", err, tt.errorMsg) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("endpointsIPFamily() = %v, want %v", got, tt.want) + } + }) + } +} From df58c042a8ae6b6a522b44d81e69614038ebbe56 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Sat, 7 Mar 2020 00:37:01 +0100 Subject: [PATCH 2/2] metaproxier logging for endpoints ipfamily The kube-proxy metaproxier implementations tries to get the IPFamily from the endpoints, but if the endpoints doesn't contains an IP address it logs a Warning. This causes that services without endpoints keep flooding the logs with warnings. We log this errors with a level of Verbosity of 4 instead of a Warning --- pkg/proxy/metaproxier/BUILD | 9 ++++++++- pkg/proxy/metaproxier/meta_proxier.go | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/proxy/metaproxier/BUILD b/pkg/proxy/metaproxier/BUILD index 4f11238d4a8..cbe5ad577c0 100644 --- a/pkg/proxy/metaproxier/BUILD +++ b/pkg/proxy/metaproxier/BUILD @@ -1,6 +1,6 @@ package(default_visibility = ["//visibility:public"]) -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -28,3 +28,10 @@ filegroup( srcs = [":package-srcs"], tags = ["automanaged"], ) + +go_test( + name = "go_default_test", + srcs = ["meta_proxier_test.go"], + embed = [":go_default_library"], + deps = ["//staging/src/k8s.io/api/core/v1:go_default_library"], +) diff --git a/pkg/proxy/metaproxier/meta_proxier.go b/pkg/proxy/metaproxier/meta_proxier.go index 9bfe44275d7..21cae0d9947 100644 --- a/pkg/proxy/metaproxier/meta_proxier.go +++ b/pkg/proxy/metaproxier/meta_proxier.go @@ -103,7 +103,7 @@ func (proxier *metaProxier) OnServiceSynced() { func (proxier *metaProxier) OnEndpointsAdd(endpoints *v1.Endpoints) { ipFamily, err := endpointsIPFamily(endpoints) if err != nil { - klog.Warningf("failed to add endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) + klog.V(4).Infof("failed to add endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) return } if *ipFamily == v1.IPv4Protocol { @@ -118,7 +118,7 @@ func (proxier *metaProxier) OnEndpointsAdd(endpoints *v1.Endpoints) { func (proxier *metaProxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) { ipFamily, err := endpointsIPFamily(endpoints) if err != nil { - klog.Warningf("failed to update endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) + klog.V(4).Infof("failed to update endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) return } @@ -134,7 +134,7 @@ func (proxier *metaProxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoi func (proxier *metaProxier) OnEndpointsDelete(endpoints *v1.Endpoints) { ipFamily, err := endpointsIPFamily(endpoints) if err != nil { - klog.Warningf("failed to delete endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) + klog.V(4).Infof("failed to delete endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err) return }