-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathbuild.go
271 lines (227 loc) · 9.37 KB
/
build.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Copyright 2019 Google LLC All Rights Reserved.
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 options
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/tools/go/packages"
"github.com/google/ko/pkg/build"
)
const (
// configDefaultBaseImage is the default base image if not specified in .ko.yaml.
configDefaultBaseImage = "gcr.io/distroless/static:nonroot"
)
// BuildOptions represents options for the ko builder.
type BuildOptions struct {
// BaseImage enables setting the default base image programmatically.
// If non-empty, this takes precedence over the value in `.ko.yaml`.
BaseImage string
// BaseImageOverrides stores base image overrides for import paths.
BaseImageOverrides map[string]string
// WorkingDirectory allows for setting the working directory for invocations of the `go` tool.
// Empty string means the current working directory.
WorkingDirectory string
ConcurrentBuilds int
DisableOptimizations bool
SBOM string
Platforms []string
Labels []string
// UserAgent enables overriding the default value of the `User-Agent` HTTP
// request header used when retrieving the base image.
UserAgent string
InsecureRegistry bool
// Trimpath controls whether ko adds the `-trimpath` flag to `go build` by default.
// The `-trimpath` flags aids in achieving reproducible builds, but it removes path information that is useful for interactive debugging.
// Set this field to `false` and `DisableOptimizations` to `true` if you want to interactively debug the binary in the resulting image.
// `AddBuildOptions()` defaults this field to `true`.
Trimpath bool
// BuildConfigs stores the per-image build config from `.ko.yaml`.
BuildConfigs map[string]build.Config
// If true, don't convert Docker-typed base images to OCI when building.
PreserveMediaType bool
// ImageConfigs stores the per-image image config from `.ko.yaml`.
ImageConfigs map[string]*build.ImageConfig `yaml:",omitempty"`
// DockerRepo stores the docker repo from `.ko.yaml`.
DockerRepo string `yaml:",omitempty"`
// DefaultImageConfig stores the default image config for all the images ko built from `.ko.yaml`.
DefaultImageConfig *build.ImageConfig `yaml:",omitempty"`
}
func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
cmd.Flags().IntVarP(&bo.ConcurrentBuilds, "jobs", "j", 0,
"The maximum number of concurrent builds (default GOMAXPROCS)")
cmd.Flags().BoolVar(&bo.DisableOptimizations, "disable-optimizations", bo.DisableOptimizations,
"Disable optimizations when building Go code. Useful when you want to interactively debug the created container.")
cmd.Flags().StringVar(&bo.SBOM, "sbom", "spdx",
"The SBOM media type to use (none will disable SBOM synthesis and upload, also supports: spdx, go.version-m).")
cmd.Flags().StringSliceVar(&bo.Platforms, "platform", []string{},
"Which platform to use when pulling a multi-platform base. Format: all | <os>[/<arch>[/<variant>]][,platform]*")
cmd.Flags().StringSliceVar(&bo.Labels, "image-label", []string{},
"Which labels (key=value) to add to the image.")
cmd.Flags().BoolVar(&bo.PreserveMediaType, "preserve-media-type", false, "If false, push images in OCI format regardless of base image format")
bo.Trimpath = true
}
// LoadConfig reads build configuration from defaults, environment variables, and the `.ko.yaml` config file.
func (bo *BuildOptions) LoadConfig() error {
v := viper.New()
if bo.WorkingDirectory == "" {
bo.WorkingDirectory = "."
}
// If omitted, use this base image.
v.SetDefault("defaultBaseImage", configDefaultBaseImage)
const configName = ".ko"
v.SetConfigName(configName) // .yaml is implicit
v.SetEnvPrefix("KO")
v.AutomaticEnv()
if override := os.Getenv("KO_CONFIG_PATH"); override != "" {
path := filepath.Join(override, configName+".yaml")
file, err := os.Stat(path)
if err != nil {
return fmt.Errorf("error looking for config file: %w", err)
}
if !file.Mode().IsRegular() {
return fmt.Errorf("config file %s is not a regular file", path)
}
v.AddConfigPath(override)
}
v.AddConfigPath(bo.WorkingDirectory)
if err := v.ReadInConfig(); err != nil {
if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
return fmt.Errorf("error reading config file: %w", err)
}
}
if bo.BaseImage == "" {
ref := v.GetString("defaultBaseImage")
if _, err := name.ParseReference(ref); err != nil {
return fmt.Errorf("'defaultBaseImage': error parsing %q as image reference: %w", ref, err)
}
bo.BaseImage = ref
}
if len(bo.BaseImageOverrides) == 0 {
baseImageOverrides := map[string]string{}
overrides := v.GetStringMapString("baseImageOverrides")
for key, value := range overrides {
if _, err := name.ParseReference(value); err != nil {
return fmt.Errorf("'baseImageOverrides': error parsing %q as image reference: %w", value, err)
}
baseImageOverrides[key] = value
}
bo.BaseImageOverrides = baseImageOverrides
}
if len(bo.BuildConfigs) == 0 {
var builds []build.Config
if err := v.UnmarshalKey("builds", &builds); err != nil {
return fmt.Errorf("configuration section 'builds' cannot be parsed")
}
buildConfigs, err := createBuildConfigMap(bo.WorkingDirectory, builds)
if err != nil {
return fmt.Errorf("could not create build config map: %w", err)
}
bo.BuildConfigs = buildConfigs
}
if len(bo.ImageConfigs) == 0 {
var imageConfigs []build.ImageConfig
if err := v.UnmarshalKey("images", &imageConfigs); err != nil {
return fmt.Errorf("configuration section 'images' cannot be parsed")
}
imageConfigsPerImage, err := createImageConfigMap(imageConfigs, bo.BuildConfigs)
if err != nil {
return fmt.Errorf("could not create image config map: %w", err)
}
bo.ImageConfigs = imageConfigsPerImage
}
if bo.DockerRepo == "" {
bo.DockerRepo = v.GetString("dockerRepo")
}
if bo.DefaultImageConfig == nil {
var defaultImageConfig build.ImageConfig
if err := v.UnmarshalKey("defaultImageConfig", &defaultImageConfig); err != nil {
return fmt.Errorf("configuration section 'defaultImageConfig' cannot be parsed")
}
bo.DefaultImageConfig = &defaultImageConfig
}
return nil
}
func createImageConfigMap(imageConfigs []build.ImageConfig, buildConfigs map[string]build.Config) (map[string]*build.ImageConfig, error) {
imageConfigsPerImage := make(map[string]*build.ImageConfig)
for ip, bc := range buildConfigs {
for i, config := range imageConfigs {
if config.ID == "" {
config.ID = fmt.Sprintf("#%d", i)
}
if config.Annotations == nil {
config.Annotations = map[string]string{}
}
if config.Labels == nil {
config.Labels = map[string]string{}
}
if bc.ID == config.ID {
imageConfigsPerImage[ip] = &config
}
}
}
return imageConfigsPerImage, nil
}
func createBuildConfigMap(workingDirectory string, configs []build.Config) (map[string]build.Config, error) {
buildConfigsByImportPath := make(map[string]build.Config)
for i, config := range configs {
// In case no ID is specified, use the index of the build config in
// the ko YAML file as a reference (debug help).
if config.ID == "" {
config.ID = fmt.Sprintf("#%d", i)
}
// Make sure to behave like GoReleaser by defaulting to the current
// directory in case the build or main field is not set, check
// https://goreleaser.com/customization/build/ for details
if config.Dir == "" {
config.Dir = "."
}
if config.Main == "" {
config.Main = "."
}
// baseDir is the directory where `go list` will be run to look for package information
baseDir := filepath.Join(workingDirectory, config.Dir)
// To behave like GoReleaser, check whether the configured `main` config value points to a
// source file, and if so, just use the directory it is in
path := config.Main
if fi, err := os.Stat(filepath.Join(baseDir, config.Main)); err == nil && fi.Mode().IsRegular() {
path = filepath.Dir(config.Main)
}
// Verify that the path actually leads to a local file (https://github.com/google/ko/issues/483)
if _, err := os.Stat(filepath.Join(baseDir, path)); err != nil {
return nil, err
}
// By default, paths configured in the builds section are considered
// local import paths, therefore add a "./" equivalent as a prefix to
// the constructured import path
localImportPath := fmt.Sprint(".", string(filepath.Separator), path)
dir := filepath.Clean(baseDir)
if dir == "." {
dir = ""
}
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName, Dir: dir}, localImportPath)
if err != nil {
return nil, fmt.Errorf("'builds': entry #%d does not contain a valid local import path (%s) for directory (%s): %w", i, localImportPath, baseDir, err)
}
if len(pkgs) != 1 {
return nil, fmt.Errorf("'builds': entry #%d results in %d local packages, only 1 is expected", i, len(pkgs))
}
importPath := pkgs[0].PkgPath
buildConfigsByImportPath[importPath] = config
}
return buildConfigsByImportPath, nil
}