Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
remove exists and re-use get to list resources
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoandredinis committed Jun 9, 2022
1 parent d23fe89 commit ae054cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
18 changes: 8 additions & 10 deletions kubernetes/sidecar/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/tctl/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 10 additions & 21 deletions lib/tctl/tctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit ae054cf

Please sign in to comment.