2017-11-20 09:30:52 -05:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-01-29 16:18:43 -05:00
|
|
|
"github.com/docker/cli/cli/command/stack/loader"
|
2017-12-04 06:30:39 -05:00
|
|
|
"github.com/docker/cli/cli/command/stack/options"
|
2017-11-20 09:30:52 -05:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
// RunDeploy is the kubernetes implementation of docker stack deploy
|
|
|
|
func RunDeploy(dockerCli *KubeCli, opts options.Deploy) error {
|
2017-11-20 09:30:52 -05:00
|
|
|
cmdOut := dockerCli.Out()
|
|
|
|
// Check arguments
|
2017-09-29 08:21:40 -04:00
|
|
|
if len(opts.Composefiles) == 0 {
|
|
|
|
return errors.Errorf("Please specify only one compose file (with --compose-file).")
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
2018-01-29 16:18:43 -05:00
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
// Initialize clients
|
2018-04-09 09:07:11 -04:00
|
|
|
composeClient, err := dockerCli.composeClient()
|
2018-01-29 16:18:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-09 09:07:11 -04:00
|
|
|
stacks, err := composeClient.Stacks()
|
2018-01-29 16:18:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
// Parse the compose file
|
|
|
|
cfg, err := loader.LoadComposefile(dockerCli, opts)
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-26 09:59:44 -05:00
|
|
|
stack, err := stacks.FromCompose(dockerCli.Err(), opts.Namespace, cfg)
|
2017-11-20 09:30:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-31 09:37:14 -05:00
|
|
|
|
2018-01-02 17:56:07 -05:00
|
|
|
configMaps := composeClient.ConfigMaps()
|
|
|
|
secrets := composeClient.Secrets()
|
|
|
|
services := composeClient.Services()
|
|
|
|
pods := composeClient.Pods()
|
2017-11-20 09:30:52 -05:00
|
|
|
watcher := DeployWatcher{
|
2018-01-02 17:56:07 -05:00
|
|
|
Pods: pods,
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
if err := stacks.IsColliding(services, stack); err != nil {
|
2017-11-20 09:30:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
if err := stack.createFileBasedConfigMaps(configMaps); err != nil {
|
2017-11-20 09:30:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
if err := stack.createFileBasedSecrets(secrets); err != nil {
|
2017-11-20 09:30:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
if err = stacks.CreateOrUpdate(stack); err != nil {
|
|
|
|
return err
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(cmdOut, "Waiting for the stack to be stable and running...")
|
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
<-watcher.Watch(stack.name, stack.getServices())
|
2017-11-20 09:30:52 -05:00
|
|
|
|
2018-01-31 09:37:14 -05:00
|
|
|
fmt.Fprintf(cmdOut, "Stack %s is stable and running\n\n", stack.name)
|
2017-11-20 09:30:52 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|