Skip to content

Commit

Permalink
enh: PR feedback II
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-petersen committed Nov 30, 2022
1 parent eedf364 commit faa9990
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 368 deletions.
35 changes: 11 additions & 24 deletions internal/gardenclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
openstackv1alpha1 "github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack/v1alpha1"
gardencore "github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
operationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1"
gardenoperationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -82,12 +82,10 @@ type Client interface {
// GetShootOfManagedSeed returns shoot of seed using ManagedSeed resource, nil if not a managed seed
GetShootOfManagedSeed(ctx context.Context, name string) (*seedmanagementv1alpha1.Shoot, error)

// GetBastion returns a Gardener bastion resource in a namespace by name
GetBastion(ctx context.Context, namespace, name string) (*operationsv1alpha1.Bastion, error)
// ListBastions returns all Gardener bastion resources, filtered by a list option
ListBastions(ctx context.Context, opts ...client.ListOption) (*operationsv1alpha1.BastionList, error)
ListBastions(ctx context.Context, opts ...client.ListOption) (*gardenoperationsv1alpha1.BastionList, error)
// PatchBastion patches an existing bastion to match newBastion using the merge patch strategy
PatchBastion(ctx context.Context, newBastion, oldBastion *operationsv1alpha1.Bastion) error
PatchBastion(ctx context.Context, newBastion, oldBastion *gardenoperationsv1alpha1.Bastion) error

// Creates a token review for a user with token authentication
CreateTokenReview(ctx context.Context, token string) (*authenticationv1.TokenReview, error)
Expand Down Expand Up @@ -294,19 +292,8 @@ func (g *clientImpl) GetShootOfManagedSeed(ctx context.Context, name string) (*s
return managedSeed.Spec.Shoot, nil
}

func (g *clientImpl) GetBastion(ctx context.Context, namespace, name string) (*operationsv1alpha1.Bastion, error) {
bastion := &operationsv1alpha1.Bastion{}
key := types.NamespacedName{Namespace: namespace, Name: name}

if err := g.c.Get(ctx, key, bastion); err != nil {
return nil, fmt.Errorf("failed to get bastion %v: %w", key, err)
}

return bastion, nil
}

func (g *clientImpl) ListBastions(ctx context.Context, opts ...client.ListOption) (*operationsv1alpha1.BastionList, error) {
bastionList := &operationsv1alpha1.BastionList{}
func (g *clientImpl) ListBastions(ctx context.Context, opts ...client.ListOption) (*gardenoperationsv1alpha1.BastionList, error) {
bastionList := &gardenoperationsv1alpha1.BastionList{}

if err := g.resolveListOptions(ctx, opts...); err != nil {
return nil, err
Expand All @@ -319,7 +306,7 @@ func (g *clientImpl) ListBastions(ctx context.Context, opts ...client.ListOption
return bastionList, nil
}

func (g *clientImpl) PatchBastion(ctx context.Context, newBastion, oldBastion *operationsv1alpha1.Bastion) error {
func (g *clientImpl) PatchBastion(ctx context.Context, newBastion, oldBastion *gardenoperationsv1alpha1.Bastion) error {
return g.c.Patch(ctx, newBastion, client.MergeFrom(oldBastion))
}

Expand Down Expand Up @@ -426,8 +413,8 @@ func (cp CloudProfile) GetOpenstackProviderConfig() (*openstackv1alpha1.CloudPro
return cloudProfileConfig, nil
}

// ShootFilter restricts the list operation to the given where condition.
type ShootFilter fields.Set
// ProjectFilter restricts the list operation to the given where condition.
type ProjectFilter fields.Set

type resolver interface {
resolve(context.Context, Client) error
Expand All @@ -438,9 +425,9 @@ type listOptionResolver interface {
resolver
}

var _ listOptionResolver = &ShootFilter{}
var _ listOptionResolver = &ProjectFilter{}

func (w ShootFilter) ApplyToList(opts *client.ListOptions) {
func (w ProjectFilter) ApplyToList(opts *client.ListOptions) {
m := fields.Set{}

for key, value := range w {
Expand All @@ -457,7 +444,7 @@ func (w ShootFilter) ApplyToList(opts *client.ListOptions) {
}
}

func (w ShootFilter) resolve(ctx context.Context, g Client) error {
func (w ProjectFilter) resolve(ctx context.Context, g Client) error {
if name, ok := w["project"]; ok {
delete(w, "project")

Expand Down
24 changes: 20 additions & 4 deletions pkg/cmd/ssh/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"time"

"k8s.io/client-go/tools/clientcmd/api"

"github.com/gardener/gardenctl-v2/internal/util"
"github.com/gardener/gardenctl-v2/pkg/cmd/base"
)
Expand Down Expand Up @@ -48,6 +46,16 @@ func SetKeepAliveInterval(d time.Duration) {
keepAliveInterval = d
}

type TestSSHPatchUtils struct {
sshPatchUtils
}

func NewTestSSHPatchUtils() *TestSSHPatchUtils {
return &TestSSHPatchUtils{
sshPatchUtils: &sshPatchUtilsImpl{},
}
}

type TestSSHPatchOptions struct {
sshPatchOptions
Out *util.SafeBytesBuffer
Expand All @@ -71,6 +79,14 @@ func NewTestSSHPatchOptions() *TestSSHPatchOptions {
}
}

func (o *TestSSHPatchOptions) GetAuthInfo() (*api.AuthInfo, error) {
return o.getAuthInfo()
type TestSSHPatchCompletions struct {
sshPatchCompletions
}

func NewTestSSHPatchCompletions() *TestSSHPatchCompletions {
return &TestSSHPatchCompletions{
sshPatchCompletions: sshPatchCompletions{
Utils: &sshPatchUtilsImpl{},
},
}
}
50 changes: 49 additions & 1 deletion pkg/cmd/ssh/mocks/mock_ssh_patch_utils.go

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

4 changes: 3 additions & 1 deletion pkg/cmd/ssh/ssh_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func NewCmdSSHPatch(f util.Factory, ioStreams util.IOStreams) *cobra.Command {
o := newSSHPatchOptions(ioStreams)

cmd := &cobra.Command{
Use: "ssh-patch [BASTION_NAME]",
Short: "Update a bastion host previously created through the ssh command",
Expand All @@ -26,7 +27,8 @@ func NewCmdSSHPatch(f util.Factory, ioStreams util.IOStreams) *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
}

bastionNames, err := o.GetBastionNameCompletions(f, cmd, toComplete)
c := newSSHPatchCompletions()
bastionNames, err := c.GetBastionNameCompletions(f, cmd, toComplete)
if err != nil {
fmt.Fprintln(o.IOStreams.ErrOut, err.Error())
return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
Loading

0 comments on commit faa9990

Please sign in to comment.