Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add entrypoint overrider image #448

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2019 The Knative Authors.

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 main

import (
"flag"
"log"
"os"
"os/exec"
"syscall"
"time"

"github.com/knative/build-pipeline/pkg/entrypoint"
)

var (
ep = flag.String("entrypoint", "", "Original specified entrypoint to execute")
waitFile = flag.String("wait_file", "", "If specified, file to wait for")
postFile = flag.String("post_file", "", "If specified, file to write upon completion")
)

func main() {
flag.Parse()

entrypoint.Entrypointer{
Entrypoint: *ep,
WaitFile: *waitFile,
PostFile: *postFile,
Args: flag.Args(),
Waiter: &RealWaiter{},
Runner: &RealRunner{},
PostWriter: &RealPostWriter{},
}.Go()
}

// TODO(jasonhall): Test that original exit code is propagated and that
// stdout/stderr are collected -- needs e2e tests.

// RealWaiter actually waits for files, by polling.
type RealWaiter struct{ waitFile string }

var _ entrypoint.Waiter = (*RealWaiter)(nil)

func (*RealWaiter) Wait(file string) {
if file == "" {
return
}
for ; ; time.Sleep(time.Second) {
if _, err := os.Stat(file); err == nil {
return
} else if !os.IsNotExist(err) {
log.Fatalf("Waiting for %q: %v", file, err)
}
}
}

// RealRunner actually runs commands.
type RealRunner struct{}

var _ entrypoint.Runner = (*RealRunner)(nil)

func (*RealRunner) Run(args ...string) {
if len(args) == 0 {
return
}
name, args := args[0], args[1:]

cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// Copied from https://stackoverflow.com/questions/10385551/get-exit-code-go
// This works on both Unix and Windows. Although
// package syscall is generally platform dependent,
// WaitStatus is defined for both Unix and Windows and
// in both cases has an ExitStatus() method with the
// same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
}
log.Fatalf("Error executing command (ExitError): %v", err)
}
log.Fatalf("Error executing command: %v", err)
}
}

// RealPostWriter actually writes files.
type RealPostWriter struct{}

var _ entrypoint.PostWriter = (*RealPostWriter)(nil)

func (*RealPostWriter) Write(file string) {
if file == "" {
return
}
if _, err := os.Create(file); err != nil {
log.Fatalf("Creating %q: %v", file, err)
}
}
113 changes: 113 additions & 0 deletions pkg/entrypoint/entrypoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright 2019 The Knative Authors.

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 entrypoint

import (
"reflect"
"testing"
)

func TestEntrypointer(t *testing.T) {
for _, c := range []struct {
desc, entrypoint, waitFile, postFile string
args []string
}{{
desc: "do nothing",
}, {
desc: "just entrypoint",
entrypoint: "echo",
}, {
desc: "entrypoint and args",
entrypoint: "echo", args: []string{"some", "args"},
}, {
desc: "just args",
args: []string{"just", "args"},
}, {
desc: "wait file",
waitFile: "waitforme",
}, {
desc: "post file",
postFile: "writeme",
}, {
desc: "all together now",
entrypoint: "echo", args: []string{"some", "args"},
waitFile: "waitforme",
postFile: "writeme",
}} {
t.Run(c.desc, func(t *testing.T) {
fw, fr, fpw := &fakeWaiter{}, &fakeRunner{}, &fakePostWriter{}
Entrypointer{
Entrypoint: c.entrypoint,
WaitFile: c.waitFile,
PostFile: c.postFile,
Args: c.args,
Waiter: fw,
Runner: fr,
PostWriter: fpw,
}.Go()

if c.waitFile != "" {
if fw.waited == nil {
t.Error("Wanted waited file, got nil")
} else if *fw.waited != c.waitFile {
t.Errorf("Waited for %q, want %q", *fw.waited, c.waitFile)
}
}
if c.waitFile == "" && fw.waited != nil {
t.Errorf("Waited for file when not required")
}

wantArgs := c.args
if c.entrypoint != "" {
wantArgs = append([]string{c.entrypoint}, c.args...)
}
if len(wantArgs) != 0 {
if fr.args == nil {
t.Error("Wanted command to be run, got nil")
} else if !reflect.DeepEqual(*fr.args, wantArgs) {
t.Errorf("Ran %s, want %s", *fr.args, wantArgs)
}
}
if len(wantArgs) == 0 && c.args != nil {
t.Errorf("Ran command when not required")
}

if c.postFile != "" {
if fpw.wrote == nil {
t.Error("Wanted post file written, got nil")
} else if *fpw.wrote != c.postFile {
t.Errorf("Wrote post file %q, want %q", *fpw.wrote, c.postFile)
}
}
if c.postFile == "" && fpw.wrote != nil {
t.Errorf("Wrote post file when not required")
}
})
}
}

type fakeWaiter struct{ waited *string }

func (f *fakeWaiter) Wait(file string) { f.waited = &file }

type fakeRunner struct{ args *[]string }

func (f *fakeRunner) Run(args ...string) { f.args = &args }

type fakePostWriter struct{ wrote *string }

func (f *fakePostWriter) Write(file string) { f.wrote = &file }
73 changes: 73 additions & 0 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2019 The Knative Authors.

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 entrypoint

// Entrypointer holds fields for running commands with redirected
// entrypoints.
type Entrypointer struct {
// Entrypoint is the original specified entrypoint, if any.
Entrypoint string
// Args are the original specified args, if any.
Args []string
// WaitFile is the file to wait for. If not specified, execution begins
// immediately.
WaitFile string
// PostFile is the file to write when complete. If not specified, no
// file is written.
PostFile string

// Waiter encapsulates waiting for files to exist.
Waiter Waiter
// Runner encapsulates running commands.
Runner Runner
// PostWriter encapsulates writing files when complete.
PostWriter PostWriter
}

// Waiter encapsulates waiting for files to exist.
type Waiter interface {
// Wait blocks until the specified file exists.
Wait(file string)
}

// Runner encapsulates running commands.
type Runner interface {
Run(args ...string)
}

// PostWriter encapsulates writing a file when complete.
type PostWriter interface {
// Write writes to the path when complete.
Write(file string)
}

// Go optionally waits for a file, runs the command, and writes a
// post file.
func (e Entrypointer) Go() {
if e.WaitFile != "" {
e.Waiter.Wait(e.WaitFile)
}

if e.Entrypoint != "" {
e.Args = append([]string{e.Entrypoint}, e.Args...)
}
e.Runner.Run(e.Args...)

if e.PostFile != "" {
e.PostWriter.Write(e.PostFile)
}
}