This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from storageos/add-promote-command
Add promote command
- Loading branch information
Showing
8 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package promote | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"code.storageos.net/storageos/c2-cli/cmd/argwrappers" | ||
"code.storageos.net/storageos/c2-cli/cmd/interfaces" | ||
"code.storageos.net/storageos/c2-cli/cmd/runwrappers" | ||
"code.storageos.net/storageos/c2-cli/pkg/health" | ||
"code.storageos.net/storageos/c2-cli/pkg/id" | ||
) | ||
|
||
type promoteCommand struct { | ||
config interfaces.ConfigProvider | ||
client interfaces.Client | ||
|
||
cordonTargetNode bool | ||
} | ||
|
||
// NewCommand configures the promote command | ||
func NewCommand(client interfaces.Client, config interfaces.ConfigProvider) *cobra.Command { | ||
c := &promoteCommand{ | ||
config: config, | ||
client: client, | ||
} | ||
|
||
command := &cobra.Command{ | ||
Hidden: true, | ||
Use: "promote", | ||
Short: "promotes a volume's replica deployment to a primary", | ||
Example: ` | ||
$ storageos promote my-namespace my-volume my-replica-id`, | ||
Args: argwrappers.WrapInvalidArgsError(func(_ *cobra.Command, args []string) error { | ||
if len(args) < 3 { | ||
return errors.New("received too few args") | ||
} | ||
if len(args) > 3 { | ||
return fmt.Errorf("received more args than expected: %v", args) | ||
} | ||
return nil | ||
}), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
run := runwrappers.Chain( | ||
runwrappers.RunWithTimeout(config), | ||
runwrappers.EnsureNamespaceSetWhenUseIDs(config), | ||
runwrappers.AuthenticateClient(config, client), | ||
)(c.runWithCtx) | ||
|
||
return run(context.Background(), cmd, args) | ||
}, | ||
SilenceUsage: true, | ||
} | ||
|
||
return command | ||
} | ||
|
||
func (c *promoteCommand) runWithCtx(ctx context.Context, cmd *cobra.Command, args []string) error { | ||
useIDs, err := c.config.UseIDs() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nsID := id.Namespace(args[0]) | ||
vID := id.Volume(args[1]) | ||
dID := id.Deployment(args[2]) | ||
|
||
// A deployment has no name, so an ID will always be provided | ||
if !useIDs { | ||
nn, err := c.client.GetNamespaceByName(ctx, string(nsID)) | ||
if err != nil { | ||
return err | ||
} | ||
nsID = nn.ID | ||
|
||
v, err := c.client.GetVolumeByName(ctx, nsID, string(vID)) | ||
if err != nil { | ||
return err | ||
} | ||
vID = v.ID | ||
} | ||
|
||
vol, err := c.client.GetVolume(ctx, nsID, vID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Do some basic sanity checking to ensure we're not about to destroy a volume | ||
// Controlplane does the same sanity checking, but we might as well be safe | ||
if vol.Master.Health != health.MasterOnline { | ||
return fmt.Errorf("cannot promote deployment - master unhealthy: %v", vol.Master.Health.String()) | ||
} | ||
for _, replica := range vol.Replicas { | ||
if replica.Health != health.ReplicaReady || replica.Promotable == false { | ||
return fmt.Errorf("cannot promote deployment - replica unhealthy, id: %v health: %v, promotable: %v", replica.ID, replica.Health, replica.Promotable) | ||
} | ||
} | ||
|
||
err = c.client.AttemptPromotion(ctx, nsID.String(), vID.String(), dID.String()) | ||
if err != nil { | ||
return fmt.Errorf("failed promotion attempt: %w", err) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
time.Sleep(3 * time.Second) | ||
vol, err := c.client.GetVolume(ctx, nsID, vID) | ||
if err != nil { | ||
fmt.Printf("failed getting vol: %v\n", err) | ||
continue | ||
} | ||
|
||
if vol.Master.ID == dID { | ||
fmt.Println("replica promotion successful") | ||
break | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters