func()

in pkg/provider/k8s/k8s.go [1263:1301]


func (c *K8s) serviceExists(resource runtime.Object) (bool, error) {
	req := resource.(*apiCoreV1.Service)
	kind := resource.GetObjectKind().GroupVersionKind().Kind
	if len(req.Namespace) == 0 {
		req.Namespace = "default"
	}

	switch v := resource.GetObjectKind().GroupVersionKind().Version; v {
	case "v1":
		client := c.clt.CoreV1().Services(req.Namespace)
		res, err := client.Get(c.ctx, req.Name, apiMetaV1.GetOptions{})
		if err != nil {
			return false, errors.Wrapf(err, "Checking Service resource status failed")
		}
		if res.Spec.Type == apiCoreV1.ServiceTypeLoadBalancer {
			// K8s API currently just supports LoadBalancerStatus.
			if len(res.Status.LoadBalancer.Ingress) > 0 {
				log.Printf("\tService %s Details", req.Name)
				for _, x := range res.Status.LoadBalancer.Ingress {

					ingressHostAddr := ""
					if len(x.IP) != 0 {
						ingressHostAddr = x.IP
					} else {
						ingressHostAddr = x.Hostname
					}

					log.Printf("\t\thttp://%s:%d", ingressHostAddr, res.Spec.Ports[0].Port)
				}
				return true, nil
			}
			return false, nil
		}
		// For any other type we blindly assume that it is up and running as we have no way of checking.
		return true, nil
	default:
		return false, fmt.Errorf("unknown object version: %v kind:'%v', name:'%v'", v, kind, req.Name)
	}
}