-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the installer cmd to cobra (#1523)
* Change the installer cmd to cobra * f * f * f * f
- Loading branch information
1 parent
e94caf8
commit 2c6adc4
Showing
42 changed files
with
4,116 additions
and
4,142 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func AdminConsoleCmd(ctx context.Context, name string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "admin-console", | ||
Short: fmt.Sprintf("Manage the %s Admin Console", name), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.AddCommand(AdminConsoleResetPasswordCmd(ctx, name)) | ||
|
||
return cmd | ||
} |
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,76 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
cmdutil "github.com/replicatedhq/embedded-cluster/pkg/cmd/util" | ||
"github.com/replicatedhq/embedded-cluster/pkg/defaults" | ||
"github.com/replicatedhq/embedded-cluster/pkg/k0s" | ||
"github.com/replicatedhq/embedded-cluster/pkg/kotscli" | ||
"github.com/replicatedhq/embedded-cluster/pkg/kubeutils" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func AdminConsoleResetPasswordCmd(ctx context.Context, name string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "reset-password", | ||
Short: fmt.Sprintf("Reset the %s Admin Console password", name), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
if os.Getuid() != 0 { | ||
return fmt.Errorf("reset-password command must be run as root") | ||
} | ||
if len(args) != 1 { | ||
return fmt.Errorf("expected admin console password as argument") | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
provider, err := getProviderFromCluster(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
password := args[0] | ||
if !validateAdminConsolePassword(password, password) { | ||
return ErrNothingElseToAdd | ||
} | ||
|
||
if err := kotscli.ResetPassword(provider, password); err != nil { | ||
return err | ||
} | ||
|
||
logrus.Info("Admin Console password reset successfully") | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
// getProviderFromCluster finds the kubeconfig and discovers the provider from the cluster. If this | ||
// is a prior version of EC, we will have to fall back to the filesystem. | ||
func getProviderFromCluster(ctx context.Context) (*defaults.Provider, error) { | ||
status, err := k0s.GetStatus(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get k0s status: %w", err) | ||
} | ||
kubeconfigPath := status.Vars.AdminKubeConfigPath | ||
|
||
os.Setenv("KUBECONFIG", kubeconfigPath) | ||
|
||
// Discover the provider from the cluster | ||
kcli, err := kubeutils.KubeClient() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create kube client: %w", err) | ||
} | ||
|
||
provider, err := cmdutil.NewProviderFromCluster(ctx, kcli) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get config from cluster: %w", err) | ||
} | ||
return provider, nil | ||
} |
File renamed without changes.
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,44 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/replicatedhq/embedded-cluster/pkg/netutils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// DeterminePodAndServiceCIDRS determines, based on the command line flags, | ||
// what are the pod and service CIDRs to be used for the cluster. If both | ||
// --pod-cidr and --service-cidr have been set, they are used. Otherwise, | ||
// the cidr flag is split into pod and service CIDRs. | ||
func determinePodAndServiceCIDRs(cmd *cobra.Command) (string, string, error) { | ||
podCIDRFlag, err := cmd.Flags().GetString("pod-cidr") | ||
if err != nil { | ||
return "", "", fmt.Errorf("unable to get pod-cidr flag: %w", err) | ||
} | ||
serviceCIDRFlag, err := cmd.Flags().GetString("service-cidr") | ||
if err != nil { | ||
return "", "", fmt.Errorf("unable to get service-cidr flag: %w", err) | ||
} | ||
|
||
if podCIDRFlag != "" || serviceCIDRFlag != "" { | ||
return podCIDRFlag, serviceCIDRFlag, nil | ||
} | ||
|
||
cidrFlag, err := cmd.Flags().GetString("cidr") | ||
if err != nil { | ||
return "", "", fmt.Errorf("unable to get cidr flag: %w", err) | ||
} | ||
|
||
return netutils.SplitNetworkCIDR(cidrFlag) | ||
} | ||
|
||
// cleanCIDR returns a `.0/x` subnet instead of a `.2/x` etc subnet | ||
func cleanCIDR(ipnet *net.IPNet) (string, error) { | ||
_, newNet, err := net.ParseCIDR(ipnet.String()) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse local inet CIDR %q: %w", ipnet.String(), err) | ||
} | ||
return newNet.String(), nil | ||
} |
Oops, something went wrong.