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