mirror of https://github.com/k3s-io/k3s
Added a proxy server to cloudcfg
parent
b94ceffbed
commit
5756189f0d
|
@ -21,6 +21,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -46,6 +47,7 @@ var (
|
||||||
json = flag.Bool("json", false, "If true, print raw JSON for responses")
|
json = flag.Bool("json", false, "If true, print raw JSON for responses")
|
||||||
yaml = flag.Bool("yaml", false, "If true, print raw YAML for responses")
|
yaml = flag.Bool("yaml", false, "If true, print raw YAML for responses")
|
||||||
verbose = flag.Bool("verbose", false, "If true, print extra information")
|
verbose = flag.Bool("verbose", false, "If true, print extra information")
|
||||||
|
proxy = flag.Bool("proxy", false, "If true, run a proxy to the api server")
|
||||||
)
|
)
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
|
@ -118,6 +120,15 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *proxy {
|
||||||
|
server := cloudcfg.ProxyServer{
|
||||||
|
Host: *httpServer,
|
||||||
|
Client: &http.Client{},
|
||||||
|
}
|
||||||
|
http.Handle("/api/", &server)
|
||||||
|
log.Fatal(http.ListenAndServe(":8001", nil))
|
||||||
|
}
|
||||||
|
|
||||||
matchFound := executeAPIRequest(method, auth) || executeControllerRequest(method, auth)
|
matchFound := executeAPIRequest(method, auth) || executeControllerRequest(method, auth)
|
||||||
if matchFound == false {
|
if matchFound == false {
|
||||||
log.Fatalf("Unknown command %s", method)
|
log.Fatalf("Unknown command %s", method)
|
||||||
|
|
|
@ -6,6 +6,7 @@ LINES=$(cat "$(dirname $0)/boilerplate.txt" | wc -l)
|
||||||
DIFFER=$(head -$LINES "${FILE}" | diff -q - "$(dirname $0)/boilerplate.txt")
|
DIFFER=$(head -$LINES "${FILE}" | diff -q - "$(dirname $0)/boilerplate.txt")
|
||||||
|
|
||||||
if [[ -z "${DIFFER}" ]]; then
|
if [[ -z "${DIFFER}" ]]; then
|
||||||
|
echo "${DIFFER}"
|
||||||
echo "1"
|
echo "1"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cloudcfg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProxyServer struct {
|
||||||
|
Host string
|
||||||
|
Client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
req, err := http.NewRequest(r.Method, s.Host+r.URL.Path, r.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: %#v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := s.Client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: %#v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(res.StatusCode)
|
||||||
|
defer res.Body.Close()
|
||||||
|
data, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: %#v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(data)
|
||||||
|
}
|
Loading…
Reference in New Issue