This repository was archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathvolumes.go
More file actions
155 lines (134 loc) · 4.26 KB
/
volumes.go
File metadata and controls
155 lines (134 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ecs
import (
"context"
"fmt"
"github.com/awslabs/goformation/v4/cloudformation"
"github.com/awslabs/goformation/v4/cloudformation/efs"
"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/pkg/errors"
"github.com/docker/compose-cli/api/volumes"
)
func (b *ecsAPIService) createNFSMountTarget(project *types.Project, resources awsResources, template *cloudformation.Template) {
for volume := range project.Volumes {
for _, pubSubnet := range resources.pubSubnets {
name := fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(pubSubnet.ID()))
template.Resources[name] = &efs.MountTarget{
FileSystemId: resources.filesystems[volume].ID(),
SecurityGroups: resources.allSecurityGroups(),
SubnetId: pubSubnet.ID(),
}
}
}
}
func (b *ecsAPIService) mountTargets(volume string, resources awsResources) []string {
var refs []string
for _, pubSubnet := range resources.pubSubnets {
refs = append(refs, fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(pubSubnet.ID())))
}
return refs
}
func (b *ecsAPIService) createAccessPoints(project *types.Project, r awsResources, template *cloudformation.Template) {
for name, volume := range project.Volumes {
n := fmt.Sprintf("%sAccessPoint", normalizeResourceName(name))
uid := volume.DriverOpts["uid"]
gid := volume.DriverOpts["gid"]
permissions := volume.DriverOpts["permissions"]
path := volume.DriverOpts["root_directory"]
ap := efs.AccessPoint{
AccessPointTags: []efs.AccessPoint_AccessPointTag{
{
Key: api.ProjectLabel,
Value: project.Name,
},
{
Key: api.VolumeLabel,
Value: name,
},
{
Key: "Name",
Value: volume.Name,
},
},
FileSystemId: r.filesystems[name].ID(),
}
if uid != "" {
ap.PosixUser = &efs.AccessPoint_PosixUser{
Uid: uid,
Gid: gid,
}
}
if path != "" {
root := efs.AccessPoint_RootDirectory{
Path: path,
}
ap.RootDirectory = &root
if uid != "" {
root.CreationInfo = &efs.AccessPoint_CreationInfo{
OwnerUid: uid,
OwnerGid: gid,
Permissions: permissions,
}
}
}
template.Resources[n] = &ap
}
}
// VolumeCreateOptions hold EFS filesystem creation options
type VolumeCreateOptions struct {
KmsKeyID string
PerformanceMode string
ProvisionedThroughputInMibps float64
ThroughputMode string
}
type ecsVolumeService struct {
backend *ecsAPIService
}
func (e ecsVolumeService) List(ctx context.Context) ([]volumes.Volume, error) {
filesystems, err := e.backend.aws.ListFileSystems(ctx, nil)
if err != nil {
return nil, err
}
var vol []volumes.Volume
for _, fs := range filesystems {
vol = append(vol, volumes.Volume{
ID: fs.ID(),
Description: fs.ARN(),
})
}
return vol, nil
}
func (e ecsVolumeService) Create(ctx context.Context, name string, options interface{}) (volumes.Volume, error) {
fs, err := e.backend.aws.CreateFileSystem(ctx, map[string]string{
"Name": name,
}, options.(VolumeCreateOptions))
return volumes.Volume{
ID: fs.ID(),
Description: fs.ARN(),
}, err
}
func (e ecsVolumeService) Delete(ctx context.Context, volumeID string, options interface{}) error {
return e.backend.aws.DeleteFileSystem(ctx, volumeID)
}
func (e ecsVolumeService) Inspect(ctx context.Context, volumeID string) (volumes.Volume, error) {
ok, err := e.backend.aws.ResolveFileSystem(ctx, volumeID)
if ok == nil {
err = errors.Wrapf(api.ErrNotFound, "filesystem %q does not exists", volumeID)
}
return volumes.Volume{
ID: volumeID,
Description: ok.ARN(),
}, err
}