Skip to content

Commit

Permalink
Merge pull request #17171 from vbotbuildovich/backport-pr-17170-v23.3…
Browse files Browse the repository at this point in the history
….x-98

[v23.3.x] rpk profile double pointer fixes
  • Loading branch information
twmb authored Mar 18, 2024
2 parents a326fdf + 3d53c7b commit f0a24fa
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cloud/auth/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ profile is kept.
}
}

y.MakeAuthCurrent(a)
y.MakeAuthCurrent(&a)
err = y.Write(fs)
out.MaybeDie(err, "unable to write rpk.yaml: %v", err)

Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func CreateFlow(
// * The user's prior profile was NOT the rpk-cloud profile
// * We are switching to the existing rpk-cloud profile and updating the values
fmt.Printf("Switched to existing %q profile and updated it to talk to cluster %q.\n", RpkCloudProfileName, o.FullName())
priorAuth, currentAuth := yAct.MoveProfileToFront(p)
priorAuth, currentAuth := yAct.MoveProfileToFront(&p)
config.MaybePrintAuthSwitchMessage(priorAuth, currentAuth)

case fromCloud != "" && name == RpkCloudProfileName:
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newRenameToCommand(fs afero.Fs, p *config.Params) *cobra.Command {
}
p.Name = to
y.CurrentProfile = to
priorAuth, currentAuth := y.MoveProfileToFront(p)
priorAuth, currentAuth := y.MoveProfileToFront(&p)
err = y.Write(fs)
out.MaybeDieErr(err)
fmt.Printf("Renamed current profile to %q.\n", to)
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/profile/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newUseCommand(fs afero.Fs, p *config.Params) *cobra.Command {
if p == nil {
out.Die("profile %q does not exist", name)
}
priorAuth, currentAuth := y.MoveProfileToFront(p)
priorAuth, currentAuth := y.MoveProfileToFront(&p)

err = y.Write(fs)
out.MaybeDieErr(err)
Expand Down
1 change: 1 addition & 0 deletions src/go/rpk/pkg/config/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ func (p *Params) readRpkConfig(fs afero.Fs, c *Config) error {
return fmt.Errorf("%s is using a newer rpk.yaml format than we understand, please upgrade rpk", def)
}
yaml.Unmarshal(file, &c.rpkYamlActual)
c.rpkYamlActual.Version = c.rpkYaml.Version

if p.Profile != "" {
if c.rpkYaml.Profile(p.Profile) == nil {
Expand Down
27 changes: 15 additions & 12 deletions src/go/rpk/pkg/config/rpk_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,23 @@ func (y *RpkYaml) PushProfile(p RpkProfile) (priorAuth, currentAuth *RpkCloudAut
}

// MoveProfileToFront moves the given profile to the front of the list.
func (y *RpkYaml) MoveProfileToFront(p *RpkProfile) (priorAuth, currentAuth *RpkCloudAuth) {
func (y *RpkYaml) MoveProfileToFront(p **RpkProfile) (priorAuth, currentAuth *RpkCloudAuth) {
priorAuth = y.CurrentAuth()
reordered := []RpkProfile{*p}
reordered := []RpkProfile{**p}
for i := range y.Profiles {
if &y.Profiles[i] == p {
if &y.Profiles[i] == *p {
continue
}
reordered = append(reordered, y.Profiles[i])
}
y.Profiles = reordered
y.CurrentProfile = p.Name
*p = &reordered[0]
y.CurrentProfile = (*p).Name

// If this is a cloud profile, we switch the auth as well.
if p.FromCloud {
y.CurrentCloudAuthOrgID = p.CloudCluster.AuthOrgID
y.CurrentCloudAuthKind = p.CloudCluster.AuthKind
if (*p).FromCloud {
y.CurrentCloudAuthOrgID = (*p).CloudCluster.AuthOrgID
y.CurrentCloudAuthKind = (*p).CloudCluster.AuthKind
}
currentAuth = y.CurrentAuth()
return priorAuth, currentAuth
Expand All @@ -241,11 +242,12 @@ func (y *RpkYaml) PushNewAuth(a RpkCloudAuth) {
// MakeAuthCurrent finds the given auth, moves it to the front, and updates
// the current cloud auth fields. This pointer must exist, if it does not,
// this function panics.
func (y *RpkYaml) MakeAuthCurrent(a *RpkCloudAuth) {
reordered := []RpkCloudAuth{*a}
// This updates *a to point to the new address of the auth.
func (y *RpkYaml) MakeAuthCurrent(a **RpkCloudAuth) {
reordered := []RpkCloudAuth{**a}
var found bool
for i := range y.CloudAuths {
if &y.CloudAuths[i] == a {
if &y.CloudAuths[i] == *a {
found = true
continue
}
Expand All @@ -254,9 +256,10 @@ func (y *RpkYaml) MakeAuthCurrent(a *RpkCloudAuth) {
if !found {
panic("MakeAuthCurrent called with an auth that does not exist")
}
*a = &reordered[0]
y.CloudAuths = reordered
y.CurrentCloudAuthOrgID = a.OrgID
y.CurrentCloudAuthKind = a.Kind
y.CurrentCloudAuthOrgID = (*a).OrgID
y.CurrentCloudAuthKind = (*a).Kind
}

// DropAuth removes the given auth from the list of auths. If this was the
Expand Down
6 changes: 3 additions & 3 deletions src/go/rpk/pkg/oauth/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func LoadFlow(ctx context.Context, fs afero.Fs, cfg *config.Config, cl Client, n
// This is case 1 or 2, the logic is identical. We start with
// some case 2 logic: we know the auth actually exists, so we
// enforce it is the current rpk.yaml auth.
yAct.MakeAuthCurrent(pAuthAct)
yAct.MakeAuthCurrent(&pAuthAct)

// Case 1 and 2: we check some invariants and then ensure the
// virtual rpk.yaml also has the same current auth.
Expand All @@ -185,7 +185,7 @@ func LoadFlow(ctx context.Context, fs afero.Fs, cfg *config.Config, cl Client, n
if !pAuthVir.Equals(pAuthAct) {
panic("params invariant: internal virtual auth name/org != actual name/org")
}
yVir.MakeAuthCurrent(pAuthVir)
yVir.MakeAuthCurrent(&pAuthVir)

// Finally, set authAct and authVir so they can be updated below.
authAct = pAuthAct
Expand Down Expand Up @@ -272,7 +272,7 @@ func LoadFlow(ctx context.Context, fs afero.Fs, cfg *config.Config, cl Client, n
a := &y.CloudAuths[i]
if a.OrgID == org.ID && a.Kind == authKind {
*newAuth = a
y.MakeAuthCurrent(*newAuth)
y.MakeAuthCurrent(newAuth)
return
}
}
Expand Down

0 comments on commit f0a24fa

Please sign in to comment.