• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes / kompose / 4217081086

19 Feb 2023 04:20PM UTC coverage: 53.126%. Remained the same
4217081086

push

github

GitHub
Bump golang.org/x/net from 0.5.0 to 0.7.0 (#1592)

1963 of 3695 relevant lines covered (53.13%)

7.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/utils/docker/build.go
1
/*
2
Copyright 2016 The Kubernetes Authors All rights reserved
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package docker
18

19
import (
20
        "bytes"
21
        "fmt"
22
        "os"
23
        "os/exec"
24
        "path"
25
        "strconv"
26
        "strings"
27

28
        dockerlib "github.com/fsouza/go-dockerclient"
29
        "github.com/kubernetes/kompose/pkg/utils/archive"
30
        "github.com/pkg/errors"
31
        log "github.com/sirupsen/logrus"
32
)
33

34
// Build will provide methods for interaction with API regarding building images
35
type Build struct {
36
        Client dockerlib.Client
37
}
38

39
/*
40
BuildImage builds a Docker image via the Docker API or Docker CLI.
41
Takes the source directory and image name and then builds the appropriate image. Tarball is utilized
42
in order to make building easier.
43

44
if the DOCKER_BUILDKIT is '1', then we will use the docker CLI to build the image
45
*/
46
func (c *Build) BuildImage(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg) error {
×
47
        log.Infof("Building image '%s' from directory '%s'", image, path.Base(source))
×
48

×
49
        outputBuffer := bytes.NewBuffer(nil)
×
50
        var err error
×
51

×
52
        if usecli, _ := strconv.ParseBool(os.Getenv("DOCKER_BUILDKIT")); usecli {
×
53
                err = buildDockerCli(source, image, dockerfile, buildargs, outputBuffer)
×
54
        } else {
×
55
                err = c.buildDockerClient(source, image, dockerfile, buildargs, outputBuffer)
×
56
        }
×
57

58
        log.Debugf("Image %s build output:\n%s", image, outputBuffer)
×
59

×
60
        if err != nil {
×
61
                return errors.Wrap(err, "Unable to build image. For more output, use -v or --verbose when converting.")
×
62
        }
×
63

64
        log.Infof("Image '%s' from directory '%s' built successfully", image, path.Base(source))
×
65

×
66
        return nil
×
67
}
68

69
func (c *Build) buildDockerClient(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, outputBuffer *bytes.Buffer) error {
×
70
        // Create a temporary file for tarball image packaging
×
71
        tmpFile, err := os.CreateTemp(os.TempDir(), "kompose-image-build-")
×
72
        if err != nil {
×
73
                return err
×
74
        }
×
75
        log.Debugf("Created temporary file %v for Docker image tarballing", tmpFile.Name())
×
76

×
77
        // Create a tarball of the source directory in order to build the resulting image
×
78
        err = archive.CreateTarball(strings.Join([]string{source, ""}, "/"), tmpFile.Name())
×
79
        if err != nil {
×
80
                return errors.Wrap(err, "Unable to create a tarball")
×
81
        }
×
82

83
        // Load the file into memory
84
        tarballSource, err := os.Open(tmpFile.Name())
×
85
        if err != nil {
×
86
                return errors.Wrap(err, "Unable to load tarball into memory")
×
87
        }
×
88

89
        // Let's create all the options for the image building.
90
        opts := dockerlib.BuildImageOptions{
×
91
                Name:         image,
×
92
                InputStream:  tarballSource,
×
93
                OutputStream: outputBuffer,
×
94
                Dockerfile:   dockerfile,
×
95
                BuildArgs:    buildargs,
×
96
        }
×
97

×
98
        // Build it!
×
99
        return c.Client.BuildImage(opts)
×
100
}
101

102
func buildDockerCli(source string, image string, dockerfile string, buildargs []dockerlib.BuildArg, outputBuffer *bytes.Buffer) error {
×
103
        args := []string{"build", "-t", image}
×
104

×
105
        if dockerfile != "" {
×
106
                args = append(args, "-f", dockerfile)
×
107
        }
×
108

109
        for _, buildarg := range buildargs {
×
110
                args = append(args, "--build-arg", fmt.Sprintf("%s=%s", buildarg.Name, buildarg.Value))
×
111
        }
×
112

113
        args = append(args, source)
×
114

×
115
        cmd := exec.Command("docker", args...)
×
116
        cmd.Stdout = outputBuffer
×
117
        cmd.Stderr = outputBuffer
×
118

×
119
        log.Debugf("Image %s build calling command %v", image, cmd)
×
120

×
121
        return cmd.Run()
×
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc