Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
hirenko-v committed Sep 26, 2024
1 parent 2d87ea7 commit 8b59b1b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cmd/job/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

go_plugin "github.com/hashicorp/go-plugin"
"github.com/kubeshop/botkube/pkg/api"
Expand Down Expand Up @@ -130,19 +132,19 @@ func (e *MsgExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (ex

case "run":
fields := strings.Fields(value)
jobName := fmt.Sprintf("%s-%s",fields[0], strconv.FormatInt(time.Now().Unix(), 10))
filePath := fmt.Sprintf("/tmp/%s.json",jobName)

args := fields[2:]
runCmd := fmt.Sprintf("kubectl create job --from=cronjob/%s -n %s test --dry-run -ojson", fields[0], fields[1])
runCmd := fmt.Sprintf("kubectl create job --from=cronjob/%s -n %s &s --dry-run -ojson", fields[0], fields[1], jobName)
out, _ := plugin.ExecuteCommand(ctx, runCmd, plugin.ExecuteCommandEnvs(envs))
err = os.WriteFile("/tmp/stdout.json", []byte(out.Stdout), 0644) // Create or overwrite the file
if err != nil {
log.Fatalf("error writing patched JSON to file: %w", err)
}
var cronJob map[string]interface{}
err := json.Unmarshal([]byte(out.Stdout), &cronJob)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
}

annotations := cronJob["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})
annotations["botkube"] = "true"
// Navigate to the container args
template := cronJob["spec"].(map[string]interface{})["template"].(map[string]interface{})
container := template["spec"].(map[string]interface{})["containers"].([]interface{})[0].(map[string]interface{})
Expand All @@ -157,14 +159,14 @@ func (e *MsgExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (ex
}

// // Save the patched JSON to a file
err = os.WriteFile("/tmp/patched_cronjob.json", modifiedJSON, 0644) // Create or overwrite the file
err = os.WriteFile(filePath, modifiedJSON, 0644) // Create or overwrite the file
if err != nil {
log.Fatalf("error writing patched JSON to file: %w", err)
}
createCmd := fmt.Sprintf("kubectl apply -f /tmp/patched_cronjob.json")
createCmd := fmt.Sprintf("kubectl apply -f %s", filePath)
plugin.ExecuteCommand(ctx, createCmd, plugin.ExecuteCommandEnvs(envs))
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage(fmt.Sprintf("saved, %s",args), true),
Message: api.NewCodeBlockMessage(fmt.Sprintf("Job %s started", jobName), true),
}, nil
}

Expand Down

0 comments on commit 8b59b1b

Please sign in to comment.