compose/azure/backend.go

89 строки
2.4 KiB
Go
Исходник Обычный вид История

package azure
import (
"context"
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/pkg/errors"
"github.com/docker/api/backend"
"github.com/docker/api/containers"
apicontext "github.com/docker/api/context"
"github.com/docker/api/context/store"
)
type containerService struct {
2020-05-04 10:57:52 +03:00
containerGroupsClient containerinstance.ContainerGroupsClient
ctx store.AciContext
}
func init() {
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
return New(ctx)
})
}
func getter() interface{} {
return &store.AciContext{}
}
func New(ctx context.Context) (containers.ContainerService, error) {
2020-05-04 10:57:52 +03:00
currentContext := apicontext.CurrentContext(ctx)
2020-04-30 00:44:01 +03:00
contextStore, err := store.New()
if err != nil {
return nil, err
}
2020-05-04 10:57:52 +03:00
metadata, err := contextStore.Get(currentContext, getter)
if err != nil {
return nil, errors.Wrap(err, "wrong context type")
}
2020-05-04 10:57:52 +03:00
aciContext, _ := metadata.Metadata.Data.(store.AciContext)
auth, _ := auth.NewAuthorizerFromCLI()
2020-05-04 10:57:52 +03:00
containerGroupsClient := containerinstance.NewContainerGroupsClient(aciContext.SubscriptionID)
containerGroupsClient.Authorizer = auth
return &containerService{
2020-05-04 10:57:52 +03:00
containerGroupsClient: containerGroupsClient,
ctx: aciContext,
}, nil
}
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
2020-05-04 10:57:52 +03:00
var containerGroups []containerinstance.ContainerGroup
result, err := cs.containerGroupsClient.ListByResourceGroup(ctx, cs.ctx.ResourceGroup)
if err != nil {
return []containers.Container{}, err
}
for result.NotDone() {
2020-05-04 10:57:52 +03:00
containerGroups = append(containerGroups, result.Values()...)
if err := result.NextWithContext(ctx); err != nil {
return []containers.Container{}, err
}
}
res := []containers.Container{}
2020-05-04 10:57:52 +03:00
for _, containerGroup := range containerGroups {
group, err := cs.containerGroupsClient.Get(ctx, cs.ctx.ResourceGroup, *containerGroup.Name)
2020-05-03 15:51:03 +03:00
if err != nil {
return []containers.Container{}, err
}
2020-05-04 10:57:52 +03:00
for _, container := range *group.Containers {
2020-05-03 15:51:03 +03:00
status := "Unknown"
2020-05-04 10:57:52 +03:00
if container.InstanceView != nil && container.InstanceView.CurrentState != nil {
status = *container.InstanceView.CurrentState.State
2020-05-03 15:51:03 +03:00
}
res = append(res, containers.Container{
2020-05-04 10:57:52 +03:00
ID: *container.Name,
Image: *container.Image,
2020-05-03 15:51:03 +03:00
Status: status,
})
}
}
return res, nil
}