Skip to content

Commit

Permalink
remove elevateContext expiration concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Tof1973 committed Apr 30, 2024
1 parent 28cbc22 commit 6b4e4d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
19 changes: 6 additions & 13 deletions pkg/elevate/elevate_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package elevate
import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/openshift/backplane-cli/pkg/utils"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -14,8 +12,7 @@ import (
)

const (
elevateExtensionName = "ElevateContext"
elevateExtensionRetentionMinutes = 20
elevateExtensionName = "ElevateContext"
)

var (
Expand All @@ -24,8 +21,7 @@ var (
)

type ElevateContext struct {
Reasons []string `json:"reasons"`
LastUsed time.Time `json:"lastUsed"`
Reasons []string `json:"reasons"`
}

///////////////////////////////////////////////////////////////////////
Expand All @@ -34,8 +30,7 @@ type ElevateContext struct {
// DeepCopyObject creates a deep copy of the ElevateContext.
func (r *ElevateContext) DeepCopyObject() runtime.Object {
return &ElevateContext{
Reasons: append([]string(nil), r.Reasons...),
LastUsed: r.LastUsed,
Reasons: append([]string(nil), r.Reasons...),
}
}

Expand Down Expand Up @@ -67,8 +62,7 @@ func GetElevateContextReasons(config api.Config) []string {
_ = json.Unmarshal([]byte(unknownObject.Raw), &elevateContext)
}
}
// We should keep the stored ElevateContext only if it is still valid
if elevateContext != nil && time.Since(elevateContext.LastUsed) <= elevateExtensionRetentionMinutes*time.Minute {
if elevateContext != nil {
return elevateContext.Reasons
}
}
Expand All @@ -91,7 +85,7 @@ func ComputeElevateContextAndStoreToKubeConfigFileAndGetReasons(config api.Confi
if len(elevationReasons) == 0 {
elevationReasons = utils.AppendUniqNoneEmptyString(
elevationReasons,
AskQuestionFromPrompt(fmt.Sprintf("Please enter a reason for elevation, it will be stored in current context for %d minutes : ", elevateExtensionRetentionMinutes)),
AskQuestionFromPrompt("Please enter a reason for elevation, it will be stored in current context : "),
)
}
// and raise an error if not possible
Expand All @@ -104,8 +98,7 @@ func ComputeElevateContextAndStoreToKubeConfigFileAndGetReasons(config api.Confi
currentCtx.Extensions = map[string]runtime.Object{}
}
currentCtx.Extensions[elevateExtensionName] = &ElevateContext{
Reasons: elevationReasons,
LastUsed: time.Now(),
Reasons: elevationReasons,
}

// Save the config to default path.
Expand Down
30 changes: 9 additions & 21 deletions pkg/elevate/elevate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"testing"
"time"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -57,17 +56,14 @@ func fakeReadKubeConfigRaw() (api.Config, error) {
return *fakeAPIConfig.DeepCopy(), nil
}

func fakeReadKubeConfigRawWithReasons(lastUsedMinutes time.Duration) func() (api.Config, error) {
return func() (api.Config, error) {
config := *fakeAPIConfig.DeepCopy()
config.Contexts[config.CurrentContext].Extensions = map[string]runtime.Object{
elevateExtensionName: &ElevateContext{
Reasons: []string{"dymmy reason"},
LastUsed: time.Now().Add(-lastUsedMinutes * time.Minute),
},
}
return config, nil
func fakeReadKubeConfigRawWithReasons() (api.Config, error) {
config := *fakeAPIConfig.DeepCopy()
config.Contexts[config.CurrentContext].Extensions = map[string]runtime.Object{
elevateExtensionName: &ElevateContext{
Reasons: []string{"dymmy reason"},
},
}
return config, nil
}

func TestHelperProcessError(t *testing.T) {
Expand Down Expand Up @@ -178,21 +174,13 @@ func TestRunElevate(t *testing.T) {
}
})

t.Run("It suceeds if reason is empty and ElevateContext present with Reasons still valid", func(t *testing.T) {
t.Run("It suceeds if reason is empty and ElevateContext present with Reasons", func(t *testing.T) {
ExecCmd = fakeExecCommandSuccess
AskQuestionFromPrompt = func(name string) string { return "" }
ReadKubeConfigRaw = fakeReadKubeConfigRawWithReasons(elevateExtensionRetentionMinutes - 1)
ReadKubeConfigRaw = fakeReadKubeConfigRawWithReasons
if err := RunElevate([]string{"", "get", "pods"}); err != nil {
t.Error("Expected nil, got", err)
}
})

t.Run("It returns an error if reason is empty and ElevateContext present with Reasons to old", func(t *testing.T) {
ExecCmd = fakeExecCommandSuccess
AskQuestionFromPrompt = func(name string) string { return "" }
ReadKubeConfigRaw = fakeReadKubeConfigRawWithReasons(elevateExtensionRetentionMinutes + 1)
if err := RunElevate([]string{"", "get", "pods"}); err == nil {
t.Error("Expected err, got nil")
}
})
}

0 comments on commit 6b4e4d1

Please sign in to comment.