Created
March 24, 2022 09:07
-
-
Save x893675/12d8b259f96591b564f5bc09f9aa5c61 to your computer and use it in GitHub Desktop.
use client-go to proxy request to in cluster svc
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
package main | |
import ( | |
"context" | |
"fmt" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"github.com/kubeclipper/kubeclipper/pkg/utils/cmdutil" | |
) | |
//https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster-services/#manually-constructing-apiserver-proxy-urls | |
var cfgformat = ` | |
apiVersion: v1 | |
clusters: | |
- cluster: | |
certificate-authority-data: %s | |
server: %s | |
name: %s | |
contexts: | |
- context: | |
cluster: %s | |
user: %s | |
name: %s-%s | |
current-context: %s-%s | |
kind: Config | |
preferences: {} | |
users: | |
- name: %s | |
user: | |
token: %s` | |
func main() { | |
token, cacrt := runCmd() | |
//c := clientcmdapi.NewConfig() | |
//c.APIVersion = "v1" | |
//c.Kind = "Config" | |
//c.Clusters["clustername"] = &clientcmdapi.Cluster{ | |
// Server: "https://apiserver.cluster.local:6443", | |
// CertificateAuthorityData: []byte(cacrt), | |
//} | |
//c.Contexts["clustername@user"] = &clientcmdapi.Context{ | |
// Cluster: "clustername", | |
// AuthInfo: "user", | |
//} | |
//c.CurrentContext = "clustername@user" | |
//c.AuthInfos["user"] = &clientcmdapi.AuthInfo{ | |
// Token: token, | |
//} | |
cstr := fmt.Sprintf(cfgformat, cacrt, "https://apiserver.cluster.local:6443", "clustername", "clustername", "user", "user", "clustername", "user", "clustername", "user", token) | |
//kubeconfig, err := ioutil.ReadFile("kubeconfig.yaml") | |
//if err != nil { | |
// panic(err) | |
//} | |
//kubeconfig, err := json.Marshal(c) | |
//if err != nil { | |
// panic(err) | |
//} | |
cfg, err := clientcmd.NewClientConfigFromBytes([]byte(cstr)) | |
if err != nil { | |
panic(err) | |
} | |
clientCfg, err := cfg.ClientConfig() | |
if err != nil { | |
panic(err) | |
} | |
clientset, err := kubernetes.NewForConfig(clientCfg) | |
if err != nil { | |
panic(err) | |
} | |
content, err := clientset.Discovery().RESTClient().Get().AbsPath("/healthz").DoRaw(context.TODO()) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(content)) | |
content, err = clientset.Discovery().RESTClient().Get().AbsPath("/api/v1/namespaces/kubesphere-system/services/ks-apiserver/proxy/kapis/version").DoRaw(context.TODO()) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(content)) | |
} | |
func runCmd() (string, string) { | |
cmds := []string{"/bin/bash", "-c", `kubectl get secret $(kubectl get sa kc-server -n kube-system -o jsonpath={.secrets[0].name}) -n kube-system -o jsonpath={.data.token} | base64 -d`} | |
ec, err := cmdutil.RunCmdWithContext(context.TODO(), false, cmds[0], cmds[1:]...) | |
if err != nil { | |
panic(err) | |
} | |
//fmt.Println(ec.StdOut()) | |
cmds2 := []string{"kubectl", "config", "view", "--raw", "-o", "jsonpath={.clusters[0].cluster..certificate-authority-data}"} | |
ec2, err := cmdutil.RunCmdWithContext(context.TODO(), false, cmds2[0], cmds2[1:]...) | |
if err != nil { | |
panic(err) | |
} | |
//fmt.Println(ec2.StdOut()) | |
return ec.StdOut(), ec2.StdOut() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment