-
Notifications
You must be signed in to change notification settings - Fork 42
/
image_build_spec.go
79 lines (65 loc) · 2.82 KB
/
image_build_spec.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
package image_build_spec
import (
"encoding/json"
"github.com/kurtosis-tech/stacktrace"
)
type ImageBuildSpec struct {
// we do this way in order to have exported fields which can be marshalled
// and an unexported type for encapsulation
privateImageBuildSpec *privateImageBuildSpec
}
// ImageBuildSpec contains the information need for building a container image.
type privateImageBuildSpec struct {
// Location of the container image to build (eg. Dockerfile) on the machine
ContainerImageFilePath string
// Location of the build context needed for building the container image
// Build context are the files, directories, config referenced in the container image used to build the image
ContextDirPath string
// For multi-stage image builds, targetStage specifies the stage to actually build
// For Docker: using the same Dockerfile, you can specify building multiple binaries in the same Dockerfile.
// Then, you can specify the targetStage to instruct Docker which binary to build when building the image.
// eg. (info here: https://docs.docker.com/build/guide/multi-stage/#build-targets)
// ...
//+ FROM scratch AS client
//+ COPY --from=build-client /bin/client /bin/
//+ ENTRYPOINT [ "/bin/client" ]
//
//+ FROM scratch AS server
//+ COPY --from=build-server /bin/server /bin/
//+ ENTRYPOINT [ "/bin/server" ]
// ...
// targetStage could be set to "server" or "client",
// Default value is the empty string if the image build is not multi-stage.
//
TargetStage string
}
func NewImageBuildSpec(contextDirPath string, containerImageFilePath string, targetStage string) *ImageBuildSpec {
internalImageBuildSpec := &privateImageBuildSpec{
ContainerImageFilePath: containerImageFilePath,
ContextDirPath: contextDirPath,
TargetStage: targetStage,
}
return &ImageBuildSpec{internalImageBuildSpec}
}
func (imageBuildSpec *ImageBuildSpec) GetContainerImageFilePath() string {
return imageBuildSpec.privateImageBuildSpec.ContainerImageFilePath
}
func (imageBuildSpec *ImageBuildSpec) GetBuildContextDir() string {
return imageBuildSpec.privateImageBuildSpec.ContextDirPath
}
func (imageBuildSpec *ImageBuildSpec) GetTargetStage() string {
return imageBuildSpec.privateImageBuildSpec.TargetStage
}
func (imageBuildSpec *ImageBuildSpec) MarshalJSON() ([]byte, error) {
return json.Marshal(imageBuildSpec.privateImageBuildSpec)
}
func (imageBuildSpec *ImageBuildSpec) UnmarshalJSON(data []byte) error {
// Suppressing exhaustruct requirement because we want an object with zero values
// nolint: exhaustruct
unmarshalledPrivateStructPtr := &privateImageBuildSpec{}
if err := json.Unmarshal(data, unmarshalledPrivateStructPtr); err != nil {
return stacktrace.Propagate(err, "An error occurred unmarshalling the private struct")
}
imageBuildSpec.privateImageBuildSpec = unmarshalledPrivateStructPtr
return nil
}