Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 11, 2024
1 parent a0dc7db commit 4bce276
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 77 deletions.
8 changes: 3 additions & 5 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
}

cid, err := clustermeta.ClusterUID(mgr.GetAPIReader())
// clustername := clusterid.ClusterName()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -333,17 +332,16 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
}
}
{
bc, err := b3.NewClient(c.ExtraConfig.BaseURL, c.ExtraConfig.Token, c.ExtraConfig.CACert)
bc, err := b3.NewClient(c.ExtraConfig.BaseURL, c.ExtraConfig.Token, c.ExtraConfig.CACert, cid)
if err != nil {
return nil, errors.Wrap(err, "failed to create b3 api client")
}

apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(identityapi.GroupName, Scheme, metav1.ParameterCodec, Codecs)

v1alpha1storage := map[string]rest.Storage{}

v1alpha1storage[identityapi.ResourceClusterIdentities] = clusteridstorage.NewStorage(ctrlClient, bc, cid)
v1alpha1storage[identityapi.ResourceInboxTokenRequests] = inboxtokenreqstorage.NewStorage(ctrlClient, bc, cid)
v1alpha1storage[identityapi.ResourceClusterIdentities] = clusteridstorage.NewStorage(ctrlClient, bc)
v1alpha1storage[identityapi.ResourceInboxTokenRequests] = inboxtokenreqstorage.NewStorage(ctrlClient, bc)
v1alpha1storage[identityapi.ResourceSelfSubjectNamespaceAccessReviews] = selfsubjectnamespaceaccessreview.NewStorage(kc, ctrlClient)
apiGroupInfo.VersionedResourcesStorageMap["v1alpha1"] = v1alpha1storage

Expand Down
50 changes: 44 additions & 6 deletions pkg/b3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import (
"path"

"go.bytebuilders.dev/license-verifier/info"
"gomodules.xyz/sync"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
identityapi "kmodules.xyz/resource-metadata/apis/identity/v1alpha1"
)
Expand All @@ -35,11 +38,11 @@ type Client struct {
token string
caCert []byte
client *http.Client
}

var Identity *identityapi.ClusterIdentity
clusterUID string
}

func NewClient(baseURL, token string, caCert []byte) (*Client, error) {
func NewClient(baseURL, token string, caCert []byte, clusterUID string) (*Client, error) {
c := &Client{
baseURL: baseURL,
token: token,
Expand Down Expand Up @@ -111,9 +114,13 @@ func (c *Client) GetToken() string {
if err != nil {
return "" // TODO
}
clusterID := Identity.UID
clusterName := Identity.Name
u.Path = path.Join(u.Path, "api/v1/agent", clusterName, string(clusterID), "token")

id, err := c.GetIdentity()
if err != nil {
return "" // TODO
}

u.Path = path.Join(u.Path, "api/v1/agent", id.Status.Name, id.Status.UID, "token")

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
Expand All @@ -135,3 +142,34 @@ func (c *Client) GetToken() string {
}
return string(body)
}

const SelfName = "self"

var (
identity *identityapi.ClusterIdentity
once sync.Once
idError error
creationTimestamp = metav1.Now()
)

func (c *Client) GetIdentity() (*identityapi.ClusterIdentity, error) {
once.Do(func() error {
var status *identityapi.ClusterIdentityStatus
status, idError = c.Identify(c.clusterUID)
if idError != nil {
return idError
}
identity = &identityapi.ClusterIdentity{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID("cid-" + c.clusterUID),
Name: SelfName,
CreationTimestamp: creationTimestamp,
Generation: 1,
},
Status: *status,
}
idError = nil
return idError
})
return identity, idError
}
1 change: 0 additions & 1 deletion pkg/cmds/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import (
ui "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/log"
_ "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

Expand Down
64 changes: 12 additions & 52 deletions pkg/registry/identity/clusteridentity/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

"kubeops.dev/ui-server/pkg/b3"

"gomodules.xyz/sync"
core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,18 +33,11 @@ import (
)

type Storage struct {
kc client.Client
bc *b3.Client
clusterUID string
convertor rest.TableConvertor

identity *identityapi.ClusterIdentity
once sync.Once
idError error
kc client.Client
bc *b3.Client
convertor rest.TableConvertor
}

const selfName = "self"

var (
_ rest.GroupVersionKindProvider = &Storage{}
_ rest.Scoper = &Storage{}
Expand All @@ -55,11 +46,10 @@ var (
_ rest.SingularNameProvider = &Storage{}
)

func NewStorage(kc client.Client, bc *b3.Client, clusterUID string) *Storage {
func NewStorage(kc client.Client, bc *b3.Client) *Storage {
return &Storage{
kc: kc,
bc: bc,
clusterUID: clusterUID,
kc: kc,
bc: bc,
convertor: rest.NewDefaultTableConvertor(schema.GroupResource{
Group: identityapi.GroupName,
Resource: identityapi.ResourceClusterIdentities,
Expand All @@ -86,40 +76,10 @@ func (r *Storage) New() runtime.Object {
func (r *Storage) Destroy() {}

func (r *Storage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
if name != selfName {
if name != b3.SelfName {
return nil, apierrors.NewNotFound(schema.GroupResource{Group: identityapi.GroupName, Resource: identityapi.ResourceClusterIdentities}, name)
}
r.knowThyself()
if r.idError != nil {
return nil, r.idError
}
return r.identity, nil
}

func (r *Storage) knowThyself() {
r.once.Do(func() error {
var ns core.Namespace
err := r.kc.Get(context.TODO(), client.ObjectKey{Name: metav1.NamespaceSystem}, &ns)
if err != nil {
return err
}

status, err := r.bc.Identify(r.clusterUID)
if err != nil {
// TODO
}
r.identity = &identityapi.ClusterIdentity{
ObjectMeta: metav1.ObjectMeta{
UID: "cid-" + ns.UID,
Name: selfName,
CreationTimestamp: ns.CreationTimestamp,
Generation: 1,
},
Status: *status,
}
b3.Identity = r.identity // set identity for further use in client.go (GetToken method)
return nil
})
return r.bc.GetIdentity()
}

// Lister
Expand All @@ -128,14 +88,14 @@ func (r *Storage) NewList() runtime.Object {
}

func (r *Storage) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
r.knowThyself()
if r.idError != nil {
return nil, r.idError
id, err := r.bc.GetIdentity()
if err != nil {
return nil, err
}
result := identityapi.ClusterIdentityList{
TypeMeta: metav1.TypeMeta{},
Items: []identityapi.ClusterIdentity{
*r.identity,
*id,
},
}
return &result, nil
Expand Down
18 changes: 5 additions & 13 deletions pkg/registry/identity/inboxtokenrequest/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package inboxtokenrequest
import (
"context"
"strings"
"sync"

"kubeops.dev/ui-server/pkg/b3"

Expand All @@ -32,14 +31,8 @@ import (
)

type Storage struct {
kc client.Client
bc *b3.Client
clusterUID string
convertor rest.TableConvertor

identity *identityapi.ClusterIdentity
idError error
once sync.Once
kc client.Client
bc *b3.Client
}

var (
Expand All @@ -50,11 +43,10 @@ var (
_ rest.SingularNameProvider = &Storage{}
)

func NewStorage(kc client.Client, bc *b3.Client, clusterUID string) *Storage {
func NewStorage(kc client.Client, bc *b3.Client) *Storage {
return &Storage{
kc: kc,
bc: bc,
clusterUID: clusterUID,
kc: kc,
bc: bc,
}
}

Expand Down

0 comments on commit 4bce276

Please sign in to comment.