mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Merge pull request #3854 from natasha41575/updateOpenApiFetch
update openapi fetch command
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -28,31 +27,17 @@ in the user's kubeconfig`,
|
||||
}
|
||||
|
||||
func printSchema(w io.Writer) {
|
||||
fmt.Fprintln(w, "Fetching schema from cluster")
|
||||
errMsg := `
|
||||
Error fetching schema from cluster.
|
||||
Please make sure port 8081 is available, kubectl is installed, and its context is set correctly.
|
||||
Please make sure kubectl is installed and its context is set correctly.
|
||||
Installation and setup instructions: https://kubernetes.io/docs/tasks/tools/install-kubectl/`
|
||||
|
||||
command := exec.Command("kubectl", []string{"proxy", "--port=8081", "&"}...)
|
||||
command := exec.Command("kubectl", []string{"get", "--raw", "/openapi/v2"}...)
|
||||
var stderr bytes.Buffer
|
||||
command.Stderr = &stderr
|
||||
err := command.Start()
|
||||
defer killProcess(command)
|
||||
|
||||
// give the proxy a second to start up
|
||||
time.Sleep(time.Second)
|
||||
|
||||
if err != nil || stderr.String() != "" {
|
||||
fmt.Fprintln(w, err, stderr.String()+errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
commandCurl := exec.Command("curl", []string{"http://localhost:8081/openapi/v2"}...)
|
||||
var stdout bytes.Buffer
|
||||
commandCurl.Stdout = &stdout
|
||||
commandCurl.Stderr = &stderr
|
||||
err = commandCurl.Run()
|
||||
command.Stdout = &stdout
|
||||
command.Stderr = &stderr
|
||||
err := command.Run()
|
||||
if err != nil || stdout.String() == "" {
|
||||
fmt.Fprintln(w, err, stderr.String()+errMsg)
|
||||
return
|
||||
@@ -65,9 +50,3 @@ Installation and setup instructions: https://kubernetes.io/docs/tasks/tools/inst
|
||||
output, _ = json.MarshalIndent(jsonSchema, "", " ")
|
||||
fmt.Fprintln(w, string(output))
|
||||
}
|
||||
|
||||
func killProcess(command *exec.Cmd) {
|
||||
if command.Process != nil {
|
||||
command.Process.Kill()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user