mirror of
https://github.com/docker/compose.git
synced 2025-07-22 21:24:38 +02:00
Sidecar image to append resolvconf search directive
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
3685cbbf8d
commit
b56e17cf27
23
ecs/resolv/Dockerfile
Normal file
23
ecs/resolv/Dockerfile
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
FROM golang:1.14.4-alpine AS builder
|
||||||
|
WORKDIR $GOPATH/src/github.com/docker/compose-cli/ecs/resolv
|
||||||
|
COPY . .
|
||||||
|
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/resolv main/main.go
|
||||||
|
RUN chmod +x /go/bin/resolv
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
COPY --from=builder /go/bin/resolv /resolv
|
||||||
|
ENTRYPOINT ["/resolv"]
|
38
ecs/resolv/main/main.go
Normal file
38
ecs/resolv/main/main.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/ecs/resolv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const resolvconf = "/etc/resolv.conf"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fmt.Fprint(os.Stderr, "usage: resolv DOMAIN [DOMAIN]")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := resolv.SetSearchDomains(resolvconf, os.Args[1:]...)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(os.Stderr, err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
34
ecs/resolv/resolv.go
Normal file
34
ecs/resolv/resolv.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
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 resolv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetSearchDomains(file string, domains ...string) error {
|
||||||
|
search := strings.Join(domains, " ")
|
||||||
|
|
||||||
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
_, err = f.WriteString("\nsearch " + search)
|
||||||
|
return err
|
||||||
|
}
|
46
ecs/resolv/resolv_test.go
Normal file
46
ecs/resolv/resolv_test.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
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 resolv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
"gotest.tools/v3/fs"
|
||||||
|
"gotest.tools/v3/golden"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetDomain(t *testing.T) {
|
||||||
|
dir := fs.NewDir(t, "resolv").Path()
|
||||||
|
f := filepath.Join(dir, "resolv.conf")
|
||||||
|
touch(t, f)
|
||||||
|
|
||||||
|
err := SetSearchDomains(f, "foo", "bar", "zot")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
got, err := ioutil.ReadFile(f)
|
||||||
|
golden.Assert(t, string(got), "resolv.conf.golden")
|
||||||
|
}
|
||||||
|
|
||||||
|
func touch(t *testing.T, f string) {
|
||||||
|
file, err := os.Create(f)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
defer file.Close()
|
||||||
|
}
|
2
ecs/resolv/testdata/resolv.conf.golden
vendored
Normal file
2
ecs/resolv/testdata/resolv.conf.golden
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
search foo bar zot
|
Loading…
x
Reference in New Issue
Block a user