Просмотр исходного кода

Sidecar image to append resolvconf search directive

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 лет назад
Родитель
Сommit
b56e17cf27
5 измененных файлов с 143 добавлено и 0 удалено
  1. 23 0
      ecs/resolv/Dockerfile
  2. 38 0
      ecs/resolv/main/main.go
  3. 34 0
      ecs/resolv/resolv.go
  4. 46 0
      ecs/resolv/resolv_test.go
  5. 2 0
      ecs/resolv/testdata/resolv.conf.golden

+ 23 - 0
ecs/resolv/Dockerfile

@@ -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 - 0
ecs/resolv/main/main.go

@@ -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 - 0
ecs/resolv/resolv.go

@@ -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 - 0
ecs/resolv/resolv_test.go

@@ -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 - 0
ecs/resolv/testdata/resolv.conf.golden

@@ -0,0 +1,2 @@
+
+search foo bar zot