2022-10-18 10:39:45 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-12-06 10:24:26 +00:00
|
|
|
"sort"
|
2023-02-07 10:48:32 +00:00
|
|
|
"strings"
|
2022-10-18 10:39:45 +00:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
)
|
|
|
|
|
2023-02-07 10:48:32 +00:00
|
|
|
func (u *ContainerService) PageNetwork(req dto.SearchWithPage) (int64, interface{}, error) {
|
2022-10-18 10:39:45 +00:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
list, err := client.NetworkList(context.TODO(), types.NetworkListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-02-07 10:48:32 +00:00
|
|
|
if len(req.Info) != 0 {
|
|
|
|
lenth, count := len(list), 0
|
|
|
|
for count < lenth {
|
|
|
|
if !strings.Contains(list[count].Name, req.Info) {
|
|
|
|
list = append(list[:count], list[(count+1):]...)
|
|
|
|
lenth--
|
|
|
|
} else {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 10:39:45 +00:00
|
|
|
var (
|
|
|
|
data []dto.Network
|
|
|
|
records []types.NetworkResource
|
|
|
|
)
|
2022-12-06 10:24:26 +00:00
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
return list[i].Created.Before(list[j].Created)
|
|
|
|
})
|
2022-10-18 10:39:45 +00:00
|
|
|
total, start, end := len(list), (req.Page-1)*req.PageSize, req.Page*req.PageSize
|
|
|
|
if start > total {
|
|
|
|
records = make([]types.NetworkResource, 0)
|
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
|
|
|
}
|
|
|
|
records = list[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range records {
|
|
|
|
tag := make([]string, 0)
|
|
|
|
for key, val := range item.Labels {
|
|
|
|
tag = append(tag, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
|
|
|
var ipam network.IPAMConfig
|
|
|
|
if len(item.IPAM.Config) > 0 {
|
|
|
|
ipam = item.IPAM.Config[0]
|
|
|
|
}
|
|
|
|
data = append(data, dto.Network{
|
|
|
|
ID: item.ID,
|
|
|
|
CreatedAt: item.Created,
|
|
|
|
Name: item.Name,
|
|
|
|
Driver: item.Driver,
|
|
|
|
IPAMDriver: item.IPAM.Driver,
|
|
|
|
Subnet: ipam.Subnet,
|
|
|
|
Gateway: ipam.Gateway,
|
|
|
|
Attachable: item.Attachable,
|
|
|
|
Labels: tag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(total), data, nil
|
|
|
|
}
|
|
|
|
func (u *ContainerService) DeleteNetwork(req dto.BatchDelete) error {
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-13 10:54:28 +00:00
|
|
|
for _, id := range req.Names {
|
2022-10-18 10:39:45 +00:00
|
|
|
if err := client.NetworkRemove(context.TODO(), id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (u *ContainerService) CreateNetwork(req dto.NetworkCreat) error {
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
ipam network.IPAMConfig
|
|
|
|
hasConf bool
|
|
|
|
)
|
|
|
|
if len(req.Subnet) != 0 {
|
|
|
|
ipam.Subnet = req.Subnet
|
|
|
|
hasConf = true
|
|
|
|
}
|
|
|
|
if len(req.Gateway) != 0 {
|
|
|
|
ipam.Gateway = req.Gateway
|
|
|
|
hasConf = true
|
|
|
|
}
|
|
|
|
if len(req.IPRange) != 0 {
|
|
|
|
ipam.IPRange = req.IPRange
|
|
|
|
hasConf = true
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.NetworkCreate{
|
|
|
|
Driver: req.Driver,
|
|
|
|
Options: stringsToMap(req.Options),
|
|
|
|
Labels: stringsToMap(req.Labels),
|
|
|
|
}
|
|
|
|
if hasConf {
|
|
|
|
options.IPAM = &network.IPAM{Config: []network.IPAMConfig{ipam}}
|
|
|
|
}
|
|
|
|
if _, err := client.NetworkCreate(context.TODO(), req.Name, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|