sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/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 "sigs.k8s.io/prow/pkg/pod-utils/wrapper" 26 ) 27 28 const defaultCopyDst = "/tools/entrypoint" 29 30 // NewOptions returns an empty Options with no nil fields 31 func NewOptions() *Options { 32 return &Options{ 33 Options: &wrapper.Options{}, 34 } 35 } 36 37 // Options exposes the configuration necessary 38 // for defining the process being watched and 39 // where in GCS an upload will land. 40 type Options struct { 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 // PreviousMarker has no effect when empty (default). 55 // When set it causes entrypoint to: 56 // a) wait until previous_marker exists 57 // b) run args as normal if previous_marker == 0 58 // c) otherwise immediately write PreviousErrorCode to marker_file without running args 59 PreviousMarker string `json:"previous_marker,omitempty"` 60 61 // AlwaysZero will cause entrypoint to exit zero, regardless of the marker it writes. 62 // Primarily useful in case a subsequent entrypoint will read this entrypoint's marker 63 AlwaysZero bool `json:"always_zero,omitempty"` 64 65 // PropagateErrorCode will cause entrypoint to propagate the error code from its child. 66 // Primarily useful in case you want to exit with a specific error code. 67 PropagateErrorCode bool `json:"propagate_error_code,omitempty"` 68 69 CopyModeOnly bool `json:"copy_mode_only,omitempty"` 70 CopyDst string `json:"copy_dst,omitempty"` 71 72 *wrapper.Options 73 } 74 75 // Validate ensures that the set of options are 76 // self-consistent and valid 77 func (o *Options) Validate() error { 78 if len(o.Args) == 0 { 79 return errors.New("no process to wrap specified") 80 } 81 if o.PropagateErrorCode && o.AlwaysZero { 82 return errors.New("cannot propagate error code and always exit zero") 83 } 84 85 return o.Options.Validate() 86 } 87 88 const ( 89 // JSONConfigEnvVar is the environment variable that 90 // utilities expect to find a full JSON configuration 91 // in when run. 92 JSONConfigEnvVar = "ENTRYPOINT_OPTIONS" 93 ) 94 95 // ConfigVar exposes the environment variable used 96 // to store serialized configuration 97 func (o *Options) ConfigVar() string { 98 return JSONConfigEnvVar 99 } 100 101 // LoadConfig loads options from serialized config 102 func (o *Options) LoadConfig(config string) error { 103 return json.Unmarshal([]byte(config), o) 104 } 105 106 // AddFlags binds flags to options 107 func (o *Options) AddFlags(flags *flag.FlagSet) { 108 flags.DurationVar(&o.Timeout, "timeout", DefaultTimeout, "Timeout for the test command.") 109 flags.DurationVar(&o.GracePeriod, "grace-period", DefaultGracePeriod, "Grace period after timeout for the test command.") 110 flags.StringVar(&o.ArtifactDir, "artifact-dir", "", "directory where test artifacts should be placed for upload to persistent storage") 111 flags.BoolVar(&o.CopyModeOnly, "copy-mode-only", false, "If true, copy current binary to /tools/entrypoint, dst can be overridden by --copy-destination") 112 flags.StringVar(&o.CopyDst, "copy-destination", defaultCopyDst, "Must be used with --copy-mode-only, default is /tools/entrypoint") 113 flags.BoolVar(&o.PropagateErrorCode, "propagate-error-code", false, "If true, propagate the error code from the child process") 114 o.Options.AddFlags(flags) 115 } 116 117 // Complete internalizes command line arguments 118 func (o *Options) Complete(args []string) { 119 o.Args = args 120 } 121 122 // Encode will encode the set of options in the format that 123 // is expected for the configuration environment variable 124 func Encode(options Options) (string, error) { 125 encoded, err := json.Marshal(options) 126 return string(encoded), err 127 }