func()

in pkg/model/history/resourceinfo/endpoint_slices.go [97:176]


func (e *EndpointSliceInfo) GetLastDiffs(uid string) ([]*EndpointSliceEndpointDiff, error) {
	previous, current, err := e.getLastPair(uid)
	if err != nil {
		return nil, err
	}
	result := []*EndpointSliceEndpointDiff{}

	switch {
	case previous == nil:
		if len(current.Endpoints) == 0 {
			return []*EndpointSliceEndpointDiff{}, nil
		}
		for _, endpoint := range current.Endpoints {
			result = append(result, &EndpointSliceEndpointDiff{
				Operation: DiffDeleted,
				Previous:  nil,
				Current:   endpoint,
				TargetRef: endpoint.TargetRef,
			})
		}
	case current == nil:
		for _, endpoint := range previous.Endpoints {
			result = append(result, &EndpointSliceEndpointDiff{
				Operation: DiffDeleted,
				Previous:  endpoint,
				Current:   nil,
				TargetRef: endpoint.TargetRef,
			})
		}
	default:
		previousEndpointsMap := map[string]*model.EndpointSliceEndpoint{}
		currentEndpointsMap := map[string]*model.EndpointSliceEndpoint{}
		if previous.Endpoints != nil {
			for _, endpoint := range previous.Endpoints {
				if endpoint == nil || endpoint.TargetRef == nil {
					slog.Warn(fmt.Sprintf("endpoint (%s) has no uid in the target ref", strings.Join(endpoint.Addresses, ",")))
					continue
				}
				previousEndpointsMap[endpoint.TargetRef.Uid] = endpoint
			}
		}
		if current.Endpoints != nil {
			for _, endpoint := range current.Endpoints {
				if endpoint == nil || endpoint.TargetRef == nil {
					slog.Warn(fmt.Sprintf("endpoint (%s) has no uid in the target ref", strings.Join(endpoint.Addresses, ",")))
					continue
				}
				currentEndpointsMap[endpoint.TargetRef.Uid] = endpoint
			}
		}
		for key, previous := range previousEndpointsMap {
			if _, found := currentEndpointsMap[key]; !found {
				result = append(result, &EndpointSliceEndpointDiff{
					Operation: DiffDeleted,
					Previous:  previous,
					Current:   nil,
					TargetRef: previous.TargetRef,
				})
			}
		}
		for key, current := range currentEndpointsMap {
			if previous, found := previousEndpointsMap[key]; !found {
				result = append(result, &EndpointSliceEndpointDiff{
					Operation: DiffNew,
					Previous:  nil,
					Current:   current,
					TargetRef: current.TargetRef,
				})
			} else if !previous.Conditions.SameWith(current.Conditions) {
				result = append(result, &EndpointSliceEndpointDiff{
					Operation: DiffChanged,
					Previous:  previous,
					Current:   current,
					TargetRef: current.TargetRef,
				})
			}
		}
	}
	return result, nil
}