// allocate assigns an unallocated ip to a service and updates the // service's persisted state. func (ic *IngressIPController) allocate(service *kapi.Service, key string) error { // Make a copy to avoid mutating cache state t, err := kapi.Scheme.DeepCopy(service) if err != nil { return err } service = t.(*kapi.Service) ip, err := ic.allocateIP(service.Spec.LoadBalancerIP) if err != nil { return err } ipString := ip.String() glog.V(5).Infof("Allocating ip %v to service %v", ipString, key) service.Status = kapi.ServiceStatus{ LoadBalancer: kapi.LoadBalancerStatus{ Ingress: []kapi.LoadBalancerIngress{ { IP: ipString, }, }, }, } if err = ic.persistServiceStatus(service); err != nil { if releaseErr := ic.ipAllocator.Release(ip); releaseErr != nil { // Release from contiguous allocator should never return an error, but just in case... utilruntime.HandleError(fmt.Errorf("Error releasing ip %v for service %v: %v", ipString, key, releaseErr)) } return err } ic.allocationMap[ipString] = key return ic.ensureExternalIP(service, key, ipString) }
func (su *ServiceUpdater) UpdateServiceRouting(service *api.Service) error { err := su.UpdateRouteIfNeeded(service) if err != nil { log.Println("Unable to update service route", err) return err } if ServiceHasLoadBalancerAddress(service) { log.Println("The route was found and the service has an address, not updating the status") return nil } log.Println("Found route for the service", service.ObjectMeta.Name, "updating the service load-balancer status") service.Status = api.ServiceStatus{ LoadBalancer: api.LoadBalancerStatus{ Ingress: []api.LoadBalancerIngress{ api.LoadBalancerIngress{ Hostname: su.GetDomainNamesFromService(service)[0], }, }, }, } _, err = su.ServiceRepository.Update(service) if err != nil { log.Println("Error while updating the service:", err) return err } log.Println("Successfully updated the service status") return nil }
func ReviewService(client *client.Client, service *api.Service, rootDns string) error { if service.Spec.Type != api.ServiceTypeLoadBalancer { log.Println("Skipping service", service.ObjectMeta.Name, "as it is not a LoadBalancer") return nil } // If there's an IP and/or DNS address in the load balancer status, skip it if ServiceHasLoadBalancerAddress(service) { log.Println("Skipping service", service.ObjectMeta.Name, "as it already have a LoadBalancer address") return nil } log.Println("Service", service.ObjectMeta.Name, "needs to be reviewed") // Get existing proxy configuration var proxyConfiguration reverseproxy.Configuration if jsonConfiguration, found := service.ObjectMeta.Annotations["kubernetesReverseproxy"]; found { proxyConfiguration = reverseproxy.Configuration{} if err := json.Unmarshal([]byte(jsonConfiguration), &proxyConfiguration); err != nil { log.Println("Unable to unmarshal the configuration, keep the empty one") } } else { log.Println("No `kubernetesReverseproxy` annotation found") proxyConfiguration = reverseproxy.Configuration{} } // If configuration found, skip it if len(proxyConfiguration.Hosts) == 0 { // Create the expected hostname of the service host := strings.Join([]string{ service.ObjectMeta.Name, service.ObjectMeta.Namespace, rootDns, }, ".") // Append the new host to the configuration proxyConfiguration.Hosts = append(proxyConfiguration.Hosts, reverseproxy.Host{ Host: host, Port: 80, }) jsonConfiguration, err := json.Marshal(proxyConfiguration) if err != nil { log.Println("Unable to JSON-encode the proxy configuration: ", err) return err } if service.ObjectMeta.Annotations == nil { service.ObjectMeta.Annotations = map[string]string{} } service.ObjectMeta.Annotations["kubernetesReverseproxy"] = string(jsonConfiguration) // Update the service log.Println("Adding the `kubernetesReverseproxy` annotation to service") updated, err := client.Services(service.ObjectMeta.Namespace).Update(service) if err != nil { log.Println("Error while updated the service:", err) return err } log.Println("Successfully added the reverse proxy configuration", updated) } else { // Updating service load-balancer status log.Println("Updating the service load-balancer status") service.Status = api.ServiceStatus{ LoadBalancer: api.LoadBalancerStatus{ Ingress: []api.LoadBalancerIngress{ api.LoadBalancerIngress{ Hostname: proxyConfiguration.Hosts[0].Host, }, }, }, } updated, err := client.Services(service.ObjectMeta.Namespace).Update(service) if err != nil { log.Println("Error while updated the service:", err) return err } log.Println("Successfully updated the service status", updated) } return nil }