Skip to content

Commit

Permalink
Add external rancher mgiration (#9)
Browse files Browse the repository at this point in the history
* Add the ability to migrate objects between rancher environments

* Add user migrate and status

* fix

* fix setting description for active directory users

Signed-off-by: galal-hussein <[email protected]>

* fixes

Signed-off-by: galal-hussein <[email protected]>

---------

Signed-off-by: galal-hussein <[email protected]>
  • Loading branch information
galal-hussein authored Dec 10, 2024
1 parent 792928f commit 44feecd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cli/cmds/interactive/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package interactive

import (
"context"
"errors"
"fmt"
"galal-hussein/cattle-drive/cli/cmds"
"galal-hussein/cattle-drive/pkg/client"
Expand Down Expand Up @@ -85,7 +86,7 @@ func migrate(clx *cli.Context) error {
cmds.Spinner.Start()

if source == "" || target == "" {
return fmt.Errorf("source or target is not specified")
return errors.New("source or target is not specified")
}

var clusters v3.ClusterList
Expand Down Expand Up @@ -116,7 +117,7 @@ func migrate(clx *cli.Context) error {
}
}
if sourceCluster == nil || targetCluster == nil {
return fmt.Errorf("failed to find source or target cluster")
return errors.New("failed to find source or target cluster")
}
// initiate client for the cluster
scConfig := *restConfig
Expand Down
5 changes: 3 additions & 2 deletions cli/cmds/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrate

import (
"context"
"errors"
"fmt"
"galal-hussein/cattle-drive/cli/cmds"
"galal-hussein/cattle-drive/pkg/client"
Expand All @@ -26,7 +27,7 @@ var (
Destination: &source,
Aliases: []string{"s"},
},
&cli.StringFlag{
&cli.StringFlag{https://github.com/rancherlabs/cattle-drive/pull/9/conflict?name=cli%252Fcmds%252Fmigrate%252Fmigrate.go&ancestor_oid=38dfd762b78fafcff245ce52f24be4266a5b7830&base_oid=505ab5277a8713ea812d2ab04429496d3b10d096&head_oid=691928f200247163bfa64d6991e3d7dbf93445bf
Name: "target",

Check failure on line 31 in cli/cmds/migrate/migrate.go

View workflow job for this annotation

GitHub Actions / release-tag

syntax error: unexpected : in composite literal; possibly missing comma or }
Usage: "name of the target cluster",
Destination: &target,
Expand Down Expand Up @@ -77,7 +78,7 @@ func migrate(clx *cli.Context) error {
cmds.Spinner.Start()

if source == "" || target == "" {
return fmt.Errorf("source or target is not specified")
return errors.New("source or target is not specified")
}

var clusters v3.ClusterList
Expand Down
5 changes: 3 additions & 2 deletions cli/cmds/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package status

import (
"context"
"errors"
"fmt"
"galal-hussein/cattle-drive/cli/cmds"
"galal-hussein/cattle-drive/pkg/client"
Expand Down Expand Up @@ -75,7 +76,7 @@ func status(clx *cli.Context) error {
cmds.Spinner.Start()

if source == "" || target == "" {
return fmt.Errorf("source or target is not specified")
return errors.New("source or target is not specified")
}

var clusters v3.ClusterList
Expand Down Expand Up @@ -107,7 +108,7 @@ func status(clx *cli.Context) error {
}
}
if sourceCluster == nil || targetCluster == nil {
return fmt.Errorf("failed to find source or target cluster")
return errors.New("failed to find source or target cluster")
}
// initiate client for the cluster
scConfig := *restConfig
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (c *Cluster) Migrate(ctx context.Context, client *client.Clients, tc *Clust
var user v3.User
if err := client.Users.Get(ctx, "", userID, &user, v1.GetOptions{}); err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("user [%s] does not exists, please migrate user first", userID)
return errors.New("user " + userID + " does not exists, please migrate user first")
}
}

Expand Down Expand Up @@ -374,7 +374,7 @@ func (c *Cluster) Migrate(ctx context.Context, client *client.Clients, tc *Clust
var user v3.User
if err := client.Users.Get(ctx, "", userID, &user, v1.GetOptions{}); err != nil {
if apierrors.IsNotFound(err) {
return fmt.Errorf("user [%s] does not exists, please migrate user first", userID)
return errors.New("user " + userID + " does not exists, please migrate user first")
}
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/cluster/clusterroletemplatebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ func (c *ClusterRoleTemplateBinding) Mutate(tc *Cluster) {
}
}
func (c *ClusterRoleTemplateBinding) SetDescription(ctx context.Context, client *client.Clients) error {
// check if this is a group
if c.Obj.UserName == "" && c.Obj.UserPrincipalName == "" {
groupName := c.Obj.GroupName
if groupName == "" {
// handling external auth providers
groupName = c.Obj.GroupPrincipalName
}
c.Description = fmt.Sprintf("%s permission for group %s", c.Obj.RoleTemplateName, groupName)
return nil
}
// setting description for external users
if c.Obj.UserPrincipalName != "" {
c.Description = fmt.Sprintf("%s permission for user %s", c.Obj.RoleTemplateName, c.Obj.UserPrincipalName)
return nil
}
var user v3.User
userID := c.Obj.UserName
if err := client.Users.Get(ctx, "", userID, &user, v1.GetOptions{}); err != nil {
Expand Down

0 comments on commit 44feecd

Please sign in to comment.