func (w *WorkloadEndpointConverter) ConvertAPIToKVPair(a unversioned.Resource) (*model.KVPair, error) {
	ah := a.(api.WorkloadEndpoint)
	k, err := w.ConvertMetadataToKey(ah.Metadata)
	if err != nil {
		return nil, err
	}
	ipv4Nets := []net.IPNet{}
	ipv6Nets := []net.IPNet{}
	for _, n := range ah.Spec.IPNetworks {
		n = *(n.Network())
		if n.Version() == 4 {
			ipv4Nets = append(ipv4Nets, n)
		} else {
			ipv6Nets = append(ipv6Nets, n)
		}
	}
	ipv4NAT := []model.IPNAT{}
	ipv6NAT := []model.IPNAT{}
	for _, n := range ah.Spec.IPNATs {
		nat := model.IPNAT{IntIP: n.InternalIP, ExtIP: n.ExternalIP}
		if n.InternalIP.Version() == 4 {
			ipv4NAT = append(ipv4NAT, nat)
		} else {
			ipv6NAT = append(ipv6NAT, nat)
		}
	}
	var ports []model.EndpointPort
	for _, port := range ah.Spec.Ports {
		ports = append(ports, model.EndpointPort{
			Name:     port.Name,
			Protocol: port.Protocol,
			Port:     port.Port,
		})
	}
	d := model.KVPair{
		Key: k,
		Value: &model.WorkloadEndpoint{
			Labels:           ah.Metadata.Labels,
			ActiveInstanceID: ah.Metadata.ActiveInstanceID,
			State:            "active",
			Name:             ah.Spec.InterfaceName,
			Mac:              ah.Spec.MAC,
			ProfileIDs:       ah.Spec.Profiles,
			IPv4Nets:         ipv4Nets,
			IPv6Nets:         ipv6Nets,
			IPv4NAT:          ipv4NAT,
			IPv6NAT:          ipv6NAT,
			IPv4Gateway:      ah.Spec.IPv4Gateway,
			IPv6Gateway:      ah.Spec.IPv6Gateway,
			Ports:            ports,
		},
		Revision: ah.Metadata.Revision,
	}
	return &d, nil
}
ConvertAPIToKVPair converts an API WorkloadEndpoint structure to a KVPair containing a
backend WorkloadEndpoint and WorkloadEndpointKey.
package converter
import (
	api "github.com/projectcalico/libcalico-go/lib/apis/v1"
	"github.com/projectcalico/libcalico-go/lib/apis/v1/unversioned"
	"github.com/projectcalico/libcalico-go/lib/backend/model"
	"github.com/projectcalico/libcalico-go/lib/net"
)
type WorkloadEndpointConverter struct{}
func (w *WorkloadEndpointConverter) ConvertMetadataToKey(m unversioned.ResourceMetadata) (model.Key, error) {
	hm := m.(api.WorkloadEndpointMetadata)
	k := model.WorkloadEndpointKey{
		Hostname:       hm.Node,
		OrchestratorID: hm.Orchestrator,
		WorkloadID:     hm.Workload,
		EndpointID:     hm.Name,
	}
	return k, nil
}