From 91bc902e20e754a5a72004c373ae0184fe516dec Mon Sep 17 00:00:00 2001 From: Rob Scott Date: Tue, 23 Jun 2020 15:35:12 -0700 Subject: [PATCH 1/6] Adding endpointslice.kubernetes.io/skip-mirror label --- staging/src/k8s.io/api/discovery/v1beta1/well_known_labels.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/staging/src/k8s.io/api/discovery/v1beta1/well_known_labels.go b/staging/src/k8s.io/api/discovery/v1beta1/well_known_labels.go index b0caa3c6e71..6ca64360ff9 100644 --- a/staging/src/k8s.io/api/discovery/v1beta1/well_known_labels.go +++ b/staging/src/k8s.io/api/discovery/v1beta1/well_known_labels.go @@ -25,4 +25,8 @@ const ( // same cluster. It is highly recommended to configure this label for all // EndpointSlices. LabelManagedBy = "endpointslice.kubernetes.io/managed-by" + // LabelSkipMirror can be set to true on an Endpoints resource to indicate + // that the EndpointSliceMirroring controller should not mirror this + // resource with EndpointSlices. + LabelSkipMirror = "endpointslice.kubernetes.io/skip-mirror" ) From 8691466059314c3f7d6dcffcbb76d14596ca716c Mon Sep 17 00:00:00 2001 From: Rob Scott Date: Fri, 26 Jun 2020 16:15:23 -0700 Subject: [PATCH 2/6] Adding EndpointSliceMirroring controller This will mirror custom Endpoints to EndpointSlices to ensure that applications will not need to maintain both separately. --- pkg/controller/endpointslicemirroring/BUILD | 97 ++ pkg/controller/endpointslicemirroring/OWNERS | 12 + .../endpointslicemirroring/config/BUILD | 30 + .../endpointslicemirroring/config/doc.go | 19 + .../endpointslicemirroring/config/types.go | 42 + .../config/v1alpha1/BUILD | 36 + .../config/v1alpha1/conversion.go | 40 + .../config/v1alpha1/defaults.go | 41 + .../config/v1alpha1/doc.go | 21 + .../config/v1alpha1/register.go | 34 + .../v1alpha1/zz_generated.conversion.go | 95 ++ .../config/v1alpha1/zz_generated.deepcopy.go | 21 + .../config/zz_generated.deepcopy.go | 38 + .../endpointslicemirroring/endpointset.go | 96 ++ .../endpointslice_tracker.go | 123 +++ .../endpointslice_tracker_test.go | 174 ++++ .../endpointslicemirroring_controller.go | 446 ++++++++ .../endpointslicemirroring_controller_test.go | 479 +++++++++ .../endpointslicemirroring/events.go | 29 + .../endpointslicemirroring/metrics/BUILD | 42 + .../endpointslicemirroring/metrics/cache.go | 158 +++ .../metrics/cache_test.go | 72 ++ .../endpointslicemirroring/metrics/metrics.go | 136 +++ .../endpointslicemirroring/reconciler.go | 291 ++++++ .../reconciler_helpers.go | 137 +++ .../reconciler_helpers_test.go | 180 ++++ .../endpointslicemirroring/reconciler_test.go | 968 ++++++++++++++++++ .../endpointslicemirroring/utils.go | 250 +++++ .../endpointslicemirroring/utils_test.go | 93 ++ 29 files changed, 4200 insertions(+) create mode 100644 pkg/controller/endpointslicemirroring/BUILD create mode 100644 pkg/controller/endpointslicemirroring/OWNERS create mode 100644 pkg/controller/endpointslicemirroring/config/BUILD create mode 100644 pkg/controller/endpointslicemirroring/config/doc.go create mode 100644 pkg/controller/endpointslicemirroring/config/types.go create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/BUILD create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/conversion.go create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/defaults.go create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/doc.go create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/register.go create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.conversion.go create mode 100644 pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.deepcopy.go create mode 100644 pkg/controller/endpointslicemirroring/config/zz_generated.deepcopy.go create mode 100644 pkg/controller/endpointslicemirroring/endpointset.go create mode 100644 pkg/controller/endpointslicemirroring/endpointslice_tracker.go create mode 100644 pkg/controller/endpointslicemirroring/endpointslice_tracker_test.go create mode 100644 pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go create mode 100644 pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go create mode 100644 pkg/controller/endpointslicemirroring/events.go create mode 100644 pkg/controller/endpointslicemirroring/metrics/BUILD create mode 100644 pkg/controller/endpointslicemirroring/metrics/cache.go create mode 100644 pkg/controller/endpointslicemirroring/metrics/cache_test.go create mode 100644 pkg/controller/endpointslicemirroring/metrics/metrics.go create mode 100644 pkg/controller/endpointslicemirroring/reconciler.go create mode 100644 pkg/controller/endpointslicemirroring/reconciler_helpers.go create mode 100644 pkg/controller/endpointslicemirroring/reconciler_helpers_test.go create mode 100644 pkg/controller/endpointslicemirroring/reconciler_test.go create mode 100644 pkg/controller/endpointslicemirroring/utils.go create mode 100644 pkg/controller/endpointslicemirroring/utils_test.go diff --git a/pkg/controller/endpointslicemirroring/BUILD b/pkg/controller/endpointslicemirroring/BUILD new file mode 100644 index 00000000000..ade8a8bfef6 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/BUILD @@ -0,0 +1,97 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "endpointset.go", + "endpointslice_tracker.go", + "endpointslicemirroring_controller.go", + "events.go", + "reconciler.go", + "reconciler_helpers.go", + "utils.go", + ], + importpath = "k8s.io/kubernetes/pkg/controller/endpointslicemirroring", + visibility = ["//visibility:public"], + deps = [ + "//pkg/apis/discovery/validation:go_default_library", + "//pkg/controller:go_default_library", + "//pkg/controller/endpointslicemirroring/metrics:go_default_library", + "//pkg/controller/util/endpoint:go_default_library", + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/api/discovery/v1beta1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library", + "//staging/src/k8s.io/client-go/informers/core/v1:go_default_library", + "//staging/src/k8s.io/client-go/informers/discovery/v1beta1:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library", + "//staging/src/k8s.io/client-go/listers/core/v1:go_default_library", + "//staging/src/k8s.io/client-go/listers/discovery/v1beta1:go_default_library", + "//staging/src/k8s.io/client-go/tools/cache:go_default_library", + "//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:go_default_library", + "//staging/src/k8s.io/client-go/tools/record:go_default_library", + "//staging/src/k8s.io/client-go/util/workqueue:go_default_library", + "//staging/src/k8s.io/component-base/metrics/prometheus/ratelimiter:go_default_library", + "//vendor/golang.org/x/time/rate:go_default_library", + "//vendor/k8s.io/klog/v2:go_default_library", + ], +) + +go_test( + name = "go_default_test", + srcs = [ + "endpointslice_tracker_test.go", + "endpointslicemirroring_controller_test.go", + "reconciler_helpers_test.go", + "reconciler_test.go", + "utils_test.go", + ], + embed = [":go_default_library"], + deps = [ + "//pkg/controller:go_default_library", + "//pkg/controller/endpointslicemirroring/metrics:go_default_library", + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/api/discovery/v1beta1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library", + "//staging/src/k8s.io/client-go/informers:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library", + "//staging/src/k8s.io/client-go/testing:go_default_library", + "//staging/src/k8s.io/client-go/tools/cache:go_default_library", + "//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:go_default_library", + "//staging/src/k8s.io/client-go/tools/record:go_default_library", + "//staging/src/k8s.io/component-base/metrics/testutil:go_default_library", + "//vendor/github.com/stretchr/testify/assert:go_default_library", + "//vendor/k8s.io/utils/pointer:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [ + ":package-srcs", + "//pkg/controller/endpointslicemirroring/config:all-srcs", + "//pkg/controller/endpointslicemirroring/metrics:all-srcs", + ], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/controller/endpointslicemirroring/OWNERS b/pkg/controller/endpointslicemirroring/OWNERS new file mode 100644 index 00000000000..6337127502b --- /dev/null +++ b/pkg/controller/endpointslicemirroring/OWNERS @@ -0,0 +1,12 @@ +# See the OWNERS docs at https://go.k8s.io/owners + +approvers: +- robscott +- freehan +- sig-network-approvers +reviewers: +- robscott +- freehan +- sig-network-reviewers +labels: +- sig/network diff --git a/pkg/controller/endpointslicemirroring/config/BUILD b/pkg/controller/endpointslicemirroring/config/BUILD new file mode 100644 index 00000000000..3670c037bea --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/BUILD @@ -0,0 +1,30 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "types.go", + "zz_generated.deepcopy.go", + ], + importpath = "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config", + visibility = ["//visibility:public"], + deps = ["//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library"], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [ + ":package-srcs", + "//pkg/controller/endpointslicemirroring/config/v1alpha1:all-srcs", + ], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/controller/endpointslicemirroring/config/doc.go b/pkg/controller/endpointslicemirroring/config/doc.go new file mode 100644 index 00000000000..358fba10177 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2020 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. +*/ + +// +k8s:deepcopy-gen=package + +package config // import "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config" diff --git a/pkg/controller/endpointslicemirroring/config/types.go b/pkg/controller/endpointslicemirroring/config/types.go new file mode 100644 index 00000000000..306232ccd61 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/types.go @@ -0,0 +1,42 @@ +/* +Copyright 2020 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 config + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EndpointSliceMirroringControllerConfiguration contains elements describing +// EndpointSliceMirroringController. +type EndpointSliceMirroringControllerConfiguration struct { + // mirroringConcurrentServiceEndpointSyncs is the number of service endpoint + // syncing operations that will be done concurrently. Larger number = faster + // endpoint slice updating, but more CPU (and network) load. + MirroringConcurrentServiceEndpointSyncs int32 + + // mirroringMaxEndpointsPerSubset is the maximum number of endpoints that + // will be mirrored to an EndpointSlice for an EndpointSubset. + MirroringMaxEndpointsPerSubset int32 + + // mirroringEndpointUpdatesBatchPeriod can be used to batch EndpointSlice + // updates. All updates triggered by EndpointSlice changes will be delayed + // by up to 'mirroringEndpointUpdatesBatchPeriod'. If other addresses in the + // same Endpoints resource change in that period, they will be batched to a + // single EndpointSlice update. Default 0 value means that each Endpoints + // update triggers an EndpointSlice update. + MirroringEndpointUpdatesBatchPeriod metav1.Duration +} diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/BUILD b/pkg/controller/endpointslicemirroring/config/v1alpha1/BUILD new file mode 100644 index 00000000000..4bcda49b473 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/BUILD @@ -0,0 +1,36 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "conversion.go", + "defaults.go", + "doc.go", + "register.go", + "zz_generated.conversion.go", + "zz_generated.deepcopy.go", + ], + importpath = "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config/v1alpha1", + visibility = ["//visibility:public"], + deps = [ + "//pkg/controller/endpointslicemirroring/config:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/kube-controller-manager/config/v1alpha1:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/conversion.go b/pkg/controller/endpointslicemirroring/config/v1alpha1/conversion.go new file mode 100644 index 00000000000..d2b45a290e5 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/conversion.go @@ -0,0 +1,40 @@ +/* +Copyright 2020 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 v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/conversion" + "k8s.io/kube-controller-manager/config/v1alpha1" + endpointslicemirroringconfig "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config" +) + +// Important! The public back-and-forth conversion functions for the types in +// this package with EndpointSliceMirroringControllerConfiguratio types need to +// be manually exposed like this in order for other packages that reference this +// package to be able to call these conversion functions in an autogenerated +// manner. TODO: Fix the bug in conversion-gen so it automatically discovers +// these Convert_* functions in autogenerated code as well. + +// Convert_v1alpha1_EndpointSliceMirroringControllerConfiguration_To_config_EndpointSliceMirroringControllerConfiguration is an autogenerated conversion function. +func Convert_v1alpha1_EndpointSliceMirroringControllerConfiguration_To_config_EndpointSliceMirroringControllerConfiguration(in *v1alpha1.EndpointSliceMirroringControllerConfiguration, out *endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration, s conversion.Scope) error { + return autoConvert_v1alpha1_EndpointSliceMirroringControllerConfiguration_To_config_EndpointSliceMirroringControllerConfiguration(in, out, s) +} + +// Convert_config_EndpointSliceMirroringControllerConfiguration_To_v1alpha1_EndpointSliceMirroringControllerConfiguration is an autogenerated conversion function. +func Convert_config_EndpointSliceMirroringControllerConfiguration_To_v1alpha1_EndpointSliceMirroringControllerConfiguration(in *endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration, out *v1alpha1.EndpointSliceMirroringControllerConfiguration, s conversion.Scope) error { + return autoConvert_config_EndpointSliceMirroringControllerConfiguration_To_v1alpha1_EndpointSliceMirroringControllerConfiguration(in, out, s) +} diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/defaults.go b/pkg/controller/endpointslicemirroring/config/v1alpha1/defaults.go new file mode 100644 index 00000000000..30825a64b7c --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/defaults.go @@ -0,0 +1,41 @@ +/* +Copyright 2020 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 v1alpha1 + +import ( + kubectrlmgrconfigv1alpha1 "k8s.io/kube-controller-manager/config/v1alpha1" +) + +// RecommendedDefaultEndpointSliceMirroringControllerConfiguration defaults a +// pointer to a EndpointSliceMirroringControllerConfiguration struct. This will +// set the recommended default values, but they may be subject to change between +// API versions. This function is intentionally not registered in the scheme as +// a "normal" `SetDefaults_Foo` function to allow consumers of this type to set +// whatever defaults for their embedded configs. Forcing consumers to use these +// defaults would be problematic as defaulting in the scheme is done as part of +// the conversion, and there would be no easy way to opt-out. Instead, if you +// want to use this defaulting method run it in your wrapper struct of this type +// in its `SetDefaults_` method. +func RecommendedDefaultEndpointSliceMirroringControllerConfiguration(obj *kubectrlmgrconfigv1alpha1.EndpointSliceMirroringControllerConfiguration) { + if obj.MirroringConcurrentServiceEndpointSyncs == 0 { + obj.MirroringConcurrentServiceEndpointSyncs = 5 + } + + if obj.MirroringMaxEndpointsPerSubset == 0 { + obj.MirroringMaxEndpointsPerSubset = 1000 + } +} diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/doc.go b/pkg/controller/endpointslicemirroring/config/v1alpha1/doc.go new file mode 100644 index 00000000000..9a99542d5d8 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/doc.go @@ -0,0 +1,21 @@ +/* +Copyright 2020 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. +*/ + +// +k8s:deepcopy-gen=package +// +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config +// +k8s:conversion-gen-external-types=k8s.io/kube-controller-manager/config/v1alpha1 + +package v1alpha1 // import "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config/v1alpha1" diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/register.go b/pkg/controller/endpointslicemirroring/config/v1alpha1/register.go new file mode 100644 index 00000000000..d5f02b80660 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/register.go @@ -0,0 +1,34 @@ +/* +Copyright 2020 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 v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime" +) + +var ( + // SchemeBuilder is the scheme builder with scheme init functions to run for + // this API package + SchemeBuilder runtime.SchemeBuilder + // localSchemeBuilder extends the SchemeBuilder instance with the external + // types. In this package, defaulting and conversion init funcs are + // registered as well. + localSchemeBuilder = &SchemeBuilder + // AddToScheme is a global function that registers this API group & version + // to a scheme + AddToScheme = localSchemeBuilder.AddToScheme +) diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.conversion.go b/pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.conversion.go new file mode 100644 index 00000000000..740bf6ff5a6 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.conversion.go @@ -0,0 +1,95 @@ +// +build !ignore_autogenerated + +/* +Copyright 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. +*/ + +// Code generated by conversion-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + conversion "k8s.io/apimachinery/pkg/conversion" + runtime "k8s.io/apimachinery/pkg/runtime" + v1alpha1 "k8s.io/kube-controller-manager/config/v1alpha1" + config "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config" +) + +func init() { + localSchemeBuilder.Register(RegisterConversions) +} + +// RegisterConversions adds conversion functions to the given scheme. +// Public to allow building arbitrary schemes. +func RegisterConversions(s *runtime.Scheme) error { + if err := s.AddGeneratedConversionFunc((*v1alpha1.GroupResource)(nil), (*v1.GroupResource)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1alpha1_GroupResource_To_v1_GroupResource(a.(*v1alpha1.GroupResource), b.(*v1.GroupResource), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*v1.GroupResource)(nil), (*v1alpha1.GroupResource)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1_GroupResource_To_v1alpha1_GroupResource(a.(*v1.GroupResource), b.(*v1alpha1.GroupResource), scope) + }); err != nil { + return err + } + if err := s.AddConversionFunc((*config.EndpointSliceMirroringControllerConfiguration)(nil), (*v1alpha1.EndpointSliceMirroringControllerConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_config_EndpointSliceMirroringControllerConfiguration_To_v1alpha1_EndpointSliceMirroringControllerConfiguration(a.(*config.EndpointSliceMirroringControllerConfiguration), b.(*v1alpha1.EndpointSliceMirroringControllerConfiguration), scope) + }); err != nil { + return err + } + if err := s.AddConversionFunc((*v1alpha1.EndpointSliceMirroringControllerConfiguration)(nil), (*config.EndpointSliceMirroringControllerConfiguration)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1alpha1_EndpointSliceMirroringControllerConfiguration_To_config_EndpointSliceMirroringControllerConfiguration(a.(*v1alpha1.EndpointSliceMirroringControllerConfiguration), b.(*config.EndpointSliceMirroringControllerConfiguration), scope) + }); err != nil { + return err + } + return nil +} + +func autoConvert_v1alpha1_EndpointSliceMirroringControllerConfiguration_To_config_EndpointSliceMirroringControllerConfiguration(in *v1alpha1.EndpointSliceMirroringControllerConfiguration, out *config.EndpointSliceMirroringControllerConfiguration, s conversion.Scope) error { + out.MirroringConcurrentServiceEndpointSyncs = in.MirroringConcurrentServiceEndpointSyncs + out.MirroringMaxEndpointsPerSubset = in.MirroringMaxEndpointsPerSubset + out.MirroringEndpointUpdatesBatchPeriod = in.MirroringEndpointUpdatesBatchPeriod + return nil +} + +func autoConvert_config_EndpointSliceMirroringControllerConfiguration_To_v1alpha1_EndpointSliceMirroringControllerConfiguration(in *config.EndpointSliceMirroringControllerConfiguration, out *v1alpha1.EndpointSliceMirroringControllerConfiguration, s conversion.Scope) error { + out.MirroringConcurrentServiceEndpointSyncs = in.MirroringConcurrentServiceEndpointSyncs + out.MirroringMaxEndpointsPerSubset = in.MirroringMaxEndpointsPerSubset + out.MirroringEndpointUpdatesBatchPeriod = in.MirroringEndpointUpdatesBatchPeriod + return nil +} + +func autoConvert_v1alpha1_GroupResource_To_v1_GroupResource(in *v1alpha1.GroupResource, out *v1.GroupResource, s conversion.Scope) error { + out.Group = in.Group + out.Resource = in.Resource + return nil +} + +// Convert_v1alpha1_GroupResource_To_v1_GroupResource is an autogenerated conversion function. +func Convert_v1alpha1_GroupResource_To_v1_GroupResource(in *v1alpha1.GroupResource, out *v1.GroupResource, s conversion.Scope) error { + return autoConvert_v1alpha1_GroupResource_To_v1_GroupResource(in, out, s) +} + +func autoConvert_v1_GroupResource_To_v1alpha1_GroupResource(in *v1.GroupResource, out *v1alpha1.GroupResource, s conversion.Scope) error { + out.Group = in.Group + out.Resource = in.Resource + return nil +} + +// Convert_v1_GroupResource_To_v1alpha1_GroupResource is an autogenerated conversion function. +func Convert_v1_GroupResource_To_v1alpha1_GroupResource(in *v1.GroupResource, out *v1alpha1.GroupResource, s conversion.Scope) error { + return autoConvert_v1_GroupResource_To_v1alpha1_GroupResource(in, out, s) +} diff --git a/pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.deepcopy.go b/pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 00000000000..0ec19467c40 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,21 @@ +// +build !ignore_autogenerated + +/* +Copyright 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. +*/ + +// Code generated by deepcopy-gen. DO NOT EDIT. + +package v1alpha1 diff --git a/pkg/controller/endpointslicemirroring/config/zz_generated.deepcopy.go b/pkg/controller/endpointslicemirroring/config/zz_generated.deepcopy.go new file mode 100644 index 00000000000..1a0a424e21a --- /dev/null +++ b/pkg/controller/endpointslicemirroring/config/zz_generated.deepcopy.go @@ -0,0 +1,38 @@ +// +build !ignore_autogenerated + +/* +Copyright 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. +*/ + +// Code generated by deepcopy-gen. DO NOT EDIT. + +package config + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *EndpointSliceMirroringControllerConfiguration) DeepCopyInto(out *EndpointSliceMirroringControllerConfiguration) { + *out = *in + out.MirroringEndpointUpdatesBatchPeriod = in.MirroringEndpointUpdatesBatchPeriod + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new EndpointSliceMirroringControllerConfiguration. +func (in *EndpointSliceMirroringControllerConfiguration) DeepCopy() *EndpointSliceMirroringControllerConfiguration { + if in == nil { + return nil + } + out := new(EndpointSliceMirroringControllerConfiguration) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/controller/endpointslicemirroring/endpointset.go b/pkg/controller/endpointslicemirroring/endpointset.go new file mode 100644 index 00000000000..bd45d60e138 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/endpointset.go @@ -0,0 +1,96 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "sort" + + discovery "k8s.io/api/discovery/v1beta1" + endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint" +) + +// endpointHash is used to uniquely identify endpoints. Only including addresses +// and hostnames as unique identifiers allows us to do more in place updates +// should attributes such as topology, conditions, or targetRef change. +type endpointHash string +type endpointHashObj struct { + Addresses []string + Hostname string +} + +func hashEndpoint(endpoint *discovery.Endpoint) endpointHash { + sort.Strings(endpoint.Addresses) + hashObj := endpointHashObj{Addresses: endpoint.Addresses} + if endpoint.Hostname != nil { + hashObj.Hostname = *endpoint.Hostname + } + + return endpointHash(endpointutil.DeepHashObjectToString(hashObj)) +} + +// endpointSet provides simple methods for comparing sets of Endpoints. +type endpointSet map[endpointHash]*discovery.Endpoint + +// Insert adds items to the set. +func (s endpointSet) Insert(items ...*discovery.Endpoint) endpointSet { + for _, item := range items { + s[hashEndpoint(item)] = item + } + return s +} + +// Delete removes all items from the set. +func (s endpointSet) Delete(items ...*discovery.Endpoint) endpointSet { + for _, item := range items { + delete(s, hashEndpoint(item)) + } + return s +} + +// Has returns true if and only if item is contained in the set. +func (s endpointSet) Has(item *discovery.Endpoint) bool { + _, contained := s[hashEndpoint(item)] + return contained +} + +// Returns an endpoint matching the hash if contained in the set. +func (s endpointSet) Get(item *discovery.Endpoint) *discovery.Endpoint { + return s[hashEndpoint(item)] +} + +// UnsortedList returns the slice with contents in random order. +func (s endpointSet) UnsortedList() []*discovery.Endpoint { + endpoints := make([]*discovery.Endpoint, 0, len(s)) + for _, endpoint := range s { + endpoints = append(endpoints, endpoint) + } + return endpoints +} + +// Returns a single element from the set. +func (s endpointSet) PopAny() (*discovery.Endpoint, bool) { + for _, endpoint := range s { + s.Delete(endpoint) + return endpoint, true + } + return nil, false +} + +// Len returns the size of the set. +func (s endpointSet) Len() int { + return len(s) +} diff --git a/pkg/controller/endpointslicemirroring/endpointslice_tracker.go b/pkg/controller/endpointslicemirroring/endpointslice_tracker.go new file mode 100644 index 00000000000..17449202d3c --- /dev/null +++ b/pkg/controller/endpointslicemirroring/endpointslice_tracker.go @@ -0,0 +1,123 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "sync" + + discovery "k8s.io/api/discovery/v1beta1" + "k8s.io/apimachinery/pkg/types" +) + +// endpointSliceResourceVersions tracks expected EndpointSlice resource versions +// by EndpointSlice name. +type endpointSliceResourceVersions map[string]string + +// endpointSliceTracker tracks EndpointSlices and their associated resource +// versions to help determine if a change to an EndpointSlice has been processed +// by the EndpointSlice controller. +type endpointSliceTracker struct { + // lock protects resourceVersionsByService. + lock sync.Mutex + // resourceVersionsByService tracks the list of EndpointSlices and + // associated resource versions expected for a given Service. + resourceVersionsByService map[types.NamespacedName]endpointSliceResourceVersions +} + +// newEndpointSliceTracker creates and initializes a new endpointSliceTracker. +func newEndpointSliceTracker() *endpointSliceTracker { + return &endpointSliceTracker{ + resourceVersionsByService: map[types.NamespacedName]endpointSliceResourceVersions{}, + } +} + +// has returns true if the endpointSliceTracker has a resource version for the +// provided EndpointSlice. +func (est *endpointSliceTracker) has(endpointSlice *discovery.EndpointSlice) bool { + est.lock.Lock() + defer est.lock.Unlock() + + rrv := est.relatedResourceVersions(endpointSlice) + _, ok := rrv[endpointSlice.Name] + return ok +} + +// stale returns true if this endpointSliceTracker does not have a resource +// version for the provided EndpointSlice or it does not match the resource +// version of the provided EndpointSlice. +func (est *endpointSliceTracker) stale(endpointSlice *discovery.EndpointSlice) bool { + est.lock.Lock() + defer est.lock.Unlock() + + rrv := est.relatedResourceVersions(endpointSlice) + return rrv[endpointSlice.Name] != endpointSlice.ResourceVersion +} + +// update adds or updates the resource version in this endpointSliceTracker for +// the provided EndpointSlice. +func (est *endpointSliceTracker) update(endpointSlice *discovery.EndpointSlice) { + est.lock.Lock() + defer est.lock.Unlock() + + rrv := est.relatedResourceVersions(endpointSlice) + rrv[endpointSlice.Name] = endpointSlice.ResourceVersion +} + +// delete removes the resource version in this endpointSliceTracker for the +// provided EndpointSlice. +func (est *endpointSliceTracker) delete(endpointSlice *discovery.EndpointSlice) { + est.lock.Lock() + defer est.lock.Unlock() + + rrv := est.relatedResourceVersions(endpointSlice) + delete(rrv, endpointSlice.Name) +} + +// relatedResourceVersions returns the set of resource versions tracked for the +// Service corresponding to the provided EndpointSlice. If no resource versions +// are currently tracked for this service, an empty set is initialized. +func (est *endpointSliceTracker) relatedResourceVersions(endpointSlice *discovery.EndpointSlice) endpointSliceResourceVersions { + serviceNN := getServiceNN(endpointSlice) + vers, ok := est.resourceVersionsByService[serviceNN] + + if !ok { + vers = endpointSliceResourceVersions{} + est.resourceVersionsByService[serviceNN] = vers + } + + return vers +} + +// getServiceNN returns a namespaced name for the Service corresponding to the +// provided EndpointSlice. +func getServiceNN(endpointSlice *discovery.EndpointSlice) types.NamespacedName { + serviceName, _ := endpointSlice.Labels[discovery.LabelServiceName] + return types.NamespacedName{Name: serviceName, Namespace: endpointSlice.Namespace} +} + +// managedByChanged returns true if one of the provided EndpointSlices is +// managed by the EndpointSlice controller while the other is not. +func managedByChanged(endpointSlice1, endpointSlice2 *discovery.EndpointSlice) bool { + return managedByController(endpointSlice1) != managedByController(endpointSlice2) +} + +// managedByController returns true if the controller of the provided +// EndpointSlices is the EndpointSlice controller. +func managedByController(endpointSlice *discovery.EndpointSlice) bool { + managedBy, _ := endpointSlice.Labels[discovery.LabelManagedBy] + return managedBy == controllerName +} diff --git a/pkg/controller/endpointslicemirroring/endpointslice_tracker_test.go b/pkg/controller/endpointslicemirroring/endpointslice_tracker_test.go new file mode 100644 index 00000000000..1a5f7fbe9d1 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/endpointslice_tracker_test.go @@ -0,0 +1,174 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "testing" + + discovery "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestEndpointSliceTrackerUpdate(t *testing.T) { + epSlice1 := &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + ResourceVersion: "rv1", + Labels: map[string]string{discovery.LabelServiceName: "svc1"}, + }, + } + + epSlice1DifferentNS := epSlice1.DeepCopy() + epSlice1DifferentNS.Namespace = "ns2" + + epSlice1DifferentService := epSlice1.DeepCopy() + epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2" + + epSlice1DifferentRV := epSlice1.DeepCopy() + epSlice1DifferentRV.ResourceVersion = "rv2" + + testCases := map[string]struct { + updateParam *discovery.EndpointSlice + checksParam *discovery.EndpointSlice + expectHas bool + expectStale bool + }{ + "same slice": { + updateParam: epSlice1, + checksParam: epSlice1, + expectHas: true, + expectStale: false, + }, + "different namespace": { + updateParam: epSlice1, + checksParam: epSlice1DifferentNS, + expectHas: false, + expectStale: true, + }, + "different service": { + updateParam: epSlice1, + checksParam: epSlice1DifferentService, + expectHas: false, + expectStale: true, + }, + "different resource version": { + updateParam: epSlice1, + checksParam: epSlice1DifferentRV, + expectHas: true, + expectStale: true, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + esTracker := newEndpointSliceTracker() + esTracker.update(tc.updateParam) + if esTracker.has(tc.checksParam) != tc.expectHas { + t.Errorf("tc.tracker.has(%+v) == %t, expected %t", tc.checksParam, esTracker.has(tc.checksParam), tc.expectHas) + } + if esTracker.stale(tc.checksParam) != tc.expectStale { + t.Errorf("tc.tracker.stale(%+v) == %t, expected %t", tc.checksParam, esTracker.stale(tc.checksParam), tc.expectStale) + } + }) + } +} + +func TestEndpointSliceTrackerDelete(t *testing.T) { + epSlice1 := &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + ResourceVersion: "rv1", + Labels: map[string]string{discovery.LabelServiceName: "svc1"}, + }, + } + + epSlice1DifferentNS := epSlice1.DeepCopy() + epSlice1DifferentNS.Namespace = "ns2" + + epSlice1DifferentService := epSlice1.DeepCopy() + epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2" + + epSlice1DifferentRV := epSlice1.DeepCopy() + epSlice1DifferentRV.ResourceVersion = "rv2" + + testCases := map[string]struct { + deleteParam *discovery.EndpointSlice + checksParam *discovery.EndpointSlice + expectHas bool + expectStale bool + }{ + "same slice": { + deleteParam: epSlice1, + checksParam: epSlice1, + expectHas: false, + expectStale: true, + }, + "different namespace": { + deleteParam: epSlice1DifferentNS, + checksParam: epSlice1DifferentNS, + expectHas: false, + expectStale: true, + }, + "different namespace, check original ep slice": { + deleteParam: epSlice1DifferentNS, + checksParam: epSlice1, + expectHas: true, + expectStale: false, + }, + "different service": { + deleteParam: epSlice1DifferentService, + checksParam: epSlice1DifferentService, + expectHas: false, + expectStale: true, + }, + "different service, check original ep slice": { + deleteParam: epSlice1DifferentService, + checksParam: epSlice1, + expectHas: true, + expectStale: false, + }, + "different resource version": { + deleteParam: epSlice1DifferentRV, + checksParam: epSlice1DifferentRV, + expectHas: false, + expectStale: true, + }, + "different resource version, check original ep slice": { + deleteParam: epSlice1DifferentRV, + checksParam: epSlice1, + expectHas: false, + expectStale: true, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + esTracker := newEndpointSliceTracker() + esTracker.update(epSlice1) + + esTracker.delete(tc.deleteParam) + if esTracker.has(tc.checksParam) != tc.expectHas { + t.Errorf("esTracker.has(%+v) == %t, expected %t", tc.checksParam, esTracker.has(tc.checksParam), tc.expectHas) + } + if esTracker.stale(tc.checksParam) != tc.expectStale { + t.Errorf("esTracker.stale(%+v) == %t, expected %t", tc.checksParam, esTracker.stale(tc.checksParam), tc.expectStale) + } + }) + } +} diff --git a/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go new file mode 100644 index 00000000000..d127be14433 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go @@ -0,0 +1,446 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "fmt" + "time" + + "golang.org/x/time/rate" + + v1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/apimachinery/pkg/util/wait" + coreinformers "k8s.io/client-go/informers/core/v1" + discoveryinformers "k8s.io/client-go/informers/discovery/v1beta1" + clientset "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/scheme" + v1core "k8s.io/client-go/kubernetes/typed/core/v1" + corelisters "k8s.io/client-go/listers/core/v1" + discoverylisters "k8s.io/client-go/listers/discovery/v1beta1" + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/record" + "k8s.io/client-go/util/workqueue" + "k8s.io/component-base/metrics/prometheus/ratelimiter" + "k8s.io/klog/v2" + "k8s.io/kubernetes/pkg/controller" + "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/metrics" +) + +const ( + // maxRetries is the number of times an Endpoints resource will be retried + // before it is dropped out of the queue. Any sync error, such as a failure + // to create or update an EndpointSlice could trigger a retry. With the + // current rate-limiter in use (1s*2^(numRetries-1)) up to a max of 100s. + // The following numbers represent the sequence of delays between successive + // queuings of an Endpoints resource. + // + // 1s, 2s, 4s, 8s, 16s, 32s, 64s, 100s (max) + maxRetries = 15 + + // defaultSyncBackOff is the default backoff period for syncEndpoints calls. + defaultSyncBackOff = 1 * time.Second + // maxSyncBackOff is the max backoff period for syncEndpoints calls. + maxSyncBackOff = 100 * time.Second + + // controllerName is a unique value used with LabelManagedBy to indicated + // the component managing an EndpointSlice. + controllerName = "endpointslicemirroring-controller.k8s.io" +) + +// NewController creates and initializes a new Controller +func NewController(endpointsInformer coreinformers.EndpointsInformer, + endpointSliceInformer discoveryinformers.EndpointSliceInformer, + serviceInformer coreinformers.ServiceInformer, + maxEndpointsPerSubset int32, + client clientset.Interface, + endpointUpdatesBatchPeriod time.Duration, +) *Controller { + broadcaster := record.NewBroadcaster() + broadcaster.StartLogging(klog.Infof) + broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: client.CoreV1().Events("")}) + recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "endpoint-slice-mirroring-controller"}) + + if client != nil && client.CoreV1().RESTClient().GetRateLimiter() != nil { + ratelimiter.RegisterMetricAndTrackRateLimiterUsage("endpoint_slice_mirroring_controller", client.DiscoveryV1beta1().RESTClient().GetRateLimiter()) + } + + metrics.RegisterMetrics() + + c := &Controller{ + client: client, + // This is similar to the DefaultControllerRateLimiter, just with a + // significantly higher default backoff (1s vs 5ms). This controller + // processes events that can require significant EndpointSlice changes. + // A more significant rate limit back off here helps ensure that the + // Controller does not overwhelm the API Server. + queue: workqueue.NewNamedRateLimitingQueue(workqueue.NewMaxOfRateLimiter( + workqueue.NewItemExponentialFailureRateLimiter(defaultSyncBackOff, maxSyncBackOff), + // 10 qps, 100 bucket size. This is only for retry speed and its + // only the overall factor (not per item). + &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(10), 100)}, + ), "endpoint_slice_mirroring"), + workerLoopPeriod: time.Second, + } + + endpointsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: c.onEndpointsAdd, + UpdateFunc: c.onEndpointsUpdate, + DeleteFunc: c.onEndpointsDelete, + }) + c.endpointsLister = endpointsInformer.Lister() + c.endpointsSynced = endpointsInformer.Informer().HasSynced + + endpointSliceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: c.onEndpointSliceAdd, + UpdateFunc: c.onEndpointSliceUpdate, + DeleteFunc: c.onEndpointSliceDelete, + }) + + c.endpointSliceLister = endpointSliceInformer.Lister() + c.endpointSlicesSynced = endpointSliceInformer.Informer().HasSynced + c.endpointSliceTracker = newEndpointSliceTracker() + + c.serviceLister = serviceInformer.Lister() + c.servicesSynced = serviceInformer.Informer().HasSynced + + c.maxEndpointsPerSubset = maxEndpointsPerSubset + + c.reconciler = &reconciler{ + client: c.client, + maxEndpointsPerSubset: c.maxEndpointsPerSubset, + endpointSliceTracker: c.endpointSliceTracker, + metricsCache: metrics.NewCache(maxEndpointsPerSubset), + eventRecorder: recorder, + } + + c.eventBroadcaster = broadcaster + c.eventRecorder = recorder + + c.endpointUpdatesBatchPeriod = endpointUpdatesBatchPeriod + + return c +} + +// Controller manages selector-based service endpoint slices +type Controller struct { + client clientset.Interface + eventBroadcaster record.EventBroadcaster + eventRecorder record.EventRecorder + + // endpointsLister is able to list/get endpoints and is populated by the + // shared informer passed to NewController. + endpointsLister corelisters.EndpointsLister + // endpointsSynced returns true if the endpoints shared informer has been + // synced at least once. Added as a member to the struct to allow injection + // for testing. + endpointsSynced cache.InformerSynced + + // endpointSliceLister is able to list/get endpoint slices and is populated + // by the shared informer passed to NewController + endpointSliceLister discoverylisters.EndpointSliceLister + // endpointSlicesSynced returns true if the endpoint slice shared informer + // has been synced at least once. Added as a member to the struct to allow + // injection for testing. + endpointSlicesSynced cache.InformerSynced + // endpointSliceTracker tracks the list of EndpointSlices and associated + // resource versions expected for each Endpoints resource. It can help + // determine if a cached EndpointSlice is out of date. + endpointSliceTracker *endpointSliceTracker + + // serviceLister is able to list/get services and is populated by the shared + // informer passed to NewController. + serviceLister corelisters.ServiceLister + // servicesSynced returns true if the services shared informer has been + // synced at least once. Added as a member to the struct to allow injection + // for testing. + servicesSynced cache.InformerSynced + + // reconciler is an util used to reconcile EndpointSlice changes. + reconciler *reconciler + + // Endpoints that need to be updated. A channel is inappropriate here, + // because it allows Endpoints with lots of addresses to be serviced much + // more often than Endpoints with few addresses; it also would cause an + // Endpoints resource that's inserted multiple times to be processed more + // than necessary. + queue workqueue.RateLimitingInterface + + // maxEndpointsPerSubset references the maximum number of endpoints that + // should be added to an EndpointSlice for an EndpointSubset. + maxEndpointsPerSubset int32 + + // workerLoopPeriod is the time between worker runs. The workers process the + // queue of changes to Endpoints resources. + workerLoopPeriod time.Duration + + // endpointUpdatesBatchPeriod is an artificial delay added to all Endpoints + // syncs triggered by EndpointSlice changes. This can be used to reduce + // overall number of all EndpointSlice updates. + endpointUpdatesBatchPeriod time.Duration +} + +// Run will not return until stopCh is closed. +func (c *Controller) Run(workers int, stopCh <-chan struct{}) { + defer utilruntime.HandleCrash() + defer c.queue.ShutDown() + + klog.Infof("Starting EndpointSliceMirroring controller") + defer klog.Infof("Shutting down EndpointSliceMirroring controller") + + if !cache.WaitForNamedCacheSync("endpoint_slice_mirroring", stopCh, c.endpointsSynced, c.endpointSlicesSynced, c.servicesSynced) { + return + } + + klog.V(2).Infof("Starting %d worker threads", workers) + for i := 0; i < workers; i++ { + go wait.Until(c.worker, c.workerLoopPeriod, stopCh) + } + + <-stopCh +} + +// worker runs a worker thread that just dequeues items, processes them, and +// marks them done. You may run as many of these in parallel as you wish; the +// workqueue guarantees that they will not end up processing the same service +// at the same time +func (c *Controller) worker() { + for c.processNextWorkItem() { + } +} + +func (c *Controller) processNextWorkItem() bool { + cKey, quit := c.queue.Get() + if quit { + return false + } + defer c.queue.Done(cKey) + + err := c.syncEndpoints(cKey.(string)) + c.handleErr(err, cKey) + + return true +} + +func (c *Controller) handleErr(err error, key interface{}) { + if err == nil { + c.queue.Forget(key) + return + } + + if c.queue.NumRequeues(key) < maxRetries { + klog.Warningf("Error mirroring EndpointSlices for %q Endpoints, retrying. Error: %v", key, err) + c.queue.AddRateLimited(key) + return + } + + klog.Warningf("Retry budget exceeded, dropping %q Endpoints out of the queue: %v", key, err) + c.queue.Forget(key) + utilruntime.HandleError(err) +} + +func (c *Controller) syncEndpoints(key string) error { + startTime := time.Now() + defer func() { + syncDuration := float64(time.Since(startTime).Milliseconds()) / 1000 + metrics.EndpointsSyncDuration.WithLabelValues().Observe(syncDuration) + klog.V(4).Infof("Finished syncing EndpointSlices for %q Endpoints. (%v)", key, time.Since(startTime)) + }() + + klog.V(4).Infof("syncEndpoints(%q)", key) + + namespace, name, err := cache.SplitMetaNamespaceKey(key) + if err != nil { + return err + } + + endpointSlices, err := endpointSlicesMirroredForService(c.endpointSliceLister, namespace, name) + + if err != nil { + ep := &v1.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}} + c.eventRecorder.Eventf(ep, FailedToListEndpointSlices, + "Error listing EndpointSlices for Endpoints %s/%s: %v", ep.Namespace, ep.Name, err) + return err + } + + endpoints, err := c.endpointsLister.Endpoints(namespace).Get(name) + if err != nil || !c.shouldMirror(endpoints) { + if apierrors.IsNotFound(err) || !c.shouldMirror(endpoints) { + return c.reconciler.deleteEndpoints(namespace, name, endpointSlices) + } + return err + } + + err = c.reconciler.reconcile(endpoints, endpointSlices) + if err != nil { + c.eventRecorder.Eventf(endpoints, v1.EventTypeWarning, FailedToUpdateEndpointSlices, + "Error updating EndpointSlices for Endpoints %s/%s: %v", endpoints.Namespace, endpoints.Name, err) + return err + } + + return nil +} + +// queueEndpoints queues the Endpoints resource for processing. +func (c *Controller) queueEndpoints(obj interface{}) { + key, err := controller.KeyFunc(obj) + if err != nil { + utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %+v (type %T): %v", obj, obj, err)) + return + } + + c.queue.Add(key) +} + +// shouldMirror returns true if an Endpoints resource should be mirrored by this +// controller. This will be false if: +// - the Endpoints resource has a skip-mirror label. +// - the Endpoints resource has a leader election annotation. +// - the corresponding Service resource does not exist. +// - the corresponding Service resource has a non-nil selector. +func (c *Controller) shouldMirror(endpoints *v1.Endpoints) bool { + if endpoints == nil || skipMirror(endpoints.Labels) || hasLeaderElection(endpoints.Annotations) { + return false + } + + svc, err := c.serviceLister.Services(endpoints.Namespace).Get(endpoints.Name) + if err != nil { + if !apierrors.IsNotFound(err) { + klog.Errorf("Error fetching %s/%s Service: %v", endpoints.Namespace, endpoints.Name, err) + } + return false + } + + if svc.Spec.Selector != nil { + return false + } + + return true +} + +// onEndpointsAdd queues a sync for the relevant Endpoints resource. +func (c *Controller) onEndpointsAdd(obj interface{}) { + endpoints := obj.(*v1.Endpoints) + if endpoints == nil { + utilruntime.HandleError(fmt.Errorf("onEndpointsAdd() expected type v1.Endpoints, got %T", obj)) + return + } + if !c.shouldMirror(endpoints) { + klog.V(5).Infof("Skipping mirroring for %s/%s", endpoints.Namespace, endpoints.Name) + return + } + c.queueEndpoints(obj) +} + +// onEndpointsUpdate queues a sync for the relevant Endpoints resource. +func (c *Controller) onEndpointsUpdate(prevObj, obj interface{}) { + endpoints := obj.(*v1.Endpoints) + prevEndpoints := prevObj.(*v1.Endpoints) + if endpoints == nil || prevEndpoints == nil { + utilruntime.HandleError(fmt.Errorf("onEndpointsUpdate() expected type v1.Endpoints, got %T, %T", prevObj, obj)) + return + } + if !c.shouldMirror(endpoints) && !c.shouldMirror(prevEndpoints) { + klog.V(5).Infof("Skipping mirroring for %s/%s", endpoints.Namespace, endpoints.Name) + return + } + c.queueEndpoints(obj) +} + +// onEndpointsDelete queues a sync for the relevant Endpoints resource. +func (c *Controller) onEndpointsDelete(obj interface{}) { + endpoints := getEndpointsFromDeleteAction(obj) + if endpoints == nil { + utilruntime.HandleError(fmt.Errorf("onEndpointsDelete() expected type v1.Endpoints, got %T", obj)) + return + } + if !c.shouldMirror(endpoints) { + klog.V(5).Infof("Skipping mirroring for %s/%s", endpoints.Namespace, endpoints.Name) + return + } + c.queueEndpoints(obj) +} + +// onEndpointSliceAdd queues a sync for the relevant Endpoints resource for a +// sync if the EndpointSlice resource version does not match the expected +// version in the endpointSliceTracker. +func (c *Controller) onEndpointSliceAdd(obj interface{}) { + endpointSlice := obj.(*discovery.EndpointSlice) + if endpointSlice == nil { + utilruntime.HandleError(fmt.Errorf("onEndpointSliceAdd() expected type discovery.EndpointSlice, got %T", obj)) + return + } + if managedByController(endpointSlice) && c.endpointSliceTracker.stale(endpointSlice) { + c.queueEndpointsForEndpointSlice(endpointSlice) + } +} + +// onEndpointSliceUpdate queues a sync for the relevant Endpoints resource for a +// sync if the EndpointSlice resource version does not match the expected +// version in the endpointSliceTracker or the managed-by value of the +// EndpointSlice has changed from or to this controller. +func (c *Controller) onEndpointSliceUpdate(prevObj, obj interface{}) { + prevEndpointSlice := obj.(*discovery.EndpointSlice) + endpointSlice := prevObj.(*discovery.EndpointSlice) + if endpointSlice == nil || prevEndpointSlice == nil { + utilruntime.HandleError(fmt.Errorf("onEndpointSliceUpdated() expected type discovery.EndpointSlice, got %T, %T", prevObj, obj)) + return + } + if managedByChanged(prevEndpointSlice, endpointSlice) || (managedByController(endpointSlice) && c.endpointSliceTracker.stale(endpointSlice)) { + c.queueEndpointsForEndpointSlice(endpointSlice) + } +} + +// onEndpointSliceDelete queues a sync for the relevant Endpoints resource for a +// sync if the EndpointSlice resource version does not match the expected +// version in the endpointSliceTracker. +func (c *Controller) onEndpointSliceDelete(obj interface{}) { + endpointSlice := getEndpointSliceFromDeleteAction(obj) + if endpointSlice == nil { + utilruntime.HandleError(fmt.Errorf("onEndpointSliceDelete() expected type discovery.EndpointSlice, got %T", obj)) + return + } + if managedByController(endpointSlice) && c.endpointSliceTracker.has(endpointSlice) { + c.queueEndpointsForEndpointSlice(endpointSlice) + } +} + +// queueEndpointsForEndpointSlice attempts to queue the corresponding Endpoints +// resource for the provided EndpointSlice. +func (c *Controller) queueEndpointsForEndpointSlice(endpointSlice *discovery.EndpointSlice) { + key, err := endpointsControllerKey(endpointSlice) + if err != nil { + utilruntime.HandleError(fmt.Errorf("Couldn't get key for EndpointSlice %+v (type %T): %v", endpointSlice, endpointSlice, err)) + return + } + + c.queue.AddAfter(key, c.endpointUpdatesBatchPeriod) +} + +// endpointSlicesMirroredForService returns the EndpointSlices that have been +// mirrored for a Service by this controller. +func endpointSlicesMirroredForService(endpointSliceLister discoverylisters.EndpointSliceLister, namespace, name string) ([]*discovery.EndpointSlice, error) { + esLabelSelector := labels.Set(map[string]string{ + discovery.LabelServiceName: name, + discovery.LabelManagedBy: controllerName, + }).AsSelectorPreValidated() + return endpointSliceLister.EndpointSlices(namespace).List(esLabelSelector) +} diff --git a/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go new file mode 100644 index 00000000000..0c0a927284b --- /dev/null +++ b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go @@ -0,0 +1,479 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "context" + "fmt" + "testing" + "time" + + v1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes/fake" + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/leaderelection/resourcelock" + "k8s.io/kubernetes/pkg/controller" +) + +// Most of the tests related to EndpointSlice allocation can be found in reconciler_test.go +// Tests here primarily focus on unique controller functionality before the reconciler begins + +var alwaysReady = func() bool { return true } + +type endpointSliceMirroringController struct { + *Controller + endpointsStore cache.Store + endpointSliceStore cache.Store + serviceStore cache.Store +} + +func newController(batchPeriod time.Duration) (*fake.Clientset, *endpointSliceMirroringController) { + client := newClientset() + informerFactory := informers.NewSharedInformerFactory(client, controller.NoResyncPeriodFunc()) + + esController := NewController( + informerFactory.Core().V1().Endpoints(), + informerFactory.Discovery().V1beta1().EndpointSlices(), + informerFactory.Core().V1().Services(), + int32(1000), + client, + batchPeriod) + + esController.endpointsSynced = alwaysReady + esController.endpointSlicesSynced = alwaysReady + esController.servicesSynced = alwaysReady + + return client, &endpointSliceMirroringController{ + esController, + informerFactory.Core().V1().Endpoints().Informer().GetStore(), + informerFactory.Discovery().V1beta1().EndpointSlices().Informer().GetStore(), + informerFactory.Core().V1().Services().Informer().GetStore(), + } +} + +func TestSyncEndpoints(t *testing.T) { + endpointsName := "testing-sync-endpoints" + namespace := metav1.NamespaceDefault + + testCases := []struct { + testName string + endpoints *v1.Endpoints + endpointSlices []*discovery.EndpointSlice + expectedNumActions int + expectedNumSlices int + }{{ + testName: "Endpoints with no addresses", + endpoints: &v1.Endpoints{ + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + }}, + }, + endpointSlices: []*discovery.EndpointSlice{}, + expectedNumActions: 0, + expectedNumSlices: 0, + }, { + testName: "Endpoints with skip label true", + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{discovery.LabelSkipMirror: "true"}, + }, + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + Addresses: []v1.EndpointAddress{{IP: "10.0.0.1"}}, + }}, + }, + endpointSlices: []*discovery.EndpointSlice{}, + expectedNumActions: 0, + expectedNumSlices: 0, + }, { + testName: "Endpoints with skip label false", + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{discovery.LabelSkipMirror: "false"}, + }, + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + Addresses: []v1.EndpointAddress{{IP: "10.0.0.1"}}, + }}, + }, + endpointSlices: []*discovery.EndpointSlice{}, + expectedNumActions: 1, + expectedNumSlices: 1, + }, { + testName: "Existing EndpointSlices that need to be cleaned up", + endpoints: &v1.Endpoints{ + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + }}, + }, + endpointSlices: []*discovery.EndpointSlice{{ + ObjectMeta: metav1.ObjectMeta{ + Name: endpointsName + "-1", + Labels: map[string]string{ + discovery.LabelServiceName: endpointsName, + discovery.LabelManagedBy: controllerName, + }, + }, + }}, + expectedNumActions: 1, + expectedNumSlices: 0, + }, { + testName: "Existing EndpointSlices managed by a different controller, no addresses to sync", + endpoints: &v1.Endpoints{ + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + }}, + }, + endpointSlices: []*discovery.EndpointSlice{{ + ObjectMeta: metav1.ObjectMeta{ + Name: endpointsName + "-1", + Labels: map[string]string{ + discovery.LabelManagedBy: "something-else", + }, + }, + }}, + expectedNumActions: 0, + // This only queries for EndpointSlices managed by this controller. + expectedNumSlices: 0, + }, { + testName: "Endpoints with 1000 addresses", + endpoints: &v1.Endpoints{ + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + Addresses: generateAddresses(1000), + }}, + }, + endpointSlices: []*discovery.EndpointSlice{}, + expectedNumActions: 1, + expectedNumSlices: 1, + }, { + testName: "Endpoints with 1001 addresses - 1 should not be mirrored", + endpoints: &v1.Endpoints{ + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + Addresses: generateAddresses(1001), + }}, + }, + endpointSlices: []*discovery.EndpointSlice{}, + expectedNumActions: 1, + expectedNumSlices: 1, + }} + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + client, esController := newController(time.Duration(0)) + tc.endpoints.Name = endpointsName + tc.endpoints.Namespace = namespace + esController.endpointsStore.Add(tc.endpoints) + esController.serviceStore.Add(&v1.Service{ObjectMeta: metav1.ObjectMeta{ + Name: endpointsName, + Namespace: namespace, + }}) + + for _, epSlice := range tc.endpointSlices { + epSlice.Namespace = namespace + esController.endpointSliceStore.Add(epSlice) + _, err := client.DiscoveryV1beta1().EndpointSlices(namespace).Create(context.TODO(), epSlice, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Expected no error creating EndpointSlice, got %v", err) + } + } + + err := esController.syncEndpoints(fmt.Sprintf("%s/%s", namespace, endpointsName)) + if err != nil { + t.Errorf("Unexpected error from syncEndpoints: %v", err) + } + + numInitialActions := len(tc.endpointSlices) + numExtraActions := len(client.Actions()) - numInitialActions + if numExtraActions != tc.expectedNumActions { + t.Fatalf("Expected %d additional client actions, got %d: %#v", tc.expectedNumActions, numExtraActions, client.Actions()[numInitialActions:]) + } + + endpointSlices := fetchEndpointSlices(t, client, namespace) + expectEndpointSlices(t, tc.expectedNumSlices, int(defaultMaxEndpointsPerSubset), *tc.endpoints, endpointSlices) + }) + } +} + +func TestShouldMirror(t *testing.T) { + svcWithSelector := &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "with-selector", + Namespace: "example1", + }, + Spec: v1.ServiceSpec{ + Selector: map[string]string{"with": "selector"}, + }, + } + svcWithoutSelector := &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "without-selector", + Namespace: "example1", + }, + Spec: v1.ServiceSpec{}, + } + + testCases := []struct { + testName string + endpoints *v1.Endpoints + service *v1.Service + shouldMirror bool + }{{ + testName: "Service without selector with matching endpoints", + service: svcWithoutSelector, + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svcWithoutSelector.Name, + Namespace: svcWithoutSelector.Namespace, + }, + }, + shouldMirror: true, + }, { + testName: "Service without selector, matching Endpoints with skip-mirror=true", + service: svcWithoutSelector, + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svcWithSelector.Name, + Namespace: svcWithSelector.Namespace, + Labels: map[string]string{ + discovery.LabelSkipMirror: "true", + }, + }, + }, + shouldMirror: false, + }, { + testName: "Service without selector, matching Endpoints with skip-mirror=invalid", + service: svcWithoutSelector, + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svcWithoutSelector.Name, + Namespace: svcWithoutSelector.Namespace, + Labels: map[string]string{ + discovery.LabelSkipMirror: "invalid", + }, + }, + }, + shouldMirror: true, + }, { + testName: "Service without selector, matching Endpoints with leader election annotation", + service: svcWithoutSelector, + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svcWithSelector.Name, + Namespace: svcWithSelector.Namespace, + Annotations: map[string]string{ + resourcelock.LeaderElectionRecordAnnotationKey: "", + }, + }, + }, + shouldMirror: false, + }, { + testName: "Service without selector, matching Endpoints without skip label in different namespace", + service: svcWithSelector, + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svcWithSelector.Name, + Namespace: svcWithSelector.Namespace + "different", + }, + }, + shouldMirror: false, + }, { + testName: "Service without selector or matching endpoints", + service: svcWithoutSelector, + endpoints: nil, + shouldMirror: false, + }, { + testName: "Endpoints without matching Service", + service: nil, + endpoints: &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svcWithoutSelector.Name, + Namespace: svcWithoutSelector.Namespace, + }, + }, + shouldMirror: false, + }} + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + _, c := newController(time.Duration(0)) + + if tc.endpoints != nil { + err := c.endpointsStore.Add(tc.endpoints) + if err != nil { + t.Fatalf("Error adding Endpoints to store: %v", err) + } + } + + if tc.service != nil { + err := c.serviceStore.Add(tc.service) + if err != nil { + t.Fatalf("Error adding Service to store: %v", err) + } + } + + shouldMirror := c.shouldMirror(tc.endpoints) + + if shouldMirror != tc.shouldMirror { + t.Errorf("Expected %t to be returned, got %t", tc.shouldMirror, shouldMirror) + } + }) + } +} + +func TestEndpointSlicesMirroredForService(t *testing.T) { + testCases := []struct { + testName string + namespace string + name string + endpointSlice *discovery.EndpointSlice + expectedInList bool + }{{ + testName: "Service with matching EndpointSlice", + namespace: "ns1", + name: "svc1", + endpointSlice: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + discovery.LabelManagedBy: controllerName, + }, + }, + }, + expectedInList: true, + }, { + testName: "Service with EndpointSlice that has different namespace", + namespace: "ns1", + name: "svc1", + endpointSlice: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns2", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + discovery.LabelManagedBy: controllerName, + }, + }, + }, + expectedInList: false, + }, { + testName: "Service with EndpointSlice that has different service name", + namespace: "ns1", + name: "svc1", + endpointSlice: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + Labels: map[string]string{ + discovery.LabelServiceName: "svc2", + discovery.LabelManagedBy: controllerName, + }, + }, + }, + expectedInList: false, + }, { + testName: "Service with EndpointSlice that has different controller name", + namespace: "ns1", + name: "svc1", + endpointSlice: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + discovery.LabelManagedBy: controllerName + "foo", + }, + }, + }, + expectedInList: false, + }, { + testName: "Service with EndpointSlice that has missing controller name", + namespace: "ns1", + name: "svc1", + endpointSlice: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, + }, + }, + expectedInList: false, + }, { + testName: "Service with EndpointSlice that has missing service name", + namespace: "ns1", + name: "svc1", + endpointSlice: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-1", + Namespace: "ns1", + Labels: map[string]string{ + discovery.LabelManagedBy: controllerName, + }, + }, + }, + expectedInList: false, + }} + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + _, c := newController(time.Duration(0)) + + err := c.endpointSliceStore.Add(tc.endpointSlice) + if err != nil { + t.Fatalf("Error adding EndpointSlice to store: %v", err) + } + + endpointSlices, err := endpointSlicesMirroredForService(c.endpointSliceLister, tc.namespace, tc.name) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + if tc.expectedInList { + if len(endpointSlices) != 1 { + t.Fatalf("Expected 1 EndpointSlice to be in list, got %d", len(endpointSlices)) + } + + if endpointSlices[0].Name != tc.endpointSlice.Name { + t.Fatalf("Expected %s EndpointSlice to be in list, got %s", tc.endpointSlice.Name, endpointSlices[0].Name) + } + } else { + if len(endpointSlices) != 0 { + t.Fatalf("Expected no EndpointSlices to be in list, got %d", len(endpointSlices)) + } + } + }) + } +} + +func generateAddresses(num int) []v1.EndpointAddress { + addresses := make([]v1.EndpointAddress, num) + for i := 0; i < num; i++ { + part1 := i / 255 + part2 := i % 255 + ip := fmt.Sprintf("10.0.%d.%d", part1, part2) + addresses[i] = v1.EndpointAddress{IP: ip} + } + return addresses +} diff --git a/pkg/controller/endpointslicemirroring/events.go b/pkg/controller/endpointslicemirroring/events.go new file mode 100644 index 00000000000..5cf511cf4fb --- /dev/null +++ b/pkg/controller/endpointslicemirroring/events.go @@ -0,0 +1,29 @@ +/* +Copyright 2020 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 endpointslicemirroring + +const ( + // FailedToListEndpointSlices indicates the controller has failed to list + // EndpointSlices. + FailedToListEndpointSlices = "FailedToListEndpointSlices" + // FailedToUpdateEndpointSlices indicates the controller has failed to + // update EndpointSlices. + FailedToUpdateEndpointSlices = "FailedToUpdateEndpointSlices" + // InvalidIPAddress indicates that an IP address found in an Endpoints + // resource is invalid. + InvalidIPAddress = "InvalidIPAddress" +) diff --git a/pkg/controller/endpointslicemirroring/metrics/BUILD b/pkg/controller/endpointslicemirroring/metrics/BUILD new file mode 100644 index 00000000000..4919b3fdacf --- /dev/null +++ b/pkg/controller/endpointslicemirroring/metrics/BUILD @@ -0,0 +1,42 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "cache.go", + "metrics.go", + ], + importpath = "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/metrics", + visibility = ["//visibility:public"], + deps = [ + "//pkg/controller/util/endpoint:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", + "//staging/src/k8s.io/component-base/metrics:go_default_library", + "//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) + +go_test( + name = "go_default_test", + srcs = ["cache_test.go"], + embed = [":go_default_library"], + deps = [ + "//pkg/controller/util/endpoint:go_default_library", + "//staging/src/k8s.io/api/discovery/v1beta1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", + ], +) diff --git a/pkg/controller/endpointslicemirroring/metrics/cache.go b/pkg/controller/endpointslicemirroring/metrics/cache.go new file mode 100644 index 00000000000..553449687a8 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/metrics/cache.go @@ -0,0 +1,158 @@ +/* +Copyright 2020 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 metrics + +import ( + "math" + "sync" + + "k8s.io/apimachinery/pkg/types" + endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint" +) + +// NewCache returns a new Cache with the specified endpointsPerSlice. +func NewCache(endpointsPerSlice int32) *Cache { + return &Cache{ + maxEndpointsPerSlice: endpointsPerSlice, + cache: map[types.NamespacedName]*EndpointPortCache{}, + } +} + +// Cache tracks values for total numbers of desired endpoints as well as the +// efficiency of EndpointSlice endpoints distribution. +type Cache struct { + // maxEndpointsPerSlice references the maximum number of endpoints that + // should be added to an EndpointSlice. + maxEndpointsPerSlice int32 + + // lock protects changes to numEndpoints and cache. + lock sync.Mutex + // numEndpoints represents the total number of endpoints stored in + // EndpointSlices. + numEndpoints int + // cache stores a EndpointPortCache grouped by NamespacedNames representing + // Services. + cache map[types.NamespacedName]*EndpointPortCache +} + +// EndpointPortCache tracks values for total numbers of desired endpoints as well +// as the efficiency of EndpointSlice endpoints distribution for each unique +// Service Port combination. +type EndpointPortCache struct { + items map[endpointutil.PortMapKey]EfficiencyInfo +} + +// EfficiencyInfo stores the number of Endpoints and Slices for calculating +// total numbers of desired endpoints and the efficiency of EndpointSlice +// endpoints distribution. +type EfficiencyInfo struct { + Endpoints int + Slices int +} + +// NewEndpointPortCache initializes and returns a new EndpointPortCache. +func NewEndpointPortCache() *EndpointPortCache { + return &EndpointPortCache{ + items: map[endpointutil.PortMapKey]EfficiencyInfo{}, + } +} + +// Set updates the EndpointPortCache to contain the provided EfficiencyInfo +// for the provided PortMapKey. +func (spc *EndpointPortCache) Set(pmKey endpointutil.PortMapKey, eInfo EfficiencyInfo) { + spc.items[pmKey] = eInfo +} + +// numEndpoints returns the total number of endpoints represented by a +// EndpointPortCache. +func (spc *EndpointPortCache) numEndpoints() int { + num := 0 + for _, eInfo := range spc.items { + num += eInfo.Endpoints + } + return num +} + +// UpdateEndpointPortCache updates a EndpointPortCache in the global cache for a +// given Service and updates the corresponding metrics. +// Parameters: +// * endpointsNN refers to a NamespacedName representing the Endpoints resource. +// * epCache refers to a EndpointPortCache for the specified Endpoints reosource. +func (c *Cache) UpdateEndpointPortCache(endpointsNN types.NamespacedName, epCache *EndpointPortCache) { + c.lock.Lock() + defer c.lock.Unlock() + + prevNumEndpoints := 0 + if existingEPCache, ok := c.cache[endpointsNN]; ok { + prevNumEndpoints = existingEPCache.numEndpoints() + } + + currNumEndpoints := epCache.numEndpoints() + // To keep numEndpoints up to date, add the difference between the number of + // endpoints in the provided spCache and any spCache it might be replacing. + c.numEndpoints = c.numEndpoints + currNumEndpoints - prevNumEndpoints + + c.cache[endpointsNN] = epCache + c.updateMetrics() +} + +// DeleteEndpoints removes references to an Endpoints resource from the global +// cache and updates the corresponding metrics. +func (c *Cache) DeleteEndpoints(endpointsNN types.NamespacedName) { + c.lock.Lock() + defer c.lock.Unlock() + + if spCache, ok := c.cache[endpointsNN]; ok { + c.numEndpoints = c.numEndpoints - spCache.numEndpoints() + delete(c.cache, endpointsNN) + c.updateMetrics() + } +} + +// metricsUpdate stores a desired and actual number of EndpointSlices. +type metricsUpdate struct { + desired, actual int +} + +// desiredAndActualSlices returns a metricsUpdate with the desired and actual +// number of EndpointSlices given the current values in the cache. +// Must be called holding lock. +func (c *Cache) desiredAndActualSlices() metricsUpdate { + mUpdate := metricsUpdate{} + for _, spCache := range c.cache { + for _, eInfo := range spCache.items { + mUpdate.actual += eInfo.Slices + mUpdate.desired += numDesiredSlices(eInfo.Endpoints, int(c.maxEndpointsPerSlice)) + } + } + return mUpdate +} + +// updateMetrics updates metrics with the values from this Cache. +// Must be called holding lock. +func (c *Cache) updateMetrics() { + mUpdate := c.desiredAndActualSlices() + NumEndpointSlices.WithLabelValues().Set(float64(mUpdate.actual)) + DesiredEndpointSlices.WithLabelValues().Set(float64(mUpdate.desired)) + EndpointsDesired.WithLabelValues().Set(float64(c.numEndpoints)) +} + +// numDesiredSlices calculates the number of EndpointSlices that would exist +// with ideal endpoint distribution. +func numDesiredSlices(numEndpoints, maxPerSlice int) int { + return int(math.Ceil(float64(numEndpoints) / float64(maxPerSlice))) +} diff --git a/pkg/controller/endpointslicemirroring/metrics/cache_test.go b/pkg/controller/endpointslicemirroring/metrics/cache_test.go new file mode 100644 index 00000000000..9ee69ca8cd2 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/metrics/cache_test.go @@ -0,0 +1,72 @@ +/* +Copyright 2020 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 metrics + +import ( + "testing" + + discovery "k8s.io/api/discovery/v1beta1" + "k8s.io/apimachinery/pkg/types" + endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint" +) + +func TestNumEndpointsAndSlices(t *testing.T) { + c := NewCache(int32(100)) + + p80 := int32(80) + p443 := int32(443) + + pmKey80443 := endpointutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}, {Port: &p443}}) + pmKey80 := endpointutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}}) + + spCacheEfficient := NewEndpointPortCache() + spCacheEfficient.Set(pmKey80, EfficiencyInfo{Endpoints: 45, Slices: 1}) + spCacheEfficient.Set(pmKey80443, EfficiencyInfo{Endpoints: 35, Slices: 1}) + + spCacheInefficient := NewEndpointPortCache() + spCacheInefficient.Set(pmKey80, EfficiencyInfo{Endpoints: 12, Slices: 5}) + spCacheInefficient.Set(pmKey80443, EfficiencyInfo{Endpoints: 18, Slices: 8}) + + c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc1"}, spCacheInefficient) + expectNumEndpointsAndSlices(t, c, 2, 13, 30) + + c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc2"}, spCacheEfficient) + expectNumEndpointsAndSlices(t, c, 4, 15, 110) + + c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc3"}, spCacheInefficient) + expectNumEndpointsAndSlices(t, c, 6, 28, 140) + + c.UpdateEndpointPortCache(types.NamespacedName{Namespace: "ns1", Name: "svc1"}, spCacheEfficient) + expectNumEndpointsAndSlices(t, c, 6, 17, 190) + + c.DeleteEndpoints(types.NamespacedName{Namespace: "ns1", Name: "svc3"}) + expectNumEndpointsAndSlices(t, c, 4, 4, 160) +} + +func expectNumEndpointsAndSlices(t *testing.T, c *Cache, desired int, actual int, numEndpoints int) { + t.Helper() + mUpdate := c.desiredAndActualSlices() + if mUpdate.desired != desired { + t.Errorf("Expected numEndpointSlices to be %d, got %d", desired, mUpdate.desired) + } + if mUpdate.actual != actual { + t.Errorf("Expected desiredEndpointSlices to be %d, got %d", actual, mUpdate.actual) + } + if c.numEndpoints != numEndpoints { + t.Errorf("Expected numEndpoints to be %d, got %d", numEndpoints, c.numEndpoints) + } +} diff --git a/pkg/controller/endpointslicemirroring/metrics/metrics.go b/pkg/controller/endpointslicemirroring/metrics/metrics.go new file mode 100644 index 00000000000..c3a3d0d04ee --- /dev/null +++ b/pkg/controller/endpointslicemirroring/metrics/metrics.go @@ -0,0 +1,136 @@ +/* +Copyright 2020 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 metrics + +import ( + "sync" + + "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" +) + +// EndpointSliceMirroringSubsystem is the name of the subsystem used for +// EndpointSliceMirroring controller. +const EndpointSliceMirroringSubsystem = "endpoint_slice_mirroring_controller" + +var ( + // EndpointsAddedPerSync tracks the number of endpoints added on each + // Endpoints sync. + EndpointsAddedPerSync = metrics.NewHistogramVec( + &metrics.HistogramOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "endpoints_added_per_sync", + Help: "Number of endpoints added on each Endpoints sync", + StabilityLevel: metrics.ALPHA, + Buckets: metrics.ExponentialBuckets(2, 2, 15), + }, + []string{}, + ) + // EndpointsUpdatedPerSync tracks the number of endpoints updated on each + // Endpoints sync. + EndpointsUpdatedPerSync = metrics.NewHistogramVec( + &metrics.HistogramOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "endpoints_updated_per_sync", + Help: "Number of endpoints updated on each Endpoints sync", + StabilityLevel: metrics.ALPHA, + Buckets: metrics.ExponentialBuckets(2, 2, 15), + }, + []string{}, + ) + // EndpointsRemovedPerSync tracks the number of endpoints removed on each + // Endpoints sync. + EndpointsRemovedPerSync = metrics.NewHistogramVec( + &metrics.HistogramOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "endpoints_removed_per_sync", + Help: "Number of endpoints removed on each Endpoints sync", + StabilityLevel: metrics.ALPHA, + Buckets: metrics.ExponentialBuckets(2, 2, 15), + }, + []string{}, + ) + // EndpointsSyncDuration tracks how long syncEndpoints() takes in a number + // of Seconds. + EndpointsSyncDuration = metrics.NewHistogramVec( + &metrics.HistogramOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "endpoints_sync_duration", + Help: "Duration of syncEndpoints() in seconds", + StabilityLevel: metrics.ALPHA, + Buckets: metrics.ExponentialBuckets(0.001, 2, 15), + }, + []string{}, + ) + // EndpointsDesired tracks the total number of desired endpoints. + EndpointsDesired = metrics.NewGaugeVec( + &metrics.GaugeOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "endpoints_desired", + Help: "Number of endpoints desired", + StabilityLevel: metrics.ALPHA, + }, + []string{}, + ) + // NumEndpointSlices tracks the number of EndpointSlices in a cluster. + NumEndpointSlices = metrics.NewGaugeVec( + &metrics.GaugeOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "num_endpoint_slices", + Help: "Number of EndpointSlices", + StabilityLevel: metrics.ALPHA, + }, + []string{}, + ) + // DesiredEndpointSlices tracks the number of EndpointSlices that would + // exist with perfect endpoint allocation. + DesiredEndpointSlices = metrics.NewGaugeVec( + &metrics.GaugeOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "desired_endpoint_slices", + Help: "Number of EndpointSlices that would exist with perfect endpoint allocation", + StabilityLevel: metrics.ALPHA, + }, + []string{}, + ) + // EndpointSliceChanges tracks the number of changes to Endpoint Slices. + EndpointSliceChanges = metrics.NewCounterVec( + &metrics.CounterOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "changes", + Help: "Number of EndpointSlice changes", + StabilityLevel: metrics.ALPHA, + }, + []string{"operation"}, + ) +) + +var registerMetrics sync.Once + +// RegisterMetrics registers EndpointSlice metrics. +func RegisterMetrics() { + registerMetrics.Do(func() { + legacyregistry.MustRegister(EndpointsAddedPerSync) + legacyregistry.MustRegister(EndpointsUpdatedPerSync) + legacyregistry.MustRegister(EndpointsRemovedPerSync) + legacyregistry.MustRegister(EndpointsSyncDuration) + legacyregistry.MustRegister(EndpointsDesired) + legacyregistry.MustRegister(NumEndpointSlices) + legacyregistry.MustRegister(DesiredEndpointSlices) + legacyregistry.MustRegister(EndpointSliceChanges) + }) +} diff --git a/pkg/controller/endpointslicemirroring/reconciler.go b/pkg/controller/endpointslicemirroring/reconciler.go new file mode 100644 index 00000000000..041854fa728 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/reconciler.go @@ -0,0 +1,291 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "context" + "fmt" + + corev1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + utilerrors "k8s.io/apimachinery/pkg/util/errors" + clientset "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/record" + "k8s.io/klog/v2" + "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/metrics" + endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint" +) + +// reconciler is responsible for transforming current EndpointSlice state into +// desired state +type reconciler struct { + client clientset.Interface + maxEndpointsPerSubset int32 + endpointSliceTracker *endpointSliceTracker + metricsCache *metrics.Cache + eventRecorder record.EventRecorder +} + +// reconcile takes an Endpoints resource and ensures that corresponding +// EndpointSlices exist. It creates, updates, or deletes EndpointSlices to +// ensure the desired set of addresses are represented by EndpointSlices. +func (r *reconciler) reconcile(endpoints *corev1.Endpoints, existingSlices []*discovery.EndpointSlice) error { + // Calculate desired state. + d := newDesiredCalc() + + for _, subset := range endpoints.Subsets { + multiKey := d.initPorts(subset.Ports) + + totalAddresses := 0 + numInvalidAddresses := 0 + + for _, address := range subset.Addresses { + totalAddresses++ + if totalAddresses > int(r.maxEndpointsPerSubset) { + break + } + if ok := d.addAddress(address, multiKey, true); !ok { + numInvalidAddresses++ + klog.Warningf("Address in %s/%s Endpoints is not a valid IP, it will not be mirrored to an EndpointSlice: %s", endpoints.Namespace, endpoints.Name, address.IP) + } + } + + for _, address := range subset.NotReadyAddresses { + totalAddresses++ + if totalAddresses > int(r.maxEndpointsPerSubset) { + break + } + if ok := d.addAddress(address, multiKey, false); !ok { + numInvalidAddresses++ + klog.Warningf("Address in %s/%s Endpoints is not a valid IP, it will not be mirrored to an EndpointSlice: %s", endpoints.Namespace, endpoints.Name, address.IP) + } + } + + if numInvalidAddresses > 0 { + r.eventRecorder.Eventf(endpoints, corev1.EventTypeWarning, InvalidIPAddress, + "Skipped %d invalid IP addresses when mirroring to EndpointSlices", numInvalidAddresses) + } + } + + // Build data structures for existing state. + existingSlicesByKey := endpointSlicesByKey(existingSlices) + + // Determine changes necessary for each group of slices by port map. + epMetrics := metrics.NewEndpointPortCache() + totals := totalsByAction{} + slices := slicesByAction{} + + for portKey, desiredEndpoints := range d.endpointsByKey { + numEndpoints := len(desiredEndpoints) + pmSlices, pmTotals := r.reconcileByPortMapping( + endpoints, existingSlicesByKey[portKey], desiredEndpoints, d.portsByKey[portKey], portKey.addressType()) + + slices.append(pmSlices) + totals.add(pmTotals) + + epMetrics.Set(endpointutil.PortMapKey(portKey), metrics.EfficiencyInfo{ + Endpoints: numEndpoints, + Slices: len(existingSlicesByKey[portKey]) + len(pmSlices.toCreate) - len(pmSlices.toDelete), + }) + } + + // If there are unique sets of ports that are no longer desired, mark + // the corresponding endpoint slices for deletion. + for portKey, existingSlices := range existingSlicesByKey { + if _, ok := d.endpointsByKey[portKey]; !ok { + for _, existingSlice := range existingSlices { + slices.toDelete = append(slices.toDelete, existingSlice) + } + } + } + + metrics.EndpointsAddedPerSync.WithLabelValues().Observe(float64(totals.added)) + metrics.EndpointsUpdatedPerSync.WithLabelValues().Observe(float64(totals.updated)) + metrics.EndpointsRemovedPerSync.WithLabelValues().Observe(float64(totals.removed)) + + endpointsNN := types.NamespacedName{Name: endpoints.Name, Namespace: endpoints.Namespace} + r.metricsCache.UpdateEndpointPortCache(endpointsNN, epMetrics) + + return r.finalize(endpoints, slices) +} + +// reconcileByPortMapping compares the endpoints found in existing slices with +// the list of desired endpoints and returns lists of slices to create, update, +// and delete. +func (r *reconciler) reconcileByPortMapping( + endpoints *corev1.Endpoints, + existingSlices []*discovery.EndpointSlice, + desiredSet endpointSet, + endpointPorts []discovery.EndpointPort, + addressType discovery.AddressType, +) (slicesByAction, totalsByAction) { + slices := slicesByAction{} + totals := totalsByAction{} + + // If no endpoints are desired, mark existing slices for deletion and + // return. + if desiredSet.Len() == 0 { + slices.toDelete = existingSlices + for _, epSlice := range existingSlices { + totals.removed += len(epSlice.Endpoints) + } + return slices, totals + } + + if len(existingSlices) == 0 { + // if no existing slices, all desired endpoints will be added. + totals.added = desiredSet.Len() + } else { + // if >0 existing slices, mark all but 1 for deletion. + slices.toDelete = existingSlices[1:] + + // Return early if first slice matches desired endpoints. + totals = totalChanges(existingSlices[0], desiredSet) + if totals.added == 0 && totals.updated == 0 && totals.removed == 0 { + return slices, totals + } + } + + // generate a new slice with the desired endpoints. + var sliceName string + if len(existingSlices) > 0 { + sliceName = existingSlices[0].Name + } + newSlice := newEndpointSlice(endpoints, endpointPorts, addressType, sliceName) + for desiredSet.Len() > 0 && len(newSlice.Endpoints) < int(r.maxEndpointsPerSubset) { + endpoint, _ := desiredSet.PopAny() + newSlice.Endpoints = append(newSlice.Endpoints, *endpoint) + } + + if newSlice.Name != "" { + slices.toUpdate = []*discovery.EndpointSlice{newSlice} + } else { // Slices to be created set GenerateName instead of Name. + slices.toCreate = []*discovery.EndpointSlice{newSlice} + } + + return slices, totals +} + +// finalize creates, updates, and deletes slices as specified +func (r *reconciler) finalize(endpoints *corev1.Endpoints, slices slicesByAction) error { + // If there are slices to create and delete, recycle the slices marked for + // deletion by replacing creates with updates of slices that would otherwise + // be deleted. + recycleSlices(&slices) + + var errs []error + epsClient := r.client.DiscoveryV1beta1().EndpointSlices(endpoints.Namespace) + + // Don't create more EndpointSlices if corresponding Endpoints resource is + // being deleted. + if endpoints.DeletionTimestamp == nil { + for _, endpointSlice := range slices.toCreate { + createdSlice, err := epsClient.Create(context.TODO(), endpointSlice, metav1.CreateOptions{}) + if err != nil { + // If the namespace is terminating, creates will continue to fail. Simply drop the item. + if errors.HasStatusCause(err, corev1.NamespaceTerminatingCause) { + return nil + } + errs = append(errs, fmt.Errorf("Error creating EndpointSlice for Endpoints %s/%s: %v", endpoints.Namespace, endpoints.Name, err)) + } else { + r.endpointSliceTracker.update(createdSlice) + metrics.EndpointSliceChanges.WithLabelValues("create").Inc() + } + } + } + + for _, endpointSlice := range slices.toUpdate { + updatedSlice, err := epsClient.Update(context.TODO(), endpointSlice, metav1.UpdateOptions{}) + if err != nil { + errs = append(errs, fmt.Errorf("Error updating %s EndpointSlice for Endpoints %s/%s: %v", endpointSlice.Name, endpoints.Namespace, endpoints.Name, err)) + } else { + r.endpointSliceTracker.update(updatedSlice) + metrics.EndpointSliceChanges.WithLabelValues("update").Inc() + } + } + + for _, endpointSlice := range slices.toDelete { + err := epsClient.Delete(context.TODO(), endpointSlice.Name, metav1.DeleteOptions{}) + if err != nil { + errs = append(errs, fmt.Errorf("Error deleting %s EndpointSlice for Endpoints %s/%s: %v", endpointSlice.Name, endpoints.Namespace, endpoints.Name, err)) + } else { + r.endpointSliceTracker.delete(endpointSlice) + metrics.EndpointSliceChanges.WithLabelValues("delete").Inc() + } + } + + return utilerrors.NewAggregate(errs) +} + +// deleteEndpoints deletes any associated EndpointSlices and cleans up any +// Endpoints references from the metricsCache. +func (r *reconciler) deleteEndpoints(namespace, name string, endpointSlices []*discovery.EndpointSlice) error { + r.metricsCache.DeleteEndpoints(types.NamespacedName{Namespace: namespace, Name: name}) + var errs []error + for _, endpointSlice := range endpointSlices { + err := r.client.DiscoveryV1beta1().EndpointSlices(namespace).Delete(context.TODO(), endpointSlice.Name, metav1.DeleteOptions{}) + if err != nil { + errs = append(errs, err) + } + } + if len(errs) > 0 { + return fmt.Errorf("Error(s) deleting %d/%d EndpointSlices for %s/%s Endpoints, including: %s", len(errs), len(endpointSlices), namespace, name, errs[0]) + } + return nil +} + +// endpointSlicesByKey returns a map that groups EndpointSlices by unique +// addrTypePortMapKey values. +func endpointSlicesByKey(existingSlices []*discovery.EndpointSlice) map[addrTypePortMapKey][]*discovery.EndpointSlice { + slicesByKey := map[addrTypePortMapKey][]*discovery.EndpointSlice{} + for _, existingSlice := range existingSlices { + epKey := newAddrTypePortMapKey(existingSlice.Ports, existingSlice.AddressType) + slicesByKey[epKey] = append(slicesByKey[epKey], existingSlice) + } + return slicesByKey +} + +// totalChanges returns the total changes that will be required for an +// EndpointSlice to match a desired set of endpoints. +func totalChanges(existingSlice *discovery.EndpointSlice, desiredSet endpointSet) totalsByAction { + totals := totalsByAction{} + existingMatches := 0 + + for _, endpoint := range existingSlice.Endpoints { + got := desiredSet.Get(&endpoint) + if got == nil { + // If not desired, increment number of endpoints to be deleted. + totals.removed++ + } else { + existingMatches++ + + // If existing version of endpoint doesn't match desired version + // increment number of endpoints to be updated. + if !endpointsEqualBeyondHash(got, &endpoint) { + totals.updated++ + } + } + } + + // Any desired endpoints that have not been found in the existing slice will + // be added. + totals.added = desiredSet.Len() - existingMatches + return totals +} diff --git a/pkg/controller/endpointslicemirroring/reconciler_helpers.go b/pkg/controller/endpointslicemirroring/reconciler_helpers.go new file mode 100644 index 00000000000..0ab382a51c0 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/reconciler_helpers.go @@ -0,0 +1,137 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" +) + +// slicesByAction includes lists of slices to create, update, or delete. +type slicesByAction struct { + toCreate, toUpdate, toDelete []*discovery.EndpointSlice +} + +// append appends slices from another slicesByAction struct. +func (s *slicesByAction) append(slices slicesByAction) { + s.toCreate = append(s.toCreate, slices.toCreate...) + s.toUpdate = append(s.toUpdate, slices.toUpdate...) + s.toDelete = append(s.toDelete, slices.toDelete...) +} + +// totalsByAction includes total numbers for added and removed. +type totalsByAction struct { + added, updated, removed int +} + +// add adds totals from another totalsByAction struct. +func (t *totalsByAction) add(totals totalsByAction) { + t.added += totals.added + t.updated += totals.updated + t.removed += totals.removed +} + +// newDesiredCalc initializes and returns a new desiredCalc. +func newDesiredCalc() *desiredCalc { + return &desiredCalc{ + portsByKey: map[addrTypePortMapKey][]discovery.EndpointPort{}, + endpointsByKey: map[addrTypePortMapKey]endpointSet{}, + numDesiredEndpoints: 0, + } +} + +// desiredCalc helps calculate desired endpoints and ports. +type desiredCalc struct { + portsByKey map[addrTypePortMapKey][]discovery.EndpointPort + endpointsByKey map[addrTypePortMapKey]endpointSet + numDesiredEndpoints int +} + +// multiAddrTypePortMapKey stores addrTypePortMapKey for different address +// types. +type multiAddrTypePortMapKey map[discovery.AddressType]addrTypePortMapKey + +// initPorts initializes ports for a subset and address type and returns the +// corresponding addrTypePortMapKey. +func (d *desiredCalc) initPorts(subsetPorts []v1.EndpointPort) multiAddrTypePortMapKey { + endpointPorts := epPortsToEpsPorts(subsetPorts) + addrTypes := []discovery.AddressType{discovery.AddressTypeIPv4, discovery.AddressTypeIPv6} + multiKey := multiAddrTypePortMapKey{} + + for _, addrType := range addrTypes { + multiKey[addrType] = newAddrTypePortMapKey(endpointPorts, addrType) + if _, ok := d.endpointsByKey[multiKey[addrType]]; !ok { + d.endpointsByKey[multiKey[addrType]] = endpointSet{} + } + d.portsByKey[multiKey[addrType]] = endpointPorts + } + + return multiKey +} + +// addAddress adds an EndpointAddress to the desired state if it is valid. It +// returns false if the address was invalid. +func (d *desiredCalc) addAddress(address v1.EndpointAddress, multiKey multiAddrTypePortMapKey, ready bool) bool { + endpoint := addressToEndpoint(address, ready) + addrType := getAddressType(address.IP) + if addrType == nil { + return false + } + + d.endpointsByKey[multiKey[*addrType]].Insert(endpoint) + d.numDesiredEndpoints++ + return true +} + +type slicesByAddrType map[discovery.AddressType][]*discovery.EndpointSlice + +// recycleSlices will recycle the slices marked for deletion by replacing +// creates with updates of slices that would otherwise be deleted. +func recycleSlices(slices *slicesByAction) { + toCreateByAddrType := toSlicesByAddrType(slices.toCreate) + toDeleteByAddrType := toSlicesByAddrType(slices.toDelete) + + for addrType, slicesToCreate := range toCreateByAddrType { + slicesToDelete := toDeleteByAddrType[addrType] + for i := 0; len(slicesToCreate) > i && len(slicesToDelete) > i; i++ { + slices.toCreate = removeSlice(slices.toCreate, slicesToCreate[i]) + slices.toDelete = removeSlice(slices.toDelete, slicesToDelete[i]) + slice := slicesToCreate[i] + slice.Name = slicesToDelete[i].Name + slices.toUpdate = append(slices.toUpdate, slice) + } + } +} + +// removeSlice removes an EndpointSlice from a list of EndpointSlices. +func removeSlice(slices []*discovery.EndpointSlice, sliceToRemove *discovery.EndpointSlice) []*discovery.EndpointSlice { + for i, slice := range slices { + if slice.Name == sliceToRemove.Name { + return append(slices[:i], slices[i+1:]...) + } + } + return slices +} + +// toSliceByAddrType returns lists of EndpointSlices grouped by address. +func toSlicesByAddrType(slices []*discovery.EndpointSlice) slicesByAddrType { + byAddrType := slicesByAddrType{} + for _, slice := range slices { + byAddrType[slice.AddressType] = append(byAddrType[slice.AddressType], slice) + } + return byAddrType +} diff --git a/pkg/controller/endpointslicemirroring/reconciler_helpers_test.go b/pkg/controller/endpointslicemirroring/reconciler_helpers_test.go new file mode 100644 index 00000000000..ce9069648e5 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/reconciler_helpers_test.go @@ -0,0 +1,180 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "testing" + + discovery "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestRecycleSlices(t *testing.T) { + testCases := []struct { + testName string + startingSlices *slicesByAction + expectedSlices *slicesByAction + }{{ + testName: "Empty slices", + startingSlices: &slicesByAction{}, + expectedSlices: &slicesByAction{}, + }, { + testName: "1 to create and 1 to delete", + startingSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{simpleEndpointSlice("foo", "10.1.2.3", discovery.AddressTypeIPv4)}, + toDelete: []*discovery.EndpointSlice{simpleEndpointSlice("bar", "10.2.3.4", discovery.AddressTypeIPv4)}, + }, + expectedSlices: &slicesByAction{ + toUpdate: []*discovery.EndpointSlice{simpleEndpointSlice("bar", "10.1.2.3", discovery.AddressTypeIPv4)}, + }, + }, { + testName: "1 to create, update, and delete", + startingSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{simpleEndpointSlice("foo", "10.1.2.3", discovery.AddressTypeIPv4)}, + toUpdate: []*discovery.EndpointSlice{simpleEndpointSlice("baz", "10.2.3.4", discovery.AddressTypeIPv4)}, + toDelete: []*discovery.EndpointSlice{simpleEndpointSlice("bar", "10.3.4.5", discovery.AddressTypeIPv4)}, + }, + expectedSlices: &slicesByAction{ + toUpdate: []*discovery.EndpointSlice{ + simpleEndpointSlice("baz", "10.2.3.4", discovery.AddressTypeIPv4), + simpleEndpointSlice("bar", "10.1.2.3", discovery.AddressTypeIPv4), + }, + }, + }, { + testName: "2 to create and 1 to delete", + startingSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{ + simpleEndpointSlice("foo1", "10.1.2.3", discovery.AddressTypeIPv4), + simpleEndpointSlice("foo2", "10.3.4.5", discovery.AddressTypeIPv4), + }, + toDelete: []*discovery.EndpointSlice{simpleEndpointSlice("bar", "10.2.3.4", discovery.AddressTypeIPv4)}, + }, + expectedSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{simpleEndpointSlice("foo2", "10.3.4.5", discovery.AddressTypeIPv4)}, + toUpdate: []*discovery.EndpointSlice{simpleEndpointSlice("bar", "10.1.2.3", discovery.AddressTypeIPv4)}, + }, + }, { + testName: "1 to create and 2 to delete", + startingSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{ + simpleEndpointSlice("foo1", "10.1.2.3", discovery.AddressTypeIPv4), + }, + toDelete: []*discovery.EndpointSlice{ + simpleEndpointSlice("bar1", "10.2.3.4", discovery.AddressTypeIPv4), + simpleEndpointSlice("bar2", "10.3.4.5", discovery.AddressTypeIPv4), + }, + }, + expectedSlices: &slicesByAction{ + toUpdate: []*discovery.EndpointSlice{simpleEndpointSlice("bar1", "10.1.2.3", discovery.AddressTypeIPv4)}, + toDelete: []*discovery.EndpointSlice{simpleEndpointSlice("bar2", "10.3.4.5", discovery.AddressTypeIPv4)}, + }, + }, { + testName: "1 to create and 1 to delete for each IP family", + startingSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{ + simpleEndpointSlice("foo-v4", "10.1.2.3", discovery.AddressTypeIPv4), + simpleEndpointSlice("foo-v6", "2001:db8:1111:3333:4444:5555:6666:7777", discovery.AddressTypeIPv6), + }, + toDelete: []*discovery.EndpointSlice{ + simpleEndpointSlice("bar-v4", "10.2.2.3", discovery.AddressTypeIPv4), + simpleEndpointSlice("bar-v6", "2001:db8:2222:3333:4444:5555:6666:7777", discovery.AddressTypeIPv6), + }, + }, + expectedSlices: &slicesByAction{ + toUpdate: []*discovery.EndpointSlice{ + simpleEndpointSlice("bar-v4", "10.1.2.3", discovery.AddressTypeIPv4), + simpleEndpointSlice("bar-v6", "2001:db8:1111:3333:4444:5555:6666:7777", discovery.AddressTypeIPv6), + }, + }, + }, { + testName: "1 to create and 1 to delete, wrong IP family", + startingSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{ + simpleEndpointSlice("foo-v4", "10.1.2.3", discovery.AddressTypeIPv4), + }, + toDelete: []*discovery.EndpointSlice{ + simpleEndpointSlice("bar-v6", "2001:db8:2222:3333:4444:5555:6666:7777", discovery.AddressTypeIPv6), + }, + }, + expectedSlices: &slicesByAction{ + toCreate: []*discovery.EndpointSlice{ + simpleEndpointSlice("foo-v4", "10.1.2.3", discovery.AddressTypeIPv4), + }, + toDelete: []*discovery.EndpointSlice{ + simpleEndpointSlice("bar-v6", "2001:db8:2222:3333:4444:5555:6666:7777", discovery.AddressTypeIPv6), + }, + }, + }} + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + recycleSlices(tc.startingSlices) + + expectEqualSlices(t, tc.startingSlices.toCreate, tc.expectedSlices.toCreate) + expectEqualSlices(t, tc.startingSlices.toUpdate, tc.expectedSlices.toUpdate) + expectEqualSlices(t, tc.startingSlices.toDelete, tc.expectedSlices.toDelete) + }) + } +} + +// Test helpers +func expectEqualSlices(t *testing.T, actual, expected []*discovery.EndpointSlice) { + t.Helper() + if len(actual) != len(expected) { + t.Fatalf("Expected %d EndpointSlices, got %d: %v", len(expected), len(actual), actual) + } + + for i, expectedSlice := range expected { + if expectedSlice.AddressType != actual[i].AddressType { + t.Errorf("Expected Slice to have %s address type, got %s", expectedSlice.AddressType, actual[i].AddressType) + } + + if expectedSlice.Name != actual[i].Name { + t.Errorf("Expected Slice to have %s name, got %s", expectedSlice.Name, actual[i].Name) + } + + if len(expectedSlice.Endpoints) != len(actual[i].Endpoints) { + t.Fatalf("Expected Slice to have %d endpoints, got %d", len(expectedSlice.Endpoints), len(actual[i].Endpoints)) + } + + for j, expectedEndpoint := range expectedSlice.Endpoints { + actualEndpoint := actual[i].Endpoints[j] + if len(expectedEndpoint.Addresses) != len(actualEndpoint.Addresses) { + t.Fatalf("Expected Endpoint to have %d addresses, got %d", len(expectedEndpoint.Addresses), len(actualEndpoint.Addresses)) + } + + for k, expectedAddress := range expectedEndpoint.Addresses { + actualAddress := actualEndpoint.Addresses[k] + if expectedAddress != actualAddress { + t.Fatalf("Expected address to be %s, got %s", expectedAddress, actualAddress) + } + } + } + } +} + +func simpleEndpointSlice(name, ip string, addrType discovery.AddressType) *discovery.EndpointSlice { + return &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + AddressType: addrType, + Endpoints: []discovery.Endpoint{{ + Addresses: []string{ip}, + }}, + } +} diff --git a/pkg/controller/endpointslicemirroring/reconciler_test.go b/pkg/controller/endpointslicemirroring/reconciler_test.go new file mode 100644 index 00000000000..b43304c2baa --- /dev/null +++ b/pkg/controller/endpointslicemirroring/reconciler_test.go @@ -0,0 +1,968 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "context" + "strings" + "testing" + + corev1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/fake" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/tools/record" + "k8s.io/component-base/metrics/testutil" + "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/metrics" + utilpointer "k8s.io/utils/pointer" +) + +const defaultMaxEndpointsPerSubset = int32(1000) + +// TestReconcile ensures that Endpoints are reconciled into corresponding +// EndpointSlices with appropriate fields. +func TestReconcile(t *testing.T) { + protoTCP := corev1.ProtocolTCP + protoUDP := corev1.ProtocolUDP + + testCases := []struct { + testName string + subsets []corev1.EndpointSubset + endpointsDeletionPending bool + maxEndpointsPerSubset int32 + existingEndpointSlices []*discovery.EndpointSlice + expectedNumSlices int + expectedClientActions int + expectedMetrics *expectedMetrics + }{{ + testName: "Endpoints with no subsets", + subsets: []corev1.EndpointSubset{}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 0, + expectedClientActions: 0, + expectedMetrics: &expectedMetrics{}, + }, { + testName: "Endpoints with no addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 0, + expectedClientActions: 0, + expectedMetrics: &expectedMetrics{}, + }, { + testName: "Endpoints with 1 subset, port, and address", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 1, + expectedClientActions: 1, + expectedMetrics: &expectedMetrics{desiredSlices: 1, actualSlices: 1, desiredEndpoints: 1, addedPerSync: 1, numCreated: 1}, + }, { + testName: "Endpoints with 1 subset, port, and address, pending deletion", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }}, + }}, + endpointsDeletionPending: true, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 0, + expectedClientActions: 0, + }, { + testName: "Endpoints with 1 subset, 2 ports, and 2 addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 1, + expectedClientActions: 1, + expectedMetrics: &expectedMetrics{desiredSlices: 1, actualSlices: 1, desiredEndpoints: 2, addedPerSync: 2, numCreated: 1}, + }, { + testName: "Endpoints with 2 subsets, multiple ports and addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.1.1", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.1.2", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "10.0.1.3", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 2, + expectedClientActions: 2, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 5, addedPerSync: 5, numCreated: 2}, + }, { + testName: "Endpoints with 2 subsets, multiple ports and addresses, existing empty EndpointSlice", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.1.1", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.1.2", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "10.0.1.3", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-ep-1", + }, + AddressType: discovery.AddressTypeIPv4, + Ports: []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("http"), + Port: utilpointer.Int32Ptr(80), + Protocol: &protoTCP, + }, { + Name: utilpointer.StringPtr("https"), + Port: utilpointer.Int32Ptr(443), + Protocol: &protoUDP, + }}, + }}, + expectedNumSlices: 2, + expectedClientActions: 2, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 5, addedPerSync: 5, numCreated: 1, numUpdated: 1}, + }, { + testName: "Endpoints with 2 subsets, multiple ports and addresses, existing EndpointSlice with some addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.1.1", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.1.2", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "10.0.1.3", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-ep-1", + }, + AddressType: discovery.AddressTypeIPv4, + Ports: []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("http"), + Port: utilpointer.Int32Ptr(80), + Protocol: &protoTCP, + }, { + Name: utilpointer.StringPtr("https"), + Port: utilpointer.Int32Ptr(443), + Protocol: &protoUDP, + }}, + Endpoints: []discovery.Endpoint{{ + Addresses: []string{"10.0.0.2"}, + Hostname: utilpointer.StringPtr("pod-2"), + }, { + Addresses: []string{"10.0.0.1", "10.0.0.3"}, + Hostname: utilpointer.StringPtr("pod-1"), + }}, + }}, + expectedNumSlices: 2, + expectedClientActions: 2, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 5, addedPerSync: 4, updatedPerSync: 1, removedPerSync: 1, numCreated: 1, numUpdated: 1}, + }, { + testName: "Endpoints with 2 subsets, multiple ports and addresses, existing EndpointSlice identical to subset", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.1.1", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.1.2", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "10.0.1.3", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-ep-1", + }, + AddressType: discovery.AddressTypeIPv4, + Ports: []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("http"), + Port: utilpointer.Int32Ptr(80), + Protocol: &protoTCP, + }, { + Name: utilpointer.StringPtr("https"), + Port: utilpointer.Int32Ptr(443), + Protocol: &protoUDP, + }}, + Endpoints: []discovery.Endpoint{{ + Addresses: []string{"10.0.0.1"}, + Hostname: utilpointer.StringPtr("pod-1"), + Topology: map[string]string{"kubernetes.io/hostname": "node-1"}, + Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)}, + }, { + Addresses: []string{"10.0.0.2"}, + Hostname: utilpointer.StringPtr("pod-2"), + Topology: map[string]string{"kubernetes.io/hostname": "node-2"}, + Conditions: discovery.EndpointConditions{Ready: utilpointer.BoolPtr(true)}, + }}, + }}, + expectedNumSlices: 2, + expectedClientActions: 1, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 5, addedPerSync: 3, numCreated: 1}, + }, { + testName: "Endpoints with 2 subsets, multiple ports, and dual stack addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "2001:db8:2222:3333:4444:5555:6666:7777", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.1.1", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.1.2", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "2001:db8:3333:4444:5555:6666:7777:8888", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 4, + expectedClientActions: 4, + expectedMetrics: &expectedMetrics{desiredSlices: 4, actualSlices: 4, desiredEndpoints: 5, addedPerSync: 5, numCreated: 4}, + }, { + testName: "Endpoints with 2 subsets, multiple ports, ipv6 only addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "2001:db8:1111:3333:4444:5555:6666:7777", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "2001:db8:2222:3333:4444:5555:6666:7777", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "2001:db8:3333:3333:4444:5555:6666:7777", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "2001:db8:4444:3333:4444:5555:6666:7777", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "2001:db8:5555:3333:4444:5555:6666:7777", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 2, + expectedClientActions: 2, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 5, addedPerSync: 5, numCreated: 2}, + }, { + testName: "Endpoints with 2 subsets, multiple ports, some invalid addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "2001:db8:1111:3333:4444:5555:6666:7777", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "this-is-not-an-ip", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "this-is-also-not-an-ip", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "2001:db8:4444:3333:4444:5555:6666:7777", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "2001:db8:5555:3333:4444:5555:6666:7777", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 2, + expectedClientActions: 2, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 3, addedPerSync: 3, numCreated: 2}, + }, { + testName: "Endpoints with 2 subsets, multiple ports, all invalid addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "this-is-not-an-ip1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "this-is-not-an-ip12", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "this-is-not-an-ip11", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "this-is-not-an-ip12", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "this-is-not-an-ip3", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 0, + expectedClientActions: 0, + expectedMetrics: &expectedMetrics{desiredSlices: 0, actualSlices: 0, desiredEndpoints: 0, addedPerSync: 0, numCreated: 0}, + }, { + testName: "Endpoints with 2 subsets, multiple ports and addresses, existing EndpointSlice with some addresses", + subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 80, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 443, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + Hostname: "pod-1", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.0.2", + Hostname: "pod-2", + NodeName: utilpointer.StringPtr("node-2"), + }}, + }, { + Ports: []corev1.EndpointPort{{ + Name: "http", + Port: 3000, + Protocol: corev1.ProtocolTCP, + }, { + Name: "https", + Port: 3001, + Protocol: corev1.ProtocolUDP, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.1.1", + Hostname: "pod-11", + NodeName: utilpointer.StringPtr("node-1"), + }, { + IP: "10.0.1.2", + Hostname: "pod-12", + NodeName: utilpointer.StringPtr("node-2"), + }, { + IP: "10.0.1.3", + Hostname: "pod-13", + NodeName: utilpointer.StringPtr("node-3"), + }}, + }}, + existingEndpointSlices: []*discovery.EndpointSlice{}, + expectedNumSlices: 2, + expectedClientActions: 2, + maxEndpointsPerSubset: 2, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 4, addedPerSync: 4, updatedPerSync: 0, removedPerSync: 0, numCreated: 2, numUpdated: 0}, + }} + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + client := newClientset() + setupMetrics() + namespace := "test" + endpoints := corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{Name: "test-ep", Namespace: namespace}, + Subsets: tc.subsets, + } + + if tc.endpointsDeletionPending { + now := metav1.Now() + endpoints.DeletionTimestamp = &now + } + + numInitialActions := 0 + for _, epSlice := range tc.existingEndpointSlices { + epSlice.Labels = map[string]string{ + discovery.LabelServiceName: endpoints.Name, + discovery.LabelManagedBy: controllerName, + } + _, err := client.DiscoveryV1beta1().EndpointSlices(namespace).Create(context.TODO(), epSlice, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Expected no error creating EndpointSlice, got %v", err) + } + numInitialActions++ + } + + maxEndpointsPerSubset := tc.maxEndpointsPerSubset + if maxEndpointsPerSubset == 0 { + maxEndpointsPerSubset = defaultMaxEndpointsPerSubset + } + r := newReconciler(client, maxEndpointsPerSubset) + reconcileHelper(t, r, &endpoints, tc.existingEndpointSlices) + + numExtraActions := len(client.Actions()) - numInitialActions + if numExtraActions != tc.expectedClientActions { + t.Fatalf("Expected %d additional client actions, got %d: %#v", tc.expectedClientActions, numExtraActions, client.Actions()[numInitialActions:]) + } + + if tc.expectedMetrics != nil { + expectMetrics(t, *tc.expectedMetrics) + } + + endpointSlices := fetchEndpointSlices(t, client, namespace) + expectEndpointSlices(t, tc.expectedNumSlices, int(maxEndpointsPerSubset), endpoints, endpointSlices) + }) + } +} + +// Test Helpers + +func newReconciler(client *fake.Clientset, maxEndpointsPerSubset int32) *reconciler { + broadcaster := record.NewBroadcaster() + recorder := broadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "endpoint-slice-mirroring-controller"}) + + return &reconciler{ + client: client, + maxEndpointsPerSubset: maxEndpointsPerSubset, + endpointSliceTracker: newEndpointSliceTracker(), + metricsCache: metrics.NewCache(maxEndpointsPerSubset), + eventRecorder: recorder, + } +} + +func expectEndpointSlices(t *testing.T, num, maxEndpointsPerSubset int, endpoints corev1.Endpoints, endpointSlices []discovery.EndpointSlice) { + t.Helper() + if len(endpointSlices) != num { + t.Fatalf("Expected %d EndpointSlices, got %d", num, len(endpointSlices)) + } + + if num == 0 { + return + } + + for _, epSlice := range endpointSlices { + if !strings.HasPrefix(epSlice.Name, endpoints.Name) { + t.Errorf("Expected EndpointSlice name to start with %s, got %s", endpoints.Name, epSlice.Name) + } + + serviceNameVal, ok := epSlice.Labels[discovery.LabelServiceName] + if !ok { + t.Errorf("Expected EndpointSlice to have %s label set", discovery.LabelServiceName) + } + if serviceNameVal != endpoints.Name { + t.Errorf("Expected EndpointSlice to have %s label set to %s, got %s", discovery.LabelServiceName, endpoints.Name, serviceNameVal) + } + } + + for _, epSubset := range endpoints.Subsets { + if len(epSubset.Addresses) == 0 && len(epSubset.NotReadyAddresses) == 0 { + continue + } + + var matchingEndpointsV4, matchingEndpointsV6 []discovery.Endpoint + + for _, epSlice := range endpointSlices { + if portsMatch(epSubset.Ports, epSlice.Ports) { + switch epSlice.AddressType { + case discovery.AddressTypeIPv4: + matchingEndpointsV4 = append(matchingEndpointsV4, epSlice.Endpoints...) + case discovery.AddressTypeIPv6: + matchingEndpointsV6 = append(matchingEndpointsV6, epSlice.Endpoints...) + default: + t.Fatalf("Unexpected EndpointSlice address type found: %v", epSlice.AddressType) + } + } + } + + if len(matchingEndpointsV4) == 0 && len(matchingEndpointsV6) == 0 { + t.Fatalf("No EndpointSlices match Endpoints subset: %#v", epSubset.Ports) + } + + expectMatchingAddresses(t, epSubset, matchingEndpointsV4, discovery.AddressTypeIPv4, maxEndpointsPerSubset) + expectMatchingAddresses(t, epSubset, matchingEndpointsV6, discovery.AddressTypeIPv6, maxEndpointsPerSubset) + } +} + +func portsMatch(epPorts []corev1.EndpointPort, epsPorts []discovery.EndpointPort) bool { + if len(epPorts) != len(epsPorts) { + return false + } + + portsToBeMatched := map[int32]corev1.EndpointPort{} + + for _, epPort := range epPorts { + portsToBeMatched[epPort.Port] = epPort + } + + for _, epsPort := range epsPorts { + epPort, ok := portsToBeMatched[*epsPort.Port] + if !ok { + return false + } + delete(portsToBeMatched, *epsPort.Port) + + if epPort.Name != *epsPort.Name { + return false + } + if epPort.Port != *epsPort.Port { + return false + } + if epPort.Protocol != *epsPort.Protocol { + return false + } + if epPort.AppProtocol != epsPort.AppProtocol { + return false + } + } + + return true +} + +func expectMatchingAddresses(t *testing.T, epSubset corev1.EndpointSubset, esEndpoints []discovery.Endpoint, addrType discovery.AddressType, maxEndpointsPerSubset int) { + t.Helper() + type addressInfo struct { + ready bool + epAddress corev1.EndpointAddress + } + + // This approach assumes that each IP is unique within an EndpointSubset. + expectedEndpoints := map[string]addressInfo{} + + for _, address := range epSubset.Addresses { + at := getAddressType(address.IP) + if at != nil && *at == addrType && len(expectedEndpoints) < maxEndpointsPerSubset { + expectedEndpoints[address.IP] = addressInfo{ + ready: true, + epAddress: address, + } + } + } + + for _, address := range epSubset.NotReadyAddresses { + at := getAddressType(address.IP) + if at != nil && *at == addrType && len(expectedEndpoints) < maxEndpointsPerSubset { + expectedEndpoints[address.IP] = addressInfo{ + ready: false, + epAddress: address, + } + } + } + + if len(expectedEndpoints) != len(esEndpoints) { + t.Errorf("Expected %d endpoints, got %d", len(expectedEndpoints), len(esEndpoints)) + } + + for _, endpoint := range esEndpoints { + if len(endpoint.Addresses) != 1 { + t.Fatalf("Expected endpoint to have 1 address, got %d", len(endpoint.Addresses)) + } + address := endpoint.Addresses[0] + expectedEndpoint, ok := expectedEndpoints[address] + + if !ok { + t.Fatalf("EndpointSlice has endpoint with unexpected address: %s", address) + } + + if expectedEndpoint.ready != *endpoint.Conditions.Ready { + t.Errorf("Expected ready to be %t, got %t", expectedEndpoint.ready, *endpoint.Conditions.Ready) + } + + if endpoint.Hostname == nil { + if expectedEndpoint.epAddress.Hostname != "" { + t.Errorf("Expected hostname to be %s, got nil", expectedEndpoint.epAddress.Hostname) + } + } else if expectedEndpoint.epAddress.Hostname != *endpoint.Hostname { + t.Errorf("Expected hostname to be %s, got %s", expectedEndpoint.epAddress.Hostname, *endpoint.Hostname) + } + + if expectedEndpoint.epAddress.NodeName != nil { + topologyHostname, ok := endpoint.Topology["kubernetes.io/hostname"] + if !ok { + t.Errorf("Expected topology[kubernetes.io/hostname] to be set") + } else if *expectedEndpoint.epAddress.NodeName != topologyHostname { + t.Errorf("Expected topology[kubernetes.io/hostname] to be %s, got %s", *expectedEndpoint.epAddress.NodeName, topologyHostname) + } + } + } +} + +func fetchEndpointSlices(t *testing.T, client *fake.Clientset, namespace string) []discovery.EndpointSlice { + t.Helper() + fetchedSlices, err := client.DiscoveryV1beta1().EndpointSlices(namespace).List(context.TODO(), metav1.ListOptions{ + LabelSelector: discovery.LabelManagedBy + "=" + controllerName, + }) + if err != nil { + t.Fatalf("Expected no error fetching Endpoint Slices, got: %v", err) + return []discovery.EndpointSlice{} + } + return fetchedSlices.Items +} + +func reconcileHelper(t *testing.T, r *reconciler, endpoints *corev1.Endpoints, existingSlices []*discovery.EndpointSlice) { + t.Helper() + err := r.reconcile(endpoints, existingSlices) + if err != nil { + t.Fatalf("Expected no error reconciling Endpoint Slices, got: %v", err) + } +} + +// Metrics helpers + +type expectedMetrics struct { + desiredSlices int + actualSlices int + desiredEndpoints int + addedPerSync int + updatedPerSync int + removedPerSync int + numCreated int + numUpdated int + numDeleted int +} + +func expectMetrics(t *testing.T, em expectedMetrics) { + t.Helper() + + actualDesiredSlices, err := testutil.GetGaugeMetricValue(metrics.DesiredEndpointSlices.WithLabelValues()) + handleErr(t, err, "desiredEndpointSlices") + if actualDesiredSlices != float64(em.desiredSlices) { + t.Errorf("Expected desiredEndpointSlices to be %d, got %v", em.desiredSlices, actualDesiredSlices) + } + + actualNumSlices, err := testutil.GetGaugeMetricValue(metrics.NumEndpointSlices.WithLabelValues()) + handleErr(t, err, "numEndpointSlices") + if actualNumSlices != float64(em.actualSlices) { + t.Errorf("Expected numEndpointSlices to be %d, got %v", em.actualSlices, actualNumSlices) + } + + actualEndpointsDesired, err := testutil.GetGaugeMetricValue(metrics.EndpointsDesired.WithLabelValues()) + handleErr(t, err, "desiredEndpoints") + if actualEndpointsDesired != float64(em.desiredEndpoints) { + t.Errorf("Expected desiredEndpoints to be %d, got %v", em.desiredEndpoints, actualEndpointsDesired) + } + + actualAddedPerSync, err := testutil.GetHistogramMetricValue(metrics.EndpointsAddedPerSync.WithLabelValues()) + handleErr(t, err, "endpointsAddedPerSync") + if actualAddedPerSync != float64(em.addedPerSync) { + t.Errorf("Expected endpointsAddedPerSync to be %d, got %v", em.addedPerSync, actualAddedPerSync) + } + + actualUpdatedPerSync, err := testutil.GetHistogramMetricValue(metrics.EndpointsUpdatedPerSync.WithLabelValues()) + handleErr(t, err, "endpointsUpdatedPerSync") + if actualUpdatedPerSync != float64(em.updatedPerSync) { + t.Errorf("Expected endpointsUpdatedPerSync to be %d, got %v", em.updatedPerSync, actualUpdatedPerSync) + } + + actualRemovedPerSync, err := testutil.GetHistogramMetricValue(metrics.EndpointsRemovedPerSync.WithLabelValues()) + handleErr(t, err, "endpointsRemovedPerSync") + if actualRemovedPerSync != float64(em.removedPerSync) { + t.Errorf("Expected endpointsRemovedPerSync to be %d, got %v", em.removedPerSync, actualRemovedPerSync) + } + + actualCreated, err := testutil.GetCounterMetricValue(metrics.EndpointSliceChanges.WithLabelValues("create")) + handleErr(t, err, "endpointSliceChangesCreated") + if actualCreated != float64(em.numCreated) { + t.Errorf("Expected endpointSliceChangesCreated to be %d, got %v", em.numCreated, actualCreated) + } + + actualUpdated, err := testutil.GetCounterMetricValue(metrics.EndpointSliceChanges.WithLabelValues("update")) + handleErr(t, err, "endpointSliceChangesUpdated") + if actualUpdated != float64(em.numUpdated) { + t.Errorf("Expected endpointSliceChangesUpdated to be %d, got %v", em.numUpdated, actualUpdated) + } + + actualDeleted, err := testutil.GetCounterMetricValue(metrics.EndpointSliceChanges.WithLabelValues("delete")) + handleErr(t, err, "desiredEndpointSlices") + if actualDeleted != float64(em.numDeleted) { + t.Errorf("Expected endpointSliceChangesDeleted to be %d, got %v", em.numDeleted, actualDeleted) + } +} + +func handleErr(t *testing.T, err error, metricName string) { + if err != nil { + t.Errorf("Failed to get %s value, err: %v", metricName, err) + } +} + +func setupMetrics() { + metrics.RegisterMetrics() + metrics.NumEndpointSlices.Delete(map[string]string{}) + metrics.DesiredEndpointSlices.Delete(map[string]string{}) + metrics.EndpointsDesired.Delete(map[string]string{}) + metrics.EndpointsAddedPerSync.Delete(map[string]string{}) + metrics.EndpointsUpdatedPerSync.Delete(map[string]string{}) + metrics.EndpointsRemovedPerSync.Delete(map[string]string{}) + metrics.EndpointSliceChanges.Delete(map[string]string{"operation": "create"}) + metrics.EndpointSliceChanges.Delete(map[string]string{"operation": "update"}) + metrics.EndpointSliceChanges.Delete(map[string]string{"operation": "delete"}) +} diff --git a/pkg/controller/endpointslicemirroring/utils.go b/pkg/controller/endpointslicemirroring/utils.go new file mode 100644 index 00000000000..65b316ae681 --- /dev/null +++ b/pkg/controller/endpointslicemirroring/utils.go @@ -0,0 +1,250 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "fmt" + "net" + "strings" + + corev1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + apiequality "k8s.io/apimachinery/pkg/api/equality" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/leaderelection/resourcelock" + "k8s.io/kubernetes/pkg/apis/discovery/validation" + endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint" +) + +// addrTypePortMapKey is used to uniquely identify groups of endpoint ports and +// address types. +type addrTypePortMapKey string + +// newAddrTypePortMapKey generates a PortMapKey from endpoint ports. +func newAddrTypePortMapKey(endpointPorts []discovery.EndpointPort, addrType discovery.AddressType) addrTypePortMapKey { + pmk := fmt.Sprintf("%s-%s", addrType, endpointutil.NewPortMapKey(endpointPorts)) + return addrTypePortMapKey(pmk) +} + +func (pk addrTypePortMapKey) addressType() discovery.AddressType { + if strings.HasPrefix(string(pk), string(discovery.AddressTypeIPv6)) { + return discovery.AddressTypeIPv6 + } + return discovery.AddressTypeIPv4 +} + +func getAddressType(address string) *discovery.AddressType { + ip := net.ParseIP(address) + if ip == nil { + return nil + } + addressType := discovery.AddressTypeIPv4 + if ip.To4() == nil { + addressType = discovery.AddressTypeIPv6 + } + return &addressType +} + +// endpointsEqualBeyondHash returns true if endpoints have equal attributes +// but excludes equality checks that would have already been covered with +// endpoint hashing (see hashEndpoint func for more info). +func endpointsEqualBeyondHash(ep1, ep2 *discovery.Endpoint) bool { + if !apiequality.Semantic.DeepEqual(ep1.Topology, ep2.Topology) { + return false + } + + if !boolPtrEqual(ep1.Conditions.Ready, ep2.Conditions.Ready) { + return false + } + + if !objectRefPtrEqual(ep1.TargetRef, ep2.TargetRef) { + return false + } + + return true +} + +// newEndpointSlice returns an EndpointSlice generated from an Endpoints +// resource, ports, and address type. +func newEndpointSlice(endpoints *corev1.Endpoints, ports []discovery.EndpointPort, addrType discovery.AddressType, sliceName string) *discovery.EndpointSlice { + gvk := schema.GroupVersionKind{Version: "v1", Kind: "Endpoints"} + ownerRef := metav1.NewControllerRef(endpoints, gvk) + epSlice := &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + discovery.LabelServiceName: endpoints.Name, + discovery.LabelManagedBy: controllerName, + }, + OwnerReferences: []metav1.OwnerReference{*ownerRef}, + Namespace: endpoints.Namespace, + }, + Ports: ports, + AddressType: addrType, + Endpoints: []discovery.Endpoint{}, + } + + if sliceName == "" { + epSlice.GenerateName = getEndpointSlicePrefix(endpoints.Name) + } else { + epSlice.Name = sliceName + } + + return epSlice +} + +// getEndpointSlicePrefix returns a suitable prefix for an EndpointSlice name. +func getEndpointSlicePrefix(serviceName string) string { + // use the dash (if the name isn't too long) to make the name a bit prettier. + prefix := fmt.Sprintf("%s-", serviceName) + if len(validation.ValidateEndpointSliceName(prefix, true)) != 0 { + prefix = serviceName + } + return prefix +} + +// addressToEndpoint converts an address from an Endpoints resource to an +// EndpointSlice endpoint. +func addressToEndpoint(address corev1.EndpointAddress, ready bool) *discovery.Endpoint { + endpoint := &discovery.Endpoint{ + Addresses: []string{address.IP}, + Conditions: discovery.EndpointConditions{ + Ready: &ready, + }, + TargetRef: address.TargetRef, + } + + if address.NodeName != nil { + endpoint.Topology = map[string]string{ + "kubernetes.io/hostname": *address.NodeName, + } + } + if address.Hostname != "" { + endpoint.Hostname = &address.Hostname + } + + return endpoint +} + +// epPortsToEpsPorts converts ports from an Endpoints resource to ports for an +// EndpointSlice resource. +func epPortsToEpsPorts(epPorts []corev1.EndpointPort) []discovery.EndpointPort { + epsPorts := []discovery.EndpointPort{} + for _, epPort := range epPorts { + epp := epPort.DeepCopy() + epsPorts = append(epsPorts, discovery.EndpointPort{ + Name: &epp.Name, + Port: &epp.Port, + Protocol: &epp.Protocol, + AppProtocol: epp.AppProtocol, + }) + } + return epsPorts +} + +// boolPtrEqual returns true if a set of bool pointers have equivalent values. +func boolPtrEqual(ptr1, ptr2 *bool) bool { + if (ptr1 == nil) != (ptr2 == nil) { + return false + } + if ptr1 != nil && ptr2 != nil && *ptr1 != *ptr2 { + return false + } + return true +} + +// objectRefPtrEqual returns true if a set of object ref pointers have +// equivalent values. +func objectRefPtrEqual(ref1, ref2 *corev1.ObjectReference) bool { + if (ref1 == nil) != (ref2 == nil) { + return false + } + if ref1 != nil && ref2 != nil && !apiequality.Semantic.DeepEqual(*ref1, *ref2) { + return false + } + return true +} + +// getEndpointsFromDeleteAction parses an Endpoints resource from a delete +// action. +func getEndpointsFromDeleteAction(obj interface{}) *corev1.Endpoints { + if endpointSlice, ok := obj.(*corev1.Endpoints); ok { + return endpointSlice + } + // If we reached here it means the EndpointSlice was deleted but its final + // state is unrecorded. + tombstone, ok := obj.(cache.DeletedFinalStateUnknown) + if !ok { + utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj)) + return nil + } + endpoints, ok := tombstone.Obj.(*corev1.Endpoints) + if !ok { + utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not an Endpoints resource: %#v", obj)) + return nil + } + return endpoints +} + +// getEndpointSliceFromDeleteAction parses an EndpointSlice from a delete action. +func getEndpointSliceFromDeleteAction(obj interface{}) *discovery.EndpointSlice { + if endpointSlice, ok := obj.(*discovery.EndpointSlice); ok { + return endpointSlice + } + // If we reached here it means the EndpointSlice was deleted but its final + // state is unrecorded. + tombstone, ok := obj.(cache.DeletedFinalStateUnknown) + if !ok { + utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj)) + return nil + } + endpointSlice, ok := tombstone.Obj.(*discovery.EndpointSlice) + if !ok { + utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not an EndpointSlice resource: %#v", obj)) + return nil + } + return endpointSlice +} + +// endpointsControllerKey returns a controller key for an Endpoints resource but +// derived from an EndpointSlice. +func endpointsControllerKey(endpointSlice *discovery.EndpointSlice) (string, error) { + if endpointSlice == nil { + return "", fmt.Errorf("nil EndpointSlice passed to serviceControllerKey()") + } + serviceName, ok := endpointSlice.Labels[discovery.LabelServiceName] + if !ok || serviceName == "" { + return "", fmt.Errorf("EndpointSlice missing %s label", discovery.LabelServiceName) + } + return fmt.Sprintf("%s/%s", endpointSlice.Namespace, serviceName), nil +} + +// skipMirror return true if the the LabelSkipMirror label has been set to +// "true". +func skipMirror(labels map[string]string) bool { + skipMirror, _ := labels[discovery.LabelSkipMirror] + return skipMirror == "true" +} + +// hasLeaderElection returns true if the LeaderElectionRecordAnnotationKey is +// set as an annotation. +func hasLeaderElection(annotations map[string]string) bool { + _, ok := annotations[resourcelock.LeaderElectionRecordAnnotationKey] + return ok +} diff --git a/pkg/controller/endpointslicemirroring/utils_test.go b/pkg/controller/endpointslicemirroring/utils_test.go new file mode 100644 index 00000000000..ef374b8350b --- /dev/null +++ b/pkg/controller/endpointslicemirroring/utils_test.go @@ -0,0 +1,93 @@ +/* +Copyright 2020 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 endpointslicemirroring + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/rand" + "k8s.io/client-go/kubernetes/fake" + k8stesting "k8s.io/client-go/testing" +) + +func TestNewEndpointSlice(t *testing.T) { + portName := "foo" + protocol := v1.ProtocolTCP + + ports := []discovery.EndpointPort{{Name: &portName, Protocol: &protocol}} + addrType := discovery.AddressTypeIPv4 + + endpoints := v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test"}, + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{Port: 80}}, + }}, + } + + gvk := schema.GroupVersionKind{Version: "v1", Kind: "Endpoints"} + ownerRef := metav1.NewControllerRef(&endpoints, gvk) + + expectedSlice := discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{ + discovery.LabelServiceName: endpoints.Name, + discovery.LabelManagedBy: controllerName, + }, + GenerateName: fmt.Sprintf("%s-", endpoints.Name), + OwnerReferences: []metav1.OwnerReference{*ownerRef}, + Namespace: endpoints.Namespace, + }, + Ports: ports, + AddressType: addrType, + Endpoints: []discovery.Endpoint{}, + } + generatedSlice := newEndpointSlice(&endpoints, ports, addrType, "") + + assert.EqualValues(t, expectedSlice, *generatedSlice) +} + +// Test helpers + +func newClientset() *fake.Clientset { + client := fake.NewSimpleClientset() + + client.PrependReactor("create", "endpointslices", k8stesting.ReactionFunc(func(action k8stesting.Action) (bool, runtime.Object, error) { + endpointSlice := action.(k8stesting.CreateAction).GetObject().(*discovery.EndpointSlice) + + if endpointSlice.ObjectMeta.GenerateName != "" { + endpointSlice.ObjectMeta.Name = fmt.Sprintf("%s-%s", endpointSlice.ObjectMeta.GenerateName, rand.String(8)) + endpointSlice.ObjectMeta.GenerateName = "" + } + endpointSlice.ObjectMeta.ResourceVersion = "100" + + return false, endpointSlice, nil + })) + client.PrependReactor("update", "endpointslices", k8stesting.ReactionFunc(func(action k8stesting.Action) (bool, runtime.Object, error) { + endpointSlice := action.(k8stesting.CreateAction).GetObject().(*discovery.EndpointSlice) + endpointSlice.ObjectMeta.ResourceVersion = "200" + return false, endpointSlice, nil + })) + + return client +} From 0695896caae3039b2bcaf7a988a1f546cc8cd257 Mon Sep 17 00:00:00 2001 From: Rob Scott Date: Fri, 26 Jun 2020 16:16:06 -0700 Subject: [PATCH 3/6] Updating apiserver Endpoints management to set skip-mirror label This will ensure that the self-referential kubernetes Endpoints resources is not mirrored by the EndpointSliceMirroring controller. --- pkg/master/BUILD | 1 + pkg/master/controller_test.go | 69 +++++++------ pkg/master/reconcilers/endpointsadapter.go | 14 +++ pkg/master/reconcilers/lease.go | 6 +- pkg/master/reconcilers/lease_test.go | 107 ++++++++++++++------- pkg/master/reconcilers/mastercount.go | 8 +- 6 files changed, 136 insertions(+), 69 deletions(-) diff --git a/pkg/master/BUILD b/pkg/master/BUILD index c3f80cde78d..ecfb1718309 100644 --- a/pkg/master/BUILD +++ b/pkg/master/BUILD @@ -157,6 +157,7 @@ go_test( "//pkg/registry/registrytest:go_default_library", "//staging/src/k8s.io/api/certificates/v1beta1:go_default_library", "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/api/discovery/v1beta1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/apitesting/naming:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", diff --git a/pkg/master/controller_test.go b/pkg/master/controller_test.go index 2b6a05f43dd..a54ee417960 100644 --- a/pkg/master/controller_test.go +++ b/pkg/master/controller_test.go @@ -22,6 +22,7 @@ import ( "testing" corev1 "k8s.io/api/core/v1" + discoveryv1beta1 "k8s.io/api/discovery/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/kubernetes/fake" @@ -31,8 +32,14 @@ import ( func TestReconcileEndpoints(t *testing.T) { ns := metav1.NamespaceDefault - om := func(name string) metav1.ObjectMeta { - return metav1.ObjectMeta{Namespace: ns, Name: name} + om := func(name string, skipMirrorLabel bool) metav1.ObjectMeta { + o := metav1.ObjectMeta{Namespace: ns, Name: name} + if skipMirrorLabel { + o.Labels = map[string]string{ + discoveryv1beta1.LabelSkipMirror: "true", + } + } + return o } reconcileTests := []struct { testName string @@ -51,7 +58,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: nil, expectCreate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -65,7 +72,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -80,7 +87,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -88,7 +95,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -103,7 +110,7 @@ func TestReconcileEndpoints(t *testing.T) { additionalMasters: 3, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -117,7 +124,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -137,7 +144,7 @@ func TestReconcileEndpoints(t *testing.T) { additionalMasters: 3, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -151,7 +158,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -171,7 +178,7 @@ func TestReconcileEndpoints(t *testing.T) { additionalMasters: 3, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -191,7 +198,7 @@ func TestReconcileEndpoints(t *testing.T) { additionalMasters: 3, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -201,7 +208,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -218,7 +225,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("bar"), + ObjectMeta: om("bar", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -226,7 +233,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectCreate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -240,7 +247,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -248,7 +255,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -262,7 +269,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}, @@ -270,7 +277,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -284,7 +291,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}, @@ -292,7 +299,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -306,7 +313,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -314,7 +321,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}, @@ -332,7 +339,7 @@ func TestReconcileEndpoints(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{ @@ -354,7 +361,7 @@ func TestReconcileEndpoints(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -362,7 +369,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{ @@ -379,7 +386,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}}, endpoints: nil, expectCreate: &corev1.Endpoints{ - ObjectMeta: om("boo"), + ObjectMeta: om("boo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}}, @@ -457,7 +464,7 @@ func TestReconcileEndpoints(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -476,7 +483,7 @@ func TestReconcileEndpoints(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -484,7 +491,7 @@ func TestReconcileEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -498,7 +505,7 @@ func TestReconcileEndpoints(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: nil, expectCreate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, diff --git a/pkg/master/reconcilers/endpointsadapter.go b/pkg/master/reconcilers/endpointsadapter.go index b3d456059b8..54e74c15bae 100644 --- a/pkg/master/reconcilers/endpointsadapter.go +++ b/pkg/master/reconcilers/endpointsadapter.go @@ -193,3 +193,17 @@ func allAddressesIPv6(addresses []corev1.EndpointAddress) bool { return true } + +// setSkipMirrorTrue sets endpointslice.kubernetes.io/skip-mirror to true. It +// returns true if this has resulted in a change to the Endpoints resource. +func setSkipMirrorTrue(e *corev1.Endpoints) bool { + skipMirrorVal, ok := e.Labels[discovery.LabelSkipMirror] + if !ok || skipMirrorVal != "true" { + if e.Labels == nil { + e.Labels = map[string]string{} + } + e.Labels[discovery.LabelSkipMirror] = "true" + return true + } + return false +} diff --git a/pkg/master/reconcilers/lease.go b/pkg/master/reconcilers/lease.go index e79d80802a8..f7e0ba50f19 100644 --- a/pkg/master/reconcilers/lease.go +++ b/pkg/master/reconcilers/lease.go @@ -196,9 +196,13 @@ func (r *leaseEndpointReconciler) doReconcile(serviceName string, endpointPorts return fmt.Errorf("no master IPs were listed in storage, refusing to erase all endpoints for the kubernetes service") } + // Don't use the EndpointSliceMirroring controller to mirror this to + // EndpointSlices. This may change in the future. + skipMirrorChanged := setSkipMirrorTrue(e) + // Next, we compare the current list of endpoints with the list of master IP keys formatCorrect, ipCorrect, portsCorrect := checkEndpointSubsetFormatWithLease(e, masterIPs, endpointPorts, reconcilePorts) - if formatCorrect && ipCorrect && portsCorrect { + if !skipMirrorChanged && formatCorrect && ipCorrect && portsCorrect { return r.epAdapter.EnsureEndpointSliceFromEndpoints(corev1.NamespaceDefault, e) } diff --git a/pkg/master/reconcilers/lease_test.go b/pkg/master/reconcilers/lease_test.go index c1f4d493f67..dc659e76367 100644 --- a/pkg/master/reconcilers/lease_test.go +++ b/pkg/master/reconcilers/lease_test.go @@ -28,6 +28,7 @@ import ( "testing" corev1 "k8s.io/api/core/v1" + discoveryv1beta1 "k8s.io/api/discovery/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" ) @@ -78,8 +79,14 @@ func (f *fakeLeases) GetUpdatedKeys() []string { func TestLeaseEndpointReconciler(t *testing.T) { ns := corev1.NamespaceDefault - om := func(name string) metav1.ObjectMeta { - return metav1.ObjectMeta{Namespace: ns, Name: name} + om := func(name string, skipMirrorLabel bool) metav1.ObjectMeta { + o := metav1.ObjectMeta{Namespace: ns, Name: name} + if skipMirrorLabel { + o.Labels = map[string]string{ + discoveryv1beta1.LabelSkipMirror: "true", + } + } + return o } reconcileTests := []struct { testName string @@ -97,7 +104,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: nil, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -111,7 +118,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -127,7 +134,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointKeys: []string{"1.2.3.4"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -142,7 +149,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -150,7 +157,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -165,7 +172,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -179,7 +186,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -199,7 +206,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointKeys: []string{"4.3.2.1", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -213,7 +220,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -233,7 +240,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointKeys: []string{"4.3.2.1"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -243,7 +250,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.1"}, @@ -260,7 +267,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("bar"), + ObjectMeta: om("bar", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -268,7 +275,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -282,7 +289,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -290,7 +297,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -304,7 +311,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}}, @@ -312,7 +319,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -326,7 +333,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}}, @@ -334,7 +341,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -348,7 +355,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -356,13 +363,35 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}}, }}, }, }, + { + testName: "existing endpoints without skip mirror label", + serviceName: "foo", + ip: "1.2.3.4", + endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, + endpoints: &corev1.EndpointsList{ + Items: []corev1.Endpoints{{ + ObjectMeta: om("foo", false), + Subsets: []corev1.EndpointSubset{{ + Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, + Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, + }}, + }}, + }, + expectUpdate: &corev1.Endpoints{ + ObjectMeta: om("foo", true), + Subsets: []corev1.EndpointSubset{{ + Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, + Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, + }}, + }, + }, { testName: "existing endpoints extra service ports satisfy", serviceName: "foo", @@ -374,7 +403,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{ @@ -396,7 +425,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -404,7 +433,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{ @@ -467,7 +496,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -486,7 +515,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -494,7 +523,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -508,7 +537,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, endpoints: nil, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}}, @@ -553,8 +582,14 @@ func TestLeaseEndpointReconciler(t *testing.T) { func TestLeaseRemoveEndpoints(t *testing.T) { ns := corev1.NamespaceDefault - om := func(name string) metav1.ObjectMeta { - return metav1.ObjectMeta{Namespace: ns, Name: name} + om := func(name string, skipMirrorLabel bool) metav1.ObjectMeta { + o := metav1.ObjectMeta{Namespace: ns, Name: name} + if skipMirrorLabel { + o.Labels = map[string]string{ + discoveryv1beta1.LabelSkipMirror: "true", + } + } + return o } stopTests := []struct { testName string @@ -573,7 +608,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) { endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -586,7 +621,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) { }}, }, expectUpdate: &corev1.Endpoints{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "4.3.2.2"}, @@ -605,7 +640,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) { endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: []corev1.EndpointSubset{{ Addresses: []corev1.EndpointAddress{ {IP: "1.2.3.4"}, @@ -626,7 +661,7 @@ func TestLeaseRemoveEndpoints(t *testing.T) { endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"}, endpoints: &corev1.EndpointsList{ Items: []corev1.Endpoints{{ - ObjectMeta: om("foo"), + ObjectMeta: om("foo", true), Subsets: nil, }}, }, diff --git a/pkg/master/reconcilers/mastercount.go b/pkg/master/reconcilers/mastercount.go index 58d03d66e0a..f2fc6ad92f7 100644 --- a/pkg/master/reconcilers/mastercount.go +++ b/pkg/master/reconcilers/mastercount.go @@ -76,6 +76,11 @@ func (r *masterCountEndpointReconciler) ReconcileEndpoints(serviceName string, i }, } } + + // Don't use the EndpointSliceMirroring controller to mirror this to + // EndpointSlices. This may change in the future. + skipMirrorChanged := setSkipMirrorTrue(e) + if errors.IsNotFound(err) { // Simply create non-existing endpoints for the service. e.Subsets = []corev1.EndpointSubset{{ @@ -99,7 +104,8 @@ func (r *masterCountEndpointReconciler) ReconcileEndpoints(serviceName string, i _, err = r.epAdapter.Update(metav1.NamespaceDefault, e) return err } - if ipCorrect && portsCorrect { + + if !skipMirrorChanged && ipCorrect && portsCorrect { return r.epAdapter.EnsureEndpointSliceFromEndpoints(metav1.NamespaceDefault, e) } if !ipCorrect { From 85d5a15841a41998940fd930b8f19e02739319db Mon Sep 17 00:00:00 2001 From: Rob Scott Date: Fri, 26 Jun 2020 16:16:52 -0700 Subject: [PATCH 4/6] Adding integration and e2e tests to cover EndpointSliceMirroring --- test/e2e/network/BUILD | 1 + test/e2e/network/endpointslicemirroring.go | 185 ++++++++++++++ test/integration/BUILD | 1 + test/integration/endpointslice/BUILD | 37 +++ .../endpointslicemirroring_test.go | 227 ++++++++++++++++++ test/integration/endpointslice/main_test.go | 27 +++ 6 files changed, 478 insertions(+) create mode 100644 test/e2e/network/endpointslicemirroring.go create mode 100644 test/integration/endpointslice/BUILD create mode 100644 test/integration/endpointslice/endpointslicemirroring_test.go create mode 100644 test/integration/endpointslice/main_test.go diff --git a/test/e2e/network/BUILD b/test/e2e/network/BUILD index 979b274c153..6e20bfbfc91 100644 --- a/test/e2e/network/BUILD +++ b/test/e2e/network/BUILD @@ -16,6 +16,7 @@ go_library( "doc.go", "dual_stack.go", "endpointslice.go", + "endpointslicemirroring.go", "example_cluster_dns.go", "firewall.go", "fixture.go", diff --git a/test/e2e/network/endpointslicemirroring.go b/test/e2e/network/endpointslicemirroring.go new file mode 100644 index 00000000000..874b4f5849a --- /dev/null +++ b/test/e2e/network/endpointslicemirroring.go @@ -0,0 +1,185 @@ +/* +Copyright 2020 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 network + +import ( + "context" + "fmt" + "time" + + "github.com/onsi/ginkgo" + v1 "k8s.io/api/core/v1" + discoveryv1beta1 "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" + clientset "k8s.io/client-go/kubernetes" + "k8s.io/kubernetes/test/e2e/framework" +) + +var _ = SIGDescribe("EndpointSliceMirroring", func() { + f := framework.NewDefaultFramework("endpointslicemirroring") + + var cs clientset.Interface + + ginkgo.BeforeEach(func() { + cs = f.ClientSet + }) + + ginkgo.It("should mirror a custom Endpoints resource through create update and delete", func() { + svc := createServiceReportErr(cs, f.Namespace.Name, &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example-custom-endpoints", + }, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{{ + Name: "example", + Port: 80, + Protocol: v1.ProtocolTCP, + }}, + }, + }) + + endpoints := &v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: svc.Name, + }, + Subsets: []v1.EndpointSubset{{ + Ports: []v1.EndpointPort{{ + Port: 80, + }}, + Addresses: []v1.EndpointAddress{{ + IP: "10.1.2.3", + }}, + }}, + } + + ginkgo.By("mirroring a new custom Endpoint", func() { + _, err := cs.CoreV1().Endpoints(f.Namespace.Name).Create(context.TODO(), endpoints, metav1.CreateOptions{}) + framework.ExpectNoError(err, "Unexpected error creating Endpoints") + + if err := wait.PollImmediate(2*time.Second, 12*time.Second, func() (bool, error) { + esList, err := cs.DiscoveryV1beta1().EndpointSlices(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ + LabelSelector: discoveryv1beta1.LabelServiceName + "=" + svc.Name, + }) + if err != nil { + framework.Logf("Error listing EndpointSlices: %v", err) + return false, nil + } + if len(esList.Items) != 1 { + framework.Logf("Waiting for 1 EndpointSlice to exist, got %d", len(esList.Items)) + return false, nil + } + epSlice := esList.Items[0] + if len(epSlice.Ports) != 1 { + return false, fmt.Errorf("Expected EndpointSlice to have 1 Port, got %d", len(epSlice.Ports)) + } + port := epSlice.Ports[0] + if *port.Port != int32(80) { + return false, fmt.Errorf("Expected port to be 80, got %d", *port.Port) + } + if len(epSlice.Endpoints) != 1 { + return false, fmt.Errorf("Expected EndpointSlice to have 1 endpoints, got %d", len(epSlice.Endpoints)) + } + endpoint := epSlice.Endpoints[0] + if len(endpoint.Addresses) != 1 { + return false, fmt.Errorf("Expected EndpointSlice endpoint to have 1 address, got %d", len(endpoint.Addresses)) + } + address := endpoint.Addresses[0] + if address != "10.1.2.3" { + return false, fmt.Errorf("Expected EndpointSlice to have 10.1.2.3 as address, got %s", address) + } + + return true, nil + }); err != nil { + framework.Failf("Did not find matching EndpointSlice for %s/%s: %s", svc.Namespace, svc.Name, err) + } + }) + + ginkgo.By("mirroring an update to a custom Endpoint", func() { + endpoints.Subsets[0].Addresses = []v1.EndpointAddress{{ + IP: "10.2.3.4", + }} + _, err := cs.CoreV1().Endpoints(f.Namespace.Name).Update(context.TODO(), endpoints, metav1.UpdateOptions{}) + framework.ExpectNoError(err, "Unexpected error updating Endpoints") + + // Expect mirrored EndpointSlice resource to be updated. + if err := wait.PollImmediate(2*time.Second, 12*time.Second, func() (bool, error) { + esList, err := cs.DiscoveryV1beta1().EndpointSlices(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ + LabelSelector: discoveryv1beta1.LabelServiceName + "=" + svc.Name, + }) + if err != nil { + return false, err + } + if len(esList.Items) != 1 { + framework.Logf("Waiting for 1 EndpointSlice to exist, got %d", len(esList.Items)) + return false, nil + } + epSlice := esList.Items[0] + if len(epSlice.Ports) != 1 { + framework.Logf("Expected EndpointSlice to have 1 Port, got %d", len(epSlice.Ports)) + return false, nil + } + port := epSlice.Ports[0] + if *port.Port != int32(80) { + framework.Logf("Expected port to be 80, got %d", *port.Port) + return false, nil + } + if len(epSlice.Endpoints) != 1 { + framework.Logf("Expected EndpointSlice to have 1 endpoints, got %d", len(epSlice.Endpoints)) + return false, nil + } + endpoint := epSlice.Endpoints[0] + if len(endpoint.Addresses) != 1 { + framework.Logf("Expected EndpointSlice endpoint to have 1 address, got %d", len(endpoint.Addresses)) + return false, nil + } + address := endpoint.Addresses[0] + if address != "10.2.3.4" { + framework.Logf("Expected EndpointSlice to have 10.2.3.4 as address, got %s", address) + return false, nil + } + + return true, nil + }); err != nil { + framework.Failf("Did not find matching EndpointSlice for %s/%s: %s", svc.Namespace, svc.Name, err) + } + }) + + ginkgo.By("mirroring deletion of a custom Endpoint", func() { + err := cs.CoreV1().Endpoints(f.Namespace.Name).Delete(context.TODO(), endpoints.Name, metav1.DeleteOptions{}) + framework.ExpectNoError(err, "Unexpected error deleting Endpoints") + + // Expect mirrored EndpointSlice resource to be updated. + if err := wait.PollImmediate(2*time.Second, 12*time.Second, func() (bool, error) { + esList, err := cs.DiscoveryV1beta1().EndpointSlices(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ + LabelSelector: discoveryv1beta1.LabelServiceName + "=" + svc.Name, + }) + if err != nil { + return false, err + } + if len(esList.Items) != 0 { + framework.Logf("Waiting for 0 EndpointSlices to exist, got %d", len(esList.Items)) + return false, nil + } + + return true, nil + }); err != nil { + framework.Failf("Did not find matching EndpointSlice for %s/%s: %s", svc.Namespace, svc.Name, err) + } + }) + }) +}) diff --git a/test/integration/BUILD b/test/integration/BUILD index cda563c14b0..db7c700523b 100644 --- a/test/integration/BUILD +++ b/test/integration/BUILD @@ -50,6 +50,7 @@ filegroup( "//test/integration/deployment:all-srcs", "//test/integration/disruption:all-srcs", "//test/integration/dryrun:all-srcs", + "//test/integration/endpointslice:all-srcs", "//test/integration/etcd:all-srcs", "//test/integration/events:all-srcs", "//test/integration/evictions:all-srcs", diff --git a/test/integration/endpointslice/BUILD b/test/integration/endpointslice/BUILD new file mode 100644 index 00000000000..f926a511b6e --- /dev/null +++ b/test/integration/endpointslice/BUILD @@ -0,0 +1,37 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") + +go_test( + name = "go_default_test", + srcs = [ + "endpointslicemirroring_test.go", + "main_test.go", + ], + tags = ["integration"], + deps = [ + "//pkg/controller/endpoint:go_default_library", + "//pkg/controller/endpointslice:go_default_library", + "//pkg/controller/endpointslicemirroring:go_default_library", + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/api/discovery/v1beta1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library", + "//staging/src/k8s.io/client-go/informers:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes:go_default_library", + "//staging/src/k8s.io/client-go/rest:go_default_library", + "//test/integration/framework:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/test/integration/endpointslice/endpointslicemirroring_test.go b/test/integration/endpointslice/endpointslicemirroring_test.go new file mode 100644 index 00000000000..7eec43bbd50 --- /dev/null +++ b/test/integration/endpointslice/endpointslicemirroring_test.go @@ -0,0 +1,227 @@ +/* +Copyright 2020 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 endpointslice + +import ( + "context" + "fmt" + "testing" + "time" + + corev1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/informers" + clientset "k8s.io/client-go/kubernetes" + restclient "k8s.io/client-go/rest" + "k8s.io/kubernetes/pkg/controller/endpoint" + "k8s.io/kubernetes/pkg/controller/endpointslice" + "k8s.io/kubernetes/pkg/controller/endpointslicemirroring" + "k8s.io/kubernetes/test/integration/framework" +) + +func TestEndpointSliceMirroring(t *testing.T) { + masterConfig := framework.NewIntegrationTestMasterConfig() + _, server, closeFn := framework.RunAMaster(masterConfig) + defer closeFn() + + config := restclient.Config{Host: server.URL} + client, err := clientset.NewForConfig(&config) + if err != nil { + t.Fatalf("Error creating clientset: %v", err) + } + + resyncPeriod := 12 * time.Hour + informers := informers.NewSharedInformerFactory(client, resyncPeriod) + + epController := endpoint.NewEndpointController( + informers.Core().V1().Pods(), + informers.Core().V1().Services(), + informers.Core().V1().Endpoints(), + client, + 1*time.Second) + + epsController := endpointslice.NewController( + informers.Core().V1().Pods(), + informers.Core().V1().Services(), + informers.Core().V1().Nodes(), + informers.Discovery().V1beta1().EndpointSlices(), + int32(100), + client, + 1*time.Second) + + epsmController := endpointslicemirroring.NewController( + informers.Core().V1().Endpoints(), + informers.Discovery().V1beta1().EndpointSlices(), + informers.Core().V1().Services(), + int32(100), + client, + 1*time.Second) + + // Start informer and controllers + stopCh := make(chan struct{}) + defer close(stopCh) + informers.Start(stopCh) + go epController.Run(5, stopCh) + go epsController.Run(5, stopCh) + go epsmController.Run(5, stopCh) + + testCases := []struct { + testName string + service *corev1.Service + endpoints *corev1.Endpoints + expectEndpointSlice bool + expectEndpointSliceManagedBy string + }{{ + testName: "Service with selector", + service: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-123", + }, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{ + Port: int32(80), + }}, + Selector: map[string]string{ + "foo": "bar", + }, + }, + }, + endpoints: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-123", + }, + }, + expectEndpointSlice: true, + expectEndpointSliceManagedBy: "endpointslice-controller.k8s.io", + }, { + testName: "Service without selector", + service: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-123", + }, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{ + Port: int32(80), + }}, + }, + }, + endpoints: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-123", + }, + Subsets: []corev1.EndpointSubset{{ + Ports: []corev1.EndpointPort{{ + Port: 80, + }}, + Addresses: []corev1.EndpointAddress{{ + IP: "10.0.0.1", + }}, + }}, + }, + expectEndpointSlice: true, + expectEndpointSliceManagedBy: "endpointslicemirroring-controller.k8s.io", + }, { + testName: "Service without Endpoints", + service: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-123", + }, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{ + Port: int32(80), + }}, + Selector: map[string]string{ + "foo": "bar", + }, + }, + }, + endpoints: nil, + expectEndpointSlice: true, + expectEndpointSliceManagedBy: "endpointslice-controller.k8s.io", + }, { + testName: "Endpoints without Service", + service: nil, + endpoints: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-123", + }, + }, + expectEndpointSlice: false, + }} + + for i, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + ns := framework.CreateTestingNamespace(fmt.Sprintf("test-endpointslice-mirroring-%d", i), server, t) + defer framework.DeleteTestingNamespace(ns, server, t) + + resourceName := "" + if tc.service != nil { + resourceName = tc.service.Name + tc.service.Namespace = ns.Name + _, err = client.CoreV1().Services(ns.Name).Create(context.TODO(), tc.service, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating service: %v", err) + } + } + + if tc.endpoints != nil { + resourceName = tc.endpoints.Name + tc.endpoints.Namespace = ns.Name + _, err = client.CoreV1().Endpoints(ns.Name).Create(context.TODO(), tc.endpoints, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating endpoints: %v", err) + } + } + + err = wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) { + lSelector := discovery.LabelServiceName + "=" + resourceName + esList, err := client.DiscoveryV1beta1().EndpointSlices(ns.Name).List(context.TODO(), metav1.ListOptions{LabelSelector: lSelector}) + if err != nil { + t.Logf("Error listing EndpointSlices: %v", err) + return false, err + } + + if tc.expectEndpointSlice { + if len(esList.Items) == 0 { + t.Logf("Waiting for EndpointSlice to be created") + return false, nil + } + if len(esList.Items) > 1 { + return false, fmt.Errorf("Only expected 1 EndpointSlice, got %d", len(esList.Items)) + } + endpointSlice := esList.Items[0] + if tc.expectEndpointSliceManagedBy != "" { + if endpointSlice.Labels[discovery.LabelManagedBy] != tc.expectEndpointSliceManagedBy { + return false, fmt.Errorf("Expected EndpointSlice to be managed by %s, got %s", tc.expectEndpointSliceManagedBy, endpointSlice.Labels[discovery.LabelManagedBy]) + } + } + } else if len(esList.Items) > 0 { + t.Logf("Waiting for EndpointSlices to be removed, still %d", len(esList.Items)) + return false, nil + } + + return true, nil + }) + if err != nil { + t.Fatalf("Timed out waiting for conditions: %v", err) + } + }) + } + +} diff --git a/test/integration/endpointslice/main_test.go b/test/integration/endpointslice/main_test.go new file mode 100644 index 00000000000..31c04499b43 --- /dev/null +++ b/test/integration/endpointslice/main_test.go @@ -0,0 +1,27 @@ +/* +Copyright 2020 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 endpointslice + +import ( + "testing" + + "k8s.io/kubernetes/test/integration/framework" +) + +func TestMain(m *testing.M) { + framework.EtcdMain(m.Run) +} From e701cb020533d657850738e74df8b9147a3caef0 Mon Sep 17 00:00:00 2001 From: Rob Scott Date: Fri, 26 Jun 2020 16:17:22 -0700 Subject: [PATCH 5/6] Enabling the EndpointSliceMirroring controller, adding related config --- api/api-rules/violation_exceptions.list | 4 + .../.import-restrictions | 2 + cmd/kube-controller-manager/app/BUILD | 1 + .../app/controllermanager.go | 1 + cmd/kube-controller-manager/app/core_test.go | 6 +- cmd/kube-controller-manager/app/discovery.go | 25 +++++- cmd/kube-controller-manager/app/options/BUILD | 3 + .../endpointslicemirroringcontroller.go | 86 +++++++++++++++++++ .../app/options/options.go | 9 ++ .../app/options/options_test.go | 13 +++ hack/.golint_failures | 1 + pkg/controller/BUILD | 1 + pkg/controller/apis/config/BUILD | 1 + pkg/controller/apis/config/types.go | 4 + pkg/controller/apis/config/v1alpha1/BUILD | 1 + .../apis/config/v1alpha1/defaults.go | 3 + pkg/controller/apis/config/v1alpha1/doc.go | 2 + .../v1alpha1/zz_generated.conversion.go | 7 ++ .../apis/config/zz_generated.deepcopy.go | 1 + .../rbac/bootstrappolicy/controller_policy.go | 12 +++ .../testdata/controller-role-bindings.yaml | 17 ++++ .../testdata/controller-roles.yaml | 44 ++++++++++ .../config/v1alpha1/types.go | 24 ++++++ .../config/v1alpha1/zz_generated.deepcopy.go | 18 ++++ 24 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 cmd/kube-controller-manager/app/options/endpointslicemirroringcontroller.go diff --git a/api/api-rules/violation_exceptions.list b/api/api-rules/violation_exceptions.list index 599e55546cb..3261799ceb6 100644 --- a/api/api-rules/violation_exceptions.list +++ b/api/api-rules/violation_exceptions.list @@ -487,6 +487,9 @@ API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,E API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,EndpointSliceControllerConfiguration,ConcurrentServiceEndpointSyncs API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,EndpointSliceControllerConfiguration,EndpointUpdatesBatchPeriod API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,EndpointSliceControllerConfiguration,MaxEndpointsPerSlice +API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,EndpointSliceMirroringControllerConfiguration,MirroringConcurrentServiceEndpointSyncs +API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,EndpointSliceMirroringControllerConfiguration,MirroringEndpointUpdatesBatchPeriod +API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,EndpointSliceMirroringControllerConfiguration,MirroringMaxEndpointsPerSubset API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,GarbageCollectorControllerConfiguration,ConcurrentGCSyncs API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,GarbageCollectorControllerConfiguration,EnableGarbageCollector API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,GarbageCollectorControllerConfiguration,GCIgnoredResources @@ -528,6 +531,7 @@ API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,K API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,DeprecatedController API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,EndpointController API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,EndpointSliceController +API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,EndpointSliceMirroringController API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,GarbageCollectorController API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,Generic API rule violation: names_match,k8s.io/kube-controller-manager/config/v1alpha1,KubeControllerManagerConfiguration,HPAController diff --git a/cmd/cloud-controller-manager/.import-restrictions b/cmd/cloud-controller-manager/.import-restrictions index 962ddf3bd50..5067ab530a2 100644 --- a/cmd/cloud-controller-manager/.import-restrictions +++ b/cmd/cloud-controller-manager/.import-restrictions @@ -39,6 +39,8 @@ rules: - k8s.io/kubernetes/pkg/controller/endpoint/config/v1alpha1 - k8s.io/kubernetes/pkg/controller/endpointslice/config - k8s.io/kubernetes/pkg/controller/endpointslice/config/v1alpha1 + - k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config + - k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config/v1alpha1 - k8s.io/kubernetes/pkg/controller/garbagecollector/config - k8s.io/kubernetes/pkg/controller/garbagecollector/config/v1alpha1 - k8s.io/kubernetes/pkg/controller/job/config diff --git a/cmd/kube-controller-manager/app/BUILD b/cmd/kube-controller-manager/app/BUILD index 14962b7bda0..c857ce59358 100644 --- a/cmd/kube-controller-manager/app/BUILD +++ b/cmd/kube-controller-manager/app/BUILD @@ -56,6 +56,7 @@ go_library( "//pkg/controller/disruption:go_default_library", "//pkg/controller/endpoint:go_default_library", "//pkg/controller/endpointslice:go_default_library", + "//pkg/controller/endpointslicemirroring:go_default_library", "//pkg/controller/garbagecollector:go_default_library", "//pkg/controller/job:go_default_library", "//pkg/controller/namespace:go_default_library", diff --git a/cmd/kube-controller-manager/app/controllermanager.go b/cmd/kube-controller-manager/app/controllermanager.go index 6eb12fe70dc..98c8281f041 100644 --- a/cmd/kube-controller-manager/app/controllermanager.go +++ b/cmd/kube-controller-manager/app/controllermanager.go @@ -387,6 +387,7 @@ func NewControllerInitializers(loopMode ControllerLoopMode) map[string]InitFunc controllers := map[string]InitFunc{} controllers["endpoint"] = startEndpointController controllers["endpointslice"] = startEndpointSliceController + controllers["endpointslicemirroring"] = startEndpointSliceMirroringController controllers["replicationcontroller"] = startReplicationController controllers["podgc"] = startPodGCController controllers["resourcequota"] = startResourceQuotaController diff --git a/cmd/kube-controller-manager/app/core_test.go b/cmd/kube-controller-manager/app/core_test.go index 1ca43f6ce7e..9fdc70dcb5a 100644 --- a/cmd/kube-controller-manager/app/core_test.go +++ b/cmd/kube-controller-manager/app/core_test.go @@ -97,8 +97,10 @@ type controllerInitFunc func(ControllerContext) (http.Handler, bool, error) func TestController_DiscoveryError(t *testing.T) { controllerInitFuncMap := map[string]controllerInitFunc{ - "ResourceQuotaController": startResourceQuotaController, - "GarbageCollectorController": startGarbageCollectorController, + "ResourceQuotaController": startResourceQuotaController, + "GarbageCollectorController": startGarbageCollectorController, + "EndpointSliceController": startEndpointSliceController, + "EndpointSliceMirroringController": startEndpointSliceMirroringController, } tcs := map[string]struct { diff --git a/cmd/kube-controller-manager/app/discovery.go b/cmd/kube-controller-manager/app/discovery.go index 81af5b40832..e0d2421e047 100644 --- a/cmd/kube-controller-manager/app/discovery.go +++ b/cmd/kube-controller-manager/app/discovery.go @@ -27,12 +27,13 @@ import ( utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/klog/v2" endpointslicecontroller "k8s.io/kubernetes/pkg/controller/endpointslice" + endpointslicemirroringcontroller "k8s.io/kubernetes/pkg/controller/endpointslicemirroring" "k8s.io/kubernetes/pkg/features" ) func startEndpointSliceController(ctx ControllerContext) (http.Handler, bool, error) { if !utilfeature.DefaultFeatureGate.Enabled(features.EndpointSlice) { - klog.V(4).Infof("Not starting endpointslice-controller since EndpointSlice feature gate is disabled") + klog.V(2).Infof("Not starting endpointslice-controller since EndpointSlice feature gate is disabled") return nil, false, nil } @@ -52,3 +53,25 @@ func startEndpointSliceController(ctx ControllerContext) (http.Handler, bool, er ).Run(int(ctx.ComponentConfig.EndpointSliceController.ConcurrentServiceEndpointSyncs), ctx.Stop) return nil, true, nil } + +func startEndpointSliceMirroringController(ctx ControllerContext) (http.Handler, bool, error) { + if !utilfeature.DefaultFeatureGate.Enabled(features.EndpointSlice) { + klog.V(2).Infof("Not starting endpointslicemirroring-controller since EndpointSlice feature gate is disabled") + return nil, false, nil + } + + if !ctx.AvailableResources[discoveryv1beta1.SchemeGroupVersion.WithResource("endpointslices")] { + klog.Warningf("Not starting endpointslicemirroring-controller since discovery.k8s.io/v1beta1 resources are not available") + return nil, false, nil + } + + go endpointslicemirroringcontroller.NewController( + ctx.InformerFactory.Core().V1().Endpoints(), + ctx.InformerFactory.Discovery().V1beta1().EndpointSlices(), + ctx.InformerFactory.Core().V1().Services(), + ctx.ComponentConfig.EndpointSliceMirroringController.MirroringMaxEndpointsPerSubset, + ctx.ClientBuilder.ClientOrDie("endpointslicemirroring-controller"), + ctx.ComponentConfig.EndpointSliceMirroringController.MirroringEndpointUpdatesBatchPeriod.Duration, + ).Run(int(ctx.ComponentConfig.EndpointSliceMirroringController.MirroringConcurrentServiceEndpointSyncs), ctx.Stop) + return nil, true, nil +} diff --git a/cmd/kube-controller-manager/app/options/BUILD b/cmd/kube-controller-manager/app/options/BUILD index c4f3b148798..5ea12a3d846 100644 --- a/cmd/kube-controller-manager/app/options/BUILD +++ b/cmd/kube-controller-manager/app/options/BUILD @@ -16,6 +16,7 @@ go_library( "deprecatedcontroller.go", "endpointcontroller.go", "endpointslicecontroller.go", + "endpointslicemirroringcontroller.go", "garbagecollectorcontroller.go", "hpacontroller.go", "jobcontroller.go", @@ -43,6 +44,7 @@ go_library( "//pkg/controller/deployment/config:go_default_library", "//pkg/controller/endpoint/config:go_default_library", "//pkg/controller/endpointslice/config:go_default_library", + "//pkg/controller/endpointslicemirroring/config:go_default_library", "//pkg/controller/garbagecollector:go_default_library", "//pkg/controller/garbagecollector/config:go_default_library", "//pkg/controller/job/config:go_default_library", @@ -104,6 +106,7 @@ go_test( "//pkg/controller/deployment/config:go_default_library", "//pkg/controller/endpoint/config:go_default_library", "//pkg/controller/endpointslice/config:go_default_library", + "//pkg/controller/endpointslicemirroring/config:go_default_library", "//pkg/controller/garbagecollector/config:go_default_library", "//pkg/controller/job/config:go_default_library", "//pkg/controller/namespace/config:go_default_library", diff --git a/cmd/kube-controller-manager/app/options/endpointslicemirroringcontroller.go b/cmd/kube-controller-manager/app/options/endpointslicemirroringcontroller.go new file mode 100644 index 00000000000..b76f4e0298b --- /dev/null +++ b/cmd/kube-controller-manager/app/options/endpointslicemirroringcontroller.go @@ -0,0 +1,86 @@ +/* +Copyright 2020 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 options + +import ( + "fmt" + + "github.com/spf13/pflag" + + endpointslicemirroringconfig "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config" +) + +const ( + mirroringMinConcurrentServiceEndpointSyncs = 1 + mirroringMaxConcurrentServiceEndpointSyncs = 50 + mirroringMinMaxEndpointsPerSubset = 1 + mirroringMaxMaxEndpointsPerSubset = 1000 +) + +// EndpointSliceMirroringControllerOptions holds the +// EndpointSliceMirroringController options. +type EndpointSliceMirroringControllerOptions struct { + *endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration +} + +// AddFlags adds flags related to EndpointSliceMirroringController for +// controller manager to the specified FlagSet. +func (o *EndpointSliceMirroringControllerOptions) AddFlags(fs *pflag.FlagSet) { + if o == nil { + return + } + + fs.Int32Var(&o.MirroringConcurrentServiceEndpointSyncs, "mirroring-concurrent-service-endpoint-syncs", o.MirroringConcurrentServiceEndpointSyncs, "The number of service endpoint syncing operations that will be done concurrently by the EndpointSliceMirroring controller. Larger number = faster endpoint slice updating, but more CPU (and network) load. Defaults to 5.") + fs.Int32Var(&o.MirroringMaxEndpointsPerSubset, "mirroring-max-endpoints-per-subset", o.MirroringMaxEndpointsPerSubset, "The maximum number of endpoints that will be added to an EndpointSlice by the EndpointSliceMirroring controller. More endpoints per slice will result in less endpoint slices, but larger resources. Defaults to 100.") + fs.DurationVar(&o.MirroringEndpointUpdatesBatchPeriod.Duration, "mirroring-endpointslice-updates-batch-period", o.MirroringEndpointUpdatesBatchPeriod.Duration, "The length of EndpointSlice updates batching period for EndpointSliceMirroring controller. Processing of EndpointSlice changes will be delayed by this duration to join them with potential upcoming updates and reduce the overall number of EndpointSlice updates. Larger number = higher endpoint programming latency, but lower number of endpoints revision generated") +} + +// ApplyTo fills up EndpointSliceMirroringController config with options. +func (o *EndpointSliceMirroringControllerOptions) ApplyTo(cfg *endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration) error { + if o == nil { + return nil + } + + cfg.MirroringConcurrentServiceEndpointSyncs = o.MirroringConcurrentServiceEndpointSyncs + cfg.MirroringMaxEndpointsPerSubset = o.MirroringMaxEndpointsPerSubset + cfg.MirroringEndpointUpdatesBatchPeriod = o.MirroringEndpointUpdatesBatchPeriod + + return nil +} + +// Validate checks validation of EndpointSliceMirroringControllerOptions. +func (o *EndpointSliceMirroringControllerOptions) Validate() []error { + if o == nil { + return nil + } + + errs := []error{} + + if o.MirroringConcurrentServiceEndpointSyncs < mirroringMinConcurrentServiceEndpointSyncs { + errs = append(errs, fmt.Errorf("mirroring-concurrent-service-endpoint-syncs must not be less than %d, but got %d", mirroringMinConcurrentServiceEndpointSyncs, o.MirroringConcurrentServiceEndpointSyncs)) + } else if o.MirroringConcurrentServiceEndpointSyncs > mirroringMaxConcurrentServiceEndpointSyncs { + errs = append(errs, fmt.Errorf("mirroring-concurrent-service-endpoint-syncs must not be more than %d, but got %d", mirroringMaxConcurrentServiceEndpointSyncs, o.MirroringConcurrentServiceEndpointSyncs)) + } + + if o.MirroringMaxEndpointsPerSubset < mirroringMinMaxEndpointsPerSubset { + errs = append(errs, fmt.Errorf("mirroring-max-endpoints-per-subset must not be less than %d, but got %d", mirroringMinMaxEndpointsPerSubset, o.MirroringMaxEndpointsPerSubset)) + } else if o.MirroringMaxEndpointsPerSubset > mirroringMaxMaxEndpointsPerSubset { + errs = append(errs, fmt.Errorf("mirroring-max-endpoints-per-subset must not be more than %d, but got %d", mirroringMaxMaxEndpointsPerSubset, o.MirroringMaxEndpointsPerSubset)) + } + + return errs +} diff --git a/cmd/kube-controller-manager/app/options/options.go b/cmd/kube-controller-manager/app/options/options.go index 0ee27b178d2..eea668775a7 100644 --- a/cmd/kube-controller-manager/app/options/options.go +++ b/cmd/kube-controller-manager/app/options/options.go @@ -66,6 +66,7 @@ type KubeControllerManagerOptions struct { DeprecatedFlags *DeprecatedControllerOptions EndpointController *EndpointControllerOptions EndpointSliceController *EndpointSliceControllerOptions + EndpointSliceMirroringController *EndpointSliceMirroringControllerOptions GarbageCollectorController *GarbageCollectorControllerOptions HPAController *HPAControllerOptions JobController *JobControllerOptions @@ -129,6 +130,9 @@ func NewKubeControllerManagerOptions() (*KubeControllerManagerOptions, error) { EndpointSliceController: &EndpointSliceControllerOptions{ &componentConfig.EndpointSliceController, }, + EndpointSliceMirroringController: &EndpointSliceMirroringControllerOptions{ + &componentConfig.EndpointSliceMirroringController, + }, GarbageCollectorController: &GarbageCollectorControllerOptions{ &componentConfig.GarbageCollectorController, }, @@ -233,6 +237,7 @@ func (s *KubeControllerManagerOptions) Flags(allControllers []string, disabledBy s.DeprecatedFlags.AddFlags(fss.FlagSet("deprecated")) s.EndpointController.AddFlags(fss.FlagSet("endpoint controller")) s.EndpointSliceController.AddFlags(fss.FlagSet("endpointslice controller")) + s.EndpointSliceMirroringController.AddFlags(fss.FlagSet("endpointslicemirroring controller")) s.GarbageCollectorController.AddFlags(fss.FlagSet("garbagecollector controller")) s.HPAController.AddFlags(fss.FlagSet("horizontalpodautoscaling controller")) s.JobController.AddFlags(fss.FlagSet("job controller")) @@ -288,6 +293,9 @@ func (s *KubeControllerManagerOptions) ApplyTo(c *kubecontrollerconfig.Config) e if err := s.EndpointSliceController.ApplyTo(&c.ComponentConfig.EndpointSliceController); err != nil { return err } + if err := s.EndpointSliceMirroringController.ApplyTo(&c.ComponentConfig.EndpointSliceMirroringController); err != nil { + return err + } if err := s.GarbageCollectorController.ApplyTo(&c.ComponentConfig.GarbageCollectorController); err != nil { return err } @@ -367,6 +375,7 @@ func (s *KubeControllerManagerOptions) Validate(allControllers []string, disable errs = append(errs, s.DeprecatedFlags.Validate()...) errs = append(errs, s.EndpointController.Validate()...) errs = append(errs, s.EndpointSliceController.Validate()...) + errs = append(errs, s.EndpointSliceMirroringController.Validate()...) errs = append(errs, s.GarbageCollectorController.Validate()...) errs = append(errs, s.HPAController.Validate()...) errs = append(errs, s.JobController.Validate()...) diff --git a/cmd/kube-controller-manager/app/options/options_test.go b/cmd/kube-controller-manager/app/options/options_test.go index 5240d15d510..b6a6140489e 100644 --- a/cmd/kube-controller-manager/app/options/options_test.go +++ b/cmd/kube-controller-manager/app/options/options_test.go @@ -38,6 +38,7 @@ import ( deploymentconfig "k8s.io/kubernetes/pkg/controller/deployment/config" endpointconfig "k8s.io/kubernetes/pkg/controller/endpoint/config" endpointsliceconfig "k8s.io/kubernetes/pkg/controller/endpointslice/config" + endpointslicemirroringconfig "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config" garbagecollectorconfig "k8s.io/kubernetes/pkg/controller/garbagecollector/config" jobconfig "k8s.io/kubernetes/pkg/controller/job/config" namespaceconfig "k8s.io/kubernetes/pkg/controller/namespace/config" @@ -110,6 +111,8 @@ var args = []string{ "--master=192.168.4.20", "--max-endpoints-per-slice=200", "--min-resync-period=8h", + "--mirroring-concurrent-service-endpoint-syncs=2", + "--mirroring-max-endpoints-per-subset=1000", "--namespace-sync-period=10m", "--node-cidr-mask-size=48", "--node-cidr-mask-size-ipv4=48", @@ -250,6 +253,12 @@ func TestAddFlags(t *testing.T) { MaxEndpointsPerSlice: 200, }, }, + EndpointSliceMirroringController: &EndpointSliceMirroringControllerOptions{ + &endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{ + MirroringConcurrentServiceEndpointSyncs: 2, + MirroringMaxEndpointsPerSubset: 1000, + }, + }, GarbageCollectorController: &GarbageCollectorControllerOptions{ &garbagecollectorconfig.GarbageCollectorControllerConfiguration{ ConcurrentGCSyncs: 30, @@ -481,6 +490,10 @@ func TestApplyTo(t *testing.T) { ConcurrentServiceEndpointSyncs: 10, MaxEndpointsPerSlice: 200, }, + EndpointSliceMirroringController: endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{ + MirroringConcurrentServiceEndpointSyncs: 2, + MirroringMaxEndpointsPerSubset: 1000, + }, GarbageCollectorController: garbagecollectorconfig.GarbageCollectorControllerConfiguration{ ConcurrentGCSyncs: 30, GCIgnoredResources: []garbagecollectorconfig.GroupResource{ diff --git a/hack/.golint_failures b/hack/.golint_failures index d61c97c5bbe..a3066328fbc 100644 --- a/hack/.golint_failures +++ b/hack/.golint_failures @@ -59,6 +59,7 @@ pkg/controller/disruption pkg/controller/endpoint pkg/controller/endpoint/config/v1alpha1 pkg/controller/endpointslice/config/v1alpha1 +pkg/controller/endpointslicemirroring/config/v1alpha1 pkg/controller/garbagecollector pkg/controller/garbagecollector/config/v1alpha1 pkg/controller/job/config/v1alpha1 diff --git a/pkg/controller/BUILD b/pkg/controller/BUILD index b2adf479bad..c5e57c6c044 100644 --- a/pkg/controller/BUILD +++ b/pkg/controller/BUILD @@ -115,6 +115,7 @@ filegroup( "//pkg/controller/disruption:all-srcs", "//pkg/controller/endpoint:all-srcs", "//pkg/controller/endpointslice:all-srcs", + "//pkg/controller/endpointslicemirroring:all-srcs", "//pkg/controller/garbagecollector:all-srcs", "//pkg/controller/history:all-srcs", "//pkg/controller/job:all-srcs", diff --git a/pkg/controller/apis/config/BUILD b/pkg/controller/apis/config/BUILD index ae35f0823a6..f5340ed02bd 100644 --- a/pkg/controller/apis/config/BUILD +++ b/pkg/controller/apis/config/BUILD @@ -16,6 +16,7 @@ go_library( "//pkg/controller/deployment/config:go_default_library", "//pkg/controller/endpoint/config:go_default_library", "//pkg/controller/endpointslice/config:go_default_library", + "//pkg/controller/endpointslicemirroring/config:go_default_library", "//pkg/controller/garbagecollector/config:go_default_library", "//pkg/controller/job/config:go_default_library", "//pkg/controller/namespace/config:go_default_library", diff --git a/pkg/controller/apis/config/types.go b/pkg/controller/apis/config/types.go index b9f1d0f8fcc..1548cfc1c24 100644 --- a/pkg/controller/apis/config/types.go +++ b/pkg/controller/apis/config/types.go @@ -24,6 +24,7 @@ import ( deploymentconfig "k8s.io/kubernetes/pkg/controller/deployment/config" endpointconfig "k8s.io/kubernetes/pkg/controller/endpoint/config" endpointsliceconfig "k8s.io/kubernetes/pkg/controller/endpointslice/config" + endpointslicemirroringconfig "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config" garbagecollectorconfig "k8s.io/kubernetes/pkg/controller/garbagecollector/config" jobconfig "k8s.io/kubernetes/pkg/controller/job/config" namespaceconfig "k8s.io/kubernetes/pkg/controller/namespace/config" @@ -78,6 +79,9 @@ type KubeControllerManagerConfiguration struct { // EndpointSliceControllerConfiguration holds configuration for // EndpointSliceController related features. EndpointSliceController endpointsliceconfig.EndpointSliceControllerConfiguration + // EndpointSliceMirroringControllerConfiguration holds configuration for + // EndpointSliceMirroringController related features. + EndpointSliceMirroringController endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration // GarbageCollectorControllerConfiguration holds configuration for // GarbageCollectorController related features. GarbageCollectorController garbagecollectorconfig.GarbageCollectorControllerConfiguration diff --git a/pkg/controller/apis/config/v1alpha1/BUILD b/pkg/controller/apis/config/v1alpha1/BUILD index 75f0021b17d..82d2e12570c 100644 --- a/pkg/controller/apis/config/v1alpha1/BUILD +++ b/pkg/controller/apis/config/v1alpha1/BUILD @@ -20,6 +20,7 @@ go_library( "//pkg/controller/deployment/config/v1alpha1:go_default_library", "//pkg/controller/endpoint/config/v1alpha1:go_default_library", "//pkg/controller/endpointslice/config/v1alpha1:go_default_library", + "//pkg/controller/endpointslicemirroring/config/v1alpha1:go_default_library", "//pkg/controller/garbagecollector/config/v1alpha1:go_default_library", "//pkg/controller/job/config/v1alpha1:go_default_library", "//pkg/controller/namespace/config/v1alpha1:go_default_library", diff --git a/pkg/controller/apis/config/v1alpha1/defaults.go b/pkg/controller/apis/config/v1alpha1/defaults.go index 6074d55a009..d97580f8b3a 100644 --- a/pkg/controller/apis/config/v1alpha1/defaults.go +++ b/pkg/controller/apis/config/v1alpha1/defaults.go @@ -28,6 +28,7 @@ import ( deploymentconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/deployment/config/v1alpha1" endpointconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/endpoint/config/v1alpha1" endpointsliceconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/endpointslice/config/v1alpha1" + endpointslicemirroringconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config/v1alpha1" garbagecollectorconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/garbagecollector/config/v1alpha1" jobconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/job/config/v1alpha1" namespaceconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/namespace/config/v1alpha1" @@ -81,6 +82,8 @@ func SetDefaults_KubeControllerManagerConfiguration(obj *kubectrlmgrconfigv1alph endpointconfigv1alpha1.RecommendedDefaultEndpointControllerConfiguration(&obj.EndpointController) // Use the default RecommendedDefaultEndpointSliceControllerConfiguration options endpointsliceconfigv1alpha1.RecommendedDefaultEndpointSliceControllerConfiguration(&obj.EndpointSliceController) + // Use the default RecommendedDefaultEndpointSliceMirroringControllerConfiguration options + endpointslicemirroringconfigv1alpha1.RecommendedDefaultEndpointSliceMirroringControllerConfiguration(&obj.EndpointSliceMirroringController) // Use the default RecommendedDefaultGenericControllerManagerConfiguration options garbagecollectorconfigv1alpha1.RecommendedDefaultGarbageCollectorControllerConfiguration(&obj.GarbageCollectorController) // Use the default RecommendedDefaultJobControllerConfiguration options diff --git a/pkg/controller/apis/config/v1alpha1/doc.go b/pkg/controller/apis/config/v1alpha1/doc.go index bacd8cda6c4..8efb8e3acc4 100644 --- a/pkg/controller/apis/config/v1alpha1/doc.go +++ b/pkg/controller/apis/config/v1alpha1/doc.go @@ -23,6 +23,8 @@ limitations under the License. // +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/endpoint/config/v1alpha1 // +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/endpointslice/config/v1alpha1 // +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/garbagecollector/config/v1alpha1 +// +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/endpointslice/config/v1alpha1 +// +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config/v1alpha1 // +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/job/config/v1alpha1 // +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/namespace/config/v1alpha1 // +k8s:conversion-gen=k8s.io/kubernetes/pkg/controller/nodeipam/config/v1alpha1 diff --git a/pkg/controller/apis/config/v1alpha1/zz_generated.conversion.go b/pkg/controller/apis/config/v1alpha1/zz_generated.conversion.go index d74cb218e3a..a93b332a4c9 100644 --- a/pkg/controller/apis/config/v1alpha1/zz_generated.conversion.go +++ b/pkg/controller/apis/config/v1alpha1/zz_generated.conversion.go @@ -34,6 +34,7 @@ import ( deploymentconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/deployment/config/v1alpha1" endpointconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/endpoint/config/v1alpha1" endpointsliceconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/endpointslice/config/v1alpha1" + endpointslicemirroringconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/endpointslicemirroring/config/v1alpha1" garbagecollectorconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/garbagecollector/config/v1alpha1" jobconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/job/config/v1alpha1" namespaceconfigv1alpha1 "k8s.io/kubernetes/pkg/controller/namespace/config/v1alpha1" @@ -297,6 +298,9 @@ func autoConvert_v1alpha1_KubeControllerManagerConfiguration_To_config_KubeContr if err := endpointsliceconfigv1alpha1.Convert_v1alpha1_EndpointSliceControllerConfiguration_To_config_EndpointSliceControllerConfiguration(&in.EndpointSliceController, &out.EndpointSliceController, s); err != nil { return err } + if err := endpointslicemirroringconfigv1alpha1.Convert_v1alpha1_EndpointSliceMirroringControllerConfiguration_To_config_EndpointSliceMirroringControllerConfiguration(&in.EndpointSliceMirroringController, &out.EndpointSliceMirroringController, s); err != nil { + return err + } if err := garbagecollectorconfigv1alpha1.Convert_v1alpha1_GarbageCollectorControllerConfiguration_To_config_GarbageCollectorControllerConfiguration(&in.GarbageCollectorController, &out.GarbageCollectorController, s); err != nil { return err } @@ -378,6 +382,9 @@ func autoConvert_config_KubeControllerManagerConfiguration_To_v1alpha1_KubeContr if err := endpointsliceconfigv1alpha1.Convert_config_EndpointSliceControllerConfiguration_To_v1alpha1_EndpointSliceControllerConfiguration(&in.EndpointSliceController, &out.EndpointSliceController, s); err != nil { return err } + if err := endpointslicemirroringconfigv1alpha1.Convert_config_EndpointSliceMirroringControllerConfiguration_To_v1alpha1_EndpointSliceMirroringControllerConfiguration(&in.EndpointSliceMirroringController, &out.EndpointSliceMirroringController, s); err != nil { + return err + } if err := garbagecollectorconfigv1alpha1.Convert_config_GarbageCollectorControllerConfiguration_To_v1alpha1_GarbageCollectorControllerConfiguration(&in.GarbageCollectorController, &out.GarbageCollectorController, s); err != nil { return err } diff --git a/pkg/controller/apis/config/zz_generated.deepcopy.go b/pkg/controller/apis/config/zz_generated.deepcopy.go index 6d4565fc976..0f5dd63275e 100644 --- a/pkg/controller/apis/config/zz_generated.deepcopy.go +++ b/pkg/controller/apis/config/zz_generated.deepcopy.go @@ -116,6 +116,7 @@ func (in *KubeControllerManagerConfiguration) DeepCopyInto(out *KubeControllerMa out.DeprecatedController = in.DeprecatedController out.EndpointController = in.EndpointController out.EndpointSliceController = in.EndpointSliceController + out.EndpointSliceMirroringController = in.EndpointSliceMirroringController in.GarbageCollectorController.DeepCopyInto(&out.GarbageCollectorController) out.HPAController = in.HPAController out.JobController = in.JobController diff --git a/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/controller_policy.go b/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/controller_policy.go index e6d68801528..4871dcc9673 100644 --- a/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/controller_policy.go +++ b/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/controller_policy.go @@ -160,6 +160,18 @@ func buildControllerRoles() ([]rbacv1.ClusterRole, []rbacv1.ClusterRoleBinding) eventsRule(), }, }) + + addControllerRole(&controllerRoles, &controllerRoleBindings, rbacv1.ClusterRole{ + ObjectMeta: metav1.ObjectMeta{Name: saRolePrefix + "endpointslicemirroring-controller"}, + Rules: []rbacv1.PolicyRule{ + rbacv1helpers.NewRule("get", "list", "watch").Groups(legacyGroup).Resources("services", "endpoints").RuleOrDie(), + // The controller needs to be able to set a service's finalizers to be able to create an EndpointSlice + // resource that is owned by the service and sets blockOwnerDeletion=true in its ownerRef. + rbacv1helpers.NewRule("update").Groups(legacyGroup).Resources("services/finalizers").RuleOrDie(), + rbacv1helpers.NewRule("get", "list", "create", "update", "delete").Groups(discoveryGroup).Resources("endpointslices").RuleOrDie(), + eventsRule(), + }, + }) } if utilfeature.DefaultFeatureGate.Enabled(features.ExpandPersistentVolumes) { diff --git a/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-role-bindings.yaml b/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-role-bindings.yaml index 8a1ab919472..96b2cb3f181 100644 --- a/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-role-bindings.yaml +++ b/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-role-bindings.yaml @@ -153,6 +153,23 @@ items: - kind: ServiceAccount name: endpointslice-controller namespace: kube-system +- apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRoleBinding + metadata: + annotations: + rbac.authorization.kubernetes.io/autoupdate: "true" + creationTimestamp: null + labels: + kubernetes.io/bootstrapping: rbac-defaults + name: system:controller:endpointslicemirroring-controller + roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: system:controller:endpointslicemirroring-controller + subjects: + - kind: ServiceAccount + name: endpointslicemirroring-controller + namespace: kube-system - apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: diff --git a/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-roles.yaml b/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-roles.yaml index 954414b9eee..e3b8272e11e 100644 --- a/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-roles.yaml +++ b/plugin/pkg/auth/authorizer/rbac/bootstrappolicy/testdata/controller-roles.yaml @@ -519,6 +519,50 @@ items: - create - patch - update +- apiVersion: rbac.authorization.k8s.io/v1 + kind: ClusterRole + metadata: + annotations: + rbac.authorization.kubernetes.io/autoupdate: "true" + creationTimestamp: null + labels: + kubernetes.io/bootstrapping: rbac-defaults + name: system:controller:endpointslicemirroring-controller + rules: + - apiGroups: + - "" + resources: + - endpoints + - services + verbs: + - get + - list + - watch + - apiGroups: + - "" + resources: + - services/finalizers + verbs: + - update + - apiGroups: + - discovery.k8s.io + resources: + - endpointslices + verbs: + - create + - delete + - get + - list + - update + - apiGroups: + - "" + - events.k8s.io + resources: + - events + verbs: + - create + - patch + - update - apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: diff --git a/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/types.go b/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/types.go index f99980bec8e..8ee6d64abe8 100644 --- a/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/types.go +++ b/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/types.go @@ -114,6 +114,9 @@ type KubeControllerManagerConfiguration struct { // EndpointSliceControllerConfiguration holds configuration for // EndpointSliceController related features. EndpointSliceController EndpointSliceControllerConfiguration + // EndpointSliceMirroringControllerConfiguration holds configuration for + // EndpointSliceMirroringController related features. + EndpointSliceMirroringController EndpointSliceMirroringControllerConfiguration // GarbageCollectorControllerConfiguration holds configuration for // GarbageCollectorController related features. GarbageCollectorController GarbageCollectorControllerConfiguration @@ -319,6 +322,27 @@ type EndpointSliceControllerConfiguration struct { EndpointUpdatesBatchPeriod metav1.Duration } +// EndpointSliceMirroringControllerConfiguration contains elements describing +// EndpointSliceMirroringController. +type EndpointSliceMirroringControllerConfiguration struct { + // mirroringConcurrentServiceEndpointSyncs is the number of service endpoint + // syncing operations that will be done concurrently. Larger number = faster + // endpoint slice updating, but more CPU (and network) load. + MirroringConcurrentServiceEndpointSyncs int32 + + // mirroringMaxEndpointsPerSubset is the maximum number of endpoints that + // will be mirrored to an EndpointSlice for an EndpointSubset. + MirroringMaxEndpointsPerSubset int32 + + // mirroringEndpointUpdatesBatchPeriod can be used to batch EndpointSlice + // updates. All updates triggered by EndpointSlice changes will be delayed + // by up to 'mirroringEndpointUpdatesBatchPeriod'. If other addresses in the + // same Endpoints resource change in that period, they will be batched to a + // single EndpointSlice update. Default 0 value means that each Endpoints + // update triggers an EndpointSlice update. + MirroringEndpointUpdatesBatchPeriod metav1.Duration +} + // GarbageCollectorControllerConfiguration contains elements describing GarbageCollectorController. type GarbageCollectorControllerConfiguration struct { // enables the generic garbage collector. MUST be synced with the diff --git a/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/zz_generated.deepcopy.go b/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/zz_generated.deepcopy.go index afbba171c9d..623ed12b2aa 100644 --- a/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/zz_generated.deepcopy.go +++ b/staging/src/k8s.io/kube-controller-manager/config/v1alpha1/zz_generated.deepcopy.go @@ -157,6 +157,23 @@ func (in *EndpointSliceControllerConfiguration) DeepCopy() *EndpointSliceControl return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *EndpointSliceMirroringControllerConfiguration) DeepCopyInto(out *EndpointSliceMirroringControllerConfiguration) { + *out = *in + out.MirroringEndpointUpdatesBatchPeriod = in.MirroringEndpointUpdatesBatchPeriod + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new EndpointSliceMirroringControllerConfiguration. +func (in *EndpointSliceMirroringControllerConfiguration) DeepCopy() *EndpointSliceMirroringControllerConfiguration { + if in == nil { + return nil + } + out := new(EndpointSliceMirroringControllerConfiguration) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GarbageCollectorControllerConfiguration) DeepCopyInto(out *GarbageCollectorControllerConfiguration) { *out = *in @@ -307,6 +324,7 @@ func (in *KubeControllerManagerConfiguration) DeepCopyInto(out *KubeControllerMa out.DeprecatedController = in.DeprecatedController out.EndpointController = in.EndpointController out.EndpointSliceController = in.EndpointSliceController + out.EndpointSliceMirroringController = in.EndpointSliceMirroringController in.GarbageCollectorController.DeepCopyInto(&out.GarbageCollectorController) in.HPAController.DeepCopyInto(&out.HPAController) out.JobController = in.JobController From fa59370e9348e3f04fdf86defa1b8de132e39562 Mon Sep 17 00:00:00 2001 From: Rob Scott Date: Wed, 1 Jul 2020 18:30:24 -0700 Subject: [PATCH 6/6] Adding new logging, event, and metric to better capture when mirroring addresses is skipped --- .../endpointslicemirroring_controller.go | 1 + .../endpointslicemirroring_controller_test.go | 2 +- .../endpointslicemirroring/events.go | 4 + .../endpointslicemirroring/metrics/metrics.go | 13 ++++ .../endpointslicemirroring/reconciler.go | 73 +++++++++++++++---- .../endpointslicemirroring/reconciler_test.go | 16 +++- 6 files changed, 88 insertions(+), 21 deletions(-) diff --git a/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go index d127be14433..bc05ddaacdb 100644 --- a/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go +++ b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller.go @@ -161,6 +161,7 @@ type Controller struct { // has been synced at least once. Added as a member to the struct to allow // injection for testing. endpointSlicesSynced cache.InformerSynced + // endpointSliceTracker tracks the list of EndpointSlices and associated // resource versions expected for each Endpoints resource. It can help // determine if a cached EndpointSlice is out of date. diff --git a/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go index 0c0a927284b..83b0d8faa1c 100644 --- a/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go +++ b/pkg/controller/endpointslicemirroring/endpointslicemirroring_controller_test.go @@ -172,7 +172,7 @@ func TestSyncEndpoints(t *testing.T) { }}, }, endpointSlices: []*discovery.EndpointSlice{}, - expectedNumActions: 1, + expectedNumActions: 2, // extra action for creating warning event expectedNumSlices: 1, }} diff --git a/pkg/controller/endpointslicemirroring/events.go b/pkg/controller/endpointslicemirroring/events.go index 5cf511cf4fb..ed23181746f 100644 --- a/pkg/controller/endpointslicemirroring/events.go +++ b/pkg/controller/endpointslicemirroring/events.go @@ -26,4 +26,8 @@ const ( // InvalidIPAddress indicates that an IP address found in an Endpoints // resource is invalid. InvalidIPAddress = "InvalidIPAddress" + // TooManyAddressesToMirror indicates that some addresses were not mirrored + // due to an EndpointSubset containing more addresses to mirror than + // MaxEndpointsPerSubset allows. + TooManyAddressesToMirror = "TooManyAddressesToMirror" ) diff --git a/pkg/controller/endpointslicemirroring/metrics/metrics.go b/pkg/controller/endpointslicemirroring/metrics/metrics.go index c3a3d0d04ee..8fb8980c372 100644 --- a/pkg/controller/endpointslicemirroring/metrics/metrics.go +++ b/pkg/controller/endpointslicemirroring/metrics/metrics.go @@ -64,6 +64,18 @@ var ( }, []string{}, ) + // AddressesSkippedPerSync tracks the number of addresses skipped on each + // Endpoints sync due to being invalid or exceeding MaxEndpointsPerSubset. + AddressesSkippedPerSync = metrics.NewHistogramVec( + &metrics.HistogramOpts{ + Subsystem: EndpointSliceMirroringSubsystem, + Name: "addresses_skipped_per_sync", + Help: "Number of addresses skipped on each Endpoints sync due to being invalid or exceeding MaxEndpointsPerSubset", + StabilityLevel: metrics.ALPHA, + Buckets: metrics.ExponentialBuckets(2, 2, 15), + }, + []string{}, + ) // EndpointsSyncDuration tracks how long syncEndpoints() takes in a number // of Seconds. EndpointsSyncDuration = metrics.NewHistogramVec( @@ -127,6 +139,7 @@ func RegisterMetrics() { legacyregistry.MustRegister(EndpointsAddedPerSync) legacyregistry.MustRegister(EndpointsUpdatedPerSync) legacyregistry.MustRegister(EndpointsRemovedPerSync) + legacyregistry.MustRegister(AddressesSkippedPerSync) legacyregistry.MustRegister(EndpointsSyncDuration) legacyregistry.MustRegister(EndpointsDesired) legacyregistry.MustRegister(NumEndpointSlices) diff --git a/pkg/controller/endpointslicemirroring/reconciler.go b/pkg/controller/endpointslicemirroring/reconciler.go index 041854fa728..3aafa42c567 100644 --- a/pkg/controller/endpointslicemirroring/reconciler.go +++ b/pkg/controller/endpointslicemirroring/reconciler.go @@ -36,11 +36,25 @@ import ( // reconciler is responsible for transforming current EndpointSlice state into // desired state type reconciler struct { - client clientset.Interface + client clientset.Interface + + // endpointSliceTracker tracks the list of EndpointSlices and associated + // resource versions expected for each Endpoints resource. It can help + // determine if a cached EndpointSlice is out of date. + endpointSliceTracker *endpointSliceTracker + + // eventRecorder allows reconciler to record an event if it finds an invalid + // IP address in an Endpoints resource. + eventRecorder record.EventRecorder + + // maxEndpointsPerSubset references the maximum number of endpoints that + // should be added to an EndpointSlice for an EndpointSubset. This allows + // for a simple 1:1 mapping between EndpointSubset and EndpointSlice. maxEndpointsPerSubset int32 - endpointSliceTracker *endpointSliceTracker - metricsCache *metrics.Cache - eventRecorder record.EventRecorder + + // metricsCache tracks values for total numbers of desired endpoints as well + // as the efficiency of EndpointSlice endpoints distribution + metricsCache *metrics.Cache } // reconcile takes an Endpoints resource and ensures that corresponding @@ -50,38 +64,65 @@ func (r *reconciler) reconcile(endpoints *corev1.Endpoints, existingSlices []*di // Calculate desired state. d := newDesiredCalc() + numInvalidAddresses := 0 + addressesSkipped := 0 + for _, subset := range endpoints.Subsets { multiKey := d.initPorts(subset.Ports) - totalAddresses := 0 - numInvalidAddresses := 0 + totalAddresses := len(subset.Addresses) + len(subset.NotReadyAddresses) + totalAddressesAdded := 0 for _, address := range subset.Addresses { - totalAddresses++ - if totalAddresses > int(r.maxEndpointsPerSubset) { + // Break if we've reached the max number of addresses to mirror + // per EndpointSubset. This allows for a simple 1:1 mapping between + // EndpointSubset and EndpointSlice. + if totalAddressesAdded >= int(r.maxEndpointsPerSubset) { break } - if ok := d.addAddress(address, multiKey, true); !ok { + if ok := d.addAddress(address, multiKey, true); ok { + totalAddressesAdded++ + } else { numInvalidAddresses++ klog.Warningf("Address in %s/%s Endpoints is not a valid IP, it will not be mirrored to an EndpointSlice: %s", endpoints.Namespace, endpoints.Name, address.IP) } } for _, address := range subset.NotReadyAddresses { - totalAddresses++ - if totalAddresses > int(r.maxEndpointsPerSubset) { + // Break if we've reached the max number of addresses to mirror + // per EndpointSubset. This allows for a simple 1:1 mapping between + // EndpointSubset and EndpointSlice. + if totalAddressesAdded >= int(r.maxEndpointsPerSubset) { break } - if ok := d.addAddress(address, multiKey, false); !ok { + if ok := d.addAddress(address, multiKey, true); ok { + totalAddressesAdded++ + } else { numInvalidAddresses++ klog.Warningf("Address in %s/%s Endpoints is not a valid IP, it will not be mirrored to an EndpointSlice: %s", endpoints.Namespace, endpoints.Name, address.IP) } } - if numInvalidAddresses > 0 { - r.eventRecorder.Eventf(endpoints, corev1.EventTypeWarning, InvalidIPAddress, - "Skipped %d invalid IP addresses when mirroring to EndpointSlices", numInvalidAddresses) - } + addressesSkipped += totalAddresses - totalAddressesAdded + } + + // This metric includes addresses skipped for being invalid or exceeding + // MaxEndpointsPerSubset. + metrics.AddressesSkippedPerSync.WithLabelValues().Observe(float64(addressesSkipped)) + + // Record an event on the Endpoints resource if we skipped mirroring for any + // invalid IP addresses. + if numInvalidAddresses > 0 { + r.eventRecorder.Eventf(endpoints, corev1.EventTypeWarning, InvalidIPAddress, + "Skipped %d invalid IP addresses when mirroring to EndpointSlices", numInvalidAddresses) + } + + // Record a separate event if we skipped mirroring due to the number of + // addresses exceeding MaxEndpointsPerSubset. + if addressesSkipped > numInvalidAddresses { + klog.Warningf("%d addresses in %s/%s Endpoints were skipped due to exceeding MaxEndpointsPerSubset", addressesSkipped, endpoints.Namespace, endpoints.Name) + r.eventRecorder.Eventf(endpoints, corev1.EventTypeWarning, TooManyAddressesToMirror, + "A max of %d addresses can be mirrored to EndpointSlices per Endpoints subset. %d addresses were skipped", r.maxEndpointsPerSubset, addressesSkipped) } // Build data structures for existing state. diff --git a/pkg/controller/endpointslicemirroring/reconciler_test.go b/pkg/controller/endpointslicemirroring/reconciler_test.go index b43304c2baa..da371ee3f96 100644 --- a/pkg/controller/endpointslicemirroring/reconciler_test.go +++ b/pkg/controller/endpointslicemirroring/reconciler_test.go @@ -533,7 +533,7 @@ func TestReconcile(t *testing.T) { existingEndpointSlices: []*discovery.EndpointSlice{}, expectedNumSlices: 2, expectedClientActions: 2, - expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 3, addedPerSync: 3, numCreated: 2}, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 3, addedPerSync: 3, skippedPerSync: 2, numCreated: 2}, }, { testName: "Endpoints with 2 subsets, multiple ports, all invalid addresses", subsets: []corev1.EndpointSubset{{ @@ -582,9 +582,9 @@ func TestReconcile(t *testing.T) { existingEndpointSlices: []*discovery.EndpointSlice{}, expectedNumSlices: 0, expectedClientActions: 0, - expectedMetrics: &expectedMetrics{desiredSlices: 0, actualSlices: 0, desiredEndpoints: 0, addedPerSync: 0, numCreated: 0}, + expectedMetrics: &expectedMetrics{desiredSlices: 0, actualSlices: 0, desiredEndpoints: 0, addedPerSync: 0, skippedPerSync: 5, numCreated: 0}, }, { - testName: "Endpoints with 2 subsets, multiple ports and addresses, existing EndpointSlice with some addresses", + testName: "Endpoints with 2 subsets, 1 exceeding maxEndpointsPerSubset", subsets: []corev1.EndpointSubset{{ Ports: []corev1.EndpointPort{{ Name: "http", @@ -632,7 +632,7 @@ func TestReconcile(t *testing.T) { expectedNumSlices: 2, expectedClientActions: 2, maxEndpointsPerSubset: 2, - expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 4, addedPerSync: 4, updatedPerSync: 0, removedPerSync: 0, numCreated: 2, numUpdated: 0}, + expectedMetrics: &expectedMetrics{desiredSlices: 2, actualSlices: 2, desiredEndpoints: 4, addedPerSync: 4, updatedPerSync: 0, removedPerSync: 0, skippedPerSync: 1, numCreated: 2, numUpdated: 0}, }} for _, tc := range testCases { @@ -885,6 +885,7 @@ type expectedMetrics struct { addedPerSync int updatedPerSync int removedPerSync int + skippedPerSync int numCreated int numUpdated int numDeleted int @@ -929,6 +930,12 @@ func expectMetrics(t *testing.T, em expectedMetrics) { t.Errorf("Expected endpointsRemovedPerSync to be %d, got %v", em.removedPerSync, actualRemovedPerSync) } + actualSkippedPerSync, err := testutil.GetHistogramMetricValue(metrics.AddressesSkippedPerSync.WithLabelValues()) + handleErr(t, err, "addressesSkippedPerSync") + if actualSkippedPerSync != float64(em.skippedPerSync) { + t.Errorf("Expected addressesSkippedPerSync to be %d, got %v", em.skippedPerSync, actualSkippedPerSync) + } + actualCreated, err := testutil.GetCounterMetricValue(metrics.EndpointSliceChanges.WithLabelValues("create")) handleErr(t, err, "endpointSliceChangesCreated") if actualCreated != float64(em.numCreated) { @@ -962,6 +969,7 @@ func setupMetrics() { metrics.EndpointsAddedPerSync.Delete(map[string]string{}) metrics.EndpointsUpdatedPerSync.Delete(map[string]string{}) metrics.EndpointsRemovedPerSync.Delete(map[string]string{}) + metrics.AddressesSkippedPerSync.Delete(map[string]string{}) metrics.EndpointSliceChanges.Delete(map[string]string{"operation": "create"}) metrics.EndpointSliceChanges.Delete(map[string]string{"operation": "update"}) metrics.EndpointSliceChanges.Delete(map[string]string{"operation": "delete"})