options.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 sidecar
  14. import (
  15. "encoding/json"
  16. "flag"
  17. "k8s.io/test-infra/prow/gcsupload"
  18. "k8s.io/test-infra/prow/pod-utils/wrapper"
  19. )
  20. // NewOptions returns an empty Options with no nil fields
  21. func NewOptions() *Options {
  22. return &Options{
  23. GcsOptions: gcsupload.NewOptions(),
  24. WrapperOptions: &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. GcsOptions *gcsupload.Options `json:"gcs_options"`
  32. WrapperOptions *wrapper.Options `json:"wrapper_options"`
  33. }
  34. // Validate ensures that the set of options are
  35. // self-consistent and valid
  36. func (o *Options) Validate() error {
  37. if err := o.WrapperOptions.Validate(); err != nil {
  38. return err
  39. }
  40. return o.GcsOptions.Validate()
  41. }
  42. const (
  43. // JSONConfigEnvVar is the environment variable that
  44. // utilities expect to find a full JSON configuration
  45. // in when run.
  46. JSONConfigEnvVar = "SIDECAR_OPTIONS"
  47. )
  48. // ConfigVar exposese the environment variable used
  49. // to store serialized configuration
  50. func (o *Options) ConfigVar() string {
  51. return JSONConfigEnvVar
  52. }
  53. // LoadConfig loads options from serialized config
  54. func (o *Options) LoadConfig(config string) error {
  55. return json.Unmarshal([]byte(config), o)
  56. }
  57. // AddFlags binds flags to options
  58. func (o *Options) AddFlags(flags *flag.FlagSet) {
  59. o.GcsOptions.AddFlags(flags)
  60. o.WrapperOptions.AddFlags(flags)
  61. }
  62. // Complete internalizes command line arguments
  63. func (o *Options) Complete(args []string) {
  64. o.GcsOptions.Complete(args)
  65. }
  66. // Encode will encode the set of options in the format that
  67. // is expected for the configuration environment variable
  68. func Encode(options Options) (string, error) {
  69. encoded, err := json.Marshal(options)
  70. return string(encoded), err
  71. }