From 3ce3dca56d578aa52f26b31ddb0aa8d45ecfbe6e Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Feb 2018 12:05:42 -0800 Subject: [PATCH] Image package to append layers and push final image --- pkg/constants/constants.go | 3 ++ pkg/image/image.go | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 pkg/image/image.go diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index afe15df5b..28362e01f 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -24,4 +24,7 @@ const ( RootDir = "/" WhitelistPath = "/proc/self/mountinfo" + + // PolicyJSONPath is the path to the policy JSON + PolicyJSONPath = "/workspace/policy.json" ) diff --git a/pkg/image/image.go b/pkg/image/image.go new file mode 100644 index 000000000..c58ad0703 --- /dev/null +++ b/pkg/image/image.go @@ -0,0 +1,79 @@ +/* +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 image + +import ( + img "github.com/GoogleCloudPlatform/container-diff/pkg/image" + "github.com/GoogleCloudPlatform/k8s-container-builder/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 +var sourceImage img.MutableSource + +// InitializeSourceImage initializes the source image with the base image +func InitializeSourceImage(srcImg string) error { + ref, err := docker.ParseReference("//" + srcImg) + if err != nil { + return err + } + ms, err := img.NewMutableSource(ref) + if err != nil { + return err + } + sourceImage = *ms + return nil +} + +// AppendLayer appends a layer onto the base image +func AppendLayer(contents []byte) error { + return sourceImage.AppendLayer(contents) +} + +// PushImage pushes the final image +func PushImage(destImg string) error { + srcRef := &img.ProxyReference{ + ImageReference: nil, + Src: &sourceImage, + } + 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) + err = copy.Image(policyContext, destRef, srcRef, nil) + return err +} + +func getPolicyContext() (*signature.PolicyContext, error) { + policy, err := signature.NewPolicyFromFile(constants.PolicyJsonPath) + if err != nil { + logrus.Debugf("Error retrieving policy: %s", err) + return nil, err + } + policyContext, err := signature.NewPolicyContext(policy) + if err != nil { + logrus.Debugf("Error retrieving policy context: %s", err) + return nil, err + } + return policyContext, nil +}