Skip to content

Commit

Permalink
Merge pull request #533 from sttts/sttts-attach-space-check
Browse files Browse the repository at this point in the history
attach: check for upbound-system/mxp-config ConfigMap and check account
  • Loading branch information
sttts committed May 22, 2024
2 parents ee9838d + 4dd12ff commit 969c3b0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ PLATFORMS ?= linux_amd64 linux_arm64 linux_arm darwin_amd64 darwin_arm64 windows
# to run a target until the include commands succeeded.
-include build/makelib/common.mk

# Connect agent version
UP_CONNECT_AGENT_VERSION = 0.0.0-408.g343d295
# Connect agent version (overrides the version referenced in internal/version).
UP_CONNECT_AGENT_VERSION ?=

# Release target version
RELEASE_TARGET ?= debug
Expand All @@ -36,7 +36,9 @@ GO_TEST_PARALLEL := $(shell echo $$(( $(NPROCS) / 2 )))

GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/up $(GO_PROJECT)/cmd/docker-credential-up
GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.version=$(VERSION)
ifneq ($(UP_CONNECT_AGENT_VERSION),)
GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.agentVersion=$(UP_CONNECT_AGENT_VERSION)
endif
GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.target=$(RELEASE_TARGET)
GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.gitCommit=$(shell git rev-parse --short HEAD 2> /dev/null || true)
GO_SUBDIRS += cmd internal
Expand Down
22 changes: 17 additions & 5 deletions cmd/up/space/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"strings"

"github.com/alecthomas/kong"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/google/uuid"
"github.com/pterm/pterm"
"helm.sh/helm/v3/pkg/storage/driver"
Expand All @@ -35,6 +34,8 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crossplane/crossplane-runtime/pkg/errors"

upboundv1alpha1 "github.com/upbound/up-sdk-go/apis/upbound/v1alpha1"
sdkerrs "github.com/upbound/up-sdk-go/errors"
"github.com/upbound/up-sdk-go/service/accounts"
Expand All @@ -49,10 +50,11 @@ import (
)

const (
agentChart = "agent"
agentNs = "upbound-system"
agentSecret = "space-token"
connConfMap = "space-connect"
agentChart = "agent"
agentNs = "upbound-system"
agentSecret = "space-token"
connConfMap = "space-connect"
mxpConfigMap = "mxp-config"

keySpace = "space"
keyToken = "token"
Expand Down Expand Up @@ -142,6 +144,16 @@ func (c *attachCmd) AfterApply(kongCtx *kong.Context) error {

// Run executes the install command.
func (c *attachCmd) Run(ctx context.Context, mgr *helm.Installer, kClient *kubernetes.Clientset, upCtx *upbound.Context, ac *accounts.Client, oc *organizations.Client, tc *tokens.Client, rc *robots.Client, rest *rest.Config) (rErr error) { //nolint:gocyclo
mxpConfig, err := kClient.CoreV1().ConfigMaps(agentNs).Get(ctx, mxpConfigMap, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
return errors.New("failed to detect Space. Please run `up space init` first.")
} else if err != nil {
return errors.Wrapf(err, `failed to get ConfigMap "%s/%s"`, agentNs, mxpConfigMap)
}
if spaceAcc := mxpConfig.Data["account"]; spaceAcc != c.Upbound.Account {
return errors.Errorf("account of the Space %q and account of the profile %q mismatch. Use `--account=%s` to attach to the right organization.", spaceAcc, c.Upbound.Account, spaceAcc)
}

attachSpinner, err := upterm.CheckmarkSuccessSpinner.Start("Connecting Space to Upbound Console...")
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/Masterminds/semver/v3"

"github.com/crossplane/crossplane-runtime/pkg/logging"
)

Expand All @@ -49,7 +50,7 @@ const (

var (
version string
agentVersion string
agentVersion string = "0.0.0-408.g343d295"
gitCommit string = "unknown-commit"
target string = string(ReleaseTargetDebug)
)
Expand Down

0 comments on commit 969c3b0

Please sign in to comment.