Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use kubeconfig for gh plugin #1062

Merged
merged 2 commits into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions cmd/executor/gh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/hashicorp/go-plugin"

"github.com/kubeshop/botkube/internal/loggerx"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/executor"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/pluginx"
)

Expand All @@ -30,6 +32,7 @@ type Config struct {
Repository string
IssueTemplate string
}
Log config.Logger `yaml:"log"`
}

// Commands defines all supported GitHub plugin commands and their flags.
Expand Down Expand Up @@ -78,7 +81,19 @@ func (e *GHExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (exe
}, nil
}

issueDetails, err := getIssueDetails(ctx, cmd.Create.Issue.Namespace, cmd.Create.Issue.Type)
log := loggerx.New(cfg.Log)

kubeConfigPath, deleteFn, err := pluginx.PersistKubeConfig(ctx, in.Context.KubeConfig)
if err != nil {
return executor.ExecuteOutput{}, fmt.Errorf("while writing kubeconfig file: %w", err)
}
defer func() {
if deleteErr := deleteFn(ctx); deleteErr != nil {
log.Errorf("failed to delete kubeconfig file %s: %w", kubeConfigPath, deleteErr)
}
}()

issueDetails, err := getIssueDetails(ctx, cmd.Create.Issue.Namespace, cmd.Create.Issue.Type, kubeConfigPath)
if err != nil {
return executor.ExecuteOutput{}, fmt.Errorf("while fetching logs : %w", err)
}
Expand All @@ -105,20 +120,22 @@ func (*GHExecutor) Help(context.Context) (api.Message, error) {
}

var depsDownloadLinks = map[string]api.Dependency{
// Links source: https://github.com/cli/cli/releases/tag/v2.21.2
// Links source: https://github.com/cli/cli/releases/tag/v2.29.0
"gh": {
URLs: map[string]string{
// Using go-getter syntax to unwrap the underlying directory structure.
// Read more on https://github.com/hashicorp/go-getter#subdirectories
"darwin/amd64": "https://github.com/cli/cli/releases/download/v2.21.2/gh_2.21.2_macOS_amd64.tar.gz//gh_2.21.2_macOS_amd64/bin",
"linux/amd64": "https://github.com/cli/cli/releases/download/v2.21.2/gh_2.21.2_linux_amd64.tar.gz//gh_2.21.2_linux_amd64/bin",
"linux/arm64": "https://github.com/cli/cli/releases/download/v2.21.2/gh_2.21.2_linux_arm64.tar.gz//gh_2.21.2_linux_arm64/bin",
"linux/386": "https://github.com/cli/cli/releases/download/v2.21.2/gh_2.21.2_linux_386.tar.gz//gh_2.21.2_linux_386/bin",
"darwin/amd64": "https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_macOS_amd64.zip//gh_2.29.0_macOS_amd64/bin",
"darwin/arm64": "https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_macOS_arm64.zip//gh_2.29.0_macOS_arm64/bin",
"linux/amd64": "https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_linux_amd64.zip//gh_2.29.0_linux_amd64/bin",
"linux/arm64": "https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_linux_arm64.zip//gh_2.29.0_linux_arm64/bin",
"linux/386": "https://github.com/cli/cli/releases/download/v2.29.0/gh_2.29.0_linux_386.zip//gh_2.29.0_linux_386/bin",
},
},
"kubectl": {
URLs: map[string]string{
"darwin/amd64": "https://dl.k8s.io/release/v1.26.0/bin/darwin/amd64/kubectl",
"darwin/arm64": "https://dl.k8s.io/release/v1.26.0/bin/darwin/arm64/kubectl",
"linux/amd64": "https://dl.k8s.io/release/v1.26.0/bin/linux/amd64/kubectl",
"linux/arm64": "https://dl.k8s.io/release/v1.26.0/bin/linux/arm64/kubectl",
"linux/386": "https://dl.k8s.io/release/v1.26.0/bin/linux/386/kubectl",
Expand Down Expand Up @@ -152,15 +169,16 @@ type IssueDetails struct {
Version string
}

func getIssueDetails(ctx context.Context, namespace, name string) (IssueDetails, error) {
func getIssueDetails(ctx context.Context, namespace, name, kubeConfigPath string) (IssueDetails, error) {
if namespace == "" {
namespace = defaultNamespace
}
logs, err := pluginx.ExecuteCommand(ctx, fmt.Sprintf("kubectl logs %s -n %s --tail %d", name, namespace, logsTailLines))

logs, err := pluginx.ExecuteCommand(ctx, fmt.Sprintf("kubectl --kubeconfig=%s logs %s -n %s --tail %d", kubeConfigPath, name, namespace, logsTailLines))
if err != nil {
return IssueDetails{}, fmt.Errorf("while getting logs: %w", err)
}
ver, err := pluginx.ExecuteCommand(ctx, "kubectl version -o yaml")
ver, err := pluginx.ExecuteCommand(ctx, fmt.Sprintf("kubectl --kubeconfig=%s version -o yaml", kubeConfigPath))
if err != nil {
return IssueDetails{}, fmt.Errorf("while getting version: %w", err)
}
Expand Down