func()

in pkg/controllers/endpointslice_plan.go [75:115]


func (p *EndpointSlicePlan) trimSlices(desiredEndpoints map[string]*model.Endpoint, desiredPorts []*model.Port) (changes EndpointSliceChanges) {
	// remove all undesired existing endpoints in slices
	for _, existingSlice := range p.Current {
		updatedEndpointList := make([]discovery.Endpoint, 0)
		for _, existingEndpoint := range existingSlice.Endpoints {
			key := existingEndpoint.Addresses[0]
			if _, found := desiredEndpoints[key]; found {
				updatedEndpointList = append(updatedEndpointList, existingEndpoint)
				delete(desiredEndpoints, key)
			}
		}

		// mark slice for deletion if all endpoints were removed
		if len(updatedEndpointList) == 0 {
			changes.Delete = append(changes.Delete, existingSlice)
			continue
		}

		sliceNeedsUpdate := false

		// slice needs to be updated if ports do not match
		if !PortsEqualIgnoreOrder(desiredPorts, endpointPortSliceToPortSlice(existingSlice.Ports)) {
			existingSlice.Ports = portSliceToEndpointPortSlice(desiredPorts)
			sliceNeedsUpdate = true
		}

		// slice needs to be updated if endpoint list changed
		if len(updatedEndpointList) != len(existingSlice.Endpoints) {
			existingSlice.Endpoints = updatedEndpointList
			sliceNeedsUpdate = true
		}

		if sliceNeedsUpdate {
			changes.Update = append(changes.Update, existingSlice)
		} else {
			changes.Unmodified = append(changes.Unmodified, existingSlice)
		}
	}

	return changes
}