From ae054cf733e1100d85b4cc82b9786f64b9739b72 Mon Sep 17 00:00:00 2001 From: Marco Dinis Date: Thu, 9 Jun 2022 14:13:31 +0100 Subject: [PATCH] remove exists and re-use get to list resources --- kubernetes/sidecar/sidecar.go | 18 ++++++++---------- lib/tctl/resources.go | 2 +- lib/tctl/tctl.go | 31 ++++++++++--------------------- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/kubernetes/sidecar/sidecar.go b/kubernetes/sidecar/sidecar.go index d0e144415..985413c6f 100644 --- a/kubernetes/sidecar/sidecar.go +++ b/kubernetes/sidecar/sidecar.go @@ -63,28 +63,26 @@ func NewSidecarClient(ctx context.Context, opts Options) (*client.Client, error) resourcesToCreate := make([]types.Resource, 0) - exists, err := tctl.Exists(ctx, types.KindRole, opts.Role) - if err != nil { - return nil, trace.Wrap(err, "failed to query role") - } - if !exists { + _, err := tctl.Get(ctx, types.KindRole, opts.Role) + if trace.IsNotFound(err) { role, err := sidecarRole(opts.Role) if err != nil { return nil, trace.Wrap(err, "failed to create role") } resourcesToCreate = append(resourcesToCreate, role) + } else if err != nil { + return nil, trace.Wrap(err, "failed to query role") } - exists, err = tctl.Exists(ctx, types.KindUser, opts.User) - if err != nil { - return nil, trace.Wrap(err, "failed to query user") - } - if !exists { + _, err = tctl.Get(ctx, types.KindUser, opts.User) + if trace.IsNotFound(err) { user, err := sidecarUserWithRole(opts.User, opts.Role) if err != nil { return nil, trace.Wrap(err, "failed to create user") } resourcesToCreate = append(resourcesToCreate, user) + } else if err != nil { + return nil, trace.Wrap(err, "failed to query role") } if len(resourcesToCreate) > 0 { diff --git a/lib/tctl/resources.go b/lib/tctl/resources.go index 5e79b462b..b4916f651 100644 --- a/lib/tctl/resources.go +++ b/lib/tctl/resources.go @@ -77,7 +77,7 @@ func (res *streamResource) UnmarshalJSON(raw []byte) error { } case types.KindRole: switch header.Version { - case types.V4: + case types.V4, types.V5: resource = &types.RoleV5{} default: return trace.BadParameter("unsupported resource version %s", header.Version) diff --git a/lib/tctl/tctl.go b/lib/tctl/tctl.go index a135df1ae..5d480c37e 100644 --- a/lib/tctl/tctl.go +++ b/lib/tctl/tctl.go @@ -17,10 +17,10 @@ limitations under the License. package tctl import ( + "bytes" "context" "os/exec" "regexp" - "strings" "github.com/gravitational/teleport-plugins/lib/logger" "github.com/gravitational/teleport/api/types" @@ -113,6 +113,15 @@ func (tctl Tctl) GetAll(ctx context.Context, query string) ([]types.Resource, er return nil, trace.Wrap(err) } if err := cmd.Wait(); err != nil { + bs, errOutput := cmd.CombinedOutput() + if errOutput != nil { + return nil, trace.NewAggregate(err, errOutput) + } + + if bytes.Contains(bs, []byte("is not found")) { + return nil, nil + } + return nil, trace.Wrap(err) } return resources, nil @@ -131,26 +140,6 @@ func (tctl Tctl) Get(ctx context.Context, kind, name string) (types.Resource, er return resources[0], nil } -// Exists validates a resource existence by its kind and name identifiers. -func (tctl Tctl) Exists(ctx context.Context, kind, name string) (bool, error) { - log := logger.Get(ctx) - query := kind + "/" + name - args := append(tctl.baseArgs(), "get", query) - cmd := exec.CommandContext(ctx, tctl.cmd(), args...) - - log.Debugf("Running %s", cmd) - - commandOutput, err := cmd.CombinedOutput() - if err != nil { - if strings.Contains(string(commandOutput), "is not found") { - return false, nil - } - - return false, trace.WrapWithMessage(err, string(commandOutput)) - } - return true, nil -} - // GetCAPin sets the auth service CA Pin using output from tctl. func (tctl Tctl) GetCAPin(ctx context.Context) (string, error) { log := logger.Get(ctx)