From eabfcacf1fe07e75b38d46ec5b2e297cf1edfabd Mon Sep 17 00:00:00 2001 From: shn27 Date: Tue, 11 Jun 2024 18:31:15 +0600 Subject: [PATCH] Full flow for get token working.Tested Signed-off-by: shn27 --- pkg/apiserver/apiserver.go | 7 +--- pkg/registry/identity/client.go | 32 +++++++++---------- .../identity/clusteridentity/storage.go | 14 +++----- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 3c43b24d5..b29b99cd4 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -19,7 +19,6 @@ package apiserver import ( "context" "fmt" - "kmodules.xyz/client-go/tools/clusterid" "os" "time" @@ -248,15 +247,11 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) { } cid, err := clustermeta.ClusterUID(mgr.GetAPIReader()) - clustername := clusterid.ClusterName() - - klog.Info("=====================madrid ", clustername, " hala madrid =====================", cid, " hala halaaaaaaaaaa ") - //time.Sleep(100 * time.Second) + //clustername := clusterid.ClusterName() if err != nil { return nil, err } - //clusterName, err := clustermeta. rbacAuthorizer := authorizer.NewForManagerOrDie(ctx, mgr) diff --git a/pkg/registry/identity/client.go b/pkg/registry/identity/client.go index a45e5ade0..14765c687 100644 --- a/pkg/registry/identity/client.go +++ b/pkg/registry/identity/client.go @@ -24,7 +24,6 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/json" - "k8s.io/klog/v2" identityapi "kubeops.dev/ui-server/apis/identity/v1alpha1" "net/http" "path" @@ -37,6 +36,8 @@ type Client struct { client *http.Client } +var Identity *identityapi.ClusterIdentity + func NewClient(baseURL, token string, caCert []byte) (*Client, error) { c := &Client{ baseURL: baseURL, @@ -62,13 +63,13 @@ func (c *Client) Identify(clusterUID string) (*identityapi.ClusterIdentityStatus u, err := info.APIServerAddress(c.baseURL) if err != nil { - return nil, err + return nil, err //TODO } - u.Path = path.Join(u.Path, "api/v1/clusters", clusterUID) + u.Path = path.Join(u.Path, "api/v1/clustersv2/identity", clusterUID) req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { - return nil, err + return nil, err //TODO } req.Header.Set("Content-Type", "application/json") // add authorization header to the req @@ -77,14 +78,14 @@ func (c *Client) Identify(clusterUID string) (*identityapi.ClusterIdentityStatus } resp, err := c.client.Do(req) if err != nil { - return nil, err + return nil, err //TODO } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return nil, err + return nil, err //TODO } if resp.StatusCode != http.StatusOK { @@ -98,7 +99,6 @@ func (c *Client) Identify(clusterUID string) (*identityapi.ClusterIdentityStatus false, ) } - var ds identityapi.ClusterIdentityStatus err = json.Unmarshal(body, &ds) if err != nil { @@ -110,13 +110,15 @@ func (c *Client) Identify(clusterUID string) (*identityapi.ClusterIdentityStatus func (c *Client) GetToken() string { u, err := info.APIServerAddress(c.baseURL) if err != nil { - return "nil" + return "" //TODO } - u.Path = path.Join(u.Path, "api/v1/agent/token-test", c.token, "token") //This c.token should be clusterUUID TODO + clusterID := Identity.UID + clusterName := Identity.Name + u.Path = path.Join(u.Path, "api/v1/agent", clusterName, string(clusterID), "token") + req, err := http.NewRequest(http.MethodGet, u.String(), nil) if err != nil { - klog.Error("========================req 1 error==================================", err) - return "" + return "" //TODO } req.Header.Set("Content-Type", "application/json") // add authorization header to the req @@ -125,16 +127,12 @@ func (c *Client) GetToken() string { } resp, err := c.client.Do(req) if err != nil { - klog.Error("========================req 2 error==================================", err) - return "" + return "" //TODO } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) - klog.Info("======================================================", string(body)) - //time.Sleep(5 * time.Second) if err != nil { - klog.Error("========================req 3 error==================================", err) - return "" + return "" //TODO } return string(body) } diff --git a/pkg/registry/identity/clusteridentity/storage.go b/pkg/registry/identity/clusteridentity/storage.go index 0a4d18c3a..aae13bbb0 100644 --- a/pkg/registry/identity/clusteridentity/storage.go +++ b/pkg/registry/identity/clusteridentity/storage.go @@ -18,10 +18,9 @@ package clusteridentity import ( "context" - "strings" - identityapi "kubeops.dev/ui-server/apis/identity/v1alpha1" "kubeops.dev/ui-server/pkg/registry/identity" + "strings" "gomodules.xyz/sync" core "k8s.io/api/core/v1" @@ -105,12 +104,10 @@ func (r *Storage) knowThyself() { if err != nil { return err } - status, err := r.bc.Identify(r.clusterUID) if err != nil { - return err + //TODO } - r.identity = &identityapi.ClusterIdentity{ ObjectMeta: metav1.ObjectMeta{ UID: "cid-" + ns.UID, @@ -120,6 +117,7 @@ func (r *Storage) knowThyself() { }, Status: *status, } + identity.Identity = r.identity // set identity for further use in client.go (GetToken method) return nil }) } @@ -134,20 +132,18 @@ func (r *Storage) List(ctx context.Context, options *internalversion.ListOptions if r.idError != nil { return nil, r.idError } - result := identityapi.ClusterIdentityList{ TypeMeta: metav1.TypeMeta{}, Items: []identityapi.ClusterIdentity{ *r.identity, }, } - return &result, nil } func (r *Storage) Watch(ctx context.Context, options *internalversion.ListOptions) (watch.Interface, error) { - // TODO implement me - panic("implement me") + r.Get(ctx, "self", nil) + return nil, nil } func (r *Storage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {