Fix bug where two configurations had been created

* registry.PodmanConfig() new returns a pointer to the source of truth

Signed-off-by: Jhon Honce <jhonce@redhat.com>
This commit is contained in:
Jhon Honce
2020-04-16 16:42:33 -07:00
parent 0d2b5532c4
commit 554c663b5a
13 changed files with 49 additions and 46 deletions

View File

@ -10,7 +10,7 @@ import (
const sizeWithUnitFormat = "(format: `<number>[<unit>]`, where unit = b (bytes), k (kilobytes), m (megabytes), or g (gigabytes))" const sizeWithUnitFormat = "(format: `<number>[<unit>]`, where unit = b (bytes), k (kilobytes), m (megabytes), or g (gigabytes))"
var containerConfig = registry.NewPodmanConfig() var containerConfig = registry.PodmanConfig()
func GetCreateFlags(cf *ContainerCLIOpts) *pflag.FlagSet { func GetCreateFlags(cf *ContainerCLIOpts) *pflag.FlagSet {
createFlags := pflag.FlagSet{} createFlags := pflag.FlagSet{}

View File

@ -45,7 +45,8 @@ func init() {
flags.StringArrayVarP(&stopOptions.CIDFiles, "cidfile", "", nil, "Read the container ID from the file") flags.StringArrayVarP(&stopOptions.CIDFiles, "cidfile", "", nil, "Read the container ID from the file")
flags.BoolVarP(&stopOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") flags.BoolVarP(&stopOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.UintVarP(&stopTimeout, "time", "t", defaultContainerConfig.Engine.StopTimeout, "Seconds to wait for stop before killing the container") flags.UintVarP(&stopTimeout, "time", "t", defaultContainerConfig.Engine.StopTimeout, "Seconds to wait for stop before killing the container")
if registry.PodmanOptions.EngineMode == entities.ABIMode {
if registry.IsRemote() {
_ = flags.MarkHidden("latest") _ = flags.MarkHidden("latest")
_ = flags.MarkHidden("cidfile") _ = flags.MarkHidden("cidfile")
_ = flags.MarkHidden("ignore") _ = flags.MarkHidden("ignore")

View File

@ -43,7 +43,7 @@ func init() {
flags.DurationVarP(&waitOptions.Interval, "interval", "i", time.Duration(250), "Milliseconds to wait before polling for completion") flags.DurationVarP(&waitOptions.Interval, "interval", "i", time.Duration(250), "Milliseconds to wait before polling for completion")
flags.BoolVarP(&waitOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") flags.BoolVarP(&waitOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.StringVar(&waitCondition, "condition", "stopped", "Condition to wait on") flags.StringVar(&waitCondition, "condition", "stopped", "Condition to wait on")
if registry.PodmanOptions.EngineMode == entities.ABIMode { if registry.IsRemote() {
// TODO: This is the same as V1. We could skip creating the flag altogether in V2... // TODO: This is the same as V1. We could skip creating the flag altogether in V2...
_ = flags.MarkHidden("latest") _ = flags.MarkHidden("latest")
} }

View File

@ -14,12 +14,6 @@ import (
"github.com/containers/storage/pkg/reexec" "github.com/containers/storage/pkg/reexec"
) )
func init() {
// This is the bootstrap configuration, if user gives
// CLI flags parts of this configuration may be overwritten
registry.PodmanOptions = registry.NewPodmanConfig()
}
func main() { func main() {
if reexec.Init() { if reexec.Init() {
// We were invoked with a different argv[0] indicating that we // We were invoked with a different argv[0] indicating that we
@ -27,9 +21,10 @@ func main() {
return return
} }
cfg := registry.PodmanConfig()
for _, c := range registry.Commands { for _, c := range registry.Commands {
for _, m := range c.Mode { for _, m := range c.Mode {
if registry.PodmanOptions.EngineMode == m { if cfg.EngineMode == m {
parent := rootCmd parent := rootCmd
if c.Parent != nil { if c.Parent != nil {
parent = c.Parent parent = c.Parent

View File

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync"
"github.com/containers/common/pkg/config" "github.com/containers/common/pkg/config"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
@ -19,11 +20,18 @@ const (
) )
var ( var (
PodmanOptions entities.PodmanConfig podmanOptions entities.PodmanConfig
podmanSync sync.Once
) )
// NewPodmanConfig creates a PodmanConfig from the environment // PodmanConfig returns an entities.PodmanConfig built up from
func NewPodmanConfig() entities.PodmanConfig { // environment and CLI
func PodmanConfig() *entities.PodmanConfig {
podmanSync.Do(newPodmanConfig)
return &podmanOptions
}
func newPodmanConfig() {
if err := setXdgDirs(); err != nil { if err := setXdgDirs(); err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
@ -63,7 +71,7 @@ func NewPodmanConfig() entities.PodmanConfig {
cfg.Network.NetworkConfigDir = "" cfg.Network.NetworkConfigDir = ""
} }
return entities.PodmanConfig{Config: cfg, EngineMode: mode} podmanOptions = entities.PodmanConfig{Config: cfg, EngineMode: mode}
} }
// SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set. // SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set.

View File

@ -49,8 +49,8 @@ func ImageEngine() entities.ImageEngine {
// NewImageEngine is a wrapper for building an ImageEngine to be used for PreRunE functions // NewImageEngine is a wrapper for building an ImageEngine to be used for PreRunE functions
func NewImageEngine(cmd *cobra.Command, args []string) (entities.ImageEngine, error) { func NewImageEngine(cmd *cobra.Command, args []string) (entities.ImageEngine, error) {
if imageEngine == nil { if imageEngine == nil {
PodmanOptions.FlagSet = cmd.Flags() podmanOptions.FlagSet = cmd.Flags()
engine, err := infra.NewImageEngine(PodmanOptions) engine, err := infra.NewImageEngine(&podmanOptions)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -66,8 +66,8 @@ func ContainerEngine() entities.ContainerEngine {
// NewContainerEngine is a wrapper for building an ContainerEngine to be used for PreRunE functions // NewContainerEngine is a wrapper for building an ContainerEngine to be used for PreRunE functions
func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEngine, error) { func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEngine, error) {
if containerEngine == nil { if containerEngine == nil {
PodmanOptions.FlagSet = cmd.Flags() podmanOptions.FlagSet = cmd.Flags()
engine, err := infra.NewContainerEngine(PodmanOptions) engine, err := infra.NewContainerEngine(&podmanOptions)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -101,7 +101,7 @@ func Context() context.Context {
} }
func ContextWithOptions(ctx context.Context) context.Context { func ContextWithOptions(ctx context.Context) context.Context {
cliCtx = context.WithValue(ctx, PodmanOptionsKey{}, PodmanOptions) cliCtx = context.WithValue(ctx, PodmanOptionsKey{}, podmanOptions)
return cliCtx return cliCtx
} }

View File

@ -5,5 +5,5 @@ import (
) )
func IsRemote() bool { func IsRemote() bool {
return PodmanOptions.EngineMode == entities.TunnelMode return podmanOptions.EngineMode == entities.TunnelMode
} }

View File

@ -77,7 +77,7 @@ func init() {
syslogHook, syslogHook,
) )
rootFlags(registry.PodmanOptions, rootCmd.PersistentFlags()) rootFlags(registry.PodmanConfig(), rootCmd.PersistentFlags())
} }
func Execute() { func Execute() {
@ -98,9 +98,7 @@ func persistentPreRunE(cmd *cobra.Command, args []string) error {
// TODO: Remove trace statement in podman V2.1 // TODO: Remove trace statement in podman V2.1
logrus.Debugf("Called %s.PersistentPreRunE()", cmd.Name()) logrus.Debugf("Called %s.PersistentPreRunE()", cmd.Name())
// Update PodmanOptions now that we "know" more cfg := registry.PodmanConfig()
// TODO: pass in path overriding configuration file
registry.PodmanOptions = registry.NewPodmanConfig()
// Prep the engines // Prep the engines
if _, err := registry.NewImageEngine(cmd, args); err != nil { if _, err := registry.NewImageEngine(cmd, args); err != nil {
@ -111,10 +109,10 @@ func persistentPreRunE(cmd *cobra.Command, args []string) error {
} }
if cmd.Flag("cpu-profile").Changed { if cmd.Flag("cpu-profile").Changed {
f, err := os.Create(registry.PodmanOptions.CpuProfile) f, err := os.Create(cfg.CpuProfile)
if err != nil { if err != nil {
return errors.Wrapf(err, "unable to create cpu profiling file %s", return errors.Wrapf(err, "unable to create cpu profiling file %s",
registry.PodmanOptions.CpuProfile) cfg.CpuProfile)
} }
if err := pprof.StartCPUProfile(f); err != nil { if err := pprof.StartCPUProfile(f); err != nil {
return err return err
@ -124,11 +122,11 @@ func persistentPreRunE(cmd *cobra.Command, args []string) error {
if cmd.Flag("trace").Changed { if cmd.Flag("trace").Changed {
tracer, closer := tracing.Init("podman") tracer, closer := tracing.Init("podman")
opentracing.SetGlobalTracer(tracer) opentracing.SetGlobalTracer(tracer)
registry.PodmanOptions.SpanCloser = closer cfg.SpanCloser = closer
registry.PodmanOptions.Span = tracer.StartSpan("before-context") cfg.Span = tracer.StartSpan("before-context")
registry.PodmanOptions.SpanCtx = opentracing.ContextWithSpan(registry.Context(), registry.PodmanOptions.Span) cfg.SpanCtx = opentracing.ContextWithSpan(registry.Context(), cfg.Span)
opentracing.StartSpanFromContext(registry.PodmanOptions.SpanCtx, cmd.Name()) opentracing.StartSpanFromContext(cfg.SpanCtx, cmd.Name())
} }
// Setup Rootless environment, IFF: // Setup Rootless environment, IFF:
@ -149,12 +147,13 @@ func persistentPostRunE(cmd *cobra.Command, args []string) error {
// TODO: Remove trace statement in podman V2.1 // TODO: Remove trace statement in podman V2.1
logrus.Debugf("Called %s.PersistentPostRunE()", cmd.Name()) logrus.Debugf("Called %s.PersistentPostRunE()", cmd.Name())
cfg := registry.PodmanConfig()
if cmd.Flag("cpu-profile").Changed { if cmd.Flag("cpu-profile").Changed {
pprof.StopCPUProfile() pprof.StopCPUProfile()
} }
if cmd.Flag("trace").Changed { if cmd.Flag("trace").Changed {
registry.PodmanOptions.Span.Finish() cfg.Span.Finish()
registry.PodmanOptions.SpanCloser.Close() cfg.SpanCloser.Close()
} }
return nil return nil
} }
@ -199,7 +198,7 @@ func syslogHook() {
} }
} }
func rootFlags(opts entities.PodmanConfig, flags *pflag.FlagSet) { func rootFlags(opts *entities.PodmanConfig, flags *pflag.FlagSet) {
// V2 flags // V2 flags
flags.StringVarP(&opts.Uri, "remote", "r", "", "URL to access Podman service") flags.StringVarP(&opts.Uri, "remote", "r", "", "URL to access Podman service")
flags.StringSliceVar(&opts.Identities, "identity", []string{}, "path to SSH identity file") flags.StringSliceVar(&opts.Identities, "identity", []string{}, "path to SSH identity file")

View File

@ -12,7 +12,7 @@ import (
) )
// NewContainerEngine factory provides a libpod runtime for container-related operations // NewContainerEngine factory provides a libpod runtime for container-related operations
func NewContainerEngine(facts entities.PodmanConfig) (entities.ContainerEngine, error) { func NewContainerEngine(facts *entities.PodmanConfig) (entities.ContainerEngine, error) {
switch facts.EngineMode { switch facts.EngineMode {
case entities.ABIMode: case entities.ABIMode:
r, err := NewLibpodRuntime(facts.FlagSet, facts) r, err := NewLibpodRuntime(facts.FlagSet, facts)
@ -25,7 +25,7 @@ func NewContainerEngine(facts entities.PodmanConfig) (entities.ContainerEngine,
} }
// NewContainerEngine factory provides a libpod runtime for image-related operations // NewContainerEngine factory provides a libpod runtime for image-related operations
func NewImageEngine(facts entities.PodmanConfig) (entities.ImageEngine, error) { func NewImageEngine(facts *entities.PodmanConfig) (entities.ImageEngine, error) {
switch facts.EngineMode { switch facts.EngineMode {
case entities.ABIMode: case entities.ABIMode:
r, err := NewLibpodImageRuntime(facts.FlagSet, facts) r, err := NewLibpodImageRuntime(facts.FlagSet, facts)

View File

@ -12,7 +12,7 @@ import (
// ContainerEngine Image Proxy will be EOL'ed after podman is separated from libpod repo // ContainerEngine Image Proxy will be EOL'ed after podman is separated from libpod repo
func NewLibpodImageRuntime(flags *pflag.FlagSet, opts entities.PodmanConfig) (entities.ImageEngine, error) { func NewLibpodImageRuntime(flags *pflag.FlagSet, opts *entities.PodmanConfig) (entities.ImageEngine, error) {
r, err := GetRuntime(context.Background(), flags, opts) r, err := GetRuntime(context.Background(), flags, opts)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -24,11 +24,11 @@ type engineOpts struct {
migrate bool migrate bool
noStore bool noStore bool
withFDS bool withFDS bool
config entities.PodmanConfig config *entities.PodmanConfig
} }
// GetRuntimeMigrate gets a libpod runtime that will perform a migration of existing containers // GetRuntimeMigrate gets a libpod runtime that will perform a migration of existing containers
func GetRuntimeMigrate(ctx context.Context, fs *flag.FlagSet, cfg entities.PodmanConfig, newRuntime string) (*libpod.Runtime, error) { func GetRuntimeMigrate(ctx context.Context, fs *flag.FlagSet, cfg *entities.PodmanConfig, newRuntime string) (*libpod.Runtime, error) {
return getRuntime(ctx, fs, &engineOpts{ return getRuntime(ctx, fs, &engineOpts{
name: newRuntime, name: newRuntime,
renumber: false, renumber: false,
@ -40,7 +40,7 @@ func GetRuntimeMigrate(ctx context.Context, fs *flag.FlagSet, cfg entities.Podma
} }
// GetRuntimeDisableFDs gets a libpod runtime that will disable sd notify // GetRuntimeDisableFDs gets a libpod runtime that will disable sd notify
func GetRuntimeDisableFDs(ctx context.Context, fs *flag.FlagSet, cfg entities.PodmanConfig) (*libpod.Runtime, error) { func GetRuntimeDisableFDs(ctx context.Context, fs *flag.FlagSet, cfg *entities.PodmanConfig) (*libpod.Runtime, error) {
return getRuntime(ctx, fs, &engineOpts{ return getRuntime(ctx, fs, &engineOpts{
renumber: false, renumber: false,
migrate: false, migrate: false,
@ -51,7 +51,7 @@ func GetRuntimeDisableFDs(ctx context.Context, fs *flag.FlagSet, cfg entities.Po
} }
// GetRuntimeRenumber gets a libpod runtime that will perform a lock renumber // GetRuntimeRenumber gets a libpod runtime that will perform a lock renumber
func GetRuntimeRenumber(ctx context.Context, fs *flag.FlagSet, cfg entities.PodmanConfig) (*libpod.Runtime, error) { func GetRuntimeRenumber(ctx context.Context, fs *flag.FlagSet, cfg *entities.PodmanConfig) (*libpod.Runtime, error) {
return getRuntime(ctx, fs, &engineOpts{ return getRuntime(ctx, fs, &engineOpts{
renumber: true, renumber: true,
migrate: false, migrate: false,
@ -62,7 +62,7 @@ func GetRuntimeRenumber(ctx context.Context, fs *flag.FlagSet, cfg entities.Podm
} }
// GetRuntime generates a new libpod runtime configured by command line options // GetRuntime generates a new libpod runtime configured by command line options
func GetRuntime(ctx context.Context, flags *flag.FlagSet, cfg entities.PodmanConfig) (*libpod.Runtime, error) { func GetRuntime(ctx context.Context, flags *flag.FlagSet, cfg *entities.PodmanConfig) (*libpod.Runtime, error) {
return getRuntime(ctx, flags, &engineOpts{ return getRuntime(ctx, flags, &engineOpts{
renumber: false, renumber: false,
migrate: false, migrate: false,
@ -73,7 +73,7 @@ func GetRuntime(ctx context.Context, flags *flag.FlagSet, cfg entities.PodmanCon
} }
// GetRuntimeNoStore generates a new libpod runtime configured by command line options // GetRuntimeNoStore generates a new libpod runtime configured by command line options
func GetRuntimeNoStore(ctx context.Context, fs *flag.FlagSet, cfg entities.PodmanConfig) (*libpod.Runtime, error) { func GetRuntimeNoStore(ctx context.Context, fs *flag.FlagSet, cfg *entities.PodmanConfig) (*libpod.Runtime, error) {
return getRuntime(ctx, fs, &engineOpts{ return getRuntime(ctx, fs, &engineOpts{
renumber: false, renumber: false,
migrate: false, migrate: false,
@ -160,7 +160,7 @@ func getRuntime(ctx context.Context, fs *flag.FlagSet, opts *engineOpts) (*libpo
} }
if fs.Changed("runtime") { if fs.Changed("runtime") {
options = append(options, libpod.WithOCIRuntime(cfg.Engine.OCIRuntime)) options = append(options, libpod.WithOCIRuntime(cfg.RuntimePath))
} }
if fs.Changed("conmon") { if fs.Changed("conmon") {

View File

@ -12,7 +12,7 @@ import (
// ContainerEngine Proxy will be EOL'ed after podman is separated from libpod repo // ContainerEngine Proxy will be EOL'ed after podman is separated from libpod repo
func NewLibpodRuntime(flags *flag.FlagSet, opts entities.PodmanConfig) (entities.ContainerEngine, error) { func NewLibpodRuntime(flags *flag.FlagSet, opts *entities.PodmanConfig) (entities.ContainerEngine, error) {
r, err := GetRuntime(context.Background(), flags, opts) r, err := GetRuntime(context.Background(), flags, opts)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -11,7 +11,7 @@ import (
"github.com/containers/libpod/pkg/domain/infra/tunnel" "github.com/containers/libpod/pkg/domain/infra/tunnel"
) )
func NewContainerEngine(facts entities.PodmanConfig) (entities.ContainerEngine, error) { func NewContainerEngine(facts *entities.PodmanConfig) (entities.ContainerEngine, error) {
switch facts.EngineMode { switch facts.EngineMode {
case entities.ABIMode: case entities.ABIMode:
return nil, fmt.Errorf("direct runtime not supported") return nil, fmt.Errorf("direct runtime not supported")
@ -23,7 +23,7 @@ func NewContainerEngine(facts entities.PodmanConfig) (entities.ContainerEngine,
} }
// NewImageEngine factory provides a libpod runtime for image-related operations // NewImageEngine factory provides a libpod runtime for image-related operations
func NewImageEngine(facts entities.PodmanConfig) (entities.ImageEngine, error) { func NewImageEngine(facts *entities.PodmanConfig) (entities.ImageEngine, error) {
switch facts.EngineMode { switch facts.EngineMode {
case entities.ABIMode: case entities.ABIMode:
return nil, fmt.Errorf("direct image runtime not supported") return nil, fmt.Errorf("direct image runtime not supported")