Skip to content

Commit

Permalink
feat: add basic auth, modify data model for new bke apis
Browse files Browse the repository at this point in the history
  • Loading branch information
chpiano2000 committed Oct 3, 2023
1 parent 69bc1e2 commit ce02118
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
11 changes: 10 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Client struct {
appCredSecret string
projectID string
regionName string
basicAuth string
services []*Service
}

Expand Down Expand Up @@ -132,6 +133,13 @@ func WithProjectId(id string) Option {
}
}

func WithBasicAuth(basicAuth string) Option {
return func(c *Client) error {
c.basicAuth = basicAuth
return nil
}
}

// NewClient creates new BizFly client.
func NewClient(options ...Option) (*Client, error) {
c := &Client{
Expand Down Expand Up @@ -217,6 +225,8 @@ func (c *Client) NewRequest(ctx context.Context, method, serviceName string, url
req.Header.Add("Accept", mediaType)
req.Header.Add("User-Agent", c.userAgent)
req.Header.Add("X-Project-Id", c.projectID)
req.Header.Add("Authorization", "Basic " + c.basicAuth)


if c.authType == "" {
c.authType = defaultAuthType
Expand Down Expand Up @@ -258,7 +268,6 @@ func (c *Client) Do(ctx context.Context, req *http.Request) (resp *http.Response
defer resp.Body.Close()
buf, _ := ioutil.ReadAll(resp.Body)
err = errorFromStatus(resp.StatusCode, string(buf))

}
return
}
Expand Down
6 changes: 3 additions & 3 deletions kubernetes_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
clusterPath = "/_"
kubeConfig = "kubeconfig"
k8sVersion = "/k8s_versions"
ClusterInfo = "/engine/cluster_info"
clusterJoinEverywhere = "/engine/cluster_join_everywhere"
clusterInfo = "engine/cluster_info"
clusterJoinEverywhere = "engine/cluster_join_everywhere"
nodeEverywhere = "_/node_everywhere"
)

Expand All @@ -41,7 +41,7 @@ type KubernetesEngineService interface {
GetKubeConfig(ctx context.Context, clusterUID string) (string, error)
GetKubernetesVersion(ctx context.Context) (*KubernetesVersionResponse, error)
GetClusterInfo(ctx context.Context, pool_id string) (*ClusterInfoResponse, error)
AddClusterEverywhere(ctx context.Context, id string, cjer *clusterJoinEverywhereRequest) (*clusterJoinEverywhereResponse, error)
AddClusterEverywhere(ctx context.Context, id string, cjer *ClusterJoinEverywhereRequest) (*ClusterJoinEverywhereResponse, error)
GetEverywhere(ctx context.Context, id string) (*EverywhereNode, error)
}

Expand Down
56 changes: 31 additions & 25 deletions kubernetes_engine_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@ import (
"net/http"
)

type clusterJoinEverywhereRequest struct {
type ClusterJoinEverywhereRequest struct {
Hostname string `json:"hostname" yaml:"hostname"`
IPAddresses []string `json:"ip_addresses" yaml:"ip_addresses"`
Capacity []everywhereNodeCapacity `json:"capacity" yaml:"capacity"`
Capacity EverywhereNodeCapacity `json:"capacity" yaml:"capacity"`
}

type everywhereNodeCapacity struct {
type EverywhereNodeCapacity struct {
Cores int `json:"cores" yaml:"cores"`
MemoryKB int `json:"memory_kb" yaml:"memory_kb"`
}

type clusterCertificate struct {
type ClusterCertificate struct {
CaCert string `json:"ca.pem" yaml:"ca.pem"`
ClientKey string `json:"client-key.pem" yaml:"client-key.pem"`
ClientCert string `json:"client.pem" yaml:"client.pem"`
}

type clusterReserved struct {
SystemReserved clusterSystemReserved `json:"system_reserved" yaml:"system_reserved"`
KubeReserved clusterKubeReserved `json:"kube_reserved" yaml:"kube_reserved"`
type ClusterReserved struct {
SystemReserved ClusterSystemReserved `json:"system_reserved" yaml:"system_reserved"`
KubeReserved ClusterKubeReserved `json:"kube_reserved" yaml:"kube_reserved"`
}

type clusterSystemReserved struct {
CPU string `json:"cpu" yaml:"cpu"`
type ClusterSystemReserved struct {
CPU string `json:"cpu" yaml:"cpu"`
Memory string `json:"memory" yaml:"memory"`
}

type clusterKubeReserved struct {
CPU string `json:"cpu" yaml:"cpu"`
type ClusterKubeReserved struct {
CPU string `json:"cpu" yaml:"cpu"`
Memory string `json:"memory" yaml:"memory"`
}

type clusterJoinEverywhereResponse struct {
APIServer string `json:"apiserver" yaml:"apiserver"`
type ClusterJoinEverywhereResponse struct {
APIServer string `json:"apiserver" yaml:"apiserver"`
ClusterDNS string `json:"cluster_dns" yaml:"cluster_dns"`
ClusterCIDR string `json:"cluster_cidr" yaml:"cluster_cidr"`
CloudProvider string `json:"cloud_provider" yaml:"cloud_provider"`
Certificate clusterCertificate `json:"certificate" yaml:"certificate"`
UUID string `json:"uuid" yaml:"uuid"`
MaxPods int `json:"max_pods" yaml:"max_pods"`
Reserved clusterReserved `json:"reserved" yaml:"reserved"`
Certificate ClusterCertificate `json:"certificate" yaml:"certificate"`
UUID string `json:"uuid" yaml:"uuid"`
MaxPods int `json:"max_pods" yaml:"max_pods"`
Reserved ClusterReserved `json:"reserved" yaml:"reserved"`
}

func (c *kubernetesEngineService) AddClusterEverywhere(ctx context.Context, id string, cjer *clusterJoinEverywhereRequest) (*clusterJoinEverywhereResponse, error) {
var joinEverywhereResponse *clusterJoinEverywhereResponse
func (c *kubernetesEngineService) AddClusterEverywhere(ctx context.Context, id string, cjer *ClusterJoinEverywhereRequest) (*ClusterJoinEverywhereResponse, error) {
var joinEverywhereResponse ClusterJoinEverywhereResponse
req, err := c.client.NewRequest(ctx, http.MethodPost, kubernetesServiceName, clusterJoinEverywhere, &cjer)
if err != nil {
return nil, err
Expand All @@ -60,12 +60,17 @@ func (c *kubernetesEngineService) AddClusterEverywhere(ctx context.Context, id s
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if err := json.NewDecoder(resp.Body).Decode(joinEverywhereResponse); err != nil {
err = json.Unmarshal(body, &joinEverywhereResponse)
if err != nil {
return nil, err
}
return joinEverywhereResponse, nil
return &joinEverywhereResponse, nil
}

type WorkerConfig struct {
Expand All @@ -84,11 +89,12 @@ type ClusterInfoResponse struct {
WorkerConfig WorkerConfig `json:"worker_config" yaml:"worker_configs"`
K8sVersion string `json:"k8s_version" yaml:"k8s_version"`
ShootUid string `json:"shoot_uid" yaml:"shoot_uid"`
PoolName string `json:"pool_name" yaml:"pool_name"`
}

func (c *kubernetesEngineService) GetClusterInfo(ctx context.Context, pool_id string) (*ClusterInfoResponse, error) {
var clusterInfo ClusterInfoResponse
req, err := c.client.NewRequest(ctx, http.MethodGet, kubernetesServiceName, ClusterInfo+"/"+pool_id, nil)
var clusterInfoResponse ClusterInfoResponse
req, err := c.client.NewRequest(ctx, http.MethodGet, kubernetesServiceName, clusterInfo+"/"+pool_id, nil)
if err != nil {
return nil, err
}
Expand All @@ -102,10 +108,10 @@ func (c *kubernetesEngineService) GetClusterInfo(ctx context.Context, pool_id st
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &clusterInfo)
err = json.Unmarshal(body, &clusterInfoResponse)
if err != nil {
return nil, err
}
return &clusterInfo, nil
return &clusterInfoResponse, nil
}

4 changes: 4 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (s *service) List(ctx context.Context) ([]*Service, error) {
buf := new(bytes.Buffer)

req, err := http.NewRequest("GET", u.String(), buf)

if s.client.basicAuth != "" {
req.Header.Set("Authorization", "Basic " + s.client.basicAuth)
}
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"net/http"
"io"
)

const (
Expand Down Expand Up @@ -33,7 +34,7 @@ type TokenCreateRequest struct {
// Token contains token information.
type Token struct {
KeystoneToken string `json:"token"`
ExpiresAt string `json:"expires_at"`
ExpiresAt string `json:"expire_at"`
ProjectName string `json:"project_name"`
ProjectId string `json:"project_id"`
}
Expand Down Expand Up @@ -67,13 +68,15 @@ func (t *token) create(ctx context.Context, tcr *TokenCreateRequest) (*Token, er
return nil, err
}
resp, err := t.client.Do(ctx, req)
body, _ := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()

tok := &Token{}
if err := json.NewDecoder(resp.Body).Decode(tok); err != nil {
var tok *Token
err = json.Unmarshal(body, &tok)
if err != nil {
return nil, err
}
// Get new services catalog after create token
Expand Down

0 comments on commit ce02118

Please sign in to comment.