github.com/abayer/test-infra@v0.0.5/prow/pluginhelp/externalplugins/externalplugins.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 externalplugins provides the plugin help components to be compiled into external plugin binaries.
    18  // Since external plugins only need to serve a "/help" endpoint this package just provides an
    19  // http.HandlerFunc that wraps an ExternalPluginHelpProvider function.
    20  package externalplugins
    21  
    22  import (
    23  	"encoding/json"
    24  	"errors"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"net/http"
    28  
    29  	"github.com/sirupsen/logrus"
    30  
    31  	"k8s.io/test-infra/prow/pluginhelp"
    32  )
    33  
    34  // ExternalPluginHelpProvider is a func type that returns a PluginHelp struct for an external
    35  // plugin based on the specified enabledRepos.
    36  type ExternalPluginHelpProvider func(enabledRepos []string) (*pluginhelp.PluginHelp, error)
    37  
    38  // ServeExternalPluginHelp returns a HandlerFunc that serves plugin help information that is
    39  // provided by the specified ExternalPluginHelpProvider.
    40  func ServeExternalPluginHelp(mux *http.ServeMux, log *logrus.Entry, provider ExternalPluginHelpProvider) {
    41  	mux.HandleFunc(
    42  		"/help",
    43  		func(w http.ResponseWriter, r *http.Request) {
    44  			w.Header().Set("Cache-Control", "no-cache")
    45  
    46  			serverError := func(action string, err error) {
    47  				log.WithError(err).Errorf("Error %s.", action)
    48  				msg := fmt.Sprintf("500 Internal server error %s: %v", action, err)
    49  				http.Error(w, msg, http.StatusInternalServerError)
    50  			}
    51  
    52  			if r.Method != http.MethodPost {
    53  				log.Errorf("Invalid request method: %v.", r.Method)
    54  				http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
    55  				return
    56  			}
    57  			b, err := ioutil.ReadAll(r.Body)
    58  			if err != nil {
    59  				serverError("reading request body", err)
    60  				return
    61  			}
    62  			var enabledRepos []string
    63  			if err := json.Unmarshal(b, &enabledRepos); err != nil {
    64  				serverError("unmarshaling request body", err)
    65  				return
    66  			}
    67  			if provider == nil {
    68  				serverError("generating plugin help", errors.New("help provider is nil"))
    69  				return
    70  			}
    71  			help, err := provider(enabledRepos)
    72  			if err != nil {
    73  				serverError("generating plugin help", err)
    74  				return
    75  			}
    76  			b, err = json.Marshal(help)
    77  			if err != nil {
    78  				serverError("marshaling plugin help", err)
    79  				return
    80  			}
    81  
    82  			fmt.Fprint(w, string(b))
    83  		},
    84  	)
    85  }