github.com/abayer/test-infra@v0.0.5/prow/cmd/mkpod/main.go (about)

     1  /*
     2  Copyright 2017 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 main
    18  
    19  import (
    20  	"errors"
    21  	"flag"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  
    26  	"github.com/ghodss/yaml"
    27  	"github.com/sirupsen/logrus"
    28  	"k8s.io/api/core/v1"
    29  
    30  	"k8s.io/test-infra/prow/kube"
    31  	"k8s.io/test-infra/prow/pod-utils/decorate"
    32  )
    33  
    34  type options struct {
    35  	prowJobPath string
    36  	buildID     string
    37  }
    38  
    39  func (o *options) Validate() error {
    40  	if o.prowJobPath == "" {
    41  		return errors.New("required flag --prow-job was unset")
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func gatherOptions() options {
    48  	o := options{}
    49  	flag.StringVar(&o.prowJobPath, "prow-job", "", "ProwJob to decorate, - for stdin.")
    50  	flag.StringVar(&o.buildID, "build-id", "", "Build ID for the job run.")
    51  	flag.Parse()
    52  	return o
    53  }
    54  
    55  func main() {
    56  	o := gatherOptions()
    57  	if err := o.Validate(); err != nil {
    58  		logrus.Fatalf("Invalid options: %v", err)
    59  	}
    60  
    61  	var rawJob []byte
    62  	if o.prowJobPath == "-" {
    63  		raw, err := ioutil.ReadAll(os.Stdin)
    64  		if err != nil {
    65  			logrus.WithError(err).Fatal("Could not read ProwJob YAML from stdin.")
    66  		}
    67  		rawJob = raw
    68  	} else {
    69  		raw, err := ioutil.ReadFile(o.prowJobPath)
    70  		if err != nil {
    71  			logrus.WithError(err).Fatal("Could not open ProwJob YAML.")
    72  		}
    73  		rawJob = raw
    74  	}
    75  
    76  	var job kube.ProwJob
    77  	if err := yaml.Unmarshal(rawJob, &job); err != nil {
    78  		logrus.WithError(err).Fatal("Could not unmarshal ProwJob YAML.")
    79  	}
    80  
    81  	if o.buildID == "" && job.Status.BuildID != "" {
    82  		o.buildID = job.Status.BuildID
    83  	}
    84  
    85  	if o.buildID == "" {
    86  		logrus.Warning("No BuildID found in ProwJob status or given with --build-id, GCS interaction will be poor.")
    87  	}
    88  
    89  	pod, err := decorate.ProwJobToPod(job, o.buildID)
    90  	if err != nil {
    91  		logrus.WithError(err).Fatal("Could not decorate PodSpec.")
    92  	}
    93  
    94  	pod.GetObjectKind().SetGroupVersionKind(v1.SchemeGroupVersion.WithKind("Pod"))
    95  	podYAML, err := yaml.Marshal(pod)
    96  	if err != nil {
    97  		logrus.WithError(err).Fatal("Could not marshal Pod YAML.")
    98  	}
    99  	fmt.Println(string(podYAML))
   100  }