This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
epinject.go
199 lines (160 loc) · 4.39 KB
/
epinject.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package epinject
import (
"archive/tar"
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/docker/cli/cli/connhelper"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/hashicorp/go-hclog"
"github.com/oklog/ulid"
)
func dockerClient(ctx context.Context) (*client.Client, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, withConnectionHelper)
if err != nil {
return nil, err
}
cli.NegotiateAPIVersion(ctx)
return cli, nil
}
// To remove the entrypoint and reset it back to the docker default, return
// this value for Entrypoint. The docker commit API recognizes this value specially
// to reset the entrypoint.
var DockerDefaultEntrypoint = []string{""}
type NewEntrypoint struct {
NewImage string
Entrypoint []string
InjectFiles map[string]InjectFile
}
type InjectFile struct {
Reader io.Reader
Info os.FileInfo
}
func AlterEntrypoint(
ctx context.Context,
image string,
f func(cur []string) (*NewEntrypoint, error),
) (string, error) {
dc, err := dockerClient(ctx)
if err != nil {
return "", err
}
L := hclog.FromContext(ctx)
L.Debug("altering entrypoint of docker image", "image", image)
info, _, err := dc.ImageInspectWithRaw(ctx, image)
if err != nil {
return "", err
}
icfg := info.Config
L.Debug("extracted existing entrypoint", "image", image, "entrypoint", icfg.Entrypoint)
// Determine the new entrypoint configuration based on the existing
// entrypoint. Check if '/waypoint-entrypoint' is already found in the
// container's entrypoints and if so, don't execute the provided callback
// which would add the endpoint, and assume it's already included.
var newEp *NewEntrypoint
if containsEntrypoint(icfg.Entrypoint) {
newEp = new(NewEntrypoint)
} else {
newEp, err = f(icfg.Entrypoint)
if err != nil {
return "", err
}
}
if newEp.Entrypoint != nil {
icfg.Entrypoint = newEp.Entrypoint
}
if newEp.NewImage == "" {
newEp.NewImage = image
}
u, err := ulid.New(ulid.Now(), rand.Reader)
if err != nil {
return "", err
}
name := "epinject-" + u.String()
var (
cfg = container.Config{Image: image}
hostCfg = container.HostConfig{}
networkCfg = network.NetworkingConfig{}
)
body, err := dc.ContainerCreate(ctx, &cfg, &hostCfg, &networkCfg, nil, name)
if err != nil {
return "", err
}
// Force a cleanup of our temporary container
defer dc.ContainerRemove(ctx, body.ID, types.ContainerRemoveOptions{Force: true})
var buf bytes.Buffer
for container, f := range newEp.InjectFiles {
buf.Reset()
tw := tar.NewWriter(&buf)
hdr, err := tar.FileInfoHeader(f.Info, "")
if err != nil {
return "", err
}
hdr.Name = filepath.Base(container)
tw.WriteHeader(hdr)
io.Copy(tw, f.Reader)
err = dc.CopyToContainer(ctx, body.ID, filepath.Dir(container), &buf, types.CopyToContainerOptions{})
if err != nil {
return "", err
}
L.Debug("injected file into new image", "container", container)
}
if newEp.NewImage == image {
L.Debug("overwriting existing image with new image", "image", image)
} else {
L.Debug("creating new image", "image", newEp.NewImage)
}
idr, err := dc.ContainerCommit(ctx, body.ID, types.ContainerCommitOptions{
Reference: newEp.NewImage,
Comment: fmt.Sprintf("Alter image '%s' to modify entrypoint", image),
Config: icfg,
})
if err != nil {
return "", err
}
return idr.ID, nil
}
// withConnectionHelper applies a Docker-specific connection helper (concept from the
// Docker CLI) for a given daemon host. As an example, a connection helper makes it
// possible to use the client given a DOCKER_HOST with an ssh scheme.
func withConnectionHelper(c *client.Client) error {
host := c.DaemonHost()
helper, err := connhelper.GetConnectionHelper(host)
if err != nil {
return err
}
if helper == nil {
return nil
}
httpClient := &http.Client{
// No tls
// No proxy
Transport: &http.Transport{
DialContext: helper.Dialer,
},
}
opts := []client.Opt{
client.WithHTTPClient(httpClient),
client.WithHost(helper.Host),
client.WithDialContext(helper.Dialer),
}
// Apply options
for _, opt := range opts {
err := opt(c)
if err != nil {
return err
}
}
return nil
}
func containsEntrypoint(entrypoint []string) bool {
return len(entrypoint) > 0 && entrypoint[0] == "/waypoint-entrypoint"
}