func createIngressBackend(service core.Service) extensions.IngressBackend {
    serviceName := service.GetName()
    if len(service.Spec.Ports) > 0 {
        var servicePort32 interface{}
        var servicePort int
        servicePort32 = service.Spec.Ports[0].Port
        servicePort32Tmp := servicePort32.(int32)
        servicePort = int(servicePort32Tmp)
        return extensions.IngressBackend {
            ServiceName: serviceName,
            ServicePort: intstr.FromInt(servicePort),
        }
    }
    return extensions.IngressBackend {}
}
create an ingress backend before putting it to the ingress
package main
import (
    "os"
    "time"
    "flag"
    "reflect"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/cache"
    "k8s.io/client-go/tools/clientcmd"
    core "k8s.io/api/core/v1"
    extensions "k8s.io/api/extensions/v1beta1"
    "k8s.io/apimachinery/pkg/fields"
    "k8s.io/apimachinery/pkg/util/intstr"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    log "github.com/Sirupsen/logrus"
)
var (
    wildcardRecord = os.Getenv("AUTO_INGRESS_SERVER_NAME")
    secret = os.Getenv("AUTO_INGRESS_SECRET")
    kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
)
func main() {
    flag.Parse()
    var err error
    var config *rest.Config
    if *kubeconfig != "" {
        config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
    } else {
        config, err = rest.InClusterConfig()
    }
    if err != nil {
        log.Errorln(err.Error())
        return
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        log.Errorln(err.Error())
        return
    }
	var svcIngPair map[string]extensions.Ingress
    svcIngPair = make(map[string]extensions.Ingress)
    log.Info("Initializing mapping between ingresses and services...")
	err = createIngressServiceMap(clientset, svcIngPair)
    if err != nil {
        log.Errorln(err.Error())
        return
    }
    log.Info("Initialized map: ", reflect.ValueOf(svcIngPair).MapKeys())
    watchlist := cache.NewListWatchFromClient(clientset.Core().RESTClient(), "services", "",
        fields.Everything())
    _, controller := cache.NewInformer(
        watchlist,
        &core.Service{},
        time.Second * 0,
        cache.ResourceEventHandlerFuncs{
            AddFunc: func(obj interface{}) {
                svc := obj.(*core.Service)
                log.Info("Service added: ", svc.Name)
                lb := svc.Labels
                if _, found1 := svcIngPair[svc.Name]; !found1 {
                    if val, found2 := lb["auto-ingress/enabled"]; found2 {
                        if val == "enabled" {
                            newIng, err := createIngressForService(clientset, *svc)
                            if err != nil {
                                log.Errorln(err.Error())
                            } else {
                                log.Info("Created new ingress for service: ", svc.Name)
                                svcIngPair[svc.Name] = *newIng
                                log.Info("Updated map: ", reflect.ValueOf(svcIngPair).MapKeys())
                            }
                        }
                    }
                }
            },
            DeleteFunc: func(obj interface{}) {
                svc := obj.(*core.Service)
                log.Info("Service deleted: ", svc.Name)
				if ing, found := svcIngPair[svc.Name]; found {
					clientset.Extensions().Ingresses(svc.Namespace).Delete(ing.Name, nil)
					log.Info("Deleted ingress for service: ", svc.Name)
                    delete(svcIngPair, svc.Name)
                    log.Info("Updated map: ", reflect.ValueOf(svcIngPair).MapKeys())
				}
            },
            UpdateFunc:func(oldObj, newObj interface{}) {
                newSvc := newObj.(*core.Service)
                log.Info("Service changed: ", newSvc.Name)
                lb := newSvc.Labels
                if ing, found1 := svcIngPair[newSvc.Name]; found1 {
                    if val, found2 := lb["auto-ingress/enabled"]; !found2 {
                        clientset.ExtensionsV1beta1().Ingresses(newSvc.Namespace).Delete(ing.Name, nil)
                        log.Info("Deleted ingress for service: ", newSvc.Name)
                        delete(svcIngPair, newSvc.Name)
                        log.Info("Updated map: ", reflect.ValueOf(svcIngPair).MapKeys())
                    } else {
                        if val == "disabled" {
                            clientset.ExtensionsV1beta1().Ingresses(newSvc.Namespace).Delete(ing.Name, nil)
                            log.Info("Deleted ingress for service: ", newSvc.Name)
                            delete(svcIngPair, newSvc.Name)
                            log.Info("Updated map: ", reflect.ValueOf(svcIngPair).MapKeys())
                        }
                    }
                } else {
                    if val, found3 := lb["auto-ingress/enabled"]; found3 {
                        if val == "enabled" {
                            newIng, err := createIngressForService(clientset, *newSvc)
                            if err != nil {
                                log.Errorln(err.Error())
                            } else {
                                log.Info("created new ingress for service: ", newSvc.Name)
                                svcIngPair[newSvc.Name] = *newIng
                                log.Info("Updated map: ", reflect.ValueOf(svcIngPair).MapKeys())
                            }
                        }
                    }
                }
            },
        },
    )
    stop := make(chan struct{})
    go controller.Run(stop)
    for{
        time.Sleep(time.Second)
    }
}
func createIngressServiceMap(clientset *kubernetes.Clientset, m map[string]extensions.Ingress) error {
	services, err := clientset.CoreV1().Services("").List(metav1.ListOptions{})
    if err != nil {
        return err
    }
    ingresses, err:= clientset.ExtensionsV1beta1().Ingresses("").List(metav1.ListOptions{})
    if err != nil {
        return err
    }
    for i:=0; i < len(ingresses.Items); i++ {
        rules := ingresses.Items[i].Spec.Rules
        for j:=0; j < len(rules); j++ {
            paths := rules[j].HTTP.Paths
            for k:=0; k < len(paths); k++ {
                svcName := paths[k].Backend.ServiceName
                if _, found := m[svcName]; !found {
                    m[svcName] = ingresses.Items[i]
                }
            }
        }
    }
	for i:=0; i < len(services.Items); i++ {
		lb := services.Items[i].GetLabels()
		svcName := services.Items[i].GetName()
        if _, found1 := m[svcName]; !found1 {
            if val, found2 := lb["auto-ingress/enabled"]; found2 {
                if val == "enabled" {
                    newIng, err := createIngressForService(clientset, services.Items[i])
                    if err != nil {
                        return err
                    }
                    m[services.Items[i].GetName()] = *newIng
                }
            }
        } else {
			if val, found2 := lb["auto-ingress/enabled"]; found2 {
				if val == "disabled" {
					delete(m, svcName)
				}
			} else {
				delete(m, svcName)
			}
		}
    }
    return nil
}
func createIngressForService(clientset *kubernetes.Clientset, service core.Service) (*extensions.Ingress, error) {
	backend := createIngressBackend(service)
    ingress := createIngress(service, backend)
    newIng, err := clientset.ExtensionsV1beta1().Ingresses(service.Namespace).Create(ingress)
    return newIng, err
}