forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
128 lines (106 loc) · 3.95 KB
/
cmd.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
// Copyright 2018 The Operator-SDK 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 build
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
imageBuildArgs string
imageBuilder string
)
func NewCmd() *cobra.Command {
buildCmd := &cobra.Command{
Use: "build <image>",
Short: "Compiles code and builds artifacts",
Long: `The operator-sdk build command compiles the code, builds the executables,
and generates Kubernetes manifests.
<image> is the container image to be built, e.g. "quay.io/example/operator:v0.0.1".
This image will be automatically set in the deployment manifests.
After build completes, the image would be built locally in docker. Then it needs to
be pushed to remote registry.
For example:
$ operator-sdk build quay.io/example/operator:v0.0.1
$ docker push quay.io/example/operator:v0.0.1
`,
RunE: buildFunc,
}
buildCmd.Flags().StringVar(&imageBuildArgs, "image-build-args", "", "Extra image build arguments as one string such as \"--build-arg https_proxy=$https_proxy\"")
buildCmd.Flags().StringVar(&imageBuilder, "image-builder", "docker", "Tool to build OCI images. One of: [docker, buildah]")
return buildCmd
}
func createBuildCommand(imageBuilder, context, dockerFile, image string, imageBuildArgs ...string) (*exec.Cmd, error) {
var args []string
switch imageBuilder {
case "docker":
args = append(args, "build", "-f", dockerFile, "-t", image)
case "buildah":
args = append(args, "bud", "--format=docker", "-f", dockerFile, "-t", image)
default:
return nil, fmt.Errorf("%s is not supported image builder", imageBuilder)
}
for _, bargs := range imageBuildArgs {
if bargs != "" {
splitArgs := strings.Fields(bargs)
args = append(args, splitArgs...)
}
}
args = append(args, context)
return exec.Command(imageBuilder, args...), nil
}
func buildFunc(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("command %s requires exactly one argument", cmd.CommandPath())
}
projutil.MustInProjectRoot()
goBuildEnv := append(os.Environ(), "GOOS=linux", "GOARCH=amd64")
// If CGO_ENABLED is not set, set it to '0'.
if _, ok := os.LookupEnv("CGO_ENABLED"); !ok {
goBuildEnv = append(goBuildEnv, "CGO_ENABLED=0")
}
goTrimFlags := []string{"-gcflags", "all=-trimpath=${GOPATH}", "-asmflags", "all=-trimpath=${GOPATH}"}
absProjectPath := projutil.MustGetwd()
projectName := filepath.Base(absProjectPath)
// Don't need to build Go code if a non-Go Operator.
if projutil.IsOperatorGo() {
opts := projutil.GoCmdOptions{
BinName: filepath.Join(absProjectPath, scaffold.BuildBinDir, projectName),
PackagePath: filepath.Join(projutil.CheckAndGetProjectGoPkg(), scaffold.ManagerDir),
Args: goTrimFlags,
Env: goBuildEnv,
GoMod: projutil.IsDepManagerGoMod(),
}
if err := projutil.GoBuild(opts); err != nil {
return fmt.Errorf("failed to build operator binary: (%v)", err)
}
}
image := args[0]
log.Infof("Building OCI image %s", image)
buildCmd, err := createBuildCommand(imageBuilder, ".", "build/Dockerfile", image, imageBuildArgs)
if err != nil {
return err
}
if err := projutil.ExecCmd(buildCmd); err != nil {
return fmt.Errorf("failed to output build image %s: (%v)", image, err)
}
log.Info("Operator build complete.")
return nil
}