From 2e1b2c2d4423b9bf9aac90dc0eba4e7fa986769d Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Sun, 6 Dec 2020 22:36:34 +0100 Subject: [PATCH] add static ip to kube play Signed-off-by: Michael Ernst --- cmd/podman/play/kube.go | 14 ++++++++++++++ pkg/domain/entities/play.go | 8 +++++++- pkg/domain/infra/abi/play.go | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/podman/play/kube.go b/cmd/podman/play/kube.go index db70ad7d47..690bdeb93f 100644 --- a/cmd/podman/play/kube.go +++ b/cmd/podman/play/kube.go @@ -2,6 +2,7 @@ package pods import ( "fmt" + "net" "os" "github.com/containers/common/pkg/auth" @@ -23,6 +24,7 @@ type playKubeOptionsWrapper struct { TLSVerifyCLI bool CredentialsCLI string StartCLI bool + StaticIPCLI string } var ( @@ -75,6 +77,10 @@ func init() { flags.StringVar(&kubeOptions.Authfile, authfileFlagName, auth.GetDefaultAuthFile(), "Path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") _ = kubeCmd.RegisterFlagCompletionFunc(authfileFlagName, completion.AutocompleteDefault) + staticIPFlagName := "ip" + flags.StringVar(&kubeOptions.StaticIPCLI, staticIPFlagName, "", "Static IP address to assign to this pod") + _ = kubeCmd.RegisterFlagCompletionFunc(staticIPFlagName, completion.AutocompleteDefault) + if !registry.IsRemote() { certDirFlagName := "cert-dir" @@ -119,6 +125,14 @@ func kube(cmd *cobra.Command, args []string) error { kubeOptions.Password = creds.Password } + if kubeOptions.StaticIPCLI != "" { + ipAddress := net.ParseIP(kubeOptions.StaticIPCLI) + if ipAddress == nil { + return fmt.Errorf("failed parsing ip address: %s", kubeOptions.StaticIPCLI) + } + kubeOptions.StaticIP = ipAddress + } + report, err := registry.ContainerEngine().PlayKube(registry.GetContext(), args[0], kubeOptions.PlayKubeOptions) if err != nil { return err diff --git a/pkg/domain/entities/play.go b/pkg/domain/entities/play.go index 0b42e1a3f3..f28c047627 100644 --- a/pkg/domain/entities/play.go +++ b/pkg/domain/entities/play.go @@ -1,6 +1,10 @@ package entities -import "github.com/containers/image/v5/types" +import ( + "net" + + "github.com/containers/image/v5/types" +) // PlayKubeOptions controls playing kube YAML files. type PlayKubeOptions struct { @@ -30,6 +34,8 @@ type PlayKubeOptions struct { LogDriver string // Start - don't start the pod if false Start types.OptionalBool + // StaticIP - a static ip address that will be assigned to the pod + StaticIP net.IP } // PlayKubePod represents a single pod and associated containers created by play kube diff --git a/pkg/domain/infra/abi/play.go b/pkg/domain/infra/abi/play.go index 3aeb6a2ee7..23e6af146a 100644 --- a/pkg/domain/infra/abi/play.go +++ b/pkg/domain/infra/abi/play.go @@ -132,6 +132,11 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY } } + // Assign a static ip if it was specified + if options.StaticIP != nil { + p.StaticIP = &options.StaticIP + } + // Create the Pod pod, err := generate.MakePod(p, ic.Libpod) if err != nil {