options.go 3.1 KB

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