2017-11-20 09:30:52 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-12-04 06:30:39 -05:00
|
|
|
"github.com/docker/cli/cli/command/stack/options"
|
2017-12-04 03:44:06 -05:00
|
|
|
"github.com/docker/cli/kubernetes/labels"
|
2017-11-20 09:30:52 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
// RunServices is the kubernetes implementation of docker stack services
|
|
|
|
func RunServices(dockerCli *KubeCli, opts options.Services) error {
|
2017-11-20 09:30:52 -05:00
|
|
|
// Initialize clients
|
2017-12-04 06:30:39 -05:00
|
|
|
client, err := dockerCli.composeClient()
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-09 09:13:16 -04:00
|
|
|
stacks, err := client.Stacks(false)
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
replicas := client.ReplicaSets()
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
if _, err := stacks.Get(opts.Namespace); err != nil {
|
2017-12-04 06:30:39 -05:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace)
|
2017-11-20 09:30:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
replicasList, err := replicas.List(metav1.ListOptions{LabelSelector: labels.SelectorForStack(opts.Namespace)})
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
servicesList, err := client.Services().List(metav1.ListOptions{LabelSelector: labels.SelectorForStack(opts.Namespace)})
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert Replicas sets and kubernetes services to swam services and formatter informations
|
|
|
|
services, info, err := replicasToServices(replicasList, servicesList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
if opts.Quiet {
|
2017-11-20 09:30:52 -05:00
|
|
|
info = map[string]formatter.ServiceListInfo{}
|
|
|
|
}
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
format := opts.Format
|
2017-11-20 09:30:52 -05:00
|
|
|
if len(format) == 0 {
|
2017-12-04 06:30:39 -05:00
|
|
|
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
|
2017-11-20 09:30:52 -05:00
|
|
|
format = dockerCli.ConfigFile().ServicesFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
servicesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-12-04 06:30:39 -05:00
|
|
|
Format: formatter.NewServiceListFormat(format, opts.Quiet),
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
return formatter.ServiceListWrite(servicesCtx, services, info)
|
|
|
|
}
|