Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose flag to porter so http method can be returned to client #95781

Merged
merged 1 commit into from Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/dependencies.yaml
@@ -1,7 +1,7 @@
dependencies:
# agnhost: bump this one first
- name: "agnhost"
version: "2.24"
version: "2.25"
refPaths:
- path: test/images/agnhost/VERSION
match: \d.\d
Expand Down
2 changes: 1 addition & 1 deletion test/images/agnhost/VERSION
@@ -1 +1 @@
2.24
2.25
2 changes: 1 addition & 1 deletion test/images/agnhost/agnhost.go
Expand Up @@ -51,7 +51,7 @@ import (
func main() {
rootCmd := &cobra.Command{
Use: "app",
Version: "2.24",
Version: "2.25",
}

rootCmd.AddCommand(auditproxy.CmdAuditProxy)
Expand Down
42 changes: 40 additions & 2 deletions test/images/agnhost/porter/porter.go
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
package porter

import (
"encoding/json"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -55,6 +56,18 @@ To use a different cert/key, mount them into the pod and set the "CERT_FILE" and
Run: main,
}

// JSONResponse enables --json-response flag
var JSONResponse bool

type jsonResponse struct {
Method string
Body string
}

func init() {
CmdPorter.Flags().BoolVar(&JSONResponse, "json-response", false, "Responds to requests with a json response that includes the default value with the http.Request Method")
}

func main(cmd *cobra.Command, args []string) {
for _, vk := range os.Environ() {
// Put everything before the first = sign in parts[0], and
Expand All @@ -81,10 +94,23 @@ func main(cmd *cobra.Command, args []string) {
}

func servePort(port, value string) {

s := &http.Server{
Addr: "0.0.0.0:" + port,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, value)
body := value
if JSONResponse {
j, err := json.Marshal(&jsonResponse{
Method: r.Method,
Body: value})
if err != nil {
http.Error(w, fmt.Sprintf("Internal Server Error: %v", err), 500)
return
}
w.Header().Set("Content-Type", "application/json")
body = string(j)
}
fmt.Fprint(w, body)
}),
}
log.Printf("server on port %q failed: %v", port, s.ListenAndServe())
Expand All @@ -94,7 +120,19 @@ func serveTLSPort(port, value string) {
s := &http.Server{
Addr: "0.0.0.0:" + port,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, value)
body := value
if JSONResponse {
j, err := json.Marshal(&jsonResponse{
Method: r.Method,
Body: value})
if err != nil {
http.Error(w, fmt.Sprintf("Internal Server Error: %v", err), 500)
return
}
w.Header().Set("Content-Type", "application/json")
body = string(j)
}
fmt.Fprint(w, body)
}),
}
certFile := os.Getenv("CERT_FILE")
Expand Down