format code

This commit is contained in:
jeff
2018-06-05 20:52:50 +08:00
parent 41a64c4017
commit 85d35e42ea
32 changed files with 119 additions and 276 deletions

View File

@@ -294,4 +294,4 @@ func GetComponents() (result []Components, err error) {
return result, nil
}
}

View File

@@ -18,16 +18,17 @@ package models
import (
"bytes"
"github.com/golang/glog"
"text/template"
"encoding/base64"
"text/template"
"github.com/golang/glog"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/options"
)
var kubeconfigTemp =
"apiVersion: v1\n" +
var kubeconfigTemp = "apiVersion: v1\n" +
"clusters:\n" +
"- cluster:\n" +
" certificate-authority-data: {{.Certificate}}\n" +
@@ -51,9 +52,9 @@ const DefaultServiceAccount = "default"
type Config struct {
Certificate string
Server string
User string
Token string
Server string
User string
Token string
}
func GetKubeConfig(namespace string) (string, error) {
@@ -77,24 +78,24 @@ func GetKubeConfig(namespace string) (string, error) {
func getKubeConfig(namespace, apiserverHost string) (*Config, error) {
k8sClient := client.NewK8sClient()
saInfo, err := k8sClient.CoreV1().ServiceAccounts(namespace).Get(DefaultServiceAccount, meta_v1.GetOptions{})
if err != nil{
if err != nil {
glog.Errorln(err)
return nil, err
}
secretName := saInfo.Secrets[0].Name
secretInfo, err := k8sClient.CoreV1().Secrets(namespace).Get(secretName, meta_v1.GetOptions{})
if err != nil{
if err != nil {
glog.Errorln(err)
return nil, err
}
secretData := secretInfo.Data
certificate := string(secretData["ca.crt"])
certificate= base64.StdEncoding.EncodeToString([]byte(certificate))
certificate = base64.StdEncoding.EncodeToString([]byte(certificate))
server := apiserverHost
token := string(secretData["token"])
user := string(secretData["namespace"])
return &Config{Certificate:certificate, Server:server, Token:token, User:user}, nil
return &Config{Certificate: certificate, Server: server, Token: token, User: user}, nil
}

View File

@@ -19,13 +19,14 @@ package models
import (
"fmt"
"math/rand"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"kubesphere.io/kubesphere/pkg/client"
"github.com/golang/glog"
"k8s.io/api/core/v1"
)
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"kubesphere.io/kubesphere/pkg/client"
)
const (
deploymentName = "kubectl"
@@ -33,7 +34,7 @@ const (
type kubectlPodInfo struct {
Namespace string `json: "namespace"`
Pod string `json: "podname"`
Pod string `json: "podname"`
Container string `json: "container"`
}
@@ -47,36 +48,35 @@ func GetKubectlPod(namespace string) (kubectlPodInfo, error) {
selectors := deploy.Spec.Selector.MatchLabels
labelSelector := labels.Set(selectors).AsSelector().String()
podList, err := k8sClient.CoreV1().Pods(namespace).List(meta_v1.ListOptions{LabelSelector:labelSelector})
podList, err := k8sClient.CoreV1().Pods(namespace).List(meta_v1.ListOptions{LabelSelector: labelSelector})
if err != nil {
glog.Errorln(err)
return kubectlPodInfo{}, err
}
pod, err := selectCorrectPod(namespace, podList.Items)
if err != nil{
if err != nil {
glog.Errorln(err)
return kubectlPodInfo{}, err
}
info := kubectlPodInfo{Namespace:pod.Namespace, Pod:pod.Name, Container:pod.Status.ContainerStatuses[0].Name}
info := kubectlPodInfo{Namespace: pod.Namespace, Pod: pod.Name, Container: pod.Status.ContainerStatuses[0].Name}
return info, nil
}
func selectCorrectPod(namespace string, pods []v1.Pod) (kubectlPod v1.Pod, err error) {
var kubectPodList []v1.Pod
for _, pod := range pods{
for _, condition := range pod.Status.Conditions{
if condition.Type == "Ready" && condition.Status == "True"{
for _, pod := range pods {
for _, condition := range pod.Status.Conditions {
if condition.Type == "Ready" && condition.Status == "True" {
kubectPodList = append(kubectPodList, pod)
}
}
}
if len(kubectPodList) < 1{
if len(kubectPodList) < 1 {
err = fmt.Errorf("cannot find valid kubectl pod in namespace:%s", namespace)
return v1.Pod{}, err
}

View File

@@ -25,6 +25,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
ksutil "kubesphere.io/kubesphere/pkg/util"

View File

@@ -14,9 +14,11 @@ import (
"fmt"
"strconv"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/constants"
"math"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/constants"
)
type ResultPod struct {

View File

@@ -237,4 +237,4 @@ func QueryRegistries(project string) (regList []Registries, err error) {
return regList, nil
}
}

View File

@@ -2,8 +2,9 @@ package models
import (
"k8s.io/api/rbac/v1"
"kubesphere.io/kubesphere/pkg/client"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
)
const ClusterRoleKind = "ClusterRole"

View File

@@ -17,15 +17,15 @@ limitations under the License.
package models
import (
"strings"
"io/ioutil"
"strings"
"github.com/golang/glog"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
extensionsV1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/api/rbac/v1beta1"
"github.com/golang/glog"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"kubesphere.io/kubesphere/pkg/client"
)
@@ -37,7 +37,7 @@ type Router struct {
Annotations string `json:"annotations"`
}
func GetAllRouters() ([] *coreV1.Service, error) {
func GetAllRouters() ([]*coreV1.Service, error) {
k8sClient := client.NewK8sClient()

View File

@@ -4,6 +4,7 @@ import (
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
)

View File

@@ -3,6 +3,7 @@ package models
import (
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
)