Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private/babbara/enable proxy setting #17

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func bootstrapCmdRun(cmd *cobra.Command, args []string) {
log.Fatalf("Unable to load context: %s", err.Error())
}

c, err := clients.New(ctx.Fqdn)
c, err := clients.New(ctx.Fqdn, ctx.Proxy)
if err != nil {
log.Fatalf("Unable to load clients: %s", err.Error())
}
Expand All @@ -69,6 +69,7 @@ func bootstrapCmdRun(cmd *cobra.Command, args []string) {
MetalLBAddressPool: metallbIPRange,
AllowWorkloadOnMaster: allowWorkloadsOnMaster,
Privileged: privileged,
HTTPProxy: ctx.Proxy,
}

err = pmk.Bootstrap(ctx, c, payload)
Expand Down
5 changes: 5 additions & 0 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func contextCmdCreateRun(cmd *cobra.Command, args []string) {
service, _ := reader.ReadString('\n')
service = strings.TrimSuffix(service, "\n")

fmt.Printf("Proxy: ")
proxy, _ := reader.ReadString('\n')
proxy = strings.TrimSuffix(proxy, "\n")

if region == "" {
region = "RegionOne"
}
Expand All @@ -61,6 +65,7 @@ func contextCmdCreateRun(cmd *cobra.Command, args []string) {
Password: encodedPasswd,
Region: region,
Tenant: service,
Proxy: proxy,
}

if err := pmk.StoreContext(ctx, constants.Pf9DBLoc); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/prepNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func prepNodeRun(cmd *cobra.Command, args []string) {
log.Fatalf("Unable to load the context: %s\n", err.Error())
}

c, err := clients.New(ctx.Fqdn)
c, err := clients.New(ctx.Fqdn, ctx.Proxy)
if err != nil {
log.Fatalf("Unable to load clients needed for the Cmd. Error: %s", err.Error())
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module github.com/platform9/pf9ctl
go 1.14

require (
github.com/PuerkitoBio/rehttp v1.0.0
github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0 // indirect
github.com/benbjohnson/clock v1.0.3 // indirect
github.com/google/uuid v1.1.2
github.com/hashicorp/go-retryablehttp v0.6.7
github.com/mitchellh/go-homedir v1.1.0
Expand All @@ -11,6 +14,7 @@ require (
github.com/sethgrid/pester v1.1.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.3.0
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/rehttp v1.0.0 h1:aJ7A7YI2lIvOxcJVeUZY4P6R7kKZtLeONjgyKGwOIu8=
github.com/PuerkitoBio/rehttp v1.0.0/go.mod h1:ItsOiHl4XeMOV3rzbZqQRjLc3QQxbE6391/9iNG7rE8=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0 h1:0NmehRCgyk5rljDQLKUO+cRJCnduDyn11+zGZIc9Z48=
github.com/aybabtme/iocontrol v0.0.0-20150809002002-ad15bcfc95a0/go.mod h1:6L7zgvqo0idzI7IO8de6ZC051AfXb5ipkIJ7bIA2tGA=
github.com/benbjohnson/clock v1.0.3 h1:vkLuvpK4fmtSCuo60+yC63p7y0BmQ8gm5ZXGuBCJyXg=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down Expand Up @@ -158,6 +164,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
25 changes: 19 additions & 6 deletions pkg/pmk/clients/clients.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package clients

const HTTPMaxRetry = 5
// HTTPMaxRetry indicates the number of
// retries to be carried out before giving up.
const HTTPMaxRetry = 9

// Clients struct encapsulate the collection of
// Client struct encapsulate the collection of
// external services
type Client struct {
Resmgr Resmgr
Keystone Keystone
Qbert Qbert
Executor Executor
Segment Segment
HTTP HTTP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it an HTTPClient

}

// New creates the clients needed by the CLI
// to interact with the external services.
func New(fqdn string) (Client, error) {
func New(fqdn string, proxy string) (Client, error) {

http, err := NewHTTP(
func(impl *HTTPImpl) { impl.Proxy = proxy },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we passing in functions ? Why don't we just pass in the value ?

func(impl *HTTPImpl) { impl.Retry = HTTPMaxRetry })

if err != nil {
return Client{}, err
}

return Client{
Resmgr: NewResmgr(fqdn),
Keystone: NewKeystone(fqdn),
Qbert: NewQbert(fqdn),
Resmgr: NewResmgr(fqdn, http),
Keystone: NewKeystone(fqdn, http),
Qbert: NewQbert(fqdn, http),
Executor: ExecutorImpl{},
Segment: NewSegment(fqdn),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to understand more on how we have used in different places, in general I would like to see more "events" and segment should be able to "consume" those events instead of directly using segment.

HTTP: http,
}, nil
}
59 changes: 59 additions & 0 deletions pkg/pmk/clients/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package clients

import (
"net/http"
"net/url"
"time"

"github.com/PuerkitoBio/rehttp"
)

type HTTP interface {
Do(req *http.Request) (*http.Response, error)
}

type HTTPImpl struct {
Proxy string
Retry int
client *http.Client
ProxyURL *url.URL
}

func NewHTTP(options ...func(*HTTPImpl)) (*HTTPImpl, error) {
resp := &HTTPImpl{}

for _, option := range options {
option(resp)
}

var transport http.RoundTripper
if resp.Proxy != "" {
proxyURL, err := url.Parse(resp.Proxy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should parse it when user enters the proxy and error out. ProxyURL can be used inside the code instead of passing proxy as string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fail at the input site only you mean ?!

if err != nil {
return nil, err
}

resp.ProxyURL = proxyURL
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
} else {
transport = http.DefaultTransport
}

t := rehttp.NewTransport(transport, rehttp.RetryAll(
rehttp.RetryAny(
rehttp.RetryTemporaryErr(),
rehttp.RetryStatuses(400, 404),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about when gateway times out, I think it is 502 or 503 I can't recall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RetryTemporaryErr() covers them I think. I'll check

),
rehttp.RetryMaxRetries(resp.Retry),
),
// rehttp.ExpJitterDelay(time.Second*time.Duration(2), time.Second*time.Duration(60))
rehttp.ConstDelay(time.Second*time.Duration(10)))

resp.client = &http.Client{Transport: t}
return resp, nil
}

// Do function simply calls the underlying client to make the request.
func (c HTTPImpl) Do(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}
15 changes: 11 additions & 4 deletions pkg/pmk/clients/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type Keystone interface {
}

type KeystoneImpl struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of the organization, I would actually move up all of these: Keystone.go, Resmgr.go into their own packages: /pkg/keystone/keystone.go; /pkg/resmgr/resmgr.go;...

fqdn string
fqdn string
client HTTP
}

func NewKeystone(fqdn string) Keystone {
return KeystoneImpl{fqdn}
func NewKeystone(fqdn string, client HTTP) Keystone {
return KeystoneImpl{fqdn: fqdn, client: client}
}

func (k KeystoneImpl) GetAuth(
Expand Down Expand Up @@ -65,7 +66,13 @@ func (k KeystoneImpl) GetAuth(
}
}`, username, decodedPassword, tenant)

resp, err := http.Post(url, "application/json", strings.NewReader(body))
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return auth, nil
}
req.Header.Add("Content-Type", "application/json")

resp, err := k.client.Do(req)
if err != nil {
return auth, err
}
Expand Down
34 changes: 14 additions & 20 deletions pkg/pmk/clients/qbert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"net/http"
"strings"

rhttp "github.com/hashicorp/go-retryablehttp"
"github.com/platform9/pf9ctl/pkg/log"
"github.com/platform9/pf9ctl/pkg/util"
)

// CloudProviderType specifies the infrastructure where the cluster runs
Expand All @@ -24,12 +22,13 @@ type Qbert interface {
GetNodePoolID(projectID, token string) (string, error)
}

func NewQbert(fqdn string) Qbert {
return QbertImpl{fqdn}
func NewQbert(fqdn string, client HTTP) Qbert {
return QbertImpl{fqdn, client}
}

type QbertImpl struct {
fqdn string
fqdn string
client HTTP
}

type ClusterCreateRequest struct {
Expand All @@ -46,6 +45,7 @@ type ClusterCreateRequest struct {
NodePoolUUID string `json:"nodePoolUuid"`
EnableMetalLb bool `json:"enableMetallb"`
Masterless bool `json:"masterless"`
HTTPProxy string `json:"httpProxy,omitempty"`
}

func (c QbertImpl) CreateCluster(
Expand Down Expand Up @@ -75,17 +75,14 @@ func (c QbertImpl) CreateCluster(

url := fmt.Sprintf("%s/qbert/v3/%s/clusters", c.fqdn, projectID)

client := http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(string(byt)))

if err != nil {
fmt.Println(err.Error())
return "", err
}

req.Header.Set("X-Auth-Token", token)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
resp, err := c.client.Do(req)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -122,18 +119,14 @@ func (c QbertImpl) AttachNode(clusterID, nodeID, projectID, token string) error
"%s/qbert/v3/%s/clusters/%s/attach",
c.fqdn, projectID, clusterID)

client := rhttp.Client{}
client.RetryMax = 5
client.CheckRetry = rhttp.CheckRetry(util.RetryPolicyOn404)

req, err := rhttp.NewRequest("POST", attachEndpoint, strings.NewReader(string(byt)))
req, err := http.NewRequest("POST", attachEndpoint, strings.NewReader(string(byt)))
if err != nil {
return fmt.Errorf("Unable to create a request: %w", err)
}
req.Header.Set("X-Auth-Token", token)
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("Unable to POST request through client: %w", err)
}
Expand All @@ -148,24 +141,25 @@ func (c QbertImpl) AttachNode(clusterID, nodeID, projectID, token string) error

func (c QbertImpl) GetNodePoolID(projectID, token string) (string, error) {

qbertAPIEndpoint := fmt.Sprintf("%s/qbert/v3/%s/cloudProviders", c.fqdn, projectID) // Context should return projectID,make changes to keystoneAuth.
client := http.Client{}

qbertAPIEndpoint := fmt.Sprintf("%s/qbert/v3/%s/cloudProviders", c.fqdn, projectID)
req, err := http.NewRequest("GET", qbertAPIEndpoint, nil)

if err != nil {
return "", err
}

req.Header.Set("X-Auth-Token", token) //
req.Header.Set("X-Auth-Token", token)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)

resp, err := c.client.Do(req)
if err != nil {
return "", err
}

if resp.StatusCode != 200 {
return "", fmt.Errorf("Couldn't query the qbert Endpoint: %s", err.Error())
}

var payload []map[string]string

decoder := json.NewDecoder(resp.Body)
Expand Down
19 changes: 7 additions & 12 deletions pkg/pmk/clients/resmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,38 @@ package clients

import (
"fmt"
"net/http"

rhttp "github.com/hashicorp/go-retryablehttp"
"github.com/platform9/pf9ctl/pkg/log"
"github.com/platform9/pf9ctl/pkg/util"
)

type Resmgr interface {
AuthorizeHost(hostID, token string) error
}

type ResmgrImpl struct {
fqdn string
fqdn string
client HTTP
}

func NewResmgr(fqdn string) Resmgr {
return &ResmgrImpl{fqdn}
func NewResmgr(fqdn string, client HTTP) Resmgr {
return &ResmgrImpl{fqdn: fqdn, client: client}
}

// AuthorizeHost registers the host with hostID to the resmgr.
func (c *ResmgrImpl) AuthorizeHost(hostID string, token string) error {
log.Debugf("Authorizing the host: %s with DU: %s", hostID, c.fqdn)

client := rhttp.NewClient()
client.RetryMax = HTTPMaxRetry
client.CheckRetry = rhttp.CheckRetry(util.RetryPolicyOn404)
client.Logger = nil

url := fmt.Sprintf("%s/resmgr/v1/hosts/%s/roles/pf9-kube", c.fqdn, hostID)
req, err := rhttp.NewRequest("PUT", url, nil)
req, err := http.NewRequest("PUT", url, nil)
if err != nil {
return fmt.Errorf("Unable to create a new request: %w", err)
}

req.Header.Set("X-Auth-Token", token)
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("Unable to send request to the client: %w", err)
}
Expand Down
Loading