github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/sidecar/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 sidecar
    18  
    19  import (
    20  	"encoding/json"
    21  	"flag"
    22  
    23  	"k8s.io/test-infra/prow/gcsupload"
    24  	"k8s.io/test-infra/prow/pod-utils/wrapper"
    25  )
    26  
    27  // NewOptions returns an empty Options with no nil fields
    28  func NewOptions() *Options {
    29  	return &Options{
    30  		GcsOptions:     gcsupload.NewOptions(),
    31  		WrapperOptions: &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  	GcsOptions     *gcsupload.Options `json:"gcs_options"`
    40  	WrapperOptions *wrapper.Options   `json:"wrapper_options"`
    41  }
    42  
    43  // Validate ensures that the set of options are
    44  // self-consistent and valid
    45  func (o *Options) Validate() error {
    46  	if err := o.WrapperOptions.Validate(); err != nil {
    47  		return err
    48  	}
    49  
    50  	return o.GcsOptions.Validate()
    51  }
    52  
    53  const (
    54  	// JSONConfigEnvVar is the environment variable that
    55  	// utilities expect to find a full JSON configuration
    56  	// in when run.
    57  	JSONConfigEnvVar = "SIDECAR_OPTIONS"
    58  )
    59  
    60  // ConfigVar exposese the environment variable used
    61  // to store serialized configuration
    62  func (o *Options) ConfigVar() string {
    63  	return JSONConfigEnvVar
    64  }
    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  
    71  // AddFlags binds flags to options
    72  func (o *Options) AddFlags(flags *flag.FlagSet) {
    73  	o.GcsOptions.AddFlags(flags)
    74  	o.WrapperOptions.AddFlags(flags)
    75  }
    76  
    77  // Complete internalizes command line arguments
    78  func (o *Options) Complete(args []string) {
    79  	o.GcsOptions.Complete(args)
    80  }
    81  
    82  // Encode will encode the set of options in the format that
    83  // is expected for the configuration environment variable
    84  func Encode(options Options) (string, error) {
    85  	encoded, err := json.Marshal(options)
    86  	return string(encoded), err
    87  }