From 461fd9eca60b7a11777bd4ffb98c83b4232705da Mon Sep 17 00:00:00 2001 From: Sven Petersen <5971734+sven-petersen@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:17:15 +0100 Subject: [PATCH] expose TargetFlags on factory --- internal/fake/factory.go | 8 ++++++++ internal/util/factory.go | 19 ++++++++++++++++--- internal/util/mocks/mock_factory.go | 17 +++++++++++++++-- pkg/cmd/cmd.go | 7 ++----- pkg/cmd/cmd_test.go | 16 +++++----------- pkg/cmd/target/control-plane.go | 11 ++++------- pkg/cmd/target/project.go | 5 +---- pkg/cmd/target/seed.go | 5 +---- pkg/cmd/target/shoot.go | 9 +++------ pkg/cmd/target/target.go | 5 +---- pkg/flags/target_test.go | 10 ++-------- 11 files changed, 58 insertions(+), 54 deletions(-) diff --git a/internal/fake/factory.go b/internal/fake/factory.go index 2469b883..2aee2dc4 100644 --- a/internal/fake/factory.go +++ b/internal/fake/factory.go @@ -30,6 +30,7 @@ type Factory struct { Config *config.Config ClientProviderImpl target.ClientProvider TargetProviderImpl target.TargetProvider + TargetFlagsImpl target.TargetFlags // Override the clock implementation. Will use a real clock if not set. ClockImpl util.Clock @@ -58,11 +59,14 @@ func NewFakeFactory(cfg *config.Config, clock util.Clock, clientProvider target. clock = &util.RealClock{} } + targetFlags := target.NewTargetFlags("", "", "", "", false) + return &Factory{ Config: cfg, ClockImpl: clock, ClientProviderImpl: clientProvider, TargetProviderImpl: targetProvider, + TargetFlagsImpl: targetFlags, } } @@ -95,3 +99,7 @@ func (f *Factory) Clock() util.Clock { func (f *Factory) PublicIPs(ctx context.Context) ([]string, error) { return []string{"192.0.2.42", "2001:db8::8a2e:370:7334"}, nil } + +func (f *Factory) TargetFlags() target.TargetFlags { + return f.TargetFlagsImpl +} diff --git a/internal/util/factory.go b/internal/util/factory.go index 1555db7e..643f33b3 100644 --- a/internal/util/factory.go +++ b/internal/util/factory.go @@ -48,6 +48,9 @@ type Factory interface { // returned slice can contain IPv6, IPv4 or both, in no particular // order. PublicIPs(context.Context) ([]string, error) + // TargetFlags returns the TargetFlags to which the cobra flags are bound allowing the user to + // override the target configuration stored on the filesystem. + TargetFlags() target.TargetFlags } // FactoryImpl implements util.Factory interface. @@ -63,13 +66,19 @@ type FactoryImpl struct { // if empty. ConfigFile string - // TargetFlags can be used to completely override the target configuration + // targetFlags can be used to completely override the target configuration // stored on the filesystem via a CLI flags. - TargetFlags target.TargetFlags + targetFlags target.TargetFlags } var _ Factory = &FactoryImpl{} +func NewFactory() *FactoryImpl { + return &FactoryImpl{ + targetFlags: target.NewTargetFlags("", "", "", "", false), + } +} + func (f *FactoryImpl) Context() context.Context { return context.Background() } @@ -92,7 +101,7 @@ func (f *FactoryImpl) Manager() (target.Manager, error) { return nil, fmt.Errorf("failed to create session directory: %w", err) } - targetProvider := target.NewTargetProvider(filepath.Join(sessionDirectory, "target.yaml"), f.TargetFlags) + targetProvider := target.NewTargetProvider(filepath.Join(sessionDirectory, "target.yaml"), f.targetFlags) clientProvider := target.NewClientProvider() return target.NewManager(cfg, targetProvider, clientProvider, sessionDirectory) @@ -125,6 +134,10 @@ func (f *FactoryImpl) PublicIPs(ctx context.Context) ([]string, error) { return addresses, nil } +func (f *FactoryImpl) TargetFlags() target.TargetFlags { + return f.targetFlags +} + func callIPify(ctx context.Context, domain string) (*net.IP, error) { req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/", domain), nil) if err != nil { diff --git a/internal/util/mocks/mock_factory.go b/internal/util/mocks/mock_factory.go index 57dbab9a..a56e9a06 100644 --- a/internal/util/mocks/mock_factory.go +++ b/internal/util/mocks/mock_factory.go @@ -8,10 +8,9 @@ import ( context "context" reflect "reflect" - gomock "github.com/golang/mock/gomock" - util "github.com/gardener/gardenctl-v2/internal/util" target "github.com/gardener/gardenctl-v2/pkg/target" + gomock "github.com/golang/mock/gomock" ) // MockFactory is a mock of Factory interface. @@ -108,3 +107,17 @@ func (mr *MockFactoryMockRecorder) PublicIPs(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublicIPs", reflect.TypeOf((*MockFactory)(nil).PublicIPs), arg0) } + +// TargetFlags mocks base method. +func (m *MockFactory) TargetFlags() target.TargetFlags { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "TargetFlags") + ret0, _ := ret[0].(target.TargetFlags) + return ret0 +} + +// TargetFlags indicates an expected call of TargetFlags. +func (mr *MockFactoryMockRecorder) TargetFlags() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TargetFlags", reflect.TypeOf((*MockFactory)(nil).TargetFlags)) +} diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 76b8112f..ae6f44ef 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -27,7 +27,6 @@ import ( cmdsshpatch "github.com/gardener/gardenctl-v2/pkg/cmd/sshpatch" cmdtarget "github.com/gardener/gardenctl-v2/pkg/cmd/target" cmdversion "github.com/gardener/gardenctl-v2/pkg/cmd/version" - "github.com/gardener/gardenctl-v2/pkg/target" ) const ( @@ -52,12 +51,10 @@ func Execute() { // NewDefaultGardenctlCommand creates the `gardenctl` command with defaults. func NewDefaultGardenctlCommand() *cobra.Command { - factory := util.FactoryImpl{ - TargetFlags: target.NewTargetFlags("", "", "", "", false), - } + factory := util.NewFactory() ioStreams := util.NewIOStreams() - return NewGardenctlCommand(&factory, ioStreams) + return NewGardenctlCommand(factory, ioStreams) } // NewGardenctlCommand creates the `gardenctl` command. diff --git a/pkg/cmd/cmd_test.go b/pkg/cmd/cmd_test.go index d778a4e5..0ea4bb22 100644 --- a/pkg/cmd/cmd_test.go +++ b/pkg/cmd/cmd_test.go @@ -26,10 +26,9 @@ var _ = Describe("Gardenctl command", func() { ) var ( - gardenName string - streams util.IOStreams - out *util.SafeBytesBuffer - targetFlags target.TargetFlags + gardenName string + streams util.IOStreams + out *util.SafeBytesBuffer ) BeforeEach(func() { @@ -37,8 +36,6 @@ var _ = Describe("Gardenctl command", func() { streams, _, out, _ = util.NewTestIOStreams() - targetFlags = target.NewTargetFlags("", "", "", "", false) - targetProvider := target.NewTargetProvider(filepath.Join(sessionDir, "target.yaml"), nil) Expect(targetProvider.Write(target.NewTarget(gardenName, projectName, "", shootName))).To(Succeed()) }) @@ -47,10 +44,7 @@ var _ = Describe("Gardenctl command", func() { var factory *util.FactoryImpl BeforeEach(func() { - factory = &util.FactoryImpl{ - TargetFlags: targetFlags, - ConfigFile: configFile, - } + factory = util.NewFactory() }) Context("when running the completion command", func() { @@ -74,7 +68,7 @@ var _ = Describe("Gardenctl command", func() { // check target flags tf := manager.TargetFlags() - Expect(tf).To(BeIdenticalTo(factory.TargetFlags)) + Expect(tf).To(BeIdenticalTo(factory.TargetFlags())) // check current target values current, err := manager.CurrentTarget() diff --git a/pkg/cmd/target/control-plane.go b/pkg/cmd/target/control-plane.go index 73e03701..3edef7d4 100644 --- a/pkg/cmd/target/control-plane.go +++ b/pkg/cmd/target/control-plane.go @@ -8,7 +8,6 @@ package target import ( "github.com/spf13/cobra" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "github.com/gardener/gardenctl-v2/internal/util" "github.com/gardener/gardenctl-v2/pkg/cmd/base" @@ -38,12 +37,10 @@ gardenctl target control-plane --garden my-garden --project my-project --shoot m o.AddFlags(cmd.Flags()) - manager, err := f.Manager() - utilruntime.Must(err) - manager.TargetFlags().AddGardenFlag(cmd.Flags()) - manager.TargetFlags().AddProjectFlag(cmd.Flags()) - manager.TargetFlags().AddShootFlag(cmd.Flags()) - manager.TargetFlags().AddSeedFlag(cmd.Flags()) + f.TargetFlags().AddGardenFlag(cmd.Flags()) + f.TargetFlags().AddProjectFlag(cmd.Flags()) + f.TargetFlags().AddShootFlag(cmd.Flags()) + f.TargetFlags().AddSeedFlag(cmd.Flags()) flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags()) return cmd diff --git a/pkg/cmd/target/project.go b/pkg/cmd/target/project.go index 55457d10..1977db65 100644 --- a/pkg/cmd/target/project.go +++ b/pkg/cmd/target/project.go @@ -8,7 +8,6 @@ package target import ( "github.com/spf13/cobra" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "github.com/gardener/gardenctl-v2/internal/util" "github.com/gardener/gardenctl-v2/pkg/cmd/base" @@ -38,9 +37,7 @@ gardenctl target project my-project --garden my-garden`, o.AddFlags(cmd.Flags()) - manager, err := f.Manager() - utilruntime.Must(err) - manager.TargetFlags().AddGardenFlag(cmd.Flags()) + f.TargetFlags().AddGardenFlag(cmd.Flags()) flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags()) return cmd diff --git a/pkg/cmd/target/seed.go b/pkg/cmd/target/seed.go index 5aad2e9b..7075733e 100644 --- a/pkg/cmd/target/seed.go +++ b/pkg/cmd/target/seed.go @@ -8,7 +8,6 @@ package target import ( "github.com/spf13/cobra" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "github.com/gardener/gardenctl-v2/internal/util" "github.com/gardener/gardenctl-v2/pkg/cmd/base" @@ -38,9 +37,7 @@ gardenctl target seed my-seed --garden my-garden`, o.AddFlags(cmd.Flags()) - manager, err := f.Manager() - utilruntime.Must(err) - manager.TargetFlags().AddGardenFlag(cmd.Flags()) + f.TargetFlags().AddGardenFlag(cmd.Flags()) flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags()) return cmd diff --git a/pkg/cmd/target/shoot.go b/pkg/cmd/target/shoot.go index 9aa9dabb..8e41c13a 100644 --- a/pkg/cmd/target/shoot.go +++ b/pkg/cmd/target/shoot.go @@ -8,7 +8,6 @@ package target import ( "github.com/spf13/cobra" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "github.com/gardener/gardenctl-v2/internal/util" "github.com/gardener/gardenctl-v2/pkg/cmd/base" @@ -38,11 +37,9 @@ gardenctl target shoot my-shoot --garden my-garden --project my-project`, o.AddFlags(cmd.Flags()) - manager, err := f.Manager() - utilruntime.Must(err) - manager.TargetFlags().AddGardenFlag(cmd.Flags()) - manager.TargetFlags().AddProjectFlag(cmd.Flags()) - manager.TargetFlags().AddSeedFlag(cmd.Flags()) + f.TargetFlags().AddGardenFlag(cmd.Flags()) + f.TargetFlags().AddProjectFlag(cmd.Flags()) + f.TargetFlags().AddSeedFlag(cmd.Flags()) flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags()) return cmd diff --git a/pkg/cmd/target/target.go b/pkg/cmd/target/target.go index 563fde57..fe32a5d3 100644 --- a/pkg/cmd/target/target.go +++ b/pkg/cmd/target/target.go @@ -15,7 +15,6 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" "github.com/gardener/gardenctl-v2/internal/util" "github.com/gardener/gardenctl-v2/pkg/ac" @@ -52,9 +51,7 @@ gardenctl target value/that/matches/pattern --control-plane`, o.AddFlags(cmd.Flags()) - manager, err := f.Manager() - utilruntime.Must(err) - manager.TargetFlags().AddFlags(cmd.Flags()) + f.TargetFlags().AddFlags(cmd.Flags()) flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags()) return cmd diff --git a/pkg/flags/target_test.go b/pkg/flags/target_test.go index 14282192..12d19ae6 100644 --- a/pkg/flags/target_test.go +++ b/pkg/flags/target_test.go @@ -43,7 +43,6 @@ var _ = Describe("Target flags", func() { gardenName2 string streams util.IOStreams errOut *util.SafeBytesBuffer - targetFlags target.TargetFlags ) BeforeEach(func() { @@ -52,8 +51,6 @@ var _ = Describe("Target flags", func() { streams, _, _, errOut = util.NewTestIOStreams() - targetFlags = target.NewTargetFlags("", "", "", "", false) - targetProvider := target.NewTargetProvider(filepath.Join(sessionDir, "target.yaml"), nil) Expect(targetProvider.Write(target.NewTarget(gardenName1, projectName, "", shootName))).To(Succeed()) }) @@ -263,10 +260,7 @@ var _ = Describe("Target flags", func() { var factory *util.FactoryImpl BeforeEach(func() { - factory = &util.FactoryImpl{ - TargetFlags: targetFlags, - ConfigFile: configFile, - } + factory = util.NewFactory() }) Context("when wrapping completion functions", func() { @@ -327,7 +321,7 @@ var _ = Describe("Target flags", func() { // check target flags values tf := manager.TargetFlags() - Expect(tf).To(BeIdenticalTo(factory.TargetFlags)) + Expect(tf).To(BeIdenticalTo(factory.TargetFlags())) Expect(tf.GardenName()).To(BeEmpty()) Expect(tf.ProjectName()).To(BeEmpty()) Expect(tf.SeedName()).To(BeEmpty())