Skip to content

Commit

Permalink
Change the installer cmd to cobra (#1523)
Browse files Browse the repository at this point in the history
* Change the installer cmd to cobra

* f

* f

* f

* f
  • Loading branch information
marccampbell authored Nov 20, 2024
1 parent e94caf8 commit 2c6adc4
Show file tree
Hide file tree
Showing 42 changed files with 4,116 additions and 4,142 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ embedded-cluster:
-tags osusergo,netgo \
-ldflags="-s -w $(LD_FLAGS) -extldflags=-static" \
-o ./build/embedded-cluster-$(OS)-$(ARCH) \
./cmd/embedded-cluster
./cmd/installer

.PHONY: unit-tests
unit-tests:
Expand Down Expand Up @@ -354,3 +354,8 @@ delete-node%:
.PHONY: test-lam-e2e
test-lam-e2e: pkg/goods/bins/local-artifact-mirror
sudo go test ./cmd/local-artifact-mirror/e2e/... -v

.PHONY: bin/installer
bin/installer:
@mkdir -p bin
go build -o bin/installer ./cmd/installer
22 changes: 22 additions & 0 deletions cmd/installer/cli/adminconsole.go
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
}
76 changes: 76 additions & 0 deletions cmd/installer/cli/adminconsole_resetpassword.go
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.
44 changes: 44 additions & 0 deletions cmd/installer/cli/cidr.go
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
}
Loading

0 comments on commit 2c6adc4

Please sign in to comment.