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

Add resolve command #306

Merged
merged 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
GetShootOfManagedSeed should return error
  • Loading branch information
petersutter committed May 31, 2023
commit ac8fae15b57d862f344864d5a1ec306f90f7094e
29 changes: 20 additions & 9 deletions internal/client/garden/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Client interface {
GetSecret(ctx context.Context, namespace, name string) (*corev1.Secret, error)
// GetConfigMap returns a Kubernetes configmap resource
GetConfigMap(ctx context.Context, namespace, name string) (*corev1.ConfigMap, error)
// GetShootOfManagedSeed returns shoot of seed using ManagedSeed resource, nil if not a managed seed
// GetShootOfManagedSeed returns shoot of seed using ManagedSeed resource. An error is returned if it is not a managed seed or the referenced shoot is nil
GetShootOfManagedSeed(ctx context.Context, name string) (*seedmanagementv1alpha1.Shoot, error)

// ListBastions returns all Gardener bastion resources, filtered by a list option
Expand Down Expand Up @@ -290,14 +290,13 @@ func (g *clientImpl) GetShootOfManagedSeed(ctx context.Context, name string) (*s
key := types.NamespacedName{Namespace: "garden", Name: name} // Currently, managed seeds are restricted to the garden namespace

if err := g.c.Get(ctx, key, managedSeed); err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}

return nil, fmt.Errorf("failed to get managed seed %v: %w", key, err)
return nil, err
}

klog.V(1).Infof("using referred shoot %q for seed %q", managedSeed.Spec.Shoot.Name, name)
referredShoot := managedSeed.Spec.Shoot
if referredShoot == nil {
return nil, fmt.Errorf("no shoot referenced for managed seed %s", name)
}

return managedSeed.Spec.Shoot, nil
}
Expand Down Expand Up @@ -423,9 +422,21 @@ func (g *clientImpl) CurrentUser(ctx context.Context) (string, error) {
}

func (g *clientImpl) GetSeedClientConfig(ctx context.Context, name string) (clientcmd.ClientConfig, error) {
if shoot, err := g.GetShootOfManagedSeed(ctx, name); err != nil {
logger := klog.FromContext(ctx)

shoot, err := g.GetShootOfManagedSeed(ctx, name)
if client.IgnoreNotFound(err) != nil {
return nil, err
} else if shoot != nil {
}

if !apierrors.IsNotFound(err) {
logger.V(1).Info("using referred shoot of managed seed",
"shoot", klog.ObjectRef{
Namespace: "garden",
Name: shoot.Name,
},
"seed", name)

return g.GetShootClientConfig(ctx, "garden", shoot.Name)
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/cmd/providerenv/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"

clientgarden "github.com/gardener/gardenctl-v2/internal/client/garden"
"github.com/gardener/gardenctl-v2/internal/util"
Expand Down Expand Up @@ -101,6 +102,8 @@ func (o *options) AddFlags(flags *pflag.FlagSet) {
func (o *options) Run(f util.Factory) error {
ctx := f.Context()

logger := klog.FromContext(ctx)

manager, err := f.Manager()
if err != nil {
return err
Expand All @@ -121,15 +124,23 @@ func (o *options) Run(f util.Factory) error {
}

if o.Target.ShootName() == "" && o.Target.SeedName() != "" {
if shoot, err := client.GetShootOfManagedSeed(ctx, o.Target.SeedName()); err != nil {
shoot, err := client.GetShootOfManagedSeed(ctx, o.Target.SeedName())
if err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("cannot generate cloud provider CLI configuration script for non-managed seeds: %w", err)
}

return err
} else if shoot != nil {
o.Target = o.Target.WithProjectName("garden").WithShootName(shoot.Name)
}

logger.V(1).Info("using referred shoot of managed seed",
"shoot", klog.ObjectRef{
Namespace: "garden",
Name: shoot.Name,
},
"seed", o.Target.SeedName())

o.Target = o.Target.WithProjectName("garden").WithShootName(shoot.Name)
}

if o.Target.ShootName() == "" {
Expand Down
14 changes: 11 additions & 3 deletions pkg/cmd/ssh/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,23 @@ func (o *SSHOptions) Run(f util.Factory) error {
}

if sshTarget.ShootName() == "" && sshTarget.SeedName() != "" {
if shoot, err := gardenClient.GetShootOfManagedSeed(ctx, sshTarget.SeedName()); err != nil {
shoot, err := gardenClient.GetShootOfManagedSeed(ctx, sshTarget.SeedName())
if err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("cannot ssh to non-managed seeds: %w", err)
}

return err
} else if shoot != nil {
sshTarget = sshTarget.WithProjectName("garden").WithShootName(shoot.Name)
}

logger.V(1).Info("using referred shoot of managed seed",
"shoot", klog.ObjectRef{
Namespace: "garden",
Name: shoot.Name,
},
"seed", sshTarget.SeedName())

sshTarget = sshTarget.WithProjectName("garden").WithShootName(shoot.Name)
}

if sshTarget.ShootName() == "" {
Expand Down