Created
March 15, 2024 18:52
-
-
Save mchurichi/4780b05f89fd98e776d4182348cd64dd to your computer and use it in GitHub Desktop.
k8s utils
This file contains hidden or 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
package main | |
import ( | |
"crypto/tls" | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"time" | |
"github.com/valyala/fastjson" | |
corev1 "k8s.io/api/core/v1" | |
) | |
const ( | |
baseURL = "https://127.0.0.1:10250" | |
tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" | |
) | |
func main() { | |
allStart := time.Now() | |
defer func() { | |
fmt.Printf("all took %d ms\n", time.Since(allStart).Milliseconds()) | |
}() | |
start := time.Now() | |
podsBytes, err := getPodList() | |
if err != nil { | |
log.Fatalf("failed to get pods: %s", err) | |
} | |
fmt.Printf("getPodList took %d ms\n", time.Since(start).Milliseconds()) | |
start = time.Now() | |
var p fastjson.Parser | |
podList, err := p.ParseBytes(podsBytes) | |
if err != nil { | |
log.Fatalf("failed to parse json: %s", err) | |
} | |
fmt.Printf("fastjson.ParseBytes took %d ms\n", time.Since(start).Milliseconds()) | |
start = time.Now() | |
pods := podList.GetArray("items") | |
pod := new(corev1.Pod) | |
var scratch []byte | |
scratch = (pods[0]).MarshalTo(scratch[:0]) | |
if err := json.Unmarshal(scratch, &pod); err != nil { | |
log.Fatalf("unable to unmarshal pod info from kubelet response parser json: %v", err) | |
} | |
fmt.Printf("pod unmarshalling took %d ms\n", time.Since(start).Milliseconds()) | |
fmt.Printf("pod bytes size: %v\n", len(podsBytes)) | |
fmt.Printf("# of pods: %v\n", len(pods)) | |
} | |
func getPodList() ([]byte, error) { | |
url, err := url.Parse(baseURL) | |
if err != nil { | |
return nil, fmt.Errorf("unable to parse base URL: %v", err) | |
} | |
url.Path = "/pods" | |
token, err := getToken(tokenPath) | |
if err != nil { | |
return nil, fmt.Errorf("unable to get token: %v", err) | |
} | |
resp, err := makeRequest(url.String(), token) | |
if err != nil { | |
return nil, fmt.Errorf("unable to make request: %v", err) | |
} | |
out, err := io.ReadAll(resp.Body) | |
if err != nil { | |
return nil, fmt.Errorf("unable to read pods response: %v", err) | |
} | |
return out, nil | |
} | |
func makeRequest(url string, token string) (*http.Response, error) { | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
return nil, fmt.Errorf("unable to create request: %v", err) | |
} | |
req.Header.Add("Authorization", "Bearer "+token) | |
transport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} | |
client := &http.Client{Transport: transport} | |
resp, err := client.Do(req) | |
if err != nil { | |
return nil, fmt.Errorf("unable to perform request: %v", err) | |
} | |
return resp, nil | |
} | |
func getToken(path string) (string, error) { | |
token, err := os.ReadFile(path) | |
if err != nil { | |
return "", fmt.Errorf("unable to read token file: %v", err) | |
} | |
return string(token), nil | |
} | |
func tryRead(r io.Reader) string { | |
buf := make([]byte, 1024) | |
n, _ := r.Read(buf) | |
return string(buf[:n]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment