From 621ffa1d254c76b7371533410302028ab7fd41b8 Mon Sep 17 00:00:00 2001 From: qingliu Date: Wed, 19 Feb 2025 20:22:48 +0800 Subject: [PATCH] feat: adapt simple UI testing tool 1. Adapt fields and methods in go template. 2. Adapt gRPC server configuration to support self-signed certificates. --- tools/simpleui/main.go | 30 ++++++++++++++++++++++++--- tools/simpleui/template.go | 14 +++---------- tools/simpleui/templates/records.html | 4 ++-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/tools/simpleui/main.go b/tools/simpleui/main.go index 6c4c6a517..2b5f72bec 100644 --- a/tools/simpleui/main.go +++ b/tools/simpleui/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "html/template" "log" @@ -10,9 +11,18 @@ import ( "github.com/gorilla/mux" pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto" "google.golang.org/grpc" + "google.golang.org/grpc/credentials" ) var ( + // Command line flags + // Cert file can be generated by: kubectl get secrets tekton-results-tls -n tekton-pipelines --template='{{index .data "tls.crt"}}' | base64 -d > /tmp/results.crt + certFile = flag.String("cert", "/tmp/results.crt", "Path to the TLS certificate file.") + target = flag.String("target", "tekton-results-api-service.tekton-pipelines.svc.cluster.local", "Target server name") + port = flag.String("port", "8081", "Port to listen on") + grpcAddr = flag.String("grpc-addr", "localhost:8080", "gRPC server address") + + // Templates resultTmpl = template.Must(template.New("results.html").ParseFiles("templates/results.html")) recordTmpl = template.Must(template.New("records.html"). Funcs(template.FuncMap{ @@ -68,7 +78,19 @@ type ui struct { } func main() { - conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock()) + // Parse command line flags + flag.Parse() + + // Load the TLS certificate + creds, err := credentials.NewClientTLSFromFile(*certFile, *target) + if err != nil { + log.Fatalf("failed to load credentials: %v", err) + } + + // Connect to the gRPC server + conn, err := grpc.NewClient(*grpcAddr, + grpc.WithTransportCredentials(creds), + grpc.WithAuthority(*target)) if err != nil { log.Fatalf("did not connect: %v", err) } @@ -76,6 +98,7 @@ func main() { u := &ui{ client: pb.NewResultsClient(conn), } + log.Printf("Connected to gRPC server at %s", *grpcAddr) r := mux.NewRouter() r.HandleFunc("/", u.home) @@ -83,6 +106,7 @@ func main() { r.HandleFunc("/{namespace}/results/{name}", u.records) http.Handle("/", r) - log.Println("Running on http://localhost:8080") - log.Fatal(http.ListenAndServe(":8080", nil)) + addr := ":" + *port + log.Printf("Running on http://localhost%s", addr) + log.Fatal(http.ListenAndServe(addr, nil)) } diff --git a/tools/simpleui/template.go b/tools/simpleui/template.go index 25af1a038..db2545671 100644 --- a/tools/simpleui/template.go +++ b/tools/simpleui/template.go @@ -1,22 +1,14 @@ package main import ( - "fmt" "strings" - "google.golang.org/protobuf/encoding/prototext" - "google.golang.org/protobuf/types/known/anypb" - _ "github.com/tektoncd/results/proto/pipeline/v1/pipeline_go_proto" + pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto" ) -func textproto(a *anypb.Any) (string, error) { - m, err := a.UnmarshalNew() - if err != nil { - fmt.Println(err) - return "", err - } - return prototext.Format(m), nil +func textproto(a *pb.Any) string { + return a.String() } func parent(in string) string { diff --git a/tools/simpleui/templates/records.html b/tools/simpleui/templates/records.html index 891875904..cece53565 100644 --- a/tools/simpleui/templates/records.html +++ b/tools/simpleui/templates/records.html @@ -22,7 +22,7 @@

Records

{{range .Response.Records}} {{.Name}} - {{trimprefix .Data.TypeUrl "type.googleapis.com/"}} + {{trimprefix .Data.Type "type.googleapis.com/"}} {{parent .Name}} {{.CreatedTime.AsTime}} {{textproto .Data}} @@ -30,4 +30,4 @@

Records

{{end}} - \ No newline at end of file +