-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f34d69
commit e2bcbf7
Showing
7 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/noovertime7/kubemanage/dto" | ||
"github.com/noovertime7/kubemanage/middleware" | ||
"github.com/noovertime7/kubemanage/service" | ||
"github.com/wonderivan/logger" | ||
) | ||
|
||
type monitor struct { | ||
service *service.MonitorService | ||
} | ||
|
||
func MonitroRegister(router *gin.RouterGroup) { | ||
m := &monitor{service: service.NewMonitorService()} | ||
router.GET("/image_list", m.GetImageList) | ||
} | ||
|
||
func (m *monitor) GetImageList(ctx *gin.Context) { | ||
params := &dto.ImageListInput{} | ||
if err := params.BindingValidParams(ctx); err != nil { | ||
logger.Error("绑定参数失败:", err) | ||
middleware.ResponseError(ctx, 20001, err) | ||
return | ||
} | ||
data, err := m.service.GetClusterImageList(params) | ||
if err != nil { | ||
logger.Error("获取镜像列表失败", err) | ||
middleware.ResponseError(ctx, 20002, err) | ||
return | ||
} | ||
middleware.ResponseSuccess(ctx, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dao | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type K8SDB struct { | ||
Id int `gorm:"column:id;type:int(11);AUTO_INCREMENT;primary_key" json:"id"` | ||
Name string `gorm:"column:name;type:varchar(255)" json:"name"` | ||
Config string `gorm:"column:config;type:text" json:"config"` | ||
IsDeleted int `gorm:"column:is_deleted;type:int(11);NOT NULL" json:"is_deleted"` | ||
CreateAt time.Time `gorm:"column:create_at;type:datetime;NOT NULL" json:"create_at"` | ||
UpdateAt time.Time `gorm:"column:update_at;type:datetime;NOT NULL" json:"update_at"` | ||
} | ||
|
||
func (k *K8SDB) TableName() string { | ||
return "t_k8s" | ||
} | ||
|
||
func (k *K8SDB) Find(search *K8SDB) (*K8SDB, error) { | ||
out := &K8SDB{} | ||
return out, Gorm.Where(search).Find(out).Error | ||
} | ||
|
||
func (k *K8SDB) Save() error { | ||
return Gorm.Save(k).Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dto | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/noovertime7/kubemanage/public" | ||
) | ||
|
||
type ImageListInput struct { | ||
ClusterName string `json:"cluster_name" form:"cluster_name" validate:"required"` | ||
} | ||
|
||
type ImageListOut struct { | ||
Total int `json:"total"` | ||
List []ImageListItem `json:"list"` | ||
} | ||
|
||
type ImageListItem struct { | ||
ClusterName string `json:"cluster_name"` | ||
NameSpace string `json:"name_space"` | ||
AppName string `json:"app_name"` | ||
Image string `json:"image"` | ||
} | ||
|
||
func (params *ImageListInput) BindingValidParams(c *gin.Context) error { | ||
return public.DefaultGetValidParams(c, params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package service | ||
|
||
import ( | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func GetClient(configPath string) (*kubernetes.Clientset, error) { | ||
var err error | ||
var config *rest.Config | ||
if config, err = clientcmd.BuildConfigFromFlags("", configPath); err != nil { | ||
return nil, err | ||
} | ||
// 创建 clientSet | ||
clientSet, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
return clientSet, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"github.com/noovertime7/kubemanage/dao" | ||
"github.com/noovertime7/kubemanage/dto" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type MonitorService struct{} | ||
|
||
func NewMonitorService() *MonitorService { | ||
return &MonitorService{} | ||
} | ||
|
||
func (m *MonitorService) GetClusterImageList(in *dto.ImageListInput) (*dto.ImageListOut, error) { | ||
//获取clientSet | ||
var total int | ||
k8sDB := &dao.K8SDB{Name: in.ClusterName} | ||
k8s, err := k8sDB.Find(k8sDB) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client, err := GetClient(k8s.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var out []dto.ImageListItem | ||
//查询所有Namespace | ||
NamespaceList, err := client.CoreV1().Namespaces().List(context.TODO(), metaV1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, namespace := range NamespaceList.Items { | ||
// 查询所有deployment 镜像 | ||
podlist, err := K8s.clientSet.CoreV1().Pods(namespace.Name).List(context.TODO(), metaV1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, pod := range podlist.Items { | ||
for _, container := range pod.Spec.Containers { | ||
outItem := dto.ImageListItem{ | ||
ClusterName: k8s.Name, | ||
NameSpace: namespace.Name, | ||
AppName: pod.Name, | ||
Image: container.Image, | ||
} | ||
out = append(out, outItem) | ||
total++ | ||
} | ||
} | ||
} | ||
return &dto.ImageListOut{ | ||
Total: total, | ||
List: out, | ||
}, nil | ||
} |