Switch from containers/image to go-containerregistry (#140)

* Vendor changes for go-containerregistry switch.

* Manual changes for go-containerregistry switch.

The biggest change is refactoring the tarball unpacking.

* Pull more of container-diff out.

* More vendor removals.

* More unit tests.
This commit is contained in:
dlorenc
2018-04-25 19:21:05 -07:00
committed by GitHub
parent 806555538f
commit cd5b744904
994 changed files with 5363 additions and 141520 deletions
+12 -65
View File
@@ -17,80 +17,27 @@ limitations under the License.
package image
import (
"fmt"
"os"
"strings"
"github.com/GoogleContainerTools/kaniko/pkg/version"
"github.com/containers/image/types"
"github.com/google/go-containerregistry/v1"
img "github.com/GoogleContainerTools/container-diff/pkg/image"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/containers/image/copy"
"github.com/containers/image/docker"
"github.com/containers/image/signature"
"github.com/containers/image/transports/alltransports"
"github.com/sirupsen/logrus"
)
// sourceImage is the image that will be modified by the executor
// NewSourceImage initializes the source image with the base image
func NewSourceImage(srcImg string) (*img.MutableSource, error) {
if srcImg == constants.NoBaseImage {
return img.NewMutableSource(nil)
}
logrus.Infof("Initializing source image %s", srcImg)
ref, err := docker.ParseReference("//" + srcImg)
if err != nil {
return nil, err
}
return img.NewMutableSource(ref)
}
// PushImage pushes the final image
func PushImage(ms *img.MutableSource, destImg string, dockerInsecureSkipTLSVerify bool) error {
srcRef := &img.ProxyReference{
ImageReference: nil,
Src: ms,
}
destRef, err := alltransports.ParseImageName("docker://" + destImg)
if err != nil {
return err
}
policyContext, err := getPolicyContext()
if err != nil {
return err
}
logrus.Infof("Pushing image to %s", destImg)
opts := &copy.Options{
DestinationCtx: &types.SystemContext{
DockerRegistryUserAgent: fmt.Sprintf("kaniko/executor-%s", version.Version()),
DockerInsecureSkipTLSVerify: dockerInsecureSkipTLSVerify,
},
}
return copy.Image(policyContext, destRef, srcRef, opts)
}
// SetEnvVariables sets environment variables as specified in the image
func SetEnvVariables(ms *img.MutableSource) error {
envVars := ms.Env()
for key, val := range envVars {
if err := os.Setenv(key, val); err != nil {
func SetEnvVariables(img v1.Image) error {
cfg, err := img.ConfigFile()
if err != nil {
return err
}
envVars := cfg.Config.Env
for _, envVar := range envVars {
split := strings.SplitN(envVar, "=", 2)
if err := os.Setenv(split[0], split[1]); err != nil {
return err
}
logrus.Debugf("Setting environment variable %s=%s", key, val)
logrus.Infof("Setting environment variable %s", envVar)
}
return nil
}
func getPolicyContext() (*signature.PolicyContext, error) {
policyContext, err := signature.NewPolicyContext(&signature.Policy{
Default: signature.PolicyRequirements{signature.NewPRInsecureAcceptAnything()},
})
if err != nil {
logrus.Debugf("Error retrieving policy context: %s", err)
return nil, err
}
return policyContext, nil
}