Skip to content

Commit

Permalink
enh: target cmd - only register target flags with higher specificty (#…
Browse files Browse the repository at this point in the history
…206)

* enh: target cmd - only register target flags with higher specificty

* expose TargetFlags on factory

* PR feedback
  • Loading branch information
sven-petersen authored Jan 20, 2023
1 parent 1846bdd commit ccadb48
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 79 deletions.
13 changes: 6 additions & 7 deletions docs/help/gardenctl_target_control-plane.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ gardenctl target control-plane --garden my-garden --project my-project --shoot m
### Options

```
-h, --help help for control-plane
-o, --output string One of 'yaml' or 'json'.
--garden string target the given garden cluster
-h, --help help for control-plane
-o, --output string One of 'yaml' or 'json'.
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
```

### Options inherited from parent commands
Expand All @@ -33,17 +37,12 @@ gardenctl target control-plane --garden my-garden --project my-project --shoot m
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
5 changes: 0 additions & 5 deletions docs/help/gardenctl_target_garden.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ gardenctl target garden my-garden
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
6 changes: 1 addition & 5 deletions docs/help/gardenctl_target_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gardenctl target project my-project --garden my-garden
### Options

```
--garden string target the given garden cluster
-h, --help help for project
-o, --output string One of 'yaml' or 'json'.
```
Expand All @@ -33,17 +34,12 @@ gardenctl target project my-project --garden my-garden
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
6 changes: 1 addition & 5 deletions docs/help/gardenctl_target_seed.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gardenctl target seed my-seed --garden my-garden
### Options

```
--garden string target the given garden cluster
-h, --help help for seed
-o, --output string One of 'yaml' or 'json'.
```
Expand All @@ -33,17 +34,12 @@ gardenctl target seed my-seed --garden my-garden
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
12 changes: 5 additions & 7 deletions docs/help/gardenctl_target_shoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ gardenctl target shoot my-shoot --garden my-garden --project my-project
### Options

```
-h, --help help for shoot
-o, --output string One of 'yaml' or 'json'.
--garden string target the given garden cluster
-h, --help help for shoot
-o, --output string One of 'yaml' or 'json'.
--project string target the given project
--seed string target the given seed cluster
```

### Options inherited from parent commands
Expand All @@ -33,17 +36,12 @@ gardenctl target shoot my-shoot --garden my-garden --project my-project
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
5 changes: 0 additions & 5 deletions docs/help/gardenctl_target_unset.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@ gardenctl target unset garden
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
5 changes: 0 additions & 5 deletions docs/help/gardenctl_target_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ gardenctl target view [flags]
--add-dir-header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
--config string config file (default is ~/.garden/gardenctl-v2.yaml)
--control-plane target control plane of shoot, use together with shoot argument
--garden string target the given garden cluster
--log-backtrace-at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log-dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log-file string If non-empty, use this log file (no effect when -logtostderr=true)
--log-file-max-size uint Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
--logtostderr log to standard error instead of files (default true)
--one-output If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)
--project string target the given project
--seed string target the given seed cluster
--shoot string target the given shoot cluster
--skip-headers If true, avoid header prefixes in the log messages
--skip-log-headers If true, avoid headers when opening log files (no effect when -logtostderr=true)
--stderrthreshold severity logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=false) (default 2)
Expand Down
8 changes: 8 additions & 0 deletions internal/fake/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
}
19 changes: 16 additions & 3 deletions internal/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 NewFactoryImpl() *FactoryImpl {
return &FactoryImpl{
targetFlags: target.NewTargetFlags("", "", "", "", false),
}
}

func (f *FactoryImpl) Context() context.Context {
return context.Background()
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 15 additions & 2 deletions internal/util/mocks/mock_factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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.NewFactoryImpl()
ioStreams := util.NewIOStreams()

return NewGardenctlCommand(&factory, ioStreams)
return NewGardenctlCommand(factory, ioStreams)
}

// NewGardenctlCommand creates the `gardenctl` command.
Expand Down
Loading

0 comments on commit ccadb48

Please sign in to comment.