refactor(master): list cluster interface (#909)

This commit is contained in:
cuisongliu
2022-03-31 16:09:55 +08:00
committed by GitHub
parent d0b9063755
commit 5ffcc3e1f3
3 changed files with 21 additions and 11 deletions
+12 -11
View File
@@ -58,6 +58,15 @@ func (*ClusterService) Inspect(name string) (*types.ClusterManifest, error) {
return inspectContainer(data)
}
func (*ClusterService) List() ([]types.ClusterInfo, error) {
data := exec.BashEval("buildah containers --json")
infos, err := listContainer(data)
if err != nil {
return nil, err
}
return infos, nil
}
func inspectContainer(data string) (*types.ClusterManifest, error) {
if data != "" {
var outStruct map[string]interface{}
@@ -78,24 +87,16 @@ func inspectContainer(data string) (*types.ClusterManifest, error) {
return nil, errors.New("inspect output is empty")
}
type ClusterInfo struct {
ID string `json:"id"`
Builder bool `json:"builder"`
Imageid string `json:"imageid"`
Imagename string `json:"imagename"`
Containername string `json:"containername"`
}
func listContainer(data string) ([]ClusterInfo, error) {
func listContainer(data string) ([]types.ClusterInfo, error) {
if data != "" {
var outStruct []ClusterInfo
var outStruct []types.ClusterInfo
err := json.Unmarshal([]byte(data), &outStruct)
if err != nil {
return nil, errors.Wrap(err, "decode out json from list container failed")
}
return outStruct, nil
}
return nil, errors.New("inspect output is empty")
return nil, errors.New("containers output is empty")
}
func NewClusterService() (types.ClusterService, error) {
+1
View File
@@ -43,4 +43,5 @@ type ClusterService interface {
Delete(name string) error
// Inspect return cluster(container) manifest
Inspect(name string) (*ClusterManifest, error)
List() ([]ClusterInfo, error)
}
+8
View File
@@ -27,3 +27,11 @@ type ClusterManifest struct {
ContainerID string
MountPoint string
}
type ClusterInfo struct {
ID string `json:"id"`
Builder bool `json:"builder"`
Imageid string `json:"imageid"`
Imagename string `json:"imagename"`
Containername string `json:"containername"`
}