add component model
This commit is contained in:
47
pkg/apis/v1alpha/components/components_handler.go
Normal file
47
pkg/apis/v1alpha/components/components_handler.go
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package components
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"kubesphere.io/kubesphere/pkg/filter/route"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"net/http"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
ws.Route(ws.GET(subPath).To(handleGetComponents).Filter(route.RouteLogging)).
|
||||
Consumes(restful.MIME_JSON, restful.MIME_XML).
|
||||
Produces(restful.MIME_JSON)
|
||||
|
||||
}
|
||||
|
||||
//get all components
|
||||
|
||||
func handleGetComponents(request *restful.Request, response *restful.Response) {
|
||||
|
||||
result, err := models.GetComponents()
|
||||
|
||||
if err != nil {
|
||||
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
}
|
||||
|
||||
response.WriteAsJson(result)
|
||||
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/storage"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/volumes"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/iam"
|
||||
"kubesphere.io/kubesphere/pkg/apis/v1alpha/components"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -43,6 +44,7 @@ func init() {
|
||||
pods.Register(ws)
|
||||
containers.Register(ws)
|
||||
iam.Register(ws)
|
||||
components.Register(ws,"/components")
|
||||
// add webservice to default container
|
||||
restful.Add(ws)
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
"kubesphere.io/kubesphere/pkg/filter/route"
|
||||
"net/http"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
func Register(ws *restful.WebService, subPath string) {
|
||||
@@ -39,7 +40,7 @@ func handlerRegistryValidation(request *restful.Request, response *restful.Respo
|
||||
|
||||
if err != nil {
|
||||
|
||||
response.WriteError(http.StatusInternalServerError, err)
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||
|
||||
}
|
||||
|
||||
|
||||
263
pkg/models/components.go
Normal file
263
pkg/models/components.go
Normal file
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"kubesphere.io/kubesphere/pkg/client"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"time"
|
||||
"github.com/golang/glog"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const KUBESYSTEM = "kube-system"
|
||||
const OPENPITRIX = "openpitrix"
|
||||
|
||||
type Components struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Kind string `json:"kind"`
|
||||
HealthStatus string `json:"healthStatus"`
|
||||
Replicas int `json:"replicas"`
|
||||
AvailableReplicas int `json:"availableReplicas"`
|
||||
SelfLink string `json:"selfLink"`
|
||||
UpdateTime time.Time `json:"updateTime"`
|
||||
}
|
||||
|
||||
/***
|
||||
* get all components from k8s and kubesphere system,
|
||||
* there are master component, node component,addons component , kubesphere component
|
||||
*
|
||||
*/
|
||||
func GetComponents() (result []Components, err error) {
|
||||
|
||||
k8sClient := client.NewK8sClient()
|
||||
|
||||
label := "tier=control-plane"
|
||||
|
||||
option := meta_v1.ListOptions{
|
||||
|
||||
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
|
||||
version := strings.Split(pod.Spec.Containers[0].Image, ":")
|
||||
components.Version = version[1]
|
||||
|
||||
if pod.Status.Phase == "Running" {
|
||||
|
||||
components.HealthStatus = "health"
|
||||
components.Replicas = 1
|
||||
components.AvailableReplicas = 1
|
||||
|
||||
} else {
|
||||
|
||||
components.HealthStatus = "fault"
|
||||
components.Replicas = 1
|
||||
components.AvailableReplicas = 0
|
||||
|
||||
}
|
||||
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
|
||||
version := strings.Split(pod.Spec.Containers[0].Image, ":")
|
||||
components.Version = version[1]
|
||||
|
||||
if pod.Status.Phase == "Running" {
|
||||
|
||||
components.HealthStatus = "health"
|
||||
components.Replicas = 1
|
||||
components.AvailableReplicas = 1
|
||||
|
||||
} else {
|
||||
|
||||
components.HealthStatus = "fault"
|
||||
components.Replicas = 1
|
||||
components.AvailableReplicas = 0
|
||||
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
if len(dsList.Items) > 0 {
|
||||
|
||||
for _, ds := range dsList.Items {
|
||||
|
||||
if strings.Contains(ds.Name, "fluent-bit") {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
components.Name = ds.Name
|
||||
components.Kind = "Daemonset"
|
||||
components.SelfLink = ds.SelfLink
|
||||
version := strings.Split(ds.Spec.Template.Spec.Containers[0].Image, ":")
|
||||
components.Version = version[1]
|
||||
components.UpdateTime = ds.CreationTimestamp.Time
|
||||
components.AvailableReplicas = int(ds.Status.NumberAvailable)
|
||||
components.Replicas = int(ds.Status.DesiredNumberScheduled)
|
||||
|
||||
if components.AvailableReplicas == components.Replicas {
|
||||
|
||||
components.HealthStatus = "health"
|
||||
|
||||
} else {
|
||||
|
||||
components.HealthStatus = "fault"
|
||||
|
||||
}
|
||||
|
||||
result = append(result, components)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
templates = []string{"kube-dns", "heapster", "monitoring-influxdb", "iam", "openpitrix"}
|
||||
|
||||
namespaces := []string{KUBESYSTEM, OPENPITRIX}
|
||||
|
||||
for _, ns := range namespaces {
|
||||
|
||||
deployList, err := k8sClient.AppsV1beta1().Deployments(ns).List(option)
|
||||
|
||||
if err != nil {
|
||||
|
||||
glog.Error(err)
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
if len(deployList.Items) > 0 {
|
||||
|
||||
for _, dm := range deployList.Items {
|
||||
|
||||
for _, template := range templates {
|
||||
|
||||
if strings.Contains(dm.Name, template) {
|
||||
|
||||
components.Name = dm.Name
|
||||
components.Kind = "Deployment"
|
||||
components.SelfLink = dm.SelfLink
|
||||
version := strings.Split(dm.Spec.Template.Spec.Containers[0].Image, ":")
|
||||
if len(version) < 2 {
|
||||
|
||||
components.Version = "latest"
|
||||
|
||||
} else {
|
||||
|
||||
components.Version = version[1]
|
||||
|
||||
}
|
||||
|
||||
components.UpdateTime = dm.Status.Conditions[0].LastUpdateTime.Time
|
||||
components.AvailableReplicas = int(dm.Status.AvailableReplicas)
|
||||
components.Replicas = int(dm.Status.Replicas)
|
||||
|
||||
if components.AvailableReplicas == components.Replicas {
|
||||
|
||||
components.HealthStatus = "health"
|
||||
|
||||
} else {
|
||||
|
||||
components.HealthStatus = "fault"
|
||||
|
||||
}
|
||||
|
||||
result = append(result, components)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
}
|
||||
@@ -22,9 +22,7 @@ import (
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/api/types"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
|
||||
@@ -34,14 +32,19 @@ type AuthInfo struct {
|
||||
ServerHost string `json:"serverhost"`
|
||||
}
|
||||
|
||||
type ValidationMsg struct {
|
||||
|
||||
Message string `json:"message"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
const DOCKERCLIENTERROR = "Docker client error"
|
||||
|
||||
func RegistryLoginAuth(authinfo AuthInfo) constants.ResultMessage {
|
||||
func RegistryLoginAuth(authinfo AuthInfo) ValidationMsg {
|
||||
|
||||
var result constants.ResultMessage
|
||||
|
||||
data := make(map[string]interface{})
|
||||
var result ValidationMsg
|
||||
|
||||
datastr := []byte(authinfo.Username + ":" + authinfo.Password)
|
||||
auth := base64.StdEncoding.EncodeToString(datastr)
|
||||
@@ -50,8 +53,8 @@ func RegistryLoginAuth(authinfo AuthInfo) constants.ResultMessage {
|
||||
|
||||
if err != nil {
|
||||
|
||||
data["message"] = DOCKERCLIENTERROR
|
||||
data["reason"] = err.Error()
|
||||
glog.Error(err)
|
||||
|
||||
}
|
||||
|
||||
authcfg := types.AuthConfig{
|
||||
@@ -68,25 +71,21 @@ func RegistryLoginAuth(authinfo AuthInfo) constants.ResultMessage {
|
||||
|
||||
if err != nil {
|
||||
|
||||
data["message"] = DOCKERCLIENTERROR
|
||||
data["reason"] = err.Error()
|
||||
glog.Error(err)
|
||||
|
||||
}
|
||||
|
||||
if authmsg.Status == "Login Succeeded" {
|
||||
|
||||
data["message"] = "Verified"
|
||||
result.Message = "Verified"
|
||||
|
||||
} else {
|
||||
|
||||
data["message"] = "Unverified"
|
||||
data["reason"] = "Username or password is incorrect "
|
||||
result.Message = "Unverified"
|
||||
result.Reason = "Username or password is incorrect "
|
||||
|
||||
}
|
||||
|
||||
result.Data = data
|
||||
|
||||
|
||||
return result
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user