Skip to content

Commit

Permalink
Full flow for get token working.Tested
Browse files Browse the repository at this point in the history
Signed-off-by: shn27 <[email protected]>
  • Loading branch information
shn27 committed Jun 11, 2024
1 parent d0ae8cc commit eabfcac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
7 changes: 1 addition & 6 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package apiserver
import (
"context"
"fmt"
"kmodules.xyz/client-go/tools/clusterid"
"os"
"time"

Expand Down Expand Up @@ -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)

Expand Down
32 changes: 15 additions & 17 deletions pkg/registry/identity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
14 changes: 5 additions & 9 deletions pkg/registry/identity/clusteridentity/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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
})
}
Expand All @@ -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) {
Expand Down

0 comments on commit eabfcac

Please sign in to comment.