Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

No init containers #470

Closed
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
383 changes: 328 additions & 55 deletions Gopkg.lock

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ required = [
# It seems to be broken at HEAD.
revision = "f2b4162afba35581b6d4a50d3b8f34e33c144682"

[[override]]
name = "github.com/Azure/go-autorest"
# This is the commit at which k8s depends on this in 1.11
# It seems to be broken at HEAD.
revision = "1ff28809256a84bb6966640ff3d0371af82ccba4"

[[override]]
name = "github.com/knative/pkg"
Expand Down
62 changes: 62 additions & 0 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2018 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.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you include in the comments here some docs on what this binary is and what it does? e.g. like https://github.com/knative/build-pipeline/blob/master/cmd/bash/main.go#L17

package main

import (
"os"

"github.com/knative/build/pkg/entrypoint"
"github.com/knative/build/pkg/entrypoint/options"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/logrusutil"
)

/*
The tool is used to rewrite the entrypoint of a container image.
To override the base shell image update `.ko.yaml` file.

To use it, run
```
image: github.com/knative/build/cmd/entrypoint
```

It used in knative/build as a method of running containers in
order that are in the same pod this is done by:
1) for the Pod(containing user Steps) created by a Build,
create a shared directory with the entrypoint binary
2) change the entrypoint of all the user specified containers in Steps to be the
entrypoint binary with configuration to run the user specified entrypoint with some custom logic
3) one piece of "custom logic" is having the entrypoint binary wait for the previous step
as seen in knative/build/pkg/entrypoint/run.go -- waitForPrevStep()
*/

func main() {
o := entrypoint.NewOptions()
if err := options.Load(o); err != nil {
logrus.Fatalf("Could not resolve options: %v", err)
}

if err := o.Validate(); err != nil {
logrus.Fatalf("Invalid options: %v", err)
}

logrus.SetFormatter(
logrusutil.NewDefaultFieldsFormatter(nil, logrus.Fields{"component": "entrypoint"}),
)

os.Exit(o.Run())
}
10 changes: 10 additions & 0 deletions config/999-cache.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ spec:
---
apiVersion: caching.internal.knative.dev/v1alpha1
kind: Image
metadata:
name: entrypoint
namespace: knative-build
spec:
# This is the Go import path for the binary that is containerized
# and substituted here.
image: github.com/knative/build/cmd/entrypoint
---
apiVersion: caching.internal.knative.dev/v1alpha1
kind: Image
metadata:
name: git-init
namespace: knative-build
Expand Down
19 changes: 19 additions & 0 deletions pkg/entrypoint/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2018 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 is a library that knows how to wrap
// a process and write it's output and exit code to disk
package entrypoint
95 changes: 95 additions & 0 deletions pkg/entrypoint/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2018 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 (
"encoding/json"
"errors"
"flag"

"github.com/knative/build/pkg/entrypoint/wrapper"
)

// NewOptions returns an empty Options with no nil fields
func NewOptions() *Options {
return &Options{
Options: &wrapper.Options{},
}
}

// Options exposes the configuration necessary
// for defining the process being watched and
// where in the image repository an upload will land.
type Options struct {
// Args is the process and args to run
Args []string `json:"args"`

*wrapper.Options
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems super similar to https://github.com/kubernetes/test-infra/blob/master/prow/entrypoint/options.go - we should at least give a shout-out to it probably.

also are we 100% sure we need all the same options? (e.g. we don't do automatic artifact uploading afaik, do we need ArtifactDir?)


// Validate ensures that the set of options are
// self-consistent and valid
func (o *Options) Validate() error {
if len(o.Args) == 0 {
return errors.New("no process to wrap specified")
}

return o.Options.Validate()
}

const (
// JSONConfigEnvVar is the environment variable that
// utilities expect to find a full JSON configuration
// in when run.
JSONConfigEnvVar = "ENTRYPOINT_OPTIONS"
)

// ConfigVar exposes the environment variable used
// to store serialized configuration
func (o *Options) ConfigVar() string {
return JSONConfigEnvVar
}

// LoadConfig loads options from serialized config
func (o *Options) LoadConfig(config string) error {
return json.Unmarshal([]byte(config), o)
}

// AddFlags binds flags to options
func (o *Options) AddFlags(flags *flag.FlagSet) {
flags.BoolVar(&o.ShouldWaitForPrevStep, "should-wait-for-prev-step",
DefaultShouldWaitForPrevStep, "If we should wait for prev step.")
flags.BoolVar(&o.ShouldRunPostRun, "should-run-post-run",
DefaultShouldRunPostRun, "If the post run step should be run after execution finishes.")
flags.StringVar(&o.PreRunFile, "prerun-file",
DefaultPreRunFile, "The path of the file that acts as a lock for the entrypoint. The entrypoint binary will wait until that file is present to launch the specified command.")
flags.StringVar(&o.PostRunFile, "postrun-file",
DefaultPostRunFile, "The path of the file that will be written once the command finishes for the entrypoint. This can act as a lock for other entrypoint rewritten containers.")
o.Options.AddFlags(flags)
}

// Complete internalizes command line arguments
func (o *Options) Complete(args []string) {
o.Args = args
}

// Encode will encode the set of options in the format that
// is expected for the configuration environment variable
func Encode(options Options) (string, error) {
encoded, err := json.Marshal(options)
return string(encoded), err
}
19 changes: 19 additions & 0 deletions pkg/entrypoint/options/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2017 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 options abstracts the options loading
// flow for pod utilities
package options
50 changes: 50 additions & 0 deletions pkg/entrypoint/options/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2017 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 options

import (
"flag"
"fmt"
"os"
)

// OptionLoader allows loading options from either the environment or flags.
type OptionLoader interface {
ConfigVar() string
LoadConfig(config string) error
AddFlags(flags *flag.FlagSet)
Complete(args []string)
}

// Load loads the set of options, preferring to use
// JSON config from an env var, but falling back to
// command line flags if not possible.
func Load(loader OptionLoader) error {
if jsonConfig, provided := os.LookupEnv(loader.ConfigVar()); provided {
if err := loader.LoadConfig(jsonConfig); err != nil {
return fmt.Errorf("could not load config from JSON var %s: %v", loader.ConfigVar(), err)
}
return nil
}

fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
loader.AddFlags(fs)
fs.Parse(os.Args[1:])
loader.Complete(fs.Args())

return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add test coverage for these functions + files we're adding?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

67 changes: 67 additions & 0 deletions pkg/entrypoint/options/load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2017 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 options

import (
"flag"
"os"
"testing"

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

const (
TestEnvVar = "TEST_ENV_VAR"
)

type TestOptions struct {
*entrypoint.Options
}

func (o *TestOptions) ConfigVar() string {
return TestEnvVar
}

func (o *TestOptions) AddFlags(flags *flag.FlagSet) {
// Required to reset os.Args[1:] values used in Load()
os.Args[1] = ""
return
}

func (o *TestOptions) Complete(args []string) {
return
}

func TestOptions_Load(t *testing.T) {
tt := []struct {
name string
envmap map[string]string
in OptionLoader
err error
}{
{"successful load", map[string]string{TestEnvVar: "hello"}, &TestOptions{}, nil},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
err := Load(tc.in)
if tc.err != err {
t.Errorf("expected err to be %v; got %v", tc.err, err)
}
})
}
}
Loading