2017-11-20 09:30:52 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2017-12-05 03:35:52 -05:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2017-12-04 03:44:06 -05:00
|
|
|
apiv1beta1 "github.com/docker/cli/kubernetes/compose/v1beta1"
|
|
|
|
"github.com/docker/cli/kubernetes/labels"
|
2017-11-20 09:30:52 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsColliding verify that services defined in the stack collides with already deployed services
|
2017-12-05 03:35:52 -05:00
|
|
|
func IsColliding(services corev1.ServiceInterface, stack *apiv1beta1.Stack, cfg *composetypes.Config) error {
|
|
|
|
stackObjects := getServices(cfg)
|
2017-11-20 09:30:52 -05:00
|
|
|
|
|
|
|
for _, srv := range stackObjects {
|
|
|
|
if err := verify(services, stack.Name, srv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:35:52 -05:00
|
|
|
// verify checks wether the service is already present in kubernetes.
|
|
|
|
// If we find the service by name but it doesn't have our label or it has a different value
|
|
|
|
// than the stack name for the label, we fail (i.e. it will collide)
|
2017-11-20 09:30:52 -05:00
|
|
|
func verify(services corev1.ServiceInterface, stackName string, service string) error {
|
|
|
|
svc, err := services.Get(service, metav1.GetOptions{})
|
|
|
|
if err == nil {
|
|
|
|
if key, ok := svc.ObjectMeta.Labels[labels.ForStackName]; ok {
|
|
|
|
if key != stackName {
|
|
|
|
return fmt.Errorf("service %s already present in stack named %s", service, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("service %s already present in the cluster", service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:35:52 -05:00
|
|
|
func getServices(cfg *composetypes.Config) []string {
|
|
|
|
services := make([]string, len(cfg.Services))
|
|
|
|
for i := range cfg.Services {
|
|
|
|
services[i] = cfg.Services[i].Name
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
sort.Strings(services)
|
2017-12-05 03:35:52 -05:00
|
|
|
return services
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|