Merge pull request #116732 from czybjtu/cleanup_map_to_sets

Cleanup: use Set instead of map in endpointSlice utils
This commit is contained in:
Kubernetes Prow Robot 2023-06-07 07:38:13 -07:00 committed by GitHub
commit 3fbf67a403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 14 deletions

View File

@ -80,7 +80,7 @@ func (r *reconciler) reconcile(service *corev1.Service, pods []*corev1.Pod, exis
// for further adjustment // for further adjustment
for _, existingSlice := range existingSlices { for _, existingSlice := range existingSlices {
// service no longer supports that address type, add it to deleted slices // service no longer supports that address type, add it to deleted slices
if _, ok := serviceSupportedAddressesTypes[existingSlice.AddressType]; !ok { if !serviceSupportedAddressesTypes.Has(existingSlice.AddressType) {
if r.topologyCache != nil { if r.topologyCache != nil {
svcKey, err := serviceControllerKey(existingSlice) svcKey, err := serviceControllerKey(existingSlice)
if err != nil { if err != nil {
@ -413,9 +413,9 @@ func (r *reconciler) reconcileByPortMapping(
endpointMeta *endpointMeta, endpointMeta *endpointMeta,
) ([]*discovery.EndpointSlice, []*discovery.EndpointSlice, []*discovery.EndpointSlice, int, int) { ) ([]*discovery.EndpointSlice, []*discovery.EndpointSlice, []*discovery.EndpointSlice, int, int) {
slicesByName := map[string]*discovery.EndpointSlice{} slicesByName := map[string]*discovery.EndpointSlice{}
sliceNamesUnchanged := sets.String{} sliceNamesUnchanged := sets.New[string]()
sliceNamesToUpdate := sets.String{} sliceNamesToUpdate := sets.New[string]()
sliceNamesToDelete := sets.String{} sliceNamesToDelete := sets.New[string]()
numRemoved := 0 numRemoved := 0
// 1. Iterate through existing slices to delete endpoints no longer desired // 1. Iterate through existing slices to delete endpoints no longer desired

View File

@ -302,8 +302,8 @@ func (sl endpointSliceEndpointLen) Less(i, j int) bool {
} }
// returns a map of address types used by a service // returns a map of address types used by a service
func getAddressTypesForService(service *v1.Service) map[discovery.AddressType]struct{} { func getAddressTypesForService(service *v1.Service) sets.Set[discovery.AddressType] {
serviceSupportedAddresses := make(map[discovery.AddressType]struct{}) serviceSupportedAddresses := sets.New[discovery.AddressType]()
// TODO: (khenidak) when address types are removed in favor of // TODO: (khenidak) when address types are removed in favor of
// v1.IPFamily this will need to be removed, and work directly with // v1.IPFamily this will need to be removed, and work directly with
// v1.IPFamily types // v1.IPFamily types
@ -312,15 +312,15 @@ func getAddressTypesForService(service *v1.Service) map[discovery.AddressType]st
// as it gets deprecated // as it gets deprecated
for _, family := range service.Spec.IPFamilies { for _, family := range service.Spec.IPFamilies {
if family == v1.IPv4Protocol { if family == v1.IPv4Protocol {
serviceSupportedAddresses[discovery.AddressTypeIPv4] = struct{}{} serviceSupportedAddresses.Insert(discovery.AddressTypeIPv4)
} }
if family == v1.IPv6Protocol { if family == v1.IPv6Protocol {
serviceSupportedAddresses[discovery.AddressTypeIPv6] = struct{}{} serviceSupportedAddresses.Insert(discovery.AddressTypeIPv6)
} }
} }
if len(serviceSupportedAddresses) > 0 { if serviceSupportedAddresses.Len() > 0 {
return serviceSupportedAddresses // we have found families for this service return serviceSupportedAddresses // we have found families for this service
} }
@ -344,7 +344,7 @@ func getAddressTypesForService(service *v1.Service) map[discovery.AddressType]st
if utilnet.IsIPv6String(service.Spec.ClusterIP) { if utilnet.IsIPv6String(service.Spec.ClusterIP) {
addrType = discovery.AddressTypeIPv6 addrType = discovery.AddressTypeIPv6
} }
serviceSupportedAddresses[addrType] = struct{}{} serviceSupportedAddresses.Insert(addrType)
klog.V(2).Infof("couldn't find ipfamilies for service: %v/%v. This could happen if controller manager is connected to an old apiserver that does not support ip families yet. EndpointSlices for this Service will use %s as the IP Family based on familyOf(ClusterIP:%v).", service.Namespace, service.Name, addrType, service.Spec.ClusterIP) klog.V(2).Infof("couldn't find ipfamilies for service: %v/%v. This could happen if controller manager is connected to an old apiserver that does not support ip families yet. EndpointSlices for this Service will use %s as the IP Family based on familyOf(ClusterIP:%v).", service.Namespace, service.Name, addrType, service.Spec.ClusterIP)
return serviceSupportedAddresses return serviceSupportedAddresses
} }
@ -354,14 +354,14 @@ func getAddressTypesForService(service *v1.Service) map[discovery.AddressType]st
// if the service is headless with no selector, then this will remain the case // if the service is headless with no selector, then this will remain the case
// if the service is headless with selector then chances are pods are still using single family // if the service is headless with selector then chances are pods are still using single family
// since kubelet will need to restart in order to start patching pod status with multiple ips // since kubelet will need to restart in order to start patching pod status with multiple ips
serviceSupportedAddresses[discovery.AddressTypeIPv4] = struct{}{} serviceSupportedAddresses.Insert(discovery.AddressTypeIPv4)
serviceSupportedAddresses[discovery.AddressTypeIPv6] = struct{}{} serviceSupportedAddresses.Insert(discovery.AddressTypeIPv6)
klog.V(2).Infof("couldn't find ipfamilies for headless service: %v/%v likely because controller manager is likely connected to an old apiserver that does not support ip families yet. The service endpoint slice will use dual stack families until api-server default it correctly", service.Namespace, service.Name) klog.V(2).Infof("couldn't find ipfamilies for headless service: %v/%v likely because controller manager is likely connected to an old apiserver that does not support ip families yet. The service endpoint slice will use dual stack families until api-server default it correctly", service.Namespace, service.Name)
return serviceSupportedAddresses return serviceSupportedAddresses
} }
func unchangedSlices(existingSlices, slicesToUpdate, slicesToDelete []*discovery.EndpointSlice) []*discovery.EndpointSlice { func unchangedSlices(existingSlices, slicesToUpdate, slicesToDelete []*discovery.EndpointSlice) []*discovery.EndpointSlice {
changedSliceNames := sets.String{} changedSliceNames := sets.New[string]()
for _, slice := range slicesToUpdate { for _, slice := range slicesToUpdate {
changedSliceNames.Insert(slice.Name) changedSliceNames.Insert(slice.Name)
} }
@ -402,7 +402,7 @@ func managedByChanged(endpointSlice1, endpointSlice2 *discovery.EndpointSlice) b
// managedByController returns true if the controller of the provided // managedByController returns true if the controller of the provided
// EndpointSlices is the EndpointSlice controller. // EndpointSlices is the EndpointSlice controller.
func managedByController(endpointSlice *discovery.EndpointSlice) bool { func managedByController(endpointSlice *discovery.EndpointSlice) bool {
managedBy, _ := endpointSlice.Labels[discovery.LabelManagedBy] managedBy := endpointSlice.Labels[discovery.LabelManagedBy]
return managedBy == controllerName return managedBy == controllerName
} }