github.com/abayer/test-infra@v0.0.5/prow/entrypoint/options.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package entrypoint
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"flag"
    23  	"time"
    24  
    25  	"k8s.io/test-infra/prow/pod-utils/wrapper"
    26  )
    27  
    28  // NewOptions returns an empty Options with no nil fields
    29  func NewOptions() *Options {
    30  	return &Options{
    31  		Options: &wrapper.Options{},
    32  	}
    33  }
    34  
    35  // Options exposes the configuration necessary
    36  // for defining the process being watched and
    37  // where in GCS an upload will land.
    38  type Options struct {
    39  	// Args is the process and args to run
    40  	Args []string `json:"args"`
    41  	// Timeout determines how long to wait before the
    42  	// entrypoint sends SIGINT to the process
    43  	Timeout time.Duration `json:"timeout"`
    44  	// GracePeriod determines how long to wait after
    45  	// sending SIGINT before the entrypoint sends
    46  	// SIGKILL.
    47  	GracePeriod time.Duration `json:"grace_period"`
    48  	// ArtifactDir is a directory where test processes can dump artifacts
    49  	// for upload to persistent storage (courtesy of sidecar).
    50  	// If specified, it is created by entrypoint before starting the test process.
    51  	// May be ignored if not using sidecar.
    52  	ArtifactDir string `json:"artifact_dir,omitempty"`
    53  
    54  	*wrapper.Options
    55  }
    56  
    57  // Validate ensures that the set of options are
    58  // self-consistent and valid
    59  func (o *Options) Validate() error {
    60  	if len(o.Args) == 0 {
    61  		return errors.New("no process to wrap specified")
    62  	}
    63  
    64  	return o.Options.Validate()
    65  }
    66  
    67  const (
    68  	// JSONConfigEnvVar is the environment variable that
    69  	// utilities expect to find a full JSON configuration
    70  	// in when run.
    71  	JSONConfigEnvVar = "ENTRYPOINT_OPTIONS"
    72  )
    73  
    74  // ConfigVar exposes the environment variable used
    75  // to store serialized configuration
    76  func (o *Options) ConfigVar() string {
    77  	return JSONConfigEnvVar
    78  }
    79  
    80  // LoadConfig loads options from serialized config
    81  func (o *Options) LoadConfig(config string) error {
    82  	return json.Unmarshal([]byte(config), o)
    83  }
    84  
    85  // BindOptions binds flags to options
    86  func (o *Options) BindOptions(flags *flag.FlagSet) {
    87  	flags.DurationVar(&o.Timeout, "timeout", DefaultTimeout, "Timeout for the test command.")
    88  	flags.DurationVar(&o.GracePeriod, "grace-period", DefaultGracePeriod, "Grace period after timeout for the test command.")
    89  	flags.StringVar(&o.ArtifactDir, "artifact-dir", "", "directory where test artifacts should be placed for upload to persistent storage")
    90  	wrapper.BindOptions(o.Options, flags)
    91  }
    92  
    93  // Complete internalizes command line arguments
    94  func (o *Options) Complete(args []string) {
    95  	o.Args = args
    96  }
    97  
    98  // Encode will encode the set of options in the format that
    99  // is expected for the configuration environment variable
   100  func Encode(options Options) (string, error) {
   101  	encoded, err := json.Marshal(options)
   102  	return string(encoded), err
   103  }