update vault integration

This commit is contained in:
2024-05-10 15:56:07 +03:00
parent 385b79d457
commit a0f87046ca
55 changed files with 828 additions and 135 deletions

View File

@@ -0,0 +1,5 @@
package kubernetes_client
import "errors"
var NoSuchVarInPod = errors.New("No such variable in pod!")

View File

@@ -0,0 +1,101 @@
package kubernetes_client
import (
"bytes"
"context"
"fmt"
"kube-forge/internal/config"
"net/http"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/remotecommand"
)
func ExecuteCommandInPodContainer(commandArray []string, namespace string, podName string, container string) (string, error) {
clientset, err := kubernetes.NewForConfig(config.GetKubernetesConfig())
if err != nil {
panic(err.Error())
}
execRequest := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec").
Param("container", container).
Param("stderr", "true").
Param("stdout", "true")
for _, com := range commandArray {
execRequest = execRequest.Param("command", com)
}
stderr := bytes.NewBufferString("")
stdout := bytes.NewBufferString("")
streamOptions := remotecommand.StreamOptions{
Stdout: stdout,
Stderr: stderr,
Tty: false,
}
exec, err := remotecommand.NewSPDYExecutor(config.GetKubernetesConfig(), http.MethodPost, execRequest.URL())
if err != nil {
fmt.Println(err.Error())
}
err = exec.StreamWithContext(context.Background(), streamOptions)
if err != nil {
if stderr.Len() == 0 {
panic(err)
}
outputErr := stderr.String()
return outputErr, err
}
output := stdout.String()
return output, nil
}
func GetPodByName(podName string, podNamespace string) (*corev1.Pod, error) {
clientset, err := kubernetes.NewForConfig(config.GetKubernetesConfig())
if err != nil {
panic(err.Error())
}
for i := 15; i > 0; i-- {
time.Sleep(time.Second * 1)
pod, err := clientset.CoreV1().
Pods(podNamespace).
Get(context.Background(), podName, metav1.GetOptions{})
if err != nil || pod.Status.Phase != "Running" {
continue
}
return pod, nil
}
return nil, err
}
func GetEnvVariableFromPodContainer(envVarName string, podNamespace string, podName string, containerName string) (string, error) {
commandArray := []string{"env"}
output, err := ExecuteCommandInPodContainer(
commandArray, podNamespace, podName, containerName,
)
if err != nil {
return output, err
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.HasPrefix(line, envVarName+"=") {
parts := strings.Split(line, "=")
if len(parts) == 2 {
value := parts[1]
return value, nil
}
}
}
return "", NoSuchVarInPod
}