Skip to content

Commit

Permalink
Update success page (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: rasel <[email protected]>
  • Loading branch information
Superm4n97 authored Apr 8, 2024
1 parent 45e40b8 commit 78f2a86
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
21 changes: 18 additions & 3 deletions pkg/kubectl/bind/authenticator/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package authenticator

import (
"bytes"
"context"
"encoding/base64"
"errors"
Expand All @@ -28,6 +29,7 @@ import (
"time"

kubebindv1alpha1 "go.bytebuilders.dev/kube-bind/apis/kubebind/v1alpha1"
"go.bytebuilders.dev/kube-bind/pkg/template"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -53,11 +55,13 @@ type LocalhostCallbackAuthenticator struct {
done chan struct{}
response runtime.Object
responseGvk *schema.GroupVersionKind
redirectURL string
}

func NewLocalhostCallbackAuthenticator() *LocalhostCallbackAuthenticator {
func NewLocalhostCallbackAuthenticator(redirectURL string) *LocalhostCallbackAuthenticator {
return &LocalhostCallbackAuthenticator{
done: make(chan struct{}),
done: make(chan struct{}),
redirectURL: redirectURL,
}
}

Expand Down Expand Up @@ -109,6 +113,10 @@ func (d *LocalhostCallbackAuthenticator) WaitForResponse(ctx context.Context) (r
}
}

type SuccessOptions struct {
RedirectURL string
}

func (d *LocalhostCallbackAuthenticator) callback(w http.ResponseWriter, r *http.Request) {
d.mu.Lock()
defer d.mu.Unlock()
Expand Down Expand Up @@ -142,5 +150,12 @@ func (d *LocalhostCallbackAuthenticator) callback(w http.ResponseWriter, r *http
d.responseGvk = gvk
close(d.done)

w.Write([]byte("<h1>You have been authenticated successfully! Please head back to the command line</h1>")) // nolint: errcheck
op := SuccessOptions{RedirectURL: d.redirectURL}
bs := bytes.Buffer{}
if err = template.GetTemplate(template.TemplateSuccessPage).Execute(&bs, op); err != nil {
logger.Error(err, "error executing success template")
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
w.Write(bs.Bytes()) // nolint: errcheck
}
11 changes: 8 additions & 3 deletions pkg/kubectl/bind/plugin/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func (b *BindOptions) Validate() error {
return b.Options.Validate()
}

// redirectUrl generates the redirect url for success page
// accepts string as bind.{host}
func redirectUrl(host, user, cluster string) string {
_, domain, _ := strings.Cut(host, "bind.")
return fmt.Sprintf("https://db.%s/%s/%s", domain, user, cluster)
}

// Run starts the binding process.
func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
config, err := b.ClientConfig.ClientConfig()
Expand Down Expand Up @@ -181,7 +188,7 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
}
}

auth := authenticator.NewLocalhostCallbackAuthenticator()
auth := authenticator.NewLocalhostCallbackAuthenticator(redirectUrl(exportURL.Host, user, providerClusterName))
err = auth.Start()
fmt.Fprintf(b.Options.ErrOut, "\n\n")
if err != nil {
Expand Down Expand Up @@ -281,9 +288,7 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
"apiservice",
"--remote-kubeconfig-namespace", secret.Namespace,
"--remote-kubeconfig-name", secret.Name,
// comment the remote namespace
"--remote-namespace", remoteNamespace,
//"--konnector-image", "superm4n/konnector:v0.5.0_linux_amd64",
"-f", "-",
}
b.flags.VisitAll(func(flag *pflag.Flag) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/template/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package template

import (
"embed"
"html/template"
)

//go:embed *
var files embed.FS

type Template string

const (
TemplateSuccessPage Template = "success.gohtml"
)

func GetTemplate(t Template) *template.Template {
return template.Must(template.New(string(t)).
ParseFS(files, string(t)))
}
81 changes: 81 additions & 0 deletions pkg/template/success.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!doctype html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bulma CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<title>Success!</title>

<style>
:root {
--color-heading: hsl(196, 10%, 10%);
--color-text: hsl(192, 10%, 20%);
--color-primary: hsl(149, 100%, 30%);
--color-primary-hover: hsl(149, 100%, 28%);
}
.message-wrapper {
height: 100vh;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
.message-inner {
max-width: 800px;
margin: 30px;
&.has-text-centered {
max-width: 600px;
.icon {
margin: 0 auto;
}
p {
text-wrap: balance;
}
}
.icon {
width: 96px;
height: auto;
display: flex;
align-items: center;
margin-bottom: 8px;
}
h1 {
font-size: 64px;
margin-bottom: 8px;
color: var(--color-heading);
line-height: 1;
font-weight: 700;
}
p {
font-size: 16px;
margin-bottom: 16px;
color: var(--color-text);
}
}
}
.button{
font-weight: 500;
border-radius: 2px;
&.is-primary {
background-color: var(--color-primary);
&:hover {
background-color: var(--color-primary-hover);
}
}
}
</style>
</head>
<body>
<div class="message-wrapper">
<div class="message-inner has-text-centered">
<div class="icon">
<!-- Why it's not working? ./icons/checked.png -->
<img src="https://img.icons8.com/tiny-color/128/000000/ok.png" alt="ok"/>
</div>
<h1>Success!</h1>
<p>Now you can create selected resources to your consumer cluster. <br><b>Please head back to the command line.</b></p>
<a href="{{.RedirectURL}}" class="button is-primary">Go to Provider</a>
</div>
</div>
</body>
</html>

0 comments on commit 78f2a86

Please sign in to comment.