This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ZupIT/feature/logs-kubernetes
Feature/logs kubernetes
- Loading branch information
Showing
8 changed files
with
241 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"description": "Show logs kubernetes", | ||
"inputs" : [ | ||
{ | ||
"name": "namespace", | ||
"type": "text", | ||
"label": "Type kubernates namespace: ", | ||
"cache": { | ||
"active": true | ||
} | ||
}, | ||
{ | ||
"name" : "pod_part_name", | ||
"type" : "text", | ||
"label": "Type name or part name of pod: ", | ||
"cache": { | ||
"active": true | ||
} | ||
}, | ||
{ | ||
"name" : "kubeconfig", | ||
"type" : "CREDENTIAL_KUBECONFIG_BASE64CONFIG" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Go parameters | ||
GOCMD=go | ||
GOBUILD=$(GOCMD) build | ||
BINARY_NAME=kubernetes-logs | ||
CMD_PATH=./main.go | ||
DIST=../bin | ||
DIST_MAC=$(DIST)/$(BINARY_NAME)-darwin | ||
DIST_LINUX=$(DIST)/$(BINARY_NAME)-linux | ||
DIST_WIN=$(DIST)/$(BINARY_NAME)-windows.exe | ||
|
||
build: | ||
mkdir -p $(DIST) | ||
export MODULE=$(GO111MODULE=on go list -m) | ||
#LINUX | ||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o ./$(DIST_LINUX) -v $(CMD_PATH) | ||
#MAC | ||
GOOS=darwin GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o ./$(DIST_MAC) -v $(CMD_PATH) | ||
#WINDOWS 64 | ||
GOOS=windows GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o ./$(DIST_WIN) -v $(CMD_PATH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module kubernetes/logs | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/imdario/mergo v0.3.8 // indirect | ||
github.com/manifoldco/promptui v0.7.0 | ||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect | ||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect | ||
k8s.io/api v0.17.2 | ||
k8s.io/apimachinery v0.17.2 | ||
k8s.io/client-go v0.17.0 | ||
k8s.io/utils v0.0.0-20200124190032-861946025e34 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"kubernetes/logs/pkg/logs" | ||
"os" | ||
) | ||
|
||
func main() { | ||
loadInputs().Run() | ||
} | ||
|
||
func loadInputs() logs.Inputs { | ||
return logs.Inputs{ | ||
Namespace: os.Getenv("NAMESPACE"), | ||
PodPartName: os.Getenv("POD_PART_NAME"), | ||
Kubeconfig: os.Getenv("KUBECONFIG"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package logs | ||
|
||
import ( | ||
"bufio" | ||
"encoding/base64" | ||
"fmt" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"kubernetes/logs/pkg/prompt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Inputs struct { | ||
Namespace string | ||
PodPartName string | ||
Kubeconfig string | ||
} | ||
|
||
func (in Inputs) Run() { | ||
kubeConfigBytes, _ := base64.StdEncoding.DecodeString(in.Kubeconfig) | ||
clientConfig, err := clientcmd.NewClientConfigFromBytes(kubeConfigBytes) | ||
if err != nil { | ||
log.Fatalln("Failed to load config. Verify if you set credential kubeconfig in base64 format.") | ||
} | ||
|
||
config, err := clientConfig.ClientConfig() | ||
if err != nil { | ||
log.Fatalln("Failed to load config. Verify if you set credential kubeconfig in base64 format.") | ||
} | ||
|
||
// create the client | ||
client, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
log.Fatalln("Failed to create kubernetes client.") | ||
} | ||
|
||
pods, err := client.CoreV1().Pods(in.Namespace).List(metav1.ListOptions{}) | ||
if err != nil { | ||
log.Fatalf("Failed to list pods for namespace: %s.\n", in.Namespace) | ||
} | ||
if len(pods.Items) == 0 { | ||
log.Fatalf("No result pods to namespace: %s.\n", in.Namespace) | ||
} | ||
var podsFilter []v1.Pod | ||
for _, pod := range pods.Items { | ||
if strings.Contains(pod.Name, in.PodPartName) { | ||
podsFilter = append(podsFilter, pod) | ||
} | ||
} | ||
if len(podsFilter) == 0 { | ||
log.Fatalf("No result pods to name: %s and namespace: %s.\n", in.PodPartName, in.Namespace) | ||
} | ||
var items []string | ||
for i, pod := range podsFilter { | ||
items = append(items, fmt.Sprint(i, " - ", pod.Name)) | ||
} | ||
itemSelect , _ := prompt.List("Select Pod: ", items) | ||
ind, _ := strconv.Atoi(strings.Split(itemSelect, " - ")[0]) | ||
podSelect := podsFilter[ind] | ||
|
||
var containersItems []string | ||
for _, container := range podSelect.Spec.Containers { | ||
containersItems = append(containersItems, container.Name) | ||
} | ||
containerSelect , _ := prompt.List("Select container: ", containersItems) | ||
|
||
podLogOpts := v1.PodLogOptions{Follow:true, Container:containerSelect} | ||
|
||
req := client.CoreV1().Pods(podSelect.Namespace).GetLogs(podSelect.Name, &podLogOpts) | ||
podLogs, err := req.Stream() | ||
scanner := bufio.NewScanner(podLogs) | ||
scanner.Split(bufio.ScanLines) | ||
for scanner.Scan() { | ||
m := scanner.Text() | ||
fmt.Println(m) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package prompt | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/manifoldco/promptui" | ||
) | ||
|
||
// String show a prompt and parse to string. | ||
func String(name string, required bool) (string, error) { | ||
var prompt promptui.Prompt | ||
|
||
if required { | ||
prompt = promptui.Prompt{ | ||
Label: name, | ||
Validate: validateEmptyInput, | ||
Templates: defaultTemplate(), | ||
} | ||
} else { | ||
prompt = promptui.Prompt{ | ||
Label: name, | ||
Templates: defaultTemplate(), | ||
} | ||
} | ||
|
||
return prompt.Run() | ||
} | ||
|
||
|
||
// List show a prompt with options and parse to string. | ||
func List(name string, items []string) (string, error) { | ||
prompt := promptui.Select{ | ||
Items: items, | ||
Templates: defaultSelectTemplate(name), | ||
} | ||
_, result, err := prompt.Run() | ||
return result, err | ||
} | ||
|
||
func defaultTemplate() *promptui.PromptTemplates { | ||
return &promptui.PromptTemplates{ | ||
Prompt: "{{ . }} ", | ||
Valid: "{{ . | bold }} ", | ||
Invalid: "{{ . | red }} ", | ||
Success: "{{ . | bold }} ", | ||
} | ||
} | ||
|
||
func defaultSelectTemplate(label string) *promptui.SelectTemplates { | ||
return &promptui.SelectTemplates{ | ||
Label: label, | ||
} | ||
} | ||
|
||
func validateEmptyInput(input string) error { | ||
if len(strings.TrimSpace(input)) < 1 { | ||
return errors.New("this input must not be empty") | ||
} | ||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters