func (c *KubernetesRegistry) GetService(name string) (Service, error) {
	c.mtx.RLock()
	service, ok := c.services[name]
	c.mtx.RUnlock()

	if ok {
		return service, nil
	}

	services, err := c.Client.Services(c.Namespace).List(labels.OneTermEqualSelector("name", name))
	if err != nil {
		return nil, err
	}

	if len(services.Items) == 0 {
		return nil, fmt.Errorf("Service not found")
	}

	ks := &KubernetesService{ServiceName: name}
	for _, item := range services.Items {
		ks.ServiceNodes = append(ks.ServiceNodes, &KubernetesNode{
			NodeAddress: item.Spec.PortalIP,
			NodePort:    item.Spec.Port,
		})
	}

	return ks, nil
}
func (k KubernetesExecutor) DeleteReplicationController(id string) error {
	// Maybe find the desired ReplicationController
	rc, err := k.GetReplicationController(id)
	if err != nil {
		return err
	}

	// Maybe find Services labeled for that ReplicationController
	forService := labels.OneTermEqualSelector("service-name", rc.ObjectMeta.Name)
	sl, err := k.client.Services(namespace).List(forService)
	if err != nil {
		return err
	}

	// Delete all found Services
	for _, s := range sl.Items {
		if err := k.client.Services(namespace).Delete(s.ObjectMeta.Name); err != nil {
			return err
		}
	}

	// Scale down the ReplicationController, deleting Pods
	rc.Spec.Replicas = 0
	if _, err := k.client.ReplicationControllers(namespace).Update(&rc); err != nil {
		return err
	}

	// Delete the ReplicationController
	if err := k.client.ReplicationControllers(namespace).Delete(id); err != nil {
		return err
	}

	// Profit?
	return nil
}
Example #3
0
// NewSourceApiserver creates a config source that watches and pulls from the apiserver.
func NewSourceApiserver(client *client.Client, hostname string, updates chan<- interface{}) {
	lw := &cache.ListWatch{
		Client:        client,
		FieldSelector: labels.OneTermEqualSelector("Status.Host", hostname),
		Resource:      "pods",
	}
	newSourceApiserverFromLW(lw, updates)
}
func statusFromReplicationController(rc api.ReplicationController) (string, error) {
	desired := rc.Spec.Replicas
	actual := rc.Status.Replicas

	if actual < desired {
		return "pending", nil
	} else if desired == actual {
		selector := labels.OneTermEqualSelector("service-name", rc.ObjectMeta.Name)
		pods, err := DefaultExecutor.GetPods(selector)
		if err != nil {
			return "", err
		}
		runningCount := 0
		for _, p := range pods {
			if p.Status.Phase == api.PodRunning {
				runningCount++
			}
		}

		return fmt.Sprintf("running %v/%v", runningCount, desired), nil
	}

	return "unknown", nil
}