Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh: Add output flag #258

Merged
merged 10 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/help/gardenctl_ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gardenctl ssh [NODE_NAME] [flags]
--interactive Open an SSH connection instead of just providing the bastion host (only if NODE_NAME is provided). (default true)
--keep-bastion Do not delete immediately when gardenctl exits (Bastions will be garbage-collected after some time)
--no-keepalive Exit after the bastion host became available without keeping the bastion alive or establishing an SSH connection. Note that this flag requires the flags --interactive=false and --keep-bastion to be set
-o, --output string One of 'yaml' or 'json'.
--project string target the given project
--public-key-file string Path to the file that contains a public SSH key. If not given, a temporary keypair will be generated.
--seed string target the given seed cluster
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
golang.org/x/crypto v0.7.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.25.0
k8s.io/apimachinery v0.25.0
k8s.io/apiserver v0.25.0
Expand All @@ -27,6 +26,7 @@ require (
k8s.io/klog/v2 v2.70.1
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
sigs.k8s.io/controller-runtime v0.13.0
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -119,6 +119,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
istio.io/api v0.0.0-20221013011440-bc935762d2b9 // indirect
istio.io/client-go v1.15.3 // indirect
k8s.io/apiextensions-apiserver v0.25.0 // indirect
Expand All @@ -131,7 +132,6 @@ require (
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace k8s.io/client-go => k8s.io/client-go v0.25.0
14 changes: 7 additions & 7 deletions pkg/ac/access_restriction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ import (
// AccessRestriction is used to define an access restriction.
type AccessRestriction struct {
// Key is the identifier of an access restriction
Key string `yaml:"key,omitempty" json:"key,omitempty"`
Key string `json:"key,omitempty"`
// NotifyIf controls which value the annotation must have for a notification to be sent
NotifyIf bool `yaml:"notifyIf,omitempty" json:"notifyIf,omitempty"`
NotifyIf bool `json:"notifyIf,omitempty"`
// Msg is the notification text that is sent
Msg string `yaml:"msg,omitempty" json:"msg,omitempty"`
Msg string `json:"msg,omitempty"`
// Options is a list of access restriction options
Options []AccessRestrictionOption `yaml:"options,omitempty" json:"options,omitempty"`
Options []AccessRestrictionOption `json:"options,omitempty"`
}

// AccessRestrictionOption is used to define an access restriction option.
type AccessRestrictionOption struct {
// Key is the identifier of an access restriction option
Key string `yaml:"key,omitempty" json:"key,omitempty"`
Key string `json:"key,omitempty"`
// NotifyIf controls which value the annotation must have for a notification to be sent
NotifyIf bool `yaml:"notifyIf,omitempty" json:"notifyIf,omitempty"`
NotifyIf bool `json:"notifyIf,omitempty"`
// Msg is the notification text that is sent
Msg string `yaml:"msg,omitempty" json:"msg,omitempty"`
Msg string `json:"msg,omitempty"`
}

// AccessRestrictionHandler is a function that should display a single AccessRestrictionMessage to the user.
Expand Down
18 changes: 9 additions & 9 deletions pkg/cmd/base/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
petersutter marked this conversation as resolved.
Show resolved Hide resolved

"github.com/gardener/gardenctl-v2/internal/util"
)
Expand Down Expand Up @@ -72,18 +72,18 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
func (o *Options) PrintObject(obj interface{}) error {
switch o.Output {
case "":
fmt.Fprintf(o.IOStreams.Out, "%v", obj)

if _, ok := obj.(fmt.Stringer); ok {
fmt.Fprintf(o.IOStreams.Out, "%s", obj)
} else {
fmt.Fprintf(o.IOStreams.Out, "%v", obj)
}
case "yaml":
yamlEncoder := yaml.NewEncoder(o.IOStreams.Out)
defer yamlEncoder.Close()

yamlEncoder.SetIndent(2)

err := yamlEncoder.Encode(&obj)
marshalled, err := yaml.Marshal(&obj)
if err != nil {
return err
}

fmt.Fprintln(o.IOStreams.Out, string(marshalled))
case "json":
marshalled, err := json.MarshalIndent(&obj, "", " ")
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions pkg/cmd/base/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
var _ = Describe("Base Options", func() {
Describe("given an instance", func() {
type barType struct {
Baz string
Baz string `json:"baz"`
}

type fooType struct {
Foo string
Bar barType
Foo string `json:"foo"`
Bar barType `json:"bar"`
}

var (
Expand Down Expand Up @@ -78,9 +78,9 @@ var _ = Describe("Base Options", func() {
Expect(options.PrintObject(foo)).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf(
`{
"Foo": %q,
"Bar": {
"Baz": %q
"foo": %q,
"bar": {
"baz": %q
}
}
`, foo.Foo, foo.Bar.Baz)))
Expand All @@ -99,11 +99,12 @@ var _ = Describe("Base Options", func() {
It("should print with yaml format", func() {
Expect(options.PrintObject(foo)).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf(
`foo: %s
bar:
`bar:
baz: %s
foo: %s

`,
foo.Foo, foo.Bar.Baz)))
foo.Bar.Baz, foo.Foo)))
})
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"

cmdconfig "github.com/gardener/gardenctl-v2/pkg/cmd/config"
"github.com/gardener/gardenctl-v2/pkg/config"
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/ssh/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
"context"
"os"
"time"

operationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func SetBastionAvailabilityChecker(f func(hostname string, privateKey []byte) error) {
Expand Down Expand Up @@ -46,6 +43,6 @@ func SetKeepAliveInterval(d time.Duration) {
keepAliveInterval = d
}

func SetWaitForSignal(f func(ctx context.Context, o *SSHOptions, shootClient client.Client, bastion *operationsv1alpha1.Bastion, nodeHostname string, nodePrivateKeyFiles []string, signalChan <-chan struct{}) error) {
func SetWaitForSignal(f func(ctx context.Context, o *SSHOptions, signalChan <-chan struct{})) {
waitForSignal = f
}
Loading