add integration test
This commit is contained in:
parent
8218bd7386
commit
93e47ba806
|
|
@ -0,0 +1,46 @@
|
||||||
|
# ingegration test for pre-defined build args; see pkg/dockerfile/buildargs.go
|
||||||
|
FROM alpine:3 AS builder
|
||||||
|
ARG BUILDPLATFORM
|
||||||
|
ARG BUILDOS
|
||||||
|
ARG BUILDARCH
|
||||||
|
ARG BUILDVARIANT
|
||||||
|
ARG TARGETPLATFORM
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
ARG TARGETVARIANT
|
||||||
|
ARG TARGETSTAGE
|
||||||
|
RUN echo "BUILDPLATFORM: ${BUILDPLATFORM}" && \
|
||||||
|
echo "BUILDOS: ${BUILDOS}" && \
|
||||||
|
echo "BUILDARCH: ${BUILDARCH}" && \
|
||||||
|
echo "BUILDVARIANT: ${BUILDVARIANT}" && \
|
||||||
|
echo "TARGETPLATFORM: ${TARGETPLATFORM}" && \
|
||||||
|
echo "TARGETOS: ${TARGETOS}" && \
|
||||||
|
echo "TARGETARCH: ${TARGETARCH}" && \
|
||||||
|
echo "TARGETVARIANT: ${TARGETVARIANT}" && \
|
||||||
|
echo "TARGETSTAGE: ${TARGETSTAGE}"
|
||||||
|
RUN uname -a
|
||||||
|
COPY foo /app/foo
|
||||||
|
RUN echo 'add line' >> /app/foo
|
||||||
|
|
||||||
|
FROM alpine:3
|
||||||
|
COPY --from=builder /app/foo /app/foo
|
||||||
|
ARG BUILDPLATFORM
|
||||||
|
ARG BUILDOS
|
||||||
|
ARG BUILDARCH
|
||||||
|
ARG BUILDVARIANT
|
||||||
|
ARG TARGETPLATFORM
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
ARG TARGETVARIANT
|
||||||
|
ARG TARGETSTAGE
|
||||||
|
RUN echo "BUILDPLATFORM: ${BUILDPLATFORM}" && \
|
||||||
|
echo "BUILDOS: ${BUILDOS}" && \
|
||||||
|
echo "BUILDARCH: ${BUILDARCH}" && \
|
||||||
|
echo "BUILDVARIANT: ${BUILDVARIANT}" && \
|
||||||
|
echo "TARGETPLATFORM: ${TARGETPLATFORM}" && \
|
||||||
|
echo "TARGETOS: ${TARGETOS}" && \
|
||||||
|
echo "TARGETARCH: ${TARGETARCH}" && \
|
||||||
|
echo "TARGETVARIANT: ${TARGETVARIANT}" && \
|
||||||
|
echo "TARGETSTAGE: ${TARGETSTAGE}"
|
||||||
|
RUN uname -a
|
||||||
|
RUN cat /app/foo
|
||||||
|
|
@ -17,14 +17,21 @@ limitations under the License.
|
||||||
package dockerfile
|
package dockerfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
||||||
|
"github.com/containerd/platforms"
|
||||||
d "github.com/docker/docker/builder/dockerfile"
|
d "github.com/docker/docker/builder/dockerfile"
|
||||||
|
containerregistryV1 "github.com/google/go-containerregistry/pkg/v1"
|
||||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var defaultArgsOnce sync.Once
|
||||||
|
var defaultArgs []string
|
||||||
|
|
||||||
type BuildArgs struct {
|
type BuildArgs struct {
|
||||||
d.BuildArgs
|
d.BuildArgs
|
||||||
}
|
}
|
||||||
|
|
@ -71,41 +78,44 @@ func (b *BuildArgs) AddMetaArgs(metaArgs []instructions.ArgCommand) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPreDefinedBuildArgs adds pre-defined build args. Such as TARGETOS, TARGETARCH, BUILDPLATFORM, TARGETPLATFORM
|
// DefaultArgs get default args.
|
||||||
func (b *BuildArgs) AddPreDefinedBuildArgs(opts *config.KanikoOptions) {
|
// include pre-defined build args: TARGETOS, TARGETARCH, BUILDPLATFORM, TARGETPLATFORM ...
|
||||||
buildPlatform := runtime.GOOS + "/" + runtime.GOARCH
|
func DefaultArgs(opt config.KanikoOptions) []string {
|
||||||
buildOs := runtime.GOOS
|
defaultArgsOnce.Do(func() {
|
||||||
buildArch := runtime.GOARCH
|
// build info
|
||||||
|
buildPlatform := platforms.Format(platforms.Normalize(platforms.DefaultSpec()))
|
||||||
targetPlatform := ""
|
buildPlatformSpec, err := containerregistryV1.ParsePlatform(buildPlatform)
|
||||||
targetOs := ""
|
if err != nil {
|
||||||
targetArch := ""
|
logrus.Fatalf("Parse build platform %q: %v", buildPlatform, err)
|
||||||
targetVariant := ""
|
|
||||||
|
|
||||||
if opts.CustomPlatform == "" {
|
|
||||||
targetPlatform = buildPlatform
|
|
||||||
targetOs = buildOs
|
|
||||||
targetArch = buildArch
|
|
||||||
} else {
|
|
||||||
targetPlatform = opts.CustomPlatform
|
|
||||||
platformParts := strings.Split(opts.CustomPlatform, "/")
|
|
||||||
if len(platformParts) > 0 {
|
|
||||||
targetOs = platformParts[0]
|
|
||||||
}
|
}
|
||||||
if len(platformParts) > 1 {
|
|
||||||
targetArch = platformParts[1]
|
|
||||||
}
|
|
||||||
if len(platformParts) > 2 {
|
|
||||||
targetVariant = platformParts[2]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b.AddArg("BUILDPLATFORM", &buildPlatform)
|
// target info
|
||||||
b.AddArg("BUILDOS", &buildOs)
|
var targetPlatform string
|
||||||
b.AddArg("BUILDARCH", &buildArch)
|
if opt.CustomPlatform != "" {
|
||||||
|
targetPlatform = opt.CustomPlatform
|
||||||
|
} else {
|
||||||
|
targetPlatform = buildPlatform
|
||||||
|
}
|
||||||
|
targetPlatformSpec, err := containerregistryV1.ParsePlatform(opt.CustomPlatform)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatalf("Invalid platform %q: %v", opt.CustomPlatform, err)
|
||||||
|
}
|
||||||
|
|
||||||
b.AddArg("TARGETPLATFORM", &targetPlatform)
|
// pre-defined build args
|
||||||
b.AddArg("TARGETOS", &targetOs)
|
defaultArgs = []string{
|
||||||
b.AddArg("TARGETARCH", &targetArch)
|
fmt.Sprintf("%s=%s", "BUILDPLATFORM", buildPlatform),
|
||||||
b.AddArg("TARGETVARIANT", &targetVariant)
|
fmt.Sprintf("%s=%s", "BUILDOS", buildPlatformSpec.OS),
|
||||||
|
fmt.Sprintf("%s=%s", "BUILDOSVERSION", buildPlatformSpec.OSVersion),
|
||||||
|
fmt.Sprintf("%s=%s", "BUILDARCH", buildPlatformSpec.Architecture),
|
||||||
|
fmt.Sprintf("%s=%s", "BUILDVARIANT", buildPlatformSpec.Variant),
|
||||||
|
fmt.Sprintf("%s=%s", "TARGETPLATFORM", targetPlatform),
|
||||||
|
fmt.Sprintf("%s=%s", "TARGETOS", targetPlatformSpec.OS),
|
||||||
|
fmt.Sprintf("%s=%s", "TARGETOSVERSION", targetPlatformSpec.OSVersion),
|
||||||
|
fmt.Sprintf("%s=%s", "TARGETARCH", targetPlatformSpec.Architecture),
|
||||||
|
fmt.Sprintf("%s=%s", "TARGETVARIANT", targetPlatformSpec.Variant),
|
||||||
|
fmt.Sprintf("%s=%s", "TARGETSTAGE", opt.Target),
|
||||||
|
}
|
||||||
|
logrus.Infof("init default args: %s", defaultArgs)
|
||||||
|
})
|
||||||
|
return defaultArgs
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -146,10 +146,11 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
|
||||||
if args != nil {
|
if args != nil {
|
||||||
s.args = args.Clone()
|
s.args = args.Clone()
|
||||||
} else {
|
} else {
|
||||||
s.args = dockerfile.NewBuildArgs(s.opts.BuildArgs)
|
defaultArgs := dockerfile.DefaultArgs(*opts)
|
||||||
|
mergedArgs := append(defaultArgs, s.opts.BuildArgs...)
|
||||||
|
s.args = dockerfile.NewBuildArgs(mergedArgs)
|
||||||
}
|
}
|
||||||
s.args.AddMetaArgs(s.stage.MetaArgs)
|
s.args.AddMetaArgs(s.stage.MetaArgs)
|
||||||
s.args.AddPreDefinedBuildArgs(opts)
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue