func()

in pkg/controllers/endpointslice_plan.go [117:150]


func (p *EndpointSlicePlan) getOrCreateUnfilledEndpointSlice(changes *EndpointSliceChanges, requiredCapacity int) (sliceWithRoom *discovery.EndpointSlice, needsPortUpdate bool) {
	// Prefer slices we are already updating
	for _, sliceToUpdate := range changes.Update {
		if len(sliceToUpdate.Endpoints) < p.getMaxEndpointsPerSlice() {
			return sliceToUpdate, false
		}
	}

	// Update a slice marked for deletion if possible
	if len(changes.Delete) > 0 {
		sliceToReuse := changes.Delete[0]
		changes.Delete = changes.Delete[1:]
		changes.Update = append(changes.Update, sliceToReuse)

		// clear endpoint list that was marked for deletion before reusing
		sliceToReuse.Endpoints = []discovery.Endpoint{}
		return sliceToReuse, true
	}

	// Update an unmodified slice if it has capacity to add all endpoints
	for i, unmodifiedSlice := range changes.Unmodified {
		proposedSliceLength := len(unmodifiedSlice.Endpoints) + requiredCapacity
		if proposedSliceLength <= p.getMaxEndpointsPerSlice() {
			changes.Unmodified = append(changes.Unmodified[:i], changes.Unmodified[i+1:]...)
			changes.Update = append(changes.Update, unmodifiedSlice)
			return unmodifiedSlice, false
		}
	}

	// No existing slices can fill new endpoint requirements so create a new slice
	sliceToCreate := CreateEndpointSliceStruct(p.Service, p.ServiceImportName)
	changes.Create = append(changes.Create, sliceToCreate)
	return sliceToCreate, true
}