Skip to content

Commit

Permalink
Merge branch 'must-gather-browser' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Jooho committed Feb 24, 2021
2 parents f2ea74c + 64a51f6 commit e464121
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 81 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ISV CLI VERSION
CLI_VERSION ?= v0.2-alpha
CLI_VERSION ?= 0.2.0
CLI_PLATFORM ?= linux
CLI_ARCH ?= amd64
CLI_IMG ?= quay.io/jooholee/isv-cli:${CLI_VERSION}

.PHONY: build
build:
build: test
sed "s/cliVersion =.*/cliVersion = \"$(CLI_VERSION)\"/g" -i ./pkg/cli/cli.go
go build ./cmd/isv-cli.go
cp isv-cli ./build/.
Expand All @@ -26,4 +26,4 @@ download:
test -f ./build/isv-cli || mv ./isv-cli_${CLI_PLATFORM}_${CLI_ARCH} ./build/isv-cli

clean:
rm ./isv-cli
rm ./isv-cli ./build/isv-cli
11 changes: 10 additions & 1 deletion Test.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# Test commands
Test image is [here](https://github.com/Jooho/isv-smoke-must-gather). There are 5 different tags to test each objects but the release version must be the same as isv-cli release version.

- Standard test cmd
~~~
go run ./cmd/isv-cli.go must-gather --image quay.io/jooholee/isv-smoke-must-gather:event --dest-dir ~/dev/Managed_Git/operator-projects/tmp/cls
~~~
- Browser feature test
go run ./cmd/isv-cli.go must-gather --image quay.io/jooholee/nfs-provisioner-operator-must-gather:test --browser --context-dir ~/dev/Managed_Git/operator-projects/tmp/c --dest-dir ~/dev/Managed_Git/operator-projects/tmp/c
~~~
go run ./cmd/isv-cli.go must-gather --image quay.io/jooholee/isv-smoke-must-gather:event --browser --dest-dir ~/dev/Managed_Git/operator-projects/tmp/cls
~~~
24 changes: 23 additions & 1 deletion docs/must-gather/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
# CONTRIBUTE

## Build
## Version Change
~~~
vi Makefile
CLI_VERSION ?= v0.2-alpha <== update
~~~
## Build isv-cli
~~~
make build
~~~

## Build and push isv-cli image
~~~
make build
make cli-image
~~~

## Download a specific isv-cli release, build and push isv-cli image
~~~
# Change Version
make clean
make cli-image
~~~
## Test
~~~
make test
~~~

## Clean repo before commit
~~~
make clean
~~~
2 changes: 1 addition & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (

const (
productName = `OpenShift ISV Operator`
cliVersion = "v0.2-alpha"
cliVersion = "0.2.0"
)


Expand Down
119 changes: 44 additions & 75 deletions pkg/cli/mustgather/mustgather.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func NewMustGatherCommand(f kcmdutil.Factory, streams genericclioptions.IOStream
cmd.Flags().Int64Var(&o.Timeout, "timeout", 600, "The length of time to gather data, in seconds. Defaults to 10 minutes.")
cmd.Flags().BoolVar(&o.NoTar, "notar", o.NoTar, "Copy must-gather data without archive")
cmd.Flags().BoolVar(&o.Keep, "keep", o.Keep, "Do not delete temporary resources when command completes.")
cmd.Flags().BoolVar(&o.Browser, "browser", o.Browser, "Start web server to download must-gather file")
cmd.Flags().BoolVar(&o.Browser, "browser", o.Browser, "Start web server to download must-gather file. With this option, the dest-dir option must be set absolute path.")
cmd.Flags().MarkHidden("keep")

cmd.MarkFlagRequired("image")
Expand Down Expand Up @@ -148,23 +148,11 @@ func (o *MustGatherOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, arg
}

func (o *MustGatherOptions) Validate() error {
if strings.ContainsAny(o.DestDir, ".") {
return fmt.Errorf("--dest-dir must use absolute path.")
}
return o.MustGatherOptions.Validate()
}

func (o *MustGatherOptions) Run(f kcmdutil.Factory) error {

// if o.Browser {
// fmt.Println("webser start")

// o.startWebServer()

// // wait for goroutine started in startHttpServer() to stop

// }

currNamespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
Expand All @@ -179,31 +167,44 @@ func (o *MustGatherOptions) Run(f kcmdutil.Factory) error {
return err
}

sa, err := o.Client.CoreV1().ServiceAccounts(currNamespace).Create(context.TODO(), o.newSA(), metav1.CreateOptions{})
if err != nil {
return err
}
roleBinding, err := o.Client.RbacV1().RoleBindings(currNamespace).Create(context.TODO(), o.newRoleBinding(sa.Name), metav1.CreateOptions{})
if err != nil {
return err
saName := "isv-cli-sa"
sa := &corev1.ServiceAccount{}
roleBinding := &rbacv1.RoleBinding{}

if !o.Browser {
sa, err = o.Client.CoreV1().ServiceAccounts(currNamespace).Create(context.TODO(), o.newSA(), metav1.CreateOptions{})
if err != nil {
return err
}

roleBinding, err = o.Client.RbacV1().RoleBindings(currNamespace).Create(context.TODO(), o.newRoleBinding(sa.Name), metav1.CreateOptions{})
if err != nil {
return err
}

saName = sa.Name
}

pod, err := o.Client.CoreV1().Pods(currNamespace).Create(context.TODO(), o.newPod(o.NodeName, image, sa.Name), metav1.CreateOptions{})
pod, err := o.Client.CoreV1().Pods(currNamespace).Create(context.TODO(), o.newPod(o.NodeName, image, saName), metav1.CreateOptions{})
if err != nil {
return err
}
o.log("pod for plug-in image %s created", image)

pods = append(pods, pod)

if !o.Keep {

defer func() {
if err := o.Client.RbacV1().RoleBindings(currNamespace).Delete(context.TODO(), roleBinding.Name, metav1.DeleteOptions{}); err != nil {
fmt.Printf("%v\n", err)
return
}
if err := o.Client.CoreV1().ServiceAccounts(currNamespace).Delete(context.TODO(), sa.Name, metav1.DeleteOptions{}); err != nil {
fmt.Printf("%v\n", err)
return
if !o.Browser {
if err := o.Client.RbacV1().RoleBindings(currNamespace).Delete(context.TODO(), roleBinding.Name, metav1.DeleteOptions{}); err != nil {
fmt.Printf("%v\n", err)
return
}
if err := o.Client.CoreV1().ServiceAccounts(currNamespace).Delete(context.TODO(), saName, metav1.DeleteOptions{}); err != nil {
fmt.Printf("%v\n", err)
return
}
}
if err := o.Client.CoreV1().Pods(currNamespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{}); err != nil {
fmt.Printf("%v\n", err)
Expand All @@ -227,11 +228,6 @@ func (o *MustGatherOptions) Run(f kcmdutil.Factory) error {
var wg sync.WaitGroup
wg.Add(len(pods))

// if o.Browser {
// wg.Add(len(pods) + 1)
// } else {
// wg.Add(len(pods))
// }
errCh := make(chan error, len(pods))

for _, pod := range pods {
Expand Down Expand Up @@ -289,7 +285,7 @@ func (o *MustGatherOptions) Run(f kcmdutil.Factory) error {
}

if o.Browser {
fmt.Println("Teardown must-gather pod")
log("Teardown must-gather pod")
if err := o.Client.CoreV1().Pods(currNamespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{}); err != nil {
fmt.Printf("%v\n", err)
return
Expand Down Expand Up @@ -321,8 +317,8 @@ func (o *MustGatherOptions) Run(f kcmdutil.Factory) error {
osexec.Command("rm", o.DestDir+"/event-filter.html", o.DestDir+"/events.yaml", o.DestDir+"/timestamp"),
}
for _, cmd := range cmds {
// cmd := osexec.Command("tar", "-uvf", o.DestDir+"/must-gather.tar", "-C", o.DestDir, "event-filter.html")
fmt.Printf("%v\n", cmd)

// fmt.Printf("%v\n", cmd)
if err := cmd.Start(); err != nil {
errs = append(errs, err)
}
Expand All @@ -333,11 +329,13 @@ func (o *MustGatherOptions) Run(f kcmdutil.Factory) error {
}
}

fmt.Println("webser start")
fmt.Println("Webserver start")
fmt.Println("Please check your MustGather CR to know the download url using the following command")
fmt.Println("-----------------------------------------------------------------------")
fmt.Println("oc get mustgather -o jsonpath=\"{ .items[*].status.downloadURL}\"")

o.startWebServer()

fmt.Println("Please check your MustGather CR to know the download url")

}
return errors.NewAggregate(errs)
}
Expand Down Expand Up @@ -384,7 +382,7 @@ func (o *MustGatherOptions) execute(options *exec.ExecOptions) error {
return nil
}

// oc mustgather util fuctinos are not exported so copied them from https://github.com/openshift/oc/blob/release-4.7/pkg/cli/admin/mustgather/mustgather.go
// oc mustgather util methods are not exported so some methods are copied them from https://github.com/openshift/oc/blob/release-4.7/pkg/cli/admin/mustgather/mustgather.go

func newPodOutLogger(out io.Writer, podName string) func(string, ...interface{}) {
writer := newPrefixWriter(out, fmt.Sprintf("[%s] OUT", podName))
Expand Down Expand Up @@ -526,7 +524,7 @@ func (o *MustGatherOptions) newPod(node, image, sa string) *corev1.Pod {
if node == "" {
nodeSelector["node-role.kubernetes.io/worker"] = ""
}

fmt.Println(sa)
ret := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "must-gather-",
Expand All @@ -536,8 +534,8 @@ func (o *MustGatherOptions) newPod(node, image, sa string) *corev1.Pod {
},
Spec: corev1.PodSpec{
NodeName: node,
RestartPolicy: corev1.RestartPolicyNever,
ServiceAccountName: sa,
RestartPolicy: corev1.RestartPolicyNever,
Volumes: []corev1.Volume{
{
Name: "must-gather-output",
Expand Down Expand Up @@ -594,6 +592,7 @@ func (o *MustGatherOptions) newPod(node, image, sa string) *corev1.Pod {
},
},
}

if len(o.Command) > 0 {
// always force disk flush to ensure that all data gathered is accessible in the copy container
ret.Spec.Containers[0].Command = []string{"/bin/bash", "-c", fmt.Sprintf("%s; sync", strings.Join(o.Command, " "))}
Expand Down Expand Up @@ -660,48 +659,20 @@ func (o *MustGatherOptions) startWebServer() {
handleDownload(srv, "must-gather.tar", o.DestDir, w, r)
})

// http.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
// srv.Shutdown(context.Background())
// })

// go func() {
// defer wg.Done() // let main know we are done cleaning up

// // always returns error. ErrServerClosed on graceful close
// if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// // unexpected error. port in use?
// fmt.Println("ListenAndServe(): %v", err)
// }

// }()
// always returns error. ErrServerClosed on graceful close
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// unexpected error. port in use?
fmt.Println("ListenAndServe(): %v", err)
o.log("ListenAndServe(): %v", err)
}

}

func handleDownload(srv *http.Server, fileName, dirName string, w http.ResponseWriter, r *http.Request) {
// const receiptPath = "receipts"
// urlPathSegments := strings.Split(r.URL.Path, fmt.Sprintf("%s/", receiptPath))
// if len(urlPathSegments[1:]) > 1 {
// w.WriteHeader(http.StatusBadRequest)
// return
// }
// fileName := urlPathSegments[1:][0]
// fileName := "isv-cli"

// ReceiptDirectory:= "/opt/isv/bin"
var ReceiptDirectory string = filepath.Join(dirName)

// fileName1 := "a.sh"
// ReceiptDirectory:= "/opt/isv/bin"
// var ReceiptDirectory string = filepath.Join("/tmp")

fmt.Println(filepath.Join(ReceiptDirectory, fileName))
var ReceiptDirectory string = filepath.Join(dirName)

file, err := os.Open(filepath.Join(ReceiptDirectory, fileName))

defer file.Close()
if err != nil {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -722,6 +693,4 @@ func handleDownload(srv *http.Server, fileName, dirName string, w http.ResponseW
w.Header().Set("Content-Length", fSize)
file.Seek(0, 0)
io.Copy(w, file)

// srv.Shutdown(context.Background())
}

0 comments on commit e464121

Please sign in to comment.