Skip to content

Commit

Permalink
fix: testkube namespaces handling (#800)
Browse files Browse the repository at this point in the history
* fix: testkube installation namespace

* fix: namespaces handling
  • Loading branch information
exu authored Jan 19, 2022
1 parent 8a34c7b commit 51601ed
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 15 deletions.
5 changes: 4 additions & 1 deletion cmd/kubectl-testkube/commands/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (

func GetClient(cmd *cobra.Command) (client.Client, string) {
clientType := cmd.Flag("client").Value.String()
namespace := cmd.Flag("namespace").Value.String()
// TODO allow to install testkube in different namespace
// we need to use some config for plugin then to save testkube installation namespace after first call
// testkube installation namespace
namespace := "testkube"

client, err := client.GetClient(client.ClientType(clientType), namespace)
ui.ExitOnError("setting up client type", err)
Expand Down
7 changes: 5 additions & 2 deletions cmd/kubectl-testkube/commands/scripts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewCreateScriptsCmd() *cobra.Command {
gitPath string
tags []string
)

cmd := &cobra.Command{
Use: "create",
Short: "Create new script",
Expand All @@ -32,6 +33,7 @@ func NewCreateScriptsCmd() *cobra.Command {

var content []byte
var err error
namespace := cmd.Flag("namespace").Value.String()

if file != "" {
// read script content
Expand All @@ -42,9 +44,9 @@ func NewCreateScriptsCmd() *cobra.Command {
ui.ExitOnError("reading stdin", err)
}

client, namespace := common.GetClient(cmd)
client, _ := common.GetClient(cmd)

script, _ := client.GetScript(name)
script, _ := client.GetScript(name, namespace)
if name == script.Name {
ui.Failf("Script with name '%s' already exists in namespace %s", name, namespace)
}
Expand Down Expand Up @@ -85,6 +87,7 @@ func NewCreateScriptsCmd() *cobra.Command {
}

options.Tags = tags

script, err = client.CreateScript(options)
ui.ExitOnError("creating script "+name+" in namespace "+namespace, err)

Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/scripts/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func NewDeleteScriptsCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
ui.Logo()

client, namespace := common.GetClient(cmd)
client, _ := common.GetClient(cmd)
namespace := cmd.Flag("namespace").Value.String()
name := args[0]

err := client.DeleteScript(name, namespace)
ui.ExitOnError("delete script "+name+" from namespace "+namespace, err)

Expand Down
3 changes: 2 additions & 1 deletion cmd/kubectl-testkube/commands/scripts/deleteall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func NewDeleteAllScriptsCmd() *cobra.Command {
Args: validator.ScriptName,
Run: func(cmd *cobra.Command, args []string) {
ui.Logo()
namespace := cmd.Flag("namespace").Value.String()

client, namespace := common.GetClient(cmd)
client, _ := common.GetClient(cmd)
err := client.DeleteScripts(namespace)
ui.ExitOnError("delete all scripts from namespace "+namespace, err)

Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/scripts/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func NewGetScriptsCmd() *cobra.Command {
ui.Logo()

name := args[0]
namespace := cmd.Flag("namespace").Value.String()

client, _ := common.GetClient(cmd)
script, err := client.GetScript(name)
script, err := client.GetScript(name, namespace)
ui.ExitOnError("getting script "+name, err)

out, err := yaml.Marshal(script)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/scripts/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewUpdateScriptsCmd() *cobra.Command {

client, namespace := common.GetClient(cmd)

script, _ := client.GetScript(name)
script, _ := client.GetScript(name, namespace)
if name != script.Name {
ui.Failf("Script with name '%s' not exists in namespace %s", name, namespace)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v1/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

const Version = "v1"
const TestkubeInstallationNamespace = "testkube"

// Converts io.Reader with SSE data like `data: {"type": "event", "message":"something"}`
// to channel of output.Output objects, helps with logs streaming from SSE endpoint (passed from job executor)
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/v1/client/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ type DirectScriptsAPI struct {

// scripts and executions -----------------------------------------------------------------------------

func (c DirectScriptsAPI) GetScript(id string) (script testkube.Script, err error) {
uri := c.getURI("/scripts/%s", id)
func (c DirectScriptsAPI) GetScript(id, namespace string) (script testkube.Script, err error) {
uri := c.getURI("/scripts/%s?namespace=%s", id, namespace)
resp, err := c.client.Get(uri)
if err != nil {
return script, err
Expand Down Expand Up @@ -183,7 +183,7 @@ func (c DirectScriptsAPI) ExecuteScript(id, namespace, executionName string, exe
uri := c.getURI("/scripts/%s/executions", id)

// get script to get script tags
script, err := c.GetScript(id)
script, err := c.GetScript(id, namespace)
if err != nil {
return execution, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Client interface {
ListExecutions(scriptID string, limit int, tags []string) (executions testkube.ExecutionsResult, err error)
AbortExecution(script string, id string) error

GetScript(id string) (script testkube.Script, err error)
GetScript(id, namespace string) (script testkube.Script, err error)
CreateScript(options UpsertScriptOptions) (script testkube.Script, err error)
UpdateScript(options UpsertScriptOptions) (script testkube.Script, err error)
DeleteScript(name string, namespace string) error
Expand Down
13 changes: 9 additions & 4 deletions pkg/api/v1/client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ type ProxyScriptsAPI struct {

// scripts and executions -----------------------------------------------------------------------------

func (c ProxyScriptsAPI) GetScript(id string) (script testkube.Script, err error) {
func (c ProxyScriptsAPI) GetScript(id, namespace string) (script testkube.Script, err error) {
uri := c.getURI("/scripts/%s", id)
req := c.GetProxy("GET").Suffix(uri)
req := c.GetProxy("GET").
Suffix(uri).
Param("namespace", namespace)

resp := req.Do(context.Background())

Expand Down Expand Up @@ -184,7 +186,7 @@ func (c ProxyScriptsAPI) ExecuteScript(id, namespace, executionName string, exec
uri := c.getURI("/scripts/%s/executions", id)

// get script to get script tags
script, err := c.GetScript(id)
script, err := c.GetScript(id, namespace)
if err != nil {
return execution, nil
}
Expand Down Expand Up @@ -526,7 +528,9 @@ func (c ProxyScriptsAPI) getArtifactsFromResponse(resp rest.Result) (artifacts [

func (c ProxyScriptsAPI) GetTest(id, namespace string) (script testkube.Test, err error) {
uri := c.getURI("/tests/%s", id)
req := c.GetProxy("GET").Suffix(uri)
req := c.GetProxy("GET").
Suffix(uri).
Param("namespace", namespace)

resp := req.Do(context.Background())

Expand All @@ -544,6 +548,7 @@ func (c ProxyScriptsAPI) DeleteTest(name string, namespace string) error {
uri := c.getURI("/scripts/%s", name)
return c.makeDeleteRequest(uri, namespace, true)
}

func (c ProxyScriptsAPI) ListTests(namespace string, tags []string) (scripts testkube.Tests, err error) {
uri := c.getURI("/tests")
req := c.GetProxy("GET").
Expand Down

0 comments on commit 51601ed

Please sign in to comment.