mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 17:32:58 +02:00
I added a KanikoStage to hold each stage of the Dockerfile along with information about each stage that would be useful later on. The new KanikoStage type holds the stage itself, along with some additional information: 1. FinalStage -- whether the current stage is the final stage 2. BaseImageStoredLocally/BaseImageIndex -- whether the base image for this stage is stored locally, and if so what the index of the base image is 3. SaveStage -- whether this stage needs to be saved for use in a future stage This is the first part of a larger refactor for building stages, which will later make it easier to add layer caching.
94 lines
3.0 KiB
Go
94 lines
3.0 KiB
Go
/*
|
|
Copyright 2018 Google LLC
|
|
|
|
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 util
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"github.com/google/go-containerregistry/pkg/authn"
|
|
"github.com/google/go-containerregistry/pkg/authn/k8schain"
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
"github.com/google/go-containerregistry/pkg/v1"
|
|
"github.com/google/go-containerregistry/pkg/v1/empty"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
|
"github.com/google/go-containerregistry/pkg/v1/tarball"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
|
"github.com/GoogleContainerTools/kaniko/pkg/constants"
|
|
)
|
|
|
|
var (
|
|
// For testing
|
|
retrieveRemoteImage = remoteImage
|
|
retrieveTarImage = tarballImage
|
|
)
|
|
|
|
// RetrieveSourceImage returns the base image of the stage at index
|
|
func RetrieveSourceImage(stage config.KanikoStage, buildArgs []string) (v1.Image, error) {
|
|
currentBaseName, err := ResolveEnvironmentReplacement(stage.BaseName, buildArgs, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// First, check if the base image is a scratch image
|
|
if currentBaseName == constants.NoBaseImage {
|
|
logrus.Info("No base image, nothing to extract")
|
|
return empty.Image, nil
|
|
}
|
|
// Next, check if the base image of the current stage is built from a previous stage
|
|
// If so, retrieve the image from the stored tarball
|
|
if stage.BaseImageStoredLocally {
|
|
return retrieveTarImage(stage.BaseImageIndex)
|
|
}
|
|
|
|
// Otherwise, initialize image as usual
|
|
return retrieveRemoteImage(currentBaseName)
|
|
}
|
|
|
|
// RetrieveConfigFile returns the config file for an image
|
|
func RetrieveConfigFile(sourceImage v1.Image) (*v1.ConfigFile, error) {
|
|
imageConfig, err := sourceImage.ConfigFile()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if sourceImage == empty.Image {
|
|
imageConfig.Config.Env = constants.ScratchEnvVars
|
|
}
|
|
return imageConfig, nil
|
|
}
|
|
|
|
func tarballImage(index int) (v1.Image, error) {
|
|
tarPath := filepath.Join(constants.KanikoIntermediateStagesDir, strconv.Itoa(index))
|
|
logrus.Infof("Base image from previous stage %d found, using saved tar at path %s", index, tarPath)
|
|
return tarball.ImageFromPath(tarPath, nil)
|
|
}
|
|
|
|
func remoteImage(image string) (v1.Image, error) {
|
|
logrus.Infof("Downloading base image %s", image)
|
|
ref, err := name.ParseReference(image, name.WeakValidation)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
k8sc, err := k8schain.NewNoClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
kc := authn.NewMultiKeychain(authn.DefaultKeychain, k8sc)
|
|
return remote.Image(ref, remote.WithAuthFromKeychain(kc))
|
|
}
|