alter components api,change docker sdk version

This commit is contained in:
yanmingfan
2018-06-16 17:36:32 +08:00
parent a83c797df3
commit c7bbe1d004
4 changed files with 204 additions and 206 deletions

View File

@@ -31,6 +31,10 @@ func Register(ws *restful.WebService, subPath string) {
Consumes(restful.MIME_JSON, restful.MIME_XML). Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON) Produces(restful.MIME_JSON)
ws.Route(ws.GET(subPath+"/{namespace}").To(handleGetComponentsByNamespace).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
} }
//get all components //get all components
@@ -50,3 +54,22 @@ func handleGetComponents(request *restful.Request, response *restful.Response) {
} }
} }
//get components from ns
func handleGetComponentsByNamespace(request *restful.Request, response *restful.Response) {
ns := request.PathParameter("namespace")
result, err := models.GetComponentsByNamespace(ns)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
} else {
response.WriteAsJson(result)
}
}

View File

@@ -17,7 +17,6 @@ limitations under the License.
package models package models
import ( import (
"strings"
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
@@ -31,208 +30,46 @@ const OPENPITRIX = "openpitrix-system"
const ISTIO = "istio-system" const ISTIO = "istio-system"
const KUBESPHERE = "kubesphere-system" const KUBESPHERE = "kubesphere-system"
type ComponentsCount struct {
KubernetesCount int `json:"kubernetesCount"`
OpenpitrixCount int `json:"openpitrixCount"`
KubesphereCount int `json:"kubesphereCount"`
IstioCount int `json:"istioCount"`
}
type Components struct { type Components struct {
Name string `json:"name"` Name string `json:"name"`
Version string `json:"version"`
Kind string `json:"kind"`
Namespace string `json:"namespace"` Namespace string `json:"namespace"`
Label interface{} `json:"label"`
Replicas int `json:"replicas"`
HealthStatus string `json:"healthStatus"`
SelfLink string `json:"selfLink"` SelfLink string `json:"selfLink"`
UpdateTime time.Time `json:"updateTime"` Label interface{} `json:"label"`
HealthStatus string `json:"healthStatus"`
CreateTime time.Time `json:"updateTime"`
} }
/*** /***
* get all components from k8s and kubesphere system, * get all components from k8s and kubesphere system
* there are master component, node component,addons component , kubesphere component
* *
*/ */
func GetComponents() ([]Components, error) { func GetComponents() (map[string]interface{}, error) {
result := make([]Components, 0) result := make(map[string]interface{})
componentsList := make([]Components, 0)
k8sClient := client.NewK8sClient() k8sClient := client.NewK8sClient()
label := "tier=control-plane" var count ComponentsCount
var components Components
label := "kubernetes.io/cluster-service=true"
option := meta_v1.ListOptions{ option := meta_v1.ListOptions{
LabelSelector: label, LabelSelector: label,
} }
podlists, err := k8sClient.CoreV1().Pods(KUBESYSTEM).List(option)
if err != nil {
glog.Error(err)
return result, err
}
var components Components
templates := []string{"kube-apiserver", "etcd", "kube-scheduler", "kube-controller-manager", "cloud-controller-manager"}
if len(podlists.Items) > 0 {
for _, pod := range podlists.Items {
for _, template := range templates {
if strings.Contains(pod.Name, template) {
components.Name = template
components.Kind = "Pod"
components.SelfLink = pod.SelfLink
components.Label = pod.Labels
components.Namespace = pod.Namespace
version := strings.Split(pod.Spec.Containers[0].Image, ":")
if len(version) < 2 {
components.Version = "latest"
} else {
components.Version = version[1]
}
components.Replicas = 1
if pod.Status.Phase == "Running" {
components.HealthStatus = "health"
} else {
components.HealthStatus = "unhealth"
}
components.UpdateTime = pod.Status.Conditions[0].LastTransitionTime.Time
result = append(result, components)
}
}
}
}
label = "component=kube-addon-manager"
option.LabelSelector = label
kubeaddon, err := k8sClient.CoreV1().Pods(KUBESYSTEM).List(option)
if err != nil {
glog.Error(err)
return result, err
}
if len(kubeaddon.Items) > 0 {
for _, pod := range kubeaddon.Items {
components.Name = "kube-addon-manager"
components.Kind = "Pod"
components.SelfLink = pod.SelfLink
components.Label = pod.Labels
components.Namespace = pod.Namespace
version := strings.Split(pod.Spec.Containers[0].Image, ":")
if len(version) < 2 {
components.Version = "latest"
} else {
components.Version = version[1]
}
components.Replicas = 1
if pod.Status.Phase == "Running" {
components.HealthStatus = "health"
} else {
components.HealthStatus = "unhealth"
}
components.UpdateTime = pod.Status.Conditions[0].LastTransitionTime.Time
result = append(result, components)
}
}
option.LabelSelector = ""
dsList, err := k8sClient.AppsV1beta2().DaemonSets(KUBESYSTEM).List(option)
if err != nil {
glog.Error(err)
return result, err
}
templates = []string{"flannel", "kube-proxy", "calico"}
if len(dsList.Items) > 0 {
for _, ds := range dsList.Items {
for _, template := range templates {
if strings.Contains(ds.Name, template) {
components.Name = ds.Name
components.Kind = "Daemonset"
components.SelfLink = ds.SelfLink
components.Label = ds.Spec.Selector.MatchLabels
components.Namespace = ds.Namespace
version := strings.Split(ds.Spec.Template.Spec.Containers[0].Image, ":")
if len(version) < 2 {
components.Version = "latest"
} else {
components.Version = version[1]
}
components.UpdateTime = ds.CreationTimestamp.Time
components.Replicas = int(ds.Status.DesiredNumberScheduled)
if ds.Status.NumberAvailable == ds.Status.DesiredNumberScheduled {
components.HealthStatus = "health"
} else {
components.HealthStatus = "unhealth"
}
result = append(result, components)
}
}
}
}
templates = []string{"kube-dns", "heapster", "monitoring-influxdb", "iam", "openpitrix", "istio", "kubesphere"}
namespaces := []string{KUBESYSTEM, OPENPITRIX, ISTIO, KUBESPHERE} namespaces := []string{KUBESYSTEM, OPENPITRIX, ISTIO, KUBESPHERE}
for _, ns := range namespaces { for _, ns := range namespaces {
deployList, err := k8sClient.AppsV1beta1().Deployments(ns).List(option) if ns != KUBESYSTEM {
option.LabelSelector = ""
}
servicelists, err := k8sClient.CoreV1().Services(ns).List(option)
if err != nil { if err != nil {
@@ -241,34 +78,61 @@ func GetComponents() ([]Components, error) {
return result, err return result, err
} }
if len(deployList.Items) > 0 { if len(servicelists.Items) > 0 {
for _, dm := range deployList.Items { for _, service := range servicelists.Items {
for _, template := range templates { switch ns {
if strings.Contains(dm.Name, template) { case KUBESYSTEM:
count.KubernetesCount++
case OPENPITRIX:
count.OpenpitrixCount++
case KUBESPHERE:
count.KubesphereCount++
components.Name = dm.Name default:
components.Kind = "Deployment" count.IstioCount++
components.SelfLink = dm.SelfLink }
components.Label = dm.Spec.Selector.MatchLabels
components.Namespace = dm.Namespace
components.Replicas = int(dm.Status.Replicas)
version := strings.Split(dm.Spec.Template.Spec.Containers[0].Image, ":")
if len(version) < 2 {
components.Version = "latest" components.Name = service.Name
components.Namespace = service.Namespace
components.CreateTime = service.CreationTimestamp.Time
components.Label = service.Spec.Selector
components.SelfLink = service.SelfLink
label := service.Spec.Selector
combination := ""
for key, val := range label {
labelstr := key + "=" + val
if combination == "" {
combination = labelstr
} else { } else {
components.Version = version[1] combination = combination + "," + labelstr
} }
components.UpdateTime = dm.Status.Conditions[0].LastUpdateTime.Time }
option := meta_v1.ListOptions{
LabelSelector: combination,
}
podsList, err := k8sClient.CoreV1().Pods(ns).List(option)
if dm.Status.AvailableReplicas == dm.Status.Replicas { if err != nil {
glog.Error(err)
return result, err
}
if len(podsList.Items) > 0 {
for _, pod := range podsList.Items {
if pod.Status.Phase == "Running" {
components.HealthStatus = "health" components.HealthStatus = "health"
@@ -278,18 +142,108 @@ func GetComponents() ([]Components, error) {
} }
}
}
componentsList = append(componentsList, components)
}
}
}
result["count"] = count
result["item"] = componentsList
return result, nil
}
func GetComponentsByNamespace(ns string) ([]Components, error) {
result := make([]Components, 0)
k8sClient := client.NewK8sClient()
var components Components
label := "kubernetes.io/cluster-service=true"
option := meta_v1.ListOptions{
LabelSelector: label,
}
if ns != KUBESYSTEM {
option.LabelSelector = ""
}
servicelists, err := k8sClient.CoreV1().Services(ns).List(option)
if err != nil {
glog.Error(err)
return result, err
}
if len(servicelists.Items) > 0 {
for _, service := range servicelists.Items {
components.Name = service.Name
components.Namespace = service.Namespace
components.CreateTime = service.CreationTimestamp.Time
components.SelfLink = service.SelfLink
components.Label = service.Spec.Selector
label := service.Spec.Selector
combination := ""
for key, val := range label {
labelstr := key + "=" + val
if combination == "" {
combination = labelstr
} else {
combination = combination + "," + labelstr
}
}
option := meta_v1.ListOptions{
LabelSelector: combination,
}
podsList, err := k8sClient.CoreV1().Pods(ns).List(option)
if err != nil {
glog.Error(err)
return result, err
}
if len(podsList.Items) > 0 {
for _, pod := range podsList.Items {
if pod.Status.Phase == "Running" {
components.HealthStatus = "health"
} else {
components.HealthStatus = "unhealth"
}
}
}
result = append(result, components) result = append(result, components)
} }
} }
}
}
}
return result, nil return result, nil
} }

View File

@@ -88,7 +88,7 @@ func RegistryLoginAuth(authinfo AuthInfo) ValidationMsg {
datastr := []byte(authinfo.Username + ":" + authinfo.Password) datastr := []byte(authinfo.Username + ":" + authinfo.Password)
auth := base64.StdEncoding.EncodeToString(datastr) auth := base64.StdEncoding.EncodeToString(datastr)
ctx := context.Background() ctx := context.Background()
cli, err := client.NewClientWithOpts() cli, err := client.NewEnvClient()
if err != nil { if err != nil {

View File

@@ -27,6 +27,7 @@ import (
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
policy "k8s.io/api/policy/v1beta1" policy "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
"kubesphere.io/kubesphere/pkg/client" "kubesphere.io/kubesphere/pkg/client"
kubeclient "kubesphere.io/kubesphere/pkg/client" kubeclient "kubesphere.io/kubesphere/pkg/client"
@@ -265,9 +266,29 @@ func DrainNode(nodename string) (msg constants.MessageResponse, err error) {
return msg, err return msg, err
} }
} }
}
data := []byte(" {\"spec\":{\"unschedulable\":true}}")
nodestatus, err := k8sclient.CoreV1().Nodes().Patch(nodename, types.StrategicMergePatchType, data)
if err != nil {
glog.Fatal(err)
return msg, err
} }
if nodestatus.Spec.Unschedulable {
msg.Message = fmt.Sprintf("success") msg.Message = fmt.Sprintf("success")
} else {
glog.Fatal(err)
return msg, err
}
return msg, nil return msg, nil
} }