1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
package flagparser
import (
"fmt"
"strings"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/spf13/cobra"
)
// expects configs in the format CONFIG_NAME:TARGET_NAME
func parseConfigString(configString string) (configName, presentName string, err error) {
tokens := strings.Split(configString, ":")
configName = strings.TrimSpace(tokens[0])
if configName == "" {
err = fmt.Errorf("invalid config name provided")
return
}
if len(tokens) > 1 {
presentName = strings.TrimSpace(tokens[1])
if presentName == "" {
err = fmt.Errorf("invalid presentation name provided")
return
}
} else {
presentName = configName
}
return
}
// ParseAddConfig validates configs passed on the command line
func ParseAddConfig(cmd *cobra.Command, spec *api.ServiceSpec, flagName string) error {
flags := cmd.Flags()
if flags.Changed(flagName) {
configs, err := flags.GetStringSlice(flagName)
if err != nil {
return err
}
container := spec.Task.GetContainer()
if container == nil {
spec.Task.Runtime = &api.TaskSpec_Container{
Container: &api.ContainerSpec{},
}
}
lookupConfigNames := []string{}
var needConfigs []*api.ConfigReference
for _, config := range configs {
n, p, err := parseConfigString(config)
if err != nil {
return err
}
// TODO(diogo): defaults to File targets, but in the future will take different types
configRef := &api.ConfigReference{
ConfigName: n,
Target: &api.ConfigReference_File{
File: &api.FileTarget{
Name: p,
Mode: 0444,
},
},
}
lookupConfigNames = append(lookupConfigNames, n)
needConfigs = append(needConfigs, configRef)
}
client, err := common.Dial(cmd)
if err != nil {
return err
}
r, err := client.ListConfigs(common.Context(cmd),
&api.ListConfigsRequest{Filters: &api.ListConfigsRequest_Filters{Names: lookupConfigNames}})
if err != nil {
return err
}
foundConfigs := make(map[string]*api.Config)
for _, config := range r.Configs {
foundConfigs[config.Spec.Annotations.Name] = config
}
for _, configRef := range needConfigs {
config, ok := foundConfigs[configRef.ConfigName]
if !ok {
return fmt.Errorf("config not found: %s", configRef.ConfigName)
}
configRef.ConfigID = config.ID
container.Configs = append(container.Configs, configRef)
}
}
return nil
}
// ParseRemoveConfig removes a set of configs from the task spec's config references
func ParseRemoveConfig(cmd *cobra.Command, spec *api.ServiceSpec, flagName string) error {
flags := cmd.Flags()
if flags.Changed(flagName) {
configs, err := flags.GetStringSlice(flagName)
if err != nil {
return err
}
container := spec.Task.GetContainer()
if container == nil {
return nil
}
wantToDelete := make(map[string]struct{})
for _, config := range configs {
n, _, err := parseConfigString(config)
if err != nil {
return err
}
wantToDelete[n] = struct{}{}
}
configRefs := []*api.ConfigReference{}
for _, configRef := range container.Configs {
if _, ok := wantToDelete[configRef.ConfigName]; ok {
continue
}
configRefs = append(configRefs, configRef)
}
container.Configs = configRefs
}
return nil
}
|