From e0f353bd7e206f24d6204ce09d51be5c9e099f8c Mon Sep 17 00:00:00 2001 From: rasel Date: Wed, 28 Feb 2024 16:28:23 +0600 Subject: [PATCH] Add providerClusterName in authorize url Signed-off-by: rasel --- apis/kubebind/v1alpha1/bindingprovider_types.go | 8 -------- contrib/example-backend/http/handler.go | 3 +-- .../kubernetes/resources/cluster_binding.go | 4 ++-- .../cluster/servicebinding/servicebinding_reconcile.go | 9 ++++++--- pkg/kubectl/bind/plugin/authenticate.go | 4 +++- pkg/kubectl/bind/plugin/bind.go | 4 +++- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/apis/kubebind/v1alpha1/bindingprovider_types.go b/apis/kubebind/v1alpha1/bindingprovider_types.go index 7af16048..7e460d75 100644 --- a/apis/kubebind/v1alpha1/bindingprovider_types.go +++ b/apis/kubebind/v1alpha1/bindingprovider_types.go @@ -27,14 +27,6 @@ import ( type BindingProvider struct { metav1.TypeMeta `json:",inline"` - // providerClusterName is the name of the provider that is displayed to the user, e.g: - // MangoDB Inc. - // - // +required - // +kubebuilder:validation:Required - // +kubebuilder:validation:MinLength=1 - ProviderClusterName string `json:"providerClusterName"` - // version is the kube-bind.appscode.com version of the provider. The kubectl bind will check // this for compatibility. // diff --git a/contrib/example-backend/http/handler.go b/contrib/example-backend/http/handler.go index ad4c485d..dedda91c 100644 --- a/contrib/example-backend/http/handler.go +++ b/contrib/example-backend/http/handler.go @@ -123,8 +123,7 @@ func (h *handler) handleServiceExport(w http.ResponseWriter, r *http.Request) { APIVersion: v1alpha1.GroupVersion, Kind: "BindingProvider", }, - Version: ver, - ProviderPrettyName: "example-backend", + Version: ver, AuthenticationMethods: []v1alpha1.AuthenticationMethod{ { Method: "OAuth2CodeGrant", diff --git a/contrib/example-backend/kubernetes/resources/cluster_binding.go b/contrib/example-backend/kubernetes/resources/cluster_binding.go index b8771f99..da8074ea 100644 --- a/contrib/example-backend/kubernetes/resources/cluster_binding.go +++ b/contrib/example-backend/kubernetes/resources/cluster_binding.go @@ -26,7 +26,7 @@ import ( "k8s.io/klog/v2" ) -func CreateClusterBinding(ctx context.Context, client bindclient.Interface, ns, secretName, providerPrettyName string) error { +func CreateClusterBinding(ctx context.Context, client bindclient.Interface, ns, secretName, clusterName string) error { logger := klog.FromContext(ctx) clusterBinding := &v1alpha1.ClusterBinding{ @@ -35,7 +35,7 @@ func CreateClusterBinding(ctx context.Context, client bindclient.Interface, ns, Namespace: ns, }, Spec: v1alpha1.ClusterBindingSpec{ - ProviderPrettyName: providerPrettyName, + ProviderClusterName: clusterName, KubeconfigSecretRef: v1alpha1.LocalSecretKeyRef{ Name: secretName, Key: "kubeconfig", diff --git a/pkg/konnector/controllers/cluster/servicebinding/servicebinding_reconcile.go b/pkg/konnector/controllers/cluster/servicebinding/servicebinding_reconcile.go index 2971ac3a..dc87e1d3 100644 --- a/pkg/konnector/controllers/cluster/servicebinding/servicebinding_reconcile.go +++ b/pkg/konnector/controllers/cluster/servicebinding/servicebinding_reconcile.go @@ -64,7 +64,7 @@ func (r *reconciler) reconcile(ctx context.Context, binding *v1alpha1.APIService errs = append(errs, err) } - if err := r.ensurePrettyName(ctx, binding); err != nil { + if err := r.ensureClusterName(ctx, binding); err != nil { errs = append(errs, err) } @@ -197,7 +197,7 @@ func (r *reconciler) ensureCRDs(ctx context.Context, binding *v1alpha1.APIServic return utilerrors.NewAggregate(errs) } -func (r *reconciler) ensurePrettyName(ctx context.Context, binding *v1alpha1.APIServiceBinding) error { +func (r *reconciler) ensureClusterName(ctx context.Context, binding *v1alpha1.APIServiceBinding) error { binding.Status.Providers = []v1alpha1.Provider{} for _, provider := range r.providerInfos { clusterBinding, err := r.getClusterBinding(ctx, provider) @@ -211,7 +211,10 @@ func (r *reconciler) ensurePrettyName(ctx context.Context, binding *v1alpha1.API LocalSecretKeyRef: clusterBinding.Spec.KubeconfigSecretRef, Namespace: clusterBinding.Namespace, } - prov.PrettyName = clusterBinding.Spec.ProviderPrettyName + if clusterBinding.Status.Provider != nil { + prov.ClusterIdentity.ClusterName = clusterBinding.Spec.ProviderClusterName + prov.ClusterIdentity.ClusterUID = clusterBinding.Status.Provider.ClusterUID + } binding.Status.Providers = append(binding.Status.Providers, prov) } diff --git a/pkg/kubectl/bind/plugin/authenticate.go b/pkg/kubectl/bind/plugin/authenticate.go index d8d2e505..13dd31d0 100644 --- a/pkg/kubectl/bind/plugin/authenticate.go +++ b/pkg/kubectl/bind/plugin/authenticate.go @@ -34,6 +34,7 @@ import ( clientgoversion "k8s.io/client-go/pkg/version" ) +// getProvider calls for /export url and returns BindingProvider which contains the oidc authentication method func getProvider(url string) (*kubebindv1alpha1.BindingProvider, error) { resp, err := http.Get(url) if err != nil { @@ -90,7 +91,7 @@ func validateProviderVersion(providerVersion string) error { return nil } -func (b *BindOptions) authenticate(provider *kubebindv1alpha1.BindingProvider, callback, sessionID, clusterID string, urlCh chan<- string) error { +func (b *BindOptions) authenticate(provider *kubebindv1alpha1.BindingProvider, callback, sessionID, clusterID, clusterName string, urlCh chan<- string) error { var oauth2Method *kubebindv1alpha1.OAuth2CodeGrant for _, m := range provider.AuthenticationMethods { if m.Method == "OAuth2CodeGrant" { @@ -120,6 +121,7 @@ func (b *BindOptions) authenticate(provider *kubebindv1alpha1.BindingProvider, c values.Add("p", cbPort) values.Add("s", sessionID) values.Add("c", clusterID) + values.Add("n", clusterName) u.RawQuery = values.Encode() fmt.Fprintf(b.Options.ErrOut, "\nTo authenticate, visit in your browser:\n\n\t%s\n", u.String()) // nolint: errcheck diff --git a/pkg/kubectl/bind/plugin/bind.go b/pkg/kubectl/bind/plugin/bind.go index 030bd9e7..10701eb2 100644 --- a/pkg/kubectl/bind/plugin/bind.go +++ b/pkg/kubectl/bind/plugin/bind.go @@ -149,6 +149,8 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error { return err // should never happen because we test this in Validate() } + providerClusterName := exportURL.Query().Get("cluster") + provider, err := getProvider(exportURL.String()) if err != nil { return fmt.Errorf("failed to fetch authentication url %q: %v", exportURL, err) @@ -182,7 +184,7 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error { } sessionID := SessionID() - if err := b.authenticate(provider, auth.Endpoint(), sessionID, ClusterID(ns), urlCh); err != nil { + if err := b.authenticate(provider, auth.Endpoint(), sessionID, ClusterID(ns), providerClusterName, urlCh); err != nil { return err }