Skip to content

Commit

Permalink
Merged master, fixed conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Apr 4, 2018
2 parents e5542e8 + 9917446 commit 32b067a
Show file tree
Hide file tree
Showing 272 changed files with 59,198 additions and 39 deletions.
139 changes: 136 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EXECUTOR_PACKAGE = $(REPOPATH)/executor
KBUILD_PACKAGE = $(REPOPATH)/kbuild

out/executor: $(GO_FILES)
GOOS=$* GOARCH=$(GOARCH) CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -tags $(GO_BUILD_TAGS) -o $@ $(EXECUTOR_PACKAGE)
GOARCH=$(GOARCH) GOOS=linux CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -tags $(GO_BUILD_TAGS) -o $@ $(EXECUTOR_PACKAGE)


out/kbuild: $(GO_FILES)
Expand Down
1 change: 1 addition & 0 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ ADD files/docker-credential-gcr /usr/local/bin/
ADD files/config.json /root/.docker/
ENV HOME /root
ENV PATH /usr/local/bin
ENTRYPOINT ["/kbuild/executor"]
55 changes: 54 additions & 1 deletion executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ import (
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/image"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/snapshot"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
)

var (
dockerfilePath string
destination string
srcContext string
bucket string
logLevel string
)

func init() {
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "/workspace/Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "", "Path to the dockerfile build context.")
RootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)")
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic")
}
Expand All @@ -49,13 +53,44 @@ var RootCmd = &cobra.Command{
return util.SetLogLevel(logLevel)
},
Run: func(cmd *cobra.Command, args []string) {
if err := resolveSourceContext(); err != nil {
logrus.Error(err)
os.Exit(1)
}
if err := execute(); err != nil {
logrus.Error(err)
os.Exit(1)
}
},
}

// resolveSourceContext unpacks the source context if it is a tar in a GCS bucket
// it resets srcContext to be the path to the unpacked build context within the image
func resolveSourceContext() error {
if srcContext == "" && bucket == "" {
return errors.New("please specify a path to the build context with the --context flag or a GCS bucket with the --bucket flag")
}
if srcContext != "" && bucket != "" {
return errors.New("please specify either --bucket or --context as the desired build context")
}
if srcContext != "" {
return nil
}
logrus.Infof("Using GCS bucket %s as source context", bucket)
buildContextPath := constants.BuildContextDir
if err := util.UnpackTarFromGCSBucket(bucket, buildContextPath); err != nil {
return err
}
logrus.Debugf("Unpacked tar from %s to path %s", bucket, buildContextPath)
srcContext = buildContextPath
// If path to dockerfile doesn't exist, assume it is in the unpacked tar
if !util.FilepathExists(dockerfilePath) {
logrus.Debugf("Expecting dockerfile to be located at %s within the tar build context", dockerfilePath)
dockerfilePath = filepath.Join(srcContext, dockerfilePath)
}
return nil
}

func execute() error {
// Parse dockerfile and unpack base image to root
d, err := ioutil.ReadFile(dockerfilePath)
Expand Down Expand Up @@ -123,5 +158,23 @@ func execute() error {
}
}
// Push the image
if err := setDefaultEnv(); err != nil {
return err
}
return image.PushImage(sourceImage, destination)
}

// setDefaultEnv sets default values for HOME and PATH so that
// config.json and docker-credential-gcr can be accessed
func setDefaultEnv() error {
defaultEnvs := map[string]string{
"HOME": "/root",
"PATH": "/usr/local/bin/",
}
for key, val := range defaultEnvs {
if err := os.Setenv(key, val); err != nil {
return err
}
}
return nil
}
19 changes: 19 additions & 0 deletions integration_tests/dockerfiles/Dockerfile_test_user_run
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018 Google, Inc. All rights reserved.
#
# 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.

FROM gcr.io/google-appengine/debian9
RUN useradd testuser
RUN groupadd testgroup
USER testuser:testgroup
RUN echo "hey" > /tmp/foo
12 changes: 12 additions & 0 deletions integration_tests/dockerfiles/config_test_bucket_buildcontext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"Image1": "gcr.io/kbuild-test/docker-test-bucket-buildcontext:latest",
"Image2": "gcr.io/kbuild-test/kbuild-test-bucket-buildcontext:latest",
"DiffType": "File",
"Diff": {
"Adds": null,
"Dels": null,
"Mods": null
}
}
]
15 changes: 15 additions & 0 deletions integration_tests/dockerfiles/test_user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
schemaVersion: '2.0.0'
commandTests:
- name: 'whoami'
command: 'whoami'
expectedOutput: ['testuser']
excludedOutput: ['root']
- name: 'file owner'
command: 'ls'
args: ['-l', '/tmp/foo']
expectedOutput: ['.*testuser.*', '.*testgroup.*']
excludedOutput: ['.*root.*']
fileContentTests:
- name: "/tmp/foo"
path: "/tmp/foo"
expectedContent: ["hey"]
Loading

0 comments on commit 32b067a

Please sign in to comment.