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

Commit

Permalink
no init containers
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-prindle committed Nov 27, 2018
1 parent 5a9612f commit 6d3f04d
Show file tree
Hide file tree
Showing 159 changed files with 14,009 additions and 4,961 deletions.
247 changes: 62 additions & 185 deletions Gopkg.lock

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2018 The Kubernetes 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 (
"os"

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

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 Kubernetes 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
115 changes: 115 additions & 0 deletions pkg/entrypoint/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2018 The Kubernetes 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"
"time"

"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 GCS an upload will land.
type Options struct {
// Args is the process and args to run
Args []string `json:"args"`
// Timeout determines how long to wait before the
// entrypoint sends SIGINT to the process
Timeout time.Duration `json:"timeout"`
// GracePeriod determines how long to wait after
// sending SIGINT before the entrypoint sends
// SIGKILL.
GracePeriod time.Duration `json:"grace_period"`
// ArtifactDir is a directory where test processes can dump artifacts
// for upload to persistent storage (courtesy of sidecar).
// If specified, it is created by entrypoint before starting the test process.
// May be ignored if not using sidecar.
ArtifactDir string `json:"artifact_dir,omitempty"`

*wrapper.Options
}

// 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.DurationVar(&o.Timeout, "timeout",
DefaultTimeout, "Timeout for the test command.")
flags.DurationVar(&o.GracePeriod, "grace-period",
DefaultGracePeriod, "Grace period after timeout for the test command.")
flags.StringVar(&o.ArtifactDir, "artifact-dir",
"", "directory where test artifacts should be placed for upload "+
"to persistent storage")
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 post run step should be run.")
flags.StringVar(&o.PreRunFile, "prerun-file",
DefaultPreRunFile, "The prerun file to wait for.")
flags.StringVar(&o.PostRunFile, "postrun-file",
DefaultPostRunFile, "If postrun file to write.")
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
}
25 changes: 25 additions & 0 deletions pkg/entrypoint/options/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"doc.go",
"load.go",
],
importpath = "k8s.io/test-infra/prow/pod-utils/options",
visibility = ["//visibility:public"],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions pkg/entrypoint/options/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
approvers:
- stevekuznetsov
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 Kubernetes 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 Kubernetes 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
}
63 changes: 63 additions & 0 deletions pkg/entrypoint/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2018 The Kubernetes 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 (
"testing"

"k8s.io/test-infra/prow/pod-utils/wrapper"
)

func TestOptions_Validate(t *testing.T) {
var testCases = []struct {
name string
input Options
expectedErr bool
}{
{
name: "all ok",
input: Options{
Args: []string{"/usr/bin/true"},
Options: &wrapper.Options{
ProcessLog: "output.txt",
MarkerFile: "marker.txt",
},
},
expectedErr: false,
},
{
name: "missing args",
input: Options{
Options: &wrapper.Options{
ProcessLog: "output.txt",
MarkerFile: "marker.txt",
},
},
expectedErr: true,
},
}

for _, testCase := range testCases {
err := testCase.input.Validate()
if testCase.expectedErr && err == nil {
t.Errorf("%s: expected an error but got none", testCase.name)
}
if !testCase.expectedErr && err != nil {
t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
}
}
}
Loading

0 comments on commit 6d3f04d

Please sign in to comment.