Skip to content

Commit

Permalink
fix(ui): Add support for bash href. Fixes ##2100 (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec authored Jan 30, 2020
1 parent 516d05f commit 7611b9f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
15 changes: 14 additions & 1 deletion cmd/argo/commands/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"os"
"time"

"github.com/argoproj/pkg/cli"
Expand All @@ -22,6 +23,7 @@ func NewServerCommand() *cobra.Command {
authMode string
configMap string
port int
baseHRef string
namespaced bool // --namespaced
managedNamespace string // --managed-namespace
)
Expand Down Expand Up @@ -60,9 +62,15 @@ func NewServerCommand() *cobra.Command {
managedNamespace = namespace
}

log.WithFields(log.Fields{"namespace": namespace, "managedNamespace": managedNamespace}).Info()
log.WithFields(log.Fields{
"authMode": authMode,
"namespace": namespace,
"managedNamespace": managedNamespace,
"baseHRef": baseHRef}).
Info()

opts := apiserver.ArgoServerOpts{
BaseHRef: baseHRef,
Namespace: namespace,
WfClientSet: wflientset,
KubeClientset: kubeConfig,
Expand All @@ -80,6 +88,11 @@ func NewServerCommand() *cobra.Command {
}

command.Flags().IntVarP(&port, "port", "p", 2746, "Port to listen on")
baseHref, ok := os.LookupEnv("BASE_HREF")
if !ok {
baseHRef = "/"
}
command.Flags().StringVar(&baseHRef, "basehref", baseHref, "Value for base href in index.html. Used if the server is running behind reverse proxy under subpath different from /. Defaults to the environment variable BASE_HREF.")
command.Flags().StringVar(&authMode, "auth-mode", "server", "API server authentication mode. One of: client|server|hybrid")
command.Flags().StringVar(&configMap, "configmap", "workflow-controller-configmap", "Name of K8s configmap to retrieve workflow controller configuration")
command.Flags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error")
Expand Down
5 changes: 4 additions & 1 deletion server/apiserver/argoserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
)

type argoServer struct {
baseHRef string
namespace string
managedNamespace string
kubeClientset *kubernetes.Clientset
Expand All @@ -49,6 +50,7 @@ type argoServer struct {
}

type ArgoServerOpts struct {
BaseHRef string
Namespace string
KubeClientset *kubernetes.Clientset
WfClientSet *versioned.Clientset
Expand All @@ -60,6 +62,7 @@ type ArgoServerOpts struct {

func NewArgoServer(opts ArgoServerOpts) *argoServer {
return &argoServer{
baseHRef: opts.BaseHRef,
namespace: opts.Namespace,
managedNamespace: opts.ManagedNamespace,
kubeClientset: opts.KubeClientset,
Expand Down Expand Up @@ -218,7 +221,7 @@ func (as *argoServer) newHTTPServer(ctx context.Context, port int, artifactServe
mux.Handle("/api/", gwmux)
mux.HandleFunc("/artifacts/", artifactServer.GetArtifact)
mux.HandleFunc("/artifacts-by-uid/", artifactServer.GetArtifactByUID)
mux.HandleFunc("/", static.ServerFiles)
mux.HandleFunc("/", static.NewFilesServer(as.baseHRef).ServerFiles)
return &httpServer
}

Expand Down
23 changes: 23 additions & 0 deletions server/static/response-rewriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package static

import (
"bytes"
"net/http"
"strconv"

log "github.com/sirupsen/logrus"
)

type responseRewriter struct {
http.ResponseWriter
old []byte
new []byte
}

func (w *responseRewriter) Write(a []byte) (int, error) {
b := bytes.Replace(a, w.old, w.new, 1)
// status code and headers are printed out when we write data
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
log.WithFields(log.Fields{"a": string(a), "b": string(b)}).Info()
return w.ResponseWriter.Write(b)
}
18 changes: 17 additions & 1 deletion server/static/static.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package static

import (
"fmt"
"net/http"
"strings"
)

func ServerFiles(w http.ResponseWriter, r *http.Request) {
type FilesServer struct {
baseHRef string
}

func NewFilesServer(baseHRef string) *FilesServer {
return &FilesServer{baseHRef}
}

func (s *FilesServer) ServerFiles(w http.ResponseWriter, r *http.Request) {

// this hack allows us to server the routes (e.g. /workflows) with the index file
if !strings.Contains(r.URL.Path, ".") {
r.URL.Path = "index.html"
}

if r.URL.Path == "index.html" {
// hack to prevent ServerHTTP from giving us gzipped content which we can do our search-and-replace on
r.Header.Del("Accept-Encoding")
w = &responseRewriter{ResponseWriter: w, old: []byte(`<base href="/">`), new: []byte(fmt.Sprintf(`<base href="%s">`, s.baseHRef))}
}

// in my IDE (IntelliJ) the next line is red for some reason - but this is fine
ServeHTTP(w, r)
}

0 comments on commit 7611b9f

Please sign in to comment.