2017-11-20 09:30:52 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"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-05 03:35:52 -05:00
|
|
|
"github.com/docker/cli/cli/compose/loader"
|
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2017-11-20 09:30:52 -05:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"vbom.ml/util/sortorder"
|
|
|
|
)
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
// RunList is the kubernetes implementation of docker stack ls
|
|
|
|
func RunList(dockerCli *KubeCli, opts options.List) error {
|
|
|
|
stacks, err := getStacks(dockerCli)
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-04 06:30:39 -05:00
|
|
|
format := opts.Format
|
2017-11-20 09:30:52 -05:00
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
stackCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewStackFormat(format),
|
|
|
|
}
|
|
|
|
sort.Sort(byName(stacks))
|
|
|
|
return formatter.StackWrite(stackCtx, stacks)
|
|
|
|
}
|
|
|
|
|
|
|
|
type byName []*formatter.Stack
|
|
|
|
|
|
|
|
func (n byName) Len() int { return len(n) }
|
|
|
|
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
|
|
|
func (n byName) Less(i, j int) bool { return sortorder.NaturalLess(n[i].Name, n[j].Name) }
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
func getStacks(kubeCli *KubeCli) ([]*formatter.Stack, error) {
|
|
|
|
stackSvc, err := kubeCli.stacks()
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stacks, err := stackSvc.List(metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var formattedStacks []*formatter.Stack
|
|
|
|
for _, stack := range stacks.Items {
|
2017-12-05 03:35:52 -05:00
|
|
|
cfg, err := loadStack(stack.Spec.ComposeFile)
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
formattedStacks = append(formattedStacks, &formatter.Stack{
|
|
|
|
Name: stack.Name,
|
2017-12-05 03:35:52 -05:00
|
|
|
Services: len(getServices(cfg)),
|
2017-11-20 09:30:52 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return formattedStacks, nil
|
|
|
|
}
|
2017-12-05 03:35:52 -05:00
|
|
|
|
|
|
|
func loadStack(composefile string) (*composetypes.Config, error) {
|
|
|
|
parsed, err := loader.ParseYAML([]byte(composefile))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return loader.Load(composetypes.ConfigDetails{
|
|
|
|
ConfigFiles: []composetypes.ConfigFile{
|
|
|
|
{
|
|
|
|
Config: parsed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|