From 3a64ab3535475b02bd04ae80d0487eddc7d1f5ad Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Wed, 30 Oct 2019 11:29:25 +0800 Subject: [PATCH 1/7] Add trimpath arg to gobuild --- pkg/build/gobuild.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index 7ac8af1459..30af87d5f4 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -154,13 +154,14 @@ func build(ip string, platform v1.Platform, disableOptimizations bool) (string, } file := filepath.Join(tmpDir, "out") - args := make([]string, 0, 6) + args := make([]string, 0, 7) args = append(args, "build") if disableOptimizations { // Disable optimizations (-N) and inlining (-l). args = append(args, "-gcflags", "all=-N -l") } args = append(args, "-o", file) + args = append(args, "-trimpath") args = append(args, ip) cmd := exec.Command("go", args...) From 4d6310050285f6e33c8b9bc310643583c01e3fb2 Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Thu, 31 Oct 2019 12:43:51 +0800 Subject: [PATCH 2/7] Add build constraints for trimpath usage --- pkg/build/gbbuild_113.go | 68 ++++++++++++++++++++++++++++++++++++++ pkg/build/gbbuild_pre13.go | 67 +++++++++++++++++++++++++++++++++++++ pkg/build/gobuild.go | 40 ---------------------- 3 files changed, 135 insertions(+), 40 deletions(-) create mode 100644 pkg/build/gbbuild_113.go create mode 100644 pkg/build/gbbuild_pre13.go diff --git a/pkg/build/gbbuild_113.go b/pkg/build/gbbuild_113.go new file mode 100644 index 0000000000..441cc0ab88 --- /dev/null +++ b/pkg/build/gbbuild_113.go @@ -0,0 +1,68 @@ +// Copyright 2018 Google LLC 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. +// +// house gb.build function for go1.13 and later version with -trimpath available +// +build go1.13 + +package build + +import ( + "bytes" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + + v1 "github.com/google/go-containerregistry/pkg/v1" +) + +func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { + tmpDir, err := ioutil.TempDir("", "ko") + if err != nil { + return "", err + } + file := filepath.Join(tmpDir, "out") + + args := make([]string, 0, 7) + args = append(args, "build") + if disableOptimizations { + // Disable optimizations (-N) and inlining (-l). + args = append(args, "-gcflags", "all=-N -l") + } + args = append(args, "-o", file) + args = append(args, "-trimpath") + args = append(args, ip) + cmd := exec.Command("go", args...) + + // Last one wins + defaultEnv := []string{ + "CGO_ENABLED=0", + "GOOS=" + platform.OS, + "GOARCH=" + platform.Architecture, + } + cmd.Env = append(defaultEnv, os.Environ()...) + + var output bytes.Buffer + cmd.Stderr = &output + cmd.Stdout = &output + + log.Printf("Building %s", ip) + if err := cmd.Run(); err != nil { + os.RemoveAll(tmpDir) + log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String()) + return "", err + } + return file, nil +} diff --git a/pkg/build/gbbuild_pre13.go b/pkg/build/gbbuild_pre13.go new file mode 100644 index 0000000000..dd0273037c --- /dev/null +++ b/pkg/build/gbbuild_pre13.go @@ -0,0 +1,67 @@ +// Copyright 2018 Google LLC 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. +// +// house gb.build function for go1.12 and earlier version without -trimpath +// +build !go1.13 + +package build + +import ( + "bytes" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + + v1 "github.com/google/go-containerregistry/pkg/v1" +) + +func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { + tmpDir, err := ioutil.TempDir("", "ko") + if err != nil { + return "", err + } + file := filepath.Join(tmpDir, "out") + + args := make([]string, 0, 6) + args = append(args, "build") + if disableOptimizations { + // Disable optimizations (-N) and inlining (-l). + args = append(args, "-gcflags", "all=-N -l") + } + args = append(args, "-o", file) + args = append(args, ip) + cmd := exec.Command("go", args...) + + // Last one wins + defaultEnv := []string{ + "CGO_ENABLED=0", + "GOOS=" + platform.OS, + "GOARCH=" + platform.Architecture, + } + cmd.Env = append(defaultEnv, os.Environ()...) + + var output bytes.Buffer + cmd.Stderr = &output + cmd.Stdout = &output + + log.Printf("Building %s", ip) + if err := cmd.Run(); err != nil { + os.RemoveAll(tmpDir) + log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String()) + return "", err + } + return file, nil +} diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index 30af87d5f4..85c732ab9e 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -23,7 +23,6 @@ import ( gb "go/build" "io" "io/ioutil" - "log" "os" "os/exec" "path/filepath" @@ -147,45 +146,6 @@ func (g *gobuild) importPackage(s string) (*gb.Package, error) { return nil, moduleErr } -func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { - tmpDir, err := ioutil.TempDir("", "ko") - if err != nil { - return "", err - } - file := filepath.Join(tmpDir, "out") - - args := make([]string, 0, 7) - args = append(args, "build") - if disableOptimizations { - // Disable optimizations (-N) and inlining (-l). - args = append(args, "-gcflags", "all=-N -l") - } - args = append(args, "-o", file) - args = append(args, "-trimpath") - args = append(args, ip) - cmd := exec.Command("go", args...) - - // Last one wins - defaultEnv := []string{ - "CGO_ENABLED=0", - "GOOS=" + platform.OS, - "GOARCH=" + platform.Architecture, - } - cmd.Env = append(defaultEnv, os.Environ()...) - - var output bytes.Buffer - cmd.Stderr = &output - cmd.Stdout = &output - - log.Printf("Building %s", ip) - if err := cmd.Run(); err != nil { - os.RemoveAll(tmpDir) - log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String()) - return "", err - } - return file, nil -} - func appFilename(importpath string) string { base := filepath.Base(importpath) From 8564d5d585c5869b8d0e2610a2990c783a5a5b49 Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Fri, 1 Nov 2019 11:49:48 +0800 Subject: [PATCH 3/7] Reduce duplications across go versions --- pkg/build/gbbuild_113.go | 52 +++----------------------------------- pkg/build/gbbuild_pre13.go | 51 +++---------------------------------- pkg/build/gobuild.go | 40 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 97 deletions(-) diff --git a/pkg/build/gbbuild_113.go b/pkg/build/gbbuild_113.go index 441cc0ab88..0eff2a36d4 100644 --- a/pkg/build/gbbuild_113.go +++ b/pkg/build/gbbuild_113.go @@ -12,57 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. // -// house gb.build function for go1.13 and later version with -trimpath available +// house flag adding function for go1.13 and later version with -trimpath available // +build go1.13 package build -import ( - "bytes" - "io/ioutil" - "log" - "os" - "os/exec" - "path/filepath" - - v1 "github.com/google/go-containerregistry/pkg/v1" -) - -func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { - tmpDir, err := ioutil.TempDir("", "ko") - if err != nil { - return "", err - } - file := filepath.Join(tmpDir, "out") - - args := make([]string, 0, 7) - args = append(args, "build") - if disableOptimizations { - // Disable optimizations (-N) and inlining (-l). - args = append(args, "-gcflags", "all=-N -l") - } - args = append(args, "-o", file) - args = append(args, "-trimpath") - args = append(args, ip) - cmd := exec.Command("go", args...) - - // Last one wins - defaultEnv := []string{ - "CGO_ENABLED=0", - "GOOS=" + platform.OS, - "GOARCH=" + platform.Architecture, - } - cmd.Env = append(defaultEnv, os.Environ()...) - - var output bytes.Buffer - cmd.Stderr = &output - cmd.Stdout = &output - - log.Printf("Building %s", ip) - if err := cmd.Run(); err != nil { - os.RemoveAll(tmpDir) - log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String()) - return "", err - } - return file, nil +func addGo113TrimPathFlag(args []string) []string { + return append(args, "-trimpath") } diff --git a/pkg/build/gbbuild_pre13.go b/pkg/build/gbbuild_pre13.go index dd0273037c..706aa9a16e 100644 --- a/pkg/build/gbbuild_pre13.go +++ b/pkg/build/gbbuild_pre13.go @@ -12,56 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. // -// house gb.build function for go1.12 and earlier version without -trimpath +// house placeholder function for go1.12 and earlier version without -trimpath // +build !go1.13 package build -import ( - "bytes" - "io/ioutil" - "log" - "os" - "os/exec" - "path/filepath" - - v1 "github.com/google/go-containerregistry/pkg/v1" -) - -func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { - tmpDir, err := ioutil.TempDir("", "ko") - if err != nil { - return "", err - } - file := filepath.Join(tmpDir, "out") - - args := make([]string, 0, 6) - args = append(args, "build") - if disableOptimizations { - // Disable optimizations (-N) and inlining (-l). - args = append(args, "-gcflags", "all=-N -l") - } - args = append(args, "-o", file) - args = append(args, ip) - cmd := exec.Command("go", args...) - - // Last one wins - defaultEnv := []string{ - "CGO_ENABLED=0", - "GOOS=" + platform.OS, - "GOARCH=" + platform.Architecture, - } - cmd.Env = append(defaultEnv, os.Environ()...) - - var output bytes.Buffer - cmd.Stderr = &output - cmd.Stdout = &output - - log.Printf("Building %s", ip) - if err := cmd.Run(); err != nil { - os.RemoveAll(tmpDir) - log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String()) - return "", err - } - return file, nil +func addGo113TrimPathFlag(args []string) []string { + return args } diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index 85c732ab9e..b569d0490b 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -23,6 +23,7 @@ import ( gb "go/build" "io" "io/ioutil" + "log" "os" "os/exec" "path/filepath" @@ -146,6 +147,45 @@ func (g *gobuild) importPackage(s string) (*gb.Package, error) { return nil, moduleErr } +func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { + tmpDir, err := ioutil.TempDir("", "ko") + if err != nil { + return "", err + } + file := filepath.Join(tmpDir, "out") + + args := make([]string, 0, 7) + args = append(args, "build") + if disableOptimizations { + // Disable optimizations (-N) and inlining (-l). + args = append(args, "-gcflags", "all=-N -l") + } + args = append(args, "-o", file) + args = addGo113TrimPathFlag(args) + args = append(args, ip) + cmd := exec.Command("go", args...) + + // Last one wins + defaultEnv := []string{ + "CGO_ENABLED=0", + "GOOS=" + platform.OS, + "GOARCH=" + platform.Architecture, + } + cmd.Env = append(defaultEnv, os.Environ()...) + + var output bytes.Buffer + cmd.Stderr = &output + cmd.Stdout = &output + + log.Printf("Building %s", ip) + if err := cmd.Run(); err != nil { + os.RemoveAll(tmpDir) + log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String()) + return "", err + } + return file, nil +} + func appFilename(importpath string) string { base := filepath.Base(importpath) From c770e14d8c5f4e282457fa5a8f3b32782c81accf Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Fri, 1 Nov 2019 12:02:09 +0800 Subject: [PATCH 4/7] Change trimpath fn-files for better names --- pkg/build/{gbbuild_113.go => trimpath_113.go} | 0 pkg/build/{gbbuild_pre13.go => trimpath_pre113.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename pkg/build/{gbbuild_113.go => trimpath_113.go} (100%) rename pkg/build/{gbbuild_pre13.go => trimpath_pre113.go} (100%) diff --git a/pkg/build/gbbuild_113.go b/pkg/build/trimpath_113.go similarity index 100% rename from pkg/build/gbbuild_113.go rename to pkg/build/trimpath_113.go diff --git a/pkg/build/gbbuild_pre13.go b/pkg/build/trimpath_pre113.go similarity index 100% rename from pkg/build/gbbuild_pre13.go rename to pkg/build/trimpath_pre113.go From 8fb6c9fb6907729052fb1e15704f095315dd31c8 Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Fri, 1 Nov 2019 12:43:25 +0800 Subject: [PATCH 5/7] Attempt to apply with minikube on Travis --- .travis.yml | 30 +++++++++++++++++++++++++----- integration_test.sh | 2 ++ 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 integration_test.sh diff --git a/.travis.yml b/.travis.yml index de6f3d3e02..3f1bafedfd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,38 @@ -sudo: false +sudo: required dist: trusty +services: + - docker + language: - go go: - - "1.12" - - "1.13" + - '1.12' + - '1.13' git: depth: 1 -install: true +install: true # skipping the default go dependencies install step + +env: + global: + - CHANGE_MINIKUBE_NONE_USER=true + - MINIKUBE_WANTUPDATENOTIFICATION=false + - MINIKUBE_WANTREPORTERRORPROMPT=false + - MINIKUBE_HOME=$HOME + - CHANGE_MINIKUBE_NONE_USER=true + - KUBECONFIG=$HOME/.kube/config + +before_script: + # Download minikube. + - curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.4.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/ + - mkdir -p $HOME/.kube $HOME/.minikube + - touch $KUBECONFIG + - sudo minikube start --vm-driver=none --kubernetes-version=v1.16.0 + - 'sudo chown -R travis: /home/travis/.minikube/' script: # Verify that all source files are correctly formatted. @@ -20,7 +40,7 @@ script: - go clean -i - go test -v -race -coverprofile=coverage.txt -covermode=atomic ./... - + - bash integration_test.sh # TODO: Set up coverage. #after_success: # - bash <(curl -s https://codecov.io/bash) diff --git a/integration_test.sh b/integration_test.sh new file mode 100644 index 0000000000..e49496c946 --- /dev/null +++ b/integration_test.sh @@ -0,0 +1,2 @@ +go install ./cmd/ko +ko apply -f ./cmd/ko/test -L \ No newline at end of file From c8989ec76ac53a49c6a11e8c5c7716d26070795f Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Fri, 1 Nov 2019 14:02:08 +0800 Subject: [PATCH 6/7] Attempt to apply with KinD on Travis --- .travis.yml | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3f1bafedfd..c14a51a87a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,22 +17,14 @@ git: install: true # skipping the default go dependencies install step -env: - global: - - CHANGE_MINIKUBE_NONE_USER=true - - MINIKUBE_WANTUPDATENOTIFICATION=false - - MINIKUBE_WANTREPORTERRORPROMPT=false - - MINIKUBE_HOME=$HOME - - CHANGE_MINIKUBE_NONE_USER=true - - KUBECONFIG=$HOME/.kube/config - before_script: - # Download minikube. - - curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.4.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/ - - mkdir -p $HOME/.kube $HOME/.minikube - - touch $KUBECONFIG - - sudo minikube start --vm-driver=none --kubernetes-version=v1.16.0 - - 'sudo chown -R travis: /home/travis/.minikube/' + # Download and install kubectl + - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/ + # Download and install KinD + - GO111MODULE=on go get sigs.k8s.io/kind + # Create a new Kubernetes cluster using KinD + - kind create cluster + - export KUBECONFIG=$(kind get kubeconfig-path) script: # Verify that all source files are correctly formatted. From 10f3f51b9efb45d9a450379f3ed4ee79a0da711f Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Fri, 1 Nov 2019 14:52:58 +0800 Subject: [PATCH 7/7] Install kind thru curl to not affect build --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c14a51a87a..8fe46c8da1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_script: # Download and install kubectl - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/ # Download and install KinD - - GO111MODULE=on go get sigs.k8s.io/kind + - curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/v0.5.1/kind-$(uname)-amd64 && chmod +x ./kind && sudo mv kind /usr/local/bin/ # Create a new Kubernetes cluster using KinD - kind create cluster - export KUBECONFIG=$(kind get kubeconfig-path)