Skip to content

Commit

Permalink
Add management/service/hive & namespace login flags
Browse files Browse the repository at this point in the history
* Adds `--manager` and `--service` flags to allow logging into MC/Hive and SC clusters for a provided `--cluster-id`.
* Adds related and other helpful ENV vars inside the container
* Adds `--namespace` flag to override the default NS on whatever cluster is being logged into
* Removes login logic from ~/.bashrc.d/* files inside the container, moving the responsibility of login to ocm-container's binary

WIP - TODO:

* Add a log message immediately before spawning login execs so users will have positive feedback on why they're waiting.  This used to be incidental as a result of the different bash scripts, making it feel like login wasn't just hanging, but even though it's the same length if ocm-container executes the login, because there's no feedback it feels like ocm-container has hung.
* Figure out how to perform the login to ocm and backplane in order, but async from the container attachment, to speed up the login.
* Do the plumbing to pick the right cluster if `--manager`, `--service` or `--namespace` are provided.

Signed-off-by: Chris Collins <[email protected]>
  • Loading branch information
clcollins committed May 16, 2024
1 parent d194063 commit c9160a4
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 172 deletions.
16 changes: 11 additions & 5 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,24 @@ var standardFlags = []cliFlag{
helpMsg: "Optional cluster ID to log into on launch",
},
{
name: "management-cluster",
name: "manager",
flagType: "bool",
value: "false",
shorthand: "M",
helpMsg: "Log in to the (HCP) management cluster of provided cluster (requires --cluster-id; mutually-exclusive with --service-cluster)",
helpMsg: "Log in to the (HCP) management cluster of provided cluster (requires --cluster-id; mutually-exclusive with --service)",
},
{
name: "service-cluster",
name: "service",
flagType: "bool",
value: "false",
shorthand: "S",
helpMsg: "Log in to the (HCP) service cluster of provided cluster (requires --cluster-id; mutually-exclusive with --management-cluster)",
helpMsg: "Log in to the (HCP) service cluster of provided cluster (requires --cluster-id; mutually-exclusive with --manager)",
},
{
name: "namespace",
flagType: "string",
helpMsg: "Override the default namespace when logging into a cluster (requires --cluster-id)",
hidden: true, // Hidden while testing
},
{
name: "engine",
Expand Down Expand Up @@ -204,7 +210,7 @@ var standardFlags = []cliFlag{

// mutuallyExclusiveFlags is a slice containing string slices of mutually exclusive flags
var mutuallyExclusiveFlags = [][]string{
{"management-cluster", "service-cluster"},
{"manager", "service"},
}

// disableFeatureFlags is a list of feature flags can be used to disable features of the container,
Expand Down
11 changes: 7 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ and other Red Hat SRE tools`,
// Run: func(cmd *cobra.Command, args []string) { },
Args: cobra.ArbitraryArgs,
PreRun: func(cmd *cobra.Command, args []string) {
// If either of --management-cluster or --service-cluster is set, --cluster-id is required
mc, _ := cmd.Flags().GetBool("management-cluster")
sc, _ := cmd.Flags().GetBool("service-cluster")
// If either of --manager or --service is set, --cluster-id is required
mc, _ := cmd.Flags().GetBool("manager")
sc, _ := cmd.Flags().GetBool("service")

if mc || sc {
// If --persistent-histories is set, --cluster-id is required
ph, _ := cmd.Flags().GetBool("persistent-histories")

if mc || sc || ph {
cmd.MarkFlagRequired("cluster-id")
}
},
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.1
require (
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/huh v0.3.1-0.20240306161957-71f31c155b08
github.com/kr/pretty v0.3.1
github.com/openshift-online/ocm-sdk-go v0.1.405
github.com/openshift/osdctl v0.28.0
github.com/sirupsen/logrus v1.9.3
Expand Down Expand Up @@ -59,6 +60,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down Expand Up @@ -89,6 +91,7 @@ require (
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -252,6 +253,7 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
14 changes: 1 addition & 13 deletions pkg/featureSet/persistentHistories/persistentHistories.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/openshift/ocm-container/pkg/deprecation"
"github.com/openshift/ocm-container/pkg/engine"
"github.com/openshift/ocm-container/pkg/ocm"
)

const (
Expand All @@ -20,22 +19,11 @@ type Config struct {
Mounts []engine.VolumeMount
}

func New(home, cluster string) (*Config, error) {
func New(home, clusterId string) (*Config, error) {
var err error

config := &Config{}

ocmClient, err := ocm.NewClient()
if err != nil {
return config, err
}
defer ocmClient.Close()

clusterId, err := ocm.GetClusterId(ocmClient, cluster)
if err != nil {
return config, err
}

mount := filepath.Join(home, sourceSubDir, clusterId)
err = os.MkdirAll(mount, os.ModePerm)
if err != nil {
Expand Down
23 changes: 19 additions & 4 deletions pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/viper"
)

const (
Expand All @@ -17,6 +18,13 @@ const (
productionGovURL = "https://api.openshiftusgov.com"
)

var HiveNSMap = map[string]string{
productionURL: "%s-production-%s",
stagingURL: "%s-stage-%s",
integrationURL: "%s-integration-%s",
productionGovURL: "%s-production-%s",
}

// supprotedUrls is a shortened list of the urlAliases, for the help message
// We actually support all the urlAliases, but that's too many for the help
var (
Expand Down Expand Up @@ -51,7 +59,8 @@ type Error string
func (e Error) Error() string { return string(e) }

const (
errInvalidOcmUrl = Error("the specified ocm-url is invalid: %s")
errInvalidOcmUrl = Error("the specified ocm-url is invalid: %s")
errInvalidOcmToken = Error("the specified OFFLINE_ACCESS_TOKEN is missing or invalid")
)

type Config struct {
Expand All @@ -68,6 +77,12 @@ func New(ocmUrl string) (*Config, error) {
if c.Env["OCM_URL"] == "" {
return c, errInvalidOcmUrl
}

c.Env["OFFLINE_ACCESS_TOKEN"] = viper.GetString("OFFLINE_ACCESS_TOKEN")
if c.Env["OFFLINE_ACCESS_TOKEN"] == "" {
return c, errInvalidOcmToken
}

return c, nil
}

Expand All @@ -77,13 +92,13 @@ func url(s string) string {
return urlAliases[s]
}

func NewClient() (*sdk.Connection, error) {
ocmClient, err := utils.CreateConnection()
func NewClient(token, url string) (*sdk.Connection, error) {
conn, err := sdk.NewConnectionBuilder().Tokens(token).URL(url).Build()
if err != nil {
return nil, err
}

return ocmClient, err
return conn, err
}

// GetCluster takes an *sdk.Connection and a cluster identifier string, and returns a *sdk.Cluster
Expand Down
Loading

0 comments on commit c9160a4

Please sign in to comment.