initial version of --port flag
This commit is contained in:
parent
5ab8d5b50d
commit
7098be1693
@ -22,8 +22,6 @@ THE SOFTWARE.
|
|||||||
package create
|
package create
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
cliutil "github.com/rancher/k3d/cmd/util"
|
cliutil "github.com/rancher/k3d/cmd/util"
|
||||||
@ -133,11 +131,11 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// volumeFilterMap will map volume mounts to applied node filters
|
// volumeFilterMap will map volume mounts to applied node filters
|
||||||
volumeFilterMap := make(map[string]string, 1)
|
volumeFilterMap := make(map[string][]string, 1)
|
||||||
for _, volumeFlag := range volumeFlags {
|
for _, volumeFlag := range volumeFlags {
|
||||||
|
|
||||||
// split node filter from the specified volume
|
// split node filter from the specified volume
|
||||||
volume, filter, err := cliutil.SplitFilterFromFlag(volumeFlag)
|
volume, filters, err := cliutil.SplitFiltersFromFlag(volumeFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
@ -150,9 +148,9 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime,
|
|||||||
|
|
||||||
// create new entry or append filter to existing entry
|
// create new entry or append filter to existing entry
|
||||||
if _, exists := volumeFilterMap[volume]; exists {
|
if _, exists := volumeFilterMap[volume]; exists {
|
||||||
volumeFilterMap[volume] = fmt.Sprintf("%s;%s", volumeFilterMap[volume], filter)
|
volumeFilterMap[volume] = append(volumeFilterMap[volume], filters...)
|
||||||
} else {
|
} else {
|
||||||
volumeFilterMap[volume] = filter
|
volumeFilterMap[volume] = filters
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,14 +159,20 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
portFilterMap := make(map[string]string, 1)
|
portFilterMap := make(map[string][]string, 1)
|
||||||
for _, portFlag := range portFlags {
|
for _, portFlag := range portFlags {
|
||||||
// split node filter from the specified volume
|
// split node filter from the specified volume
|
||||||
portmap, filter, err := cliutil.SplitFilterFromFlag(portFlag)
|
portmap, filters, err := cliutil.SplitFiltersFromFlag(portFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(filters) > 1 {
|
||||||
|
log.Fatalln("Can only apply a Portmap to one node")
|
||||||
|
}
|
||||||
|
|
||||||
|
// the same portmapping can't be applied to multiple nodes
|
||||||
|
|
||||||
// validate the specified volume mount and return it in SRC:DEST format
|
// validate the specified volume mount and return it in SRC:DEST format
|
||||||
portmap, err = cliutil.ValidatePortMap(portmap)
|
portmap, err = cliutil.ValidatePortMap(portmap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -177,9 +181,9 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime,
|
|||||||
|
|
||||||
// create new entry or append filter to existing entry
|
// create new entry or append filter to existing entry
|
||||||
if _, exists := portFilterMap[portmap]; exists {
|
if _, exists := portFilterMap[portmap]; exists {
|
||||||
portFilterMap[portmap] = fmt.Sprintf("%s;%s", portFilterMap[portmap], filter)
|
log.Fatalln("Same Portmapping can not be used for multiple nodes")
|
||||||
} else {
|
} else {
|
||||||
portFilterMap[portmap] = filter
|
portFilterMap[portmap] = filters
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,5 +235,16 @@ func parseCreateClusterCmd(cmd *cobra.Command, args []string) (runtimes.Runtime,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append ports
|
||||||
|
for portmap, filter := range portFilterMap {
|
||||||
|
nodes, err := cliutil.FilterNodes(cluster.Nodes, filter)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
for _, node := range nodes {
|
||||||
|
node.Ports = append(node.Ports, portmap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return runtime, cluster
|
return runtime, cluster
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,13 @@ import (
|
|||||||
// Regexp pattern to match node filters
|
// Regexp pattern to match node filters
|
||||||
var filterRegexp = regexp.MustCompile(`^(?P<group>master|worker|all)(?P<subsetSpec>\[(?P<subset>(?P<subsetList>(\d+,?)+)|(?P<subsetRange>\d*:\d*)|(?P<subsetWildcard>\*))\])?$`)
|
var filterRegexp = regexp.MustCompile(`^(?P<group>master|worker|all)(?P<subsetSpec>\[(?P<subset>(?P<subsetList>(\d+,?)+)|(?P<subsetRange>\d*:\d*)|(?P<subsetWildcard>\*))\])?$`)
|
||||||
|
|
||||||
// SplitFilterFromFlag separates a flag's value from the node filter, if there is one
|
// SplitFiltersFromFlag separates a flag's value from the node filter, if there is one
|
||||||
func SplitFilterFromFlag(flag string) (string, string, error) {
|
func SplitFiltersFromFlag(flag string) (string, []string, error) {
|
||||||
|
|
||||||
/* Case 1) no filter specified */
|
/* Case 1) no filter specified */
|
||||||
|
|
||||||
if !strings.Contains(flag, "@") {
|
if !strings.Contains(flag, "@") {
|
||||||
return flag, "", nil
|
return flag, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Case 2) filter indicated using '@' in flag */
|
/* Case 2) filter indicated using '@' in flag */
|
||||||
@ -51,25 +51,22 @@ func SplitFilterFromFlag(flag string) (string, string, error) {
|
|||||||
|
|
||||||
// max number of pieces after split = 2 (only one @ allowed in flag)
|
// max number of pieces after split = 2 (only one @ allowed in flag)
|
||||||
if len(split) > 2 {
|
if len(split) > 2 {
|
||||||
return "", "", fmt.Errorf("Invalid flag '%s': only one '@' for node filter allowed", flag)
|
return "", nil, fmt.Errorf("Invalid flag '%s': only one '@' for node filter allowed", flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// trailing or leading '@'
|
// trailing or leading '@'
|
||||||
if len(split) < 2 {
|
if len(split) < 2 {
|
||||||
return "", "", fmt.Errorf("Invalid flag '%s' includes '@' but is missing either an object or a filter", flag)
|
return "", nil, fmt.Errorf("Invalid flag '%s' includes '@' but is missing either an object or a filter", flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
return split[0], split[1], nil
|
return split[0], strings.Split(split[1], ";"), nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterNodes takes a string filter to return a filtered list of nodes
|
// FilterNodes takes a string filter to return a filtered list of nodes
|
||||||
func FilterNodes(nodes []*k3d.Node, filterString string) ([]*k3d.Node, error) {
|
func FilterNodes(nodes []*k3d.Node, filters []string) ([]*k3d.Node, error) {
|
||||||
|
|
||||||
// filterString is a semicolon-separated list of node filters
|
if len(filters) == 0 || len(filters[0]) == 0 {
|
||||||
filters := strings.Split(filterString, ";")
|
|
||||||
|
|
||||||
if len(filters) == 0 {
|
|
||||||
log.Warnln("No filter specified")
|
log.Warnln("No filter specified")
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
@ -104,7 +101,7 @@ func FilterNodes(nodes []*k3d.Node, filterString string) ([]*k3d.Node, error) {
|
|||||||
// if one of the filters is 'all', we only return this and drop all others
|
// if one of the filters is 'all', we only return this and drop all others
|
||||||
if submatches["group"] == "all" {
|
if submatches["group"] == "all" {
|
||||||
// TODO: only log if really more than one is specified
|
// TODO: only log if really more than one is specified
|
||||||
log.Warnf("Node filter 'all' set, but more were specified in '%s'", filterString)
|
log.Warnf("Node filter 'all' set, but more were specified in '%+v'", filters)
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,5 +70,5 @@ func ParseAPIPort(portString string) (k3d.ExposeAPI, error) {
|
|||||||
|
|
||||||
// ValidatePortMap validates a port mapping
|
// ValidatePortMap validates a port mapping
|
||||||
func ValidatePortMap(portmap string) (string, error) {
|
func ValidatePortMap(portmap string) (string, error) {
|
||||||
return "", nil // TODO:
|
return portmap, nil // TODO:
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) {
|
|||||||
/* Ports */
|
/* Ports */
|
||||||
exposedPorts, portBindings, err := nat.ParsePortSpecs(node.Ports)
|
exposedPorts, portBindings, err := nat.ParsePortSpecs(node.Ports)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("Failed to parse port specs")
|
log.Errorln("Failed to parse port specs '%v'", node.Ports)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
containerConfig.ExposedPorts = exposedPorts
|
containerConfig.ExposedPorts = exposedPorts
|
||||||
|
Loading…
Reference in New Issue
Block a user