diff --git a/pkg/kubectl/bind/authenticator/authenticator.go b/pkg/kubectl/bind/authenticator/authenticator.go index 8f9817a3..f800317c 100644 --- a/pkg/kubectl/bind/authenticator/authenticator.go +++ b/pkg/kubectl/bind/authenticator/authenticator.go @@ -17,6 +17,7 @@ limitations under the License. package authenticator import ( + "bytes" "context" "encoding/base64" "errors" @@ -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" @@ -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, } } @@ -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() @@ -142,5 +150,12 @@ func (d *LocalhostCallbackAuthenticator) callback(w http.ResponseWriter, r *http d.responseGvk = gvk close(d.done) - w.Write([]byte("

You have been authenticated successfully! Please head back to the command line

")) // 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 } diff --git a/pkg/kubectl/bind/plugin/bind.go b/pkg/kubectl/bind/plugin/bind.go index 2faae914..6681dc4b 100644 --- a/pkg/kubectl/bind/plugin/bind.go +++ b/pkg/kubectl/bind/plugin/bind.go @@ -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() @@ -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 { @@ -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) { diff --git a/pkg/template/files.go b/pkg/template/files.go new file mode 100644 index 00000000..065ce0a6 --- /dev/null +++ b/pkg/template/files.go @@ -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))) +} diff --git a/pkg/template/success.gohtml b/pkg/template/success.gohtml new file mode 100644 index 00000000..59b4437f --- /dev/null +++ b/pkg/template/success.gohtml @@ -0,0 +1,81 @@ + + + + + + + + Success! + + + + +
+
+
+ + ok +
+

Success!

+

Now you can create selected resources to your consumer cluster.
Please head back to the command line.

+ Go to Provider +
+
+ + \ No newline at end of file