GenerateKubeconfigInCluster load serviceaccount info under "/var/run/secrets/kubernetes.io/serviceaccount" and generate kubeconfig str.
()
| 1091 | // GenerateKubeconfigInCluster load serviceaccount info under |
| 1092 | // "/var/run/secrets/kubernetes.io/serviceaccount" and generate kubeconfig str. |
| 1093 | func GenerateKubeconfigInCluster() ([]byte, error) { |
| 1094 | const ( |
| 1095 | tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" |
| 1096 | rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" |
| 1097 | ) |
| 1098 | |
| 1099 | host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT") |
| 1100 | if len(host) == 0 || len(port) == 0 { |
| 1101 | return nil, errors.New("unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined") |
| 1102 | } |
| 1103 | |
| 1104 | token, err := os.ReadFile(tokenFile) |
| 1105 | if err != nil { |
| 1106 | return nil, err |
| 1107 | } |
| 1108 | |
| 1109 | rootCa, err := os.ReadFile(rootCAFile) |
| 1110 | if err != nil { |
| 1111 | return nil, err |
| 1112 | } |
| 1113 | |
| 1114 | return util.ParseTemplate(manifests.KubeconfigTmplV1, struct { |
| 1115 | CAData string |
| 1116 | Server string |
| 1117 | Token string |
| 1118 | }{ |
| 1119 | Server: "https://" + net.JoinHostPort(host, port), |
| 1120 | CAData: base64.StdEncoding.EncodeToString(rootCa), |
| 1121 | Token: string(token), |
| 1122 | }) |
| 1123 | } |
| 1124 | |
| 1125 | func (c *Controller) GetBindingWorkerFor(cloudshell *cloudshellv1alpha1.CloudShell) (*corev1.Pod, error) { |
| 1126 | podName := cloudshell.Labels[constants.CloudshellPodLabelKey] |