options.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 gcsupload
  14. import (
  15. "encoding/json"
  16. "errors"
  17. "flag"
  18. "k8s.io/test-infra/prow/kube"
  19. "k8s.io/test-infra/testgrid/util/gcs"
  20. )
  21. // NewOptions returns an empty Options with no nil fields
  22. func NewOptions() *Options {
  23. return &Options{
  24. GCSConfiguration: &kube.GCSConfiguration{},
  25. }
  26. }
  27. // Options exposes the configuration necessary
  28. // for defining where in GCS an upload will land.
  29. type Options struct {
  30. // Items are files or directories to upload
  31. Items []string `json:"items,omitempty"`
  32. // SubDir is appended to the GCS path
  33. SubDir string `json:"sub_dir,omitempty"`
  34. *kube.GCSConfiguration
  35. // GcsCredentialsFile is the path to the JSON
  36. // credentials for pushing to GCS
  37. GcsCredentialsFile string `json:"gcs_credentials_file,omitempty"`
  38. DryRun bool `json:"dry_run"`
  39. // gcsPath is used to store human-provided GCS
  40. // paths that are parsed to get more granular
  41. // fields
  42. gcsPath gcs.Path
  43. }
  44. // Validate ensures that the set of options are
  45. // self-consistent and valid
  46. func (o *Options) Validate() error {
  47. if o.gcsPath.String() != "" {
  48. o.Bucket = o.gcsPath.Bucket()
  49. o.PathPrefix = o.gcsPath.Object()
  50. }
  51. if !o.DryRun {
  52. if o.Bucket == "" {
  53. return errors.New("GCS upload was requested no GCS bucket was provided")
  54. }
  55. if o.GcsCredentialsFile == "" {
  56. return errors.New("GCS upload was requested but no GCS credentials file was provided")
  57. }
  58. }
  59. return o.GCSConfiguration.Validate()
  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. // Complete internalizes command line arguments
  71. func (o *Options) Complete(args []string) {
  72. o.Items = args
  73. }
  74. // AddFlags adds flags to the FlagSet that populate
  75. // the GCS upload options struct given.
  76. func (o *Options) AddFlags(fs *flag.FlagSet) {
  77. fs.StringVar(&o.SubDir, "sub-dir", "", "Optional sub-directory of the job's path to which artifacts are uploaded")
  78. fs.StringVar(&o.PathStrategy, "path-strategy", kube.PathStrategyExplicit, "how to encode org and repo into GCS paths")
  79. fs.StringVar(&o.DefaultOrg, "default-org", "", "optional default org for GCS path encoding")
  80. fs.StringVar(&o.DefaultRepo, "default-repo", "", "optional default repo for GCS path encoding")
  81. fs.Var(&o.gcsPath, "gcs-path", "GCS path to upload into")
  82. fs.StringVar(&o.GcsCredentialsFile, "gcs-credentials-file", "", "file where Google Cloud authentication credentials are stored")
  83. fs.BoolVar(&o.DryRun, "dry-run", true, "do not interact with GCS")
  84. }
  85. const (
  86. // JSONConfigEnvVar is the environment variable that
  87. // utilities expect to find a full JSON configuration
  88. // in when run.
  89. JSONConfigEnvVar = "GCSUPLOAD_OPTIONS"
  90. )
  91. // Encode will encode the set of options in the format that
  92. // is expected for the configuration environment variable
  93. func Encode(options Options) (string, error) {
  94. encoded, err := json.Marshal(options)
  95. return string(encoded), err
  96. }