Skip to content

Commit

Permalink
We should refactor the agent cli output (#311)
Browse files Browse the repository at this point in the history
* We should refactor the agent cli output

Signed-off-by: chen hui <[email protected]>
  • Loading branch information
huchen2021 authored Jan 14, 2022
1 parent 26b4bc2 commit a01750c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
47 changes: 47 additions & 0 deletions agent/help_flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"os/exec"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)

var _ = Describe("Help flag for host agent", func() {
Context("When the help flag is provided", func() {
var (
expectedOptions = []string{
"-downloadpath string",
"-kubeconfig string",
"-label value",
"-metricsbindaddress string",
"-namespace string",
"-skip-installation",
}
)

It("should output the expected option", func() {
command := exec.Command(pathToHostAgentBinary, "--help")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, "5s").Should(gexec.Exit(0))

output := string(session.Err.Contents())
for _, line := range strings.Split(strings.TrimRight(output, "\n"), "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "-") {
continue
}
// Any option not belongs to expectedOptions is not allowed.
Expect(line).To(BeElementOf(expectedOptions))
}

})

})
})
31 changes: 21 additions & 10 deletions agent/installer/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import (
)

var (
listSupportedFlag = flag.Bool("list-supported", false, "List all supported OS, Kubernetes versions and BYOH Bundle names")
detectOSFlag = flag.Bool("detect", false, "Detects the current operating system")
installFlag = flag.Bool("install", false, "Install a BYOH Bundle")
uninstallFlag = flag.Bool("uninstall", false, "Unnstall a BYOH Bundle")
bundleRepoFlag = flag.String("bundle-repo", "projects.registry.vmware.com", "BYOH Bundle Repository")
cachePathFlag = flag.String("cache-path", ".", "Path to the local bundle cache")
k8sFlag = flag.String("k8s", "1.22.1", "Kubernetes version")
osFlag = flag.String("os", "", "OS. If used with install/uninstall, override os detection")
tagFlag = flag.String("tag", "", "BYOH Bundle tag")
previewOSChangesFlag = flag.Bool("preview-os-changes", false, "Preview the install and uninstall changes for the specified OS")
listSupportedFlag *bool
detectOSFlag *bool
installFlag *bool
uninstallFlag *bool
bundleRepoFlag *string
cachePathFlag *string
k8sFlag *string
osFlag *string
tagFlag *string
previewOSChangesFlag *bool
)

const (
Expand All @@ -38,6 +38,17 @@ var (
func Main() {
klogger = klogr.New()

listSupportedFlag = flag.Bool("list-supported", false, "List all supported OS, Kubernetes versions and BYOH Bundle names")
detectOSFlag = flag.Bool("detect", false, "Detects the current operating system")
installFlag = flag.Bool("install", false, "Install a BYOH Bundle")
uninstallFlag = flag.Bool("uninstall", false, "Unnstall a BYOH Bundle")
bundleRepoFlag = flag.String("bundle-repo", "projects.registry.vmware.com", "BYOH Bundle Repository")
cachePathFlag = flag.String("cache-path", ".", "Path to the local bundle cache")
k8sFlag = flag.String("k8s", "1.22.1", "Kubernetes version")
osFlag = flag.String("os", "", "OS. If used with install/uninstall, override os detection")
tagFlag = flag.String("tag", "", "BYOH Bundle tag")
previewOSChangesFlag = flag.Bool("preview-os-changes", false, "Preview the install and uninstall changes for the specified OS")

flag.Parse()

if *listSupportedFlag {
Expand Down
2 changes: 0 additions & 2 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
"k8s.io/klog/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -88,7 +87,6 @@ func main() {
flag.StringVar(&metricsbindaddress, "metricsbindaddress", ":8080", "metricsbindaddress is the TCP address that the controller should bind to for serving prometheus metrics.It can be set to \"0\" to disable the metrics serving")
flag.StringVar(&downloadpath, "downloadpath", "/var/lib/byoh/bundles", "File System path to keep the downloads")
flag.BoolVar(&skipInstallation, "skip-installation", false, "If you want to skip installation of the kubernetes component binaries")
klog.InitFlags(nil)
flag.Parse()
scheme = runtime.NewScheme()
_ = infrastructurev1beta1.AddToScheme(scheme)
Expand Down

0 comments on commit a01750c

Please sign in to comment.