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

GCP CCS command #192

Merged
merged 1 commit into from
Nov 30, 2020
Merged
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
34 changes: 34 additions & 0 deletions cmd/ocm/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ limitations under the License.
package cluster

import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"time"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/openshift-online/ocm-cli/pkg/arguments"
"github.com/openshift-online/ocm-cli/pkg/cluster"
c "github.com/openshift-online/ocm-cli/pkg/cluster"
"github.com/openshift-online/ocm-cli/pkg/ocm"
)
Expand All @@ -42,6 +46,7 @@ var args struct {
private bool
multiAZ bool
ccs c.CCS
gcpServiceAccount arguments.FilePath

// Scaling options
computeMachineType string
Expand Down Expand Up @@ -75,6 +80,12 @@ func init() {

arguments.AddProviderFlag(fs, &args.provider)
arguments.AddCCSFlags(fs, &args.ccs)

fs.Var(
&args.gcpServiceAccount,
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally declaring this flag would be part of arguments.AddCCSFlags().
One way to do that that might be move the FilePath var inside CCS struct, and making constructGCPCredentials a method on CCS struct. Don't know if worth it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Opened #199 for some validations we should improve.

"service-account-file",
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT about renaming this --gcp-service-account-file? 4 more characters to type but maybe worth it to keep the flags organized.

(BTW, maybe it's time to start using Cobra's flag groups to organize the help 🤔)

"GCP service account JSON file.",
)
fs.StringVar(
&args.region,
"region",
Expand Down Expand Up @@ -249,6 +260,13 @@ func run(cmd *cobra.Command, argv []string) error {
computeNodes = 9
}

if args.gcpServiceAccount != "" {
err = constructGCPCredentials(args.gcpServiceAccount, &args.ccs)
if err != nil {
return err
}
}

err = arguments.CheckIgnoredCCSFlags(args.ccs)
if err != nil {
return err
Expand Down Expand Up @@ -313,3 +331,19 @@ func fetchFlavours(client *cmv1.Client) (flavours []*cmv1.Flavour, err error) {
}
return
}

func constructGCPCredentials(filePath arguments.FilePath, value *cluster.CCS) error {
// Open our jsonFile
jsonFile, err := os.Open(filePath.String())
if err != nil {
return err
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &value.GCP)
if err != nil {
return err
}
return nil

}
21 changes: 20 additions & 1 deletion pkg/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,33 @@ import (
"reflect"
"strings"

"github.com/mattn/go-isatty"
isatty "github.com/onsi/ginkgo/reporters/stenographer/support/go-isatty"
Copy link
Contributor

Choose a reason for hiding this comment

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

@igoihman was this deliberate?
https://github.com/onsi/ginkgo/tree/master/reporters/stenographer/support/go-isatty/
looks like an old, less maintained, copy of upstream https://github.com/mattn/go-isatty/.
Or was this simply replaced by IDE being too clever?

Copy link
Contributor

Choose a reason for hiding this comment

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

=> #201 if accidental.

sdk "github.com/openshift-online/ocm-sdk-go"
"github.com/spf13/pflag"

"github.com/openshift-online/ocm-cli/pkg/cluster"
"github.com/openshift-online/ocm-cli/pkg/debug"
)

type FilePath string

func (f *FilePath) String() string {
return string(*f)
}

func (f *FilePath) Set(v string) error {
_, err := os.Stat(v)
if err != nil {
return err
}
*f = FilePath(v)
return nil
}

func (f *FilePath) Type() string {
return "filepath"
}

// AddDebugFlag adds the '--debug' flag to the given set of command line flags.
func AddDebugFlag(fs *pflag.FlagSet) {
debug.AddFlag(fs)
Expand Down
44 changes: 38 additions & 6 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,27 @@ type Spec struct {
type CCS struct {
Enabled bool
AWS AWSCredentials
GCP GCPCredentials
}
type AWSCredentials struct {
AccountID string
AccessKeyID string
SecretAccessKey string
}

type GCPCredentials struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"`
ClientX509CertURL string `json:"client_x509_cert_url"`
}

type AddOnItem struct {
ID string
Name string
Expand Down Expand Up @@ -195,12 +209,30 @@ func CreateCluster(cmv1Client *cmv1.Client, config Spec, dryRun bool) (*cmv1.Clu

if config.CCS.Enabled {
clusterBuilder = clusterBuilder.CCS(cmv1.NewCCS().Enabled(true))
clusterBuilder = clusterBuilder.AWS(
cmv1.NewAWS().
AccountID(config.CCS.AWS.AccountID).
AccessKeyID(config.CCS.AWS.AccessKeyID).
SecretAccessKey(config.CCS.AWS.SecretAccessKey),
)
if config.Provider == "AWS" {
clusterBuilder = clusterBuilder.AWS(
cmv1.NewAWS().
AccountID(config.CCS.AWS.AccountID).
AccessKeyID(config.CCS.AWS.AccessKeyID).
SecretAccessKey(config.CCS.AWS.SecretAccessKey),
)
} else {
clusterBuilder =
clusterBuilder.GCP(
cmv1.NewGCP().
Type(config.CCS.GCP.Type).
ProjectID(config.CCS.GCP.ProjectID).
PrivateKeyID(config.CCS.GCP.PrivateKeyID).
PrivateKey(config.CCS.GCP.PrivateKey).
ClientEmail(config.CCS.GCP.ClientEmail).
ClientID(config.CCS.GCP.ClientID).
AuthURI(config.CCS.GCP.AuthURI).
TokenURI(config.CCS.GCP.TokenURI).
AuthProviderX509CertURL(config.CCS.GCP.AuthProviderX509CertURL).
ClientX509CertURL(config.CCS.GCP.ClientX509CertURL),
)

}
}

clusterSpec, err := clusterBuilder.Build()
Expand Down