Skip to content

Commit

Permalink
Refactor unlocking kube credentials lockfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonAM committed May 31, 2023
1 parent d16b9b6 commit 56bbd44
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 8 additions & 6 deletions tool/tsh/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ func (c *kubeCredentialsCommand) run(cf *CLIConf) error {
}

func (c *kubeCredentialsCommand) issueCert(cf *CLIConf) error {

tc, err := makeClient(cf)
if err != nil {
return trace.Wrap(err)
Expand Down Expand Up @@ -720,7 +719,10 @@ func (c *kubeCredentialsCommand) issueCert(cf *CLIConf) error {
if err != nil {
return trace.Wrap(err)
}
defer unlockKubeCred(false) // by default (in case of an error) we don't delete lockfile; safe to call twice
deleteKubeCredsLock := false
defer func() {
unlockKubeCred(deleteKubeCredsLock) // by default (in case of an error) we don't delete lockfile.
}()

ctx, span := tc.Tracer.Start(cf.Context, "tsh.kubeCredentials/RetryWithRelogin")
err = client.RetryWithRelogin(ctx, tc, func() error {
Expand All @@ -741,9 +743,9 @@ func (c *kubeCredentialsCommand) issueCert(cf *CLIConf) error {
if err != nil {
var urlErr *url.Error
// If we've got network error we remove the lockfile, so we could restore from temporary connection
// problems without user intervention, if possible.
// problems without requiring user intervention.
if errors.As(err, &urlErr) {
unlockKubeCred(true)
deleteKubeCredsLock = true
}
return trace.Wrap(err)
}
Expand All @@ -760,8 +762,8 @@ func (c *kubeCredentialsCommand) issueCert(cf *CLIConf) error {
return trace.Wrap(err)
}

// Unlock and remove the lockfile so subsequent tsh kube credentials calls don't exit early
unlockKubeCred(true)
// Remove the lockfile so subsequent tsh kube credentials calls don't exit early
deleteKubeCredsLock = true

return c.writeKeyResponse(cf.Stdout(), k, c.kubeCluster)
}
Expand Down
3 changes: 1 addition & 2 deletions tool/tsh/tsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import (
"testing"
"time"

"github.com/gravitational/teleport/api/utils/keypaths"

"github.com/ghodss/yaml"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
Expand All @@ -55,6 +53,7 @@ import (
"github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/wrappers"
apiutils "github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/api/utils/keypaths"
"github.com/gravitational/teleport/api/utils/keys"
"github.com/gravitational/teleport/integration/kube"
"github.com/gravitational/teleport/lib"
Expand Down

0 comments on commit 56bbd44

Please sign in to comment.