@@ -18,6 +18,7 @@ package container
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/golang/glog"
|
||||
@@ -25,14 +26,16 @@ import (
|
||||
|
||||
func logFilter() restful.FilterFunction {
|
||||
return func(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
|
||||
start := time.Now()
|
||||
chain.ProcessFilter(req, resp)
|
||||
glog.Infof("%s - \"%s %s %s\" %d %d",
|
||||
glog.Infof("%s - \"%s %s %s\" %d %d in %dms",
|
||||
strings.Split(req.Request.RemoteAddr, ":")[0],
|
||||
req.Request.Method,
|
||||
req.Request.URL.RequestURI(),
|
||||
req.Request.Proto,
|
||||
resp.StatusCode(),
|
||||
resp.ContentLength(),
|
||||
time.Since(start)/time.Millisecond,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,15 +166,7 @@ func getStatusAndRestartCount(pod v1.Pod) (string, int) {
|
||||
}
|
||||
|
||||
func (ctl *PodCtl) generateObject(item v1.Pod) *Pod {
|
||||
name := item.Name
|
||||
namespace := item.Namespace
|
||||
podIp := item.Status.PodIP
|
||||
nodeName := item.Spec.NodeName
|
||||
nodeIp := item.Status.HostIP
|
||||
status, restartCount := getStatusAndRestartCount(item)
|
||||
createTime := item.CreationTimestamp.Time
|
||||
containerStatus := item.Status.ContainerStatuses
|
||||
containerSpecs := item.Spec.Containers
|
||||
|
||||
var ownerKind, ownerName string
|
||||
|
||||
// For ReplicaSet,ReplicaController,DaemonSet,StatefulSet,Job,CronJob, k8s will automatically
|
||||
@@ -184,37 +176,18 @@ func (ctl *PodCtl) generateObject(item v1.Pod) *Pod {
|
||||
ownerName = item.OwnerReferences[0].Name
|
||||
}
|
||||
|
||||
var containers Containers
|
||||
|
||||
for _, containerSpec := range containerSpecs {
|
||||
var container Container
|
||||
container.Name = containerSpec.Name
|
||||
container.Image = containerSpec.Image
|
||||
container.Ports = containerSpec.Ports
|
||||
container.Resources = containerSpec.Resources
|
||||
for _, status := range containerStatus {
|
||||
if container.Name == status.Name {
|
||||
container.Ready = status.Ready
|
||||
}
|
||||
}
|
||||
|
||||
containers = append(containers, container)
|
||||
}
|
||||
|
||||
object := &Pod{
|
||||
Namespace: namespace,
|
||||
Name: name,
|
||||
Node: nodeName,
|
||||
PodIp: podIp,
|
||||
Status: status,
|
||||
NodeIp: nodeIp,
|
||||
CreateTime: createTime,
|
||||
Annotation: MapString{item.Annotations},
|
||||
Containers: containers,
|
||||
RestartCount: restartCount,
|
||||
OwnerKind: ownerKind,
|
||||
OwnerName: ownerName,
|
||||
Labels: MapString{item.Labels},
|
||||
Namespace: item.Namespace,
|
||||
Name: item.Name,
|
||||
Node: item.Spec.NodeName,
|
||||
Status: item.Status,
|
||||
CreateTime: item.CreationTimestamp.Time,
|
||||
OwnerKind: ownerKind,
|
||||
OwnerName: ownerName,
|
||||
Spec: item.Spec,
|
||||
Metadata: item.ObjectMeta,
|
||||
Kind: item.TypeMeta.Kind,
|
||||
APIVersion: item.TypeMeta.APIVersion,
|
||||
}
|
||||
|
||||
return object
|
||||
|
||||
@@ -19,6 +19,9 @@ package controllers
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -210,19 +213,65 @@ type Ingress struct {
|
||||
}
|
||||
|
||||
type Pod struct {
|
||||
Name string `gorm:"primary_key" json:"name"`
|
||||
Namespace string `gorm:"primary_key" json:"namespace"`
|
||||
Status string `json:"status,omitempty"`
|
||||
Node string `json:"node,omitempty"`
|
||||
NodeIp string `gorm:"column:nodeIp" json:"nodeIp,omitempty"`
|
||||
PodIp string `gorm:"column:podIp" json:"podIp,omitempty"`
|
||||
Containers Containers `gorm:"type:text" json:"containers,omitempty"`
|
||||
Annotation MapString `json:"annotations"`
|
||||
Labels MapString `json:"labels"`
|
||||
OwnerKind string `gorm:"column:ownerKind" json:"ownerKind,omitempty"`
|
||||
OwnerName string `gorm:"column:ownerName" json:"ownerName,omitempty"`
|
||||
RestartCount int `json:"restartCount"`
|
||||
CreateTime time.Time `gorm:"column:createTime" json:"createTime,omitempty"`
|
||||
// search and sort field, not seen in response
|
||||
Name string `gorm:"primary_key" json:"-"`
|
||||
Namespace string `gorm:"primary_key" json:"-"`
|
||||
Node string `json:"-"`
|
||||
OwnerKind string `gorm:"column:ownerKind" json:"-"`
|
||||
OwnerName string `gorm:"column:ownerName" json:"-"`
|
||||
CreateTime time.Time `gorm:"column:createTime" json:"-"`
|
||||
|
||||
// Kubernetes Standard Pod Specification
|
||||
Kind string `json:"kind,omitempty"`
|
||||
APIVersion string `gorm:"column:apiVersion" json:"apiVersion,omitempty"`
|
||||
Spec v1.PodSpec `sql:"-" json:"spec,omitempty"`
|
||||
Metadata v12.ObjectMeta `sql:"-" json:"metadata,omitempty"`
|
||||
Status v1.PodStatus `sql:"-" json:"status,omitempty"`
|
||||
|
||||
// shadow field, used only for database
|
||||
MetadataString string `gorm:"column:metadata;type:text" json:"-"`
|
||||
SpecString string `gorm:"column:podSpec;type:text" json:"-"`
|
||||
StatusString string `gorm:"column:status;type:text" json:"-"`
|
||||
}
|
||||
|
||||
func (pod *Pod) AfterFind(scope *gorm.Scope) (err error) {
|
||||
|
||||
if err = json.Unmarshal([]byte(pod.SpecString), &pod.Spec); err != nil {
|
||||
glog.Errorln(err)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(pod.MetadataString), &pod.Metadata); err != nil {
|
||||
glog.Errorln(err)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(pod.StatusString), &pod.Status); err != nil {
|
||||
glog.Errorln(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pod *Pod) BeforeSave(scope *gorm.Scope) (err error) {
|
||||
|
||||
if bytes, err := json.Marshal(pod.Spec); err == nil {
|
||||
pod.SpecString = string(bytes)
|
||||
} else {
|
||||
glog.Errorln(err)
|
||||
}
|
||||
|
||||
if bytes, err := json.Marshal(pod.Metadata); err == nil {
|
||||
pod.MetadataString = string(bytes)
|
||||
} else {
|
||||
glog.Errorln(err)
|
||||
}
|
||||
|
||||
if bytes, err := json.Marshal(pod.Status); err == nil {
|
||||
pod.StatusString = string(bytes)
|
||||
} else {
|
||||
glog.Errorln(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Container struct {
|
||||
|
||||
Reference in New Issue
Block a user