Skip to content

Commit

Permalink
feat(autok3s): add serve command
Browse files Browse the repository at this point in the history
Signed-off-by: Jason-ZW <zhenyang@rancher.com>
  • Loading branch information
rancher-sy-bot authored and Jason-ZW committed Jan 4, 2021
1 parent 30f2311 commit c553c99
Show file tree
Hide file tree
Showing 86 changed files with 11,498 additions and 1 deletion.
54 changes: 54 additions & 0 deletions cmd/serve.go
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/rancher/apiserver/pkg/server"
"github.com/rancher/apiserver/pkg/store/apiroot"
"github.com/rancher/apiserver/pkg/types"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
serveCmd = &cobra.Command{
Use: "serve",
Short: "Run as daemon and serve HTTP/HTTPS request",
}

bindPort = "8080"
bindAddress = "0.0.0.0"
)

func init() {
serveCmd.Flags().StringVar(&bindPort, "bind-port", bindPort, "HTTP/HTTPS bind port")
serveCmd.Flags().StringVar(&bindAddress, "bind-address", bindAddress, "HTTP/HTTPS bind address")
}

func ServeCommand() *cobra.Command {
serveCmd.Run = func(cmd *cobra.Command, args []string) {
s := server.DefaultAPIServer()

apiroot.Register(s.Schemas, []string{"v1"})

router := mux.NewRouter()
router.Handle("/{prefix}/{type}", s)
router.Handle("/{prefix}/{type}/{id}", s)

router.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
s.Handle(&types.APIRequest{
Request: r,
Response: rw,
Type: "apiRoot",
URLPrefix: "v1",
})
})

logrus.Infof("run as daemon, listening on %s:%s", bindAddress, bindPort)
logrus.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", bindAddress, bindPort), router))
}

return serveCmd
}
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -40,13 +40,15 @@ require (
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/ghodss/yaml v1.0.0
github.com/gogo/googleapis v1.4.0 // indirect
github.com/gorilla/mux v1.7.4
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f // indirect
github.com/morikuni/aec v1.0.0
github.com/olekukonko/tablewriter v0.0.4
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/rakelkar/gonetsh v0.0.0-20190930180311-e5c5ffe4bdf0 // indirect
github.com/rancher/apiserver v0.0.0-20201023000256-1a0a904f9197
github.com/rancher/k3s v1.19.5-0.20201117235738-2532c10faad4
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -663,6 +663,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/quobyte/api v0.1.2/go.mod h1:jL7lIHrmqQ7yh05OJ+eEEdHr0u/kmT1Ff9iHd+4H6VI=
github.com/rakelkar/gonetsh v0.0.0-20190930180311-e5c5ffe4bdf0 h1:iXE9kmlAqhusXxzkXictdNgWS7p4ZBnmv9SdyMgTf6E=
github.com/rakelkar/gonetsh v0.0.0-20190930180311-e5c5ffe4bdf0/go.mod h1:4XHkfaUj+URzGO9sohoAgt2V9Y8nIW7fugpu0E6gShk=
github.com/rancher/apiserver v0.0.0-20201023000256-1a0a904f9197 h1:PeTu2AgMa8uxuRc8gaQSP/p2MFvBY/4h4mkRe7GbQQM=
github.com/rancher/apiserver v0.0.0-20201023000256-1a0a904f9197/go.mod h1:8W0EwaR9dH5NDFw6mpAX437D0q+EZqKWbZyX71+z2WI=
github.com/rancher/dynamiclistener v0.2.1 h1:QiY1jxs2TOLrKB04G36vE2ehEvPMPGiWp8zEHLKB1nE=
github.com/rancher/dynamiclistener v0.2.1/go.mod h1:9WusTANoiRr8cDWCTtf5txieulezHbpv4vhLADPp0zU=
github.com/rancher/helm-controller v0.7.3 h1:WTQHcNF2vl9w6Xd1eBtXDe0JUsYMFFstqX9ghGhI5Ac=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -36,7 +36,7 @@ func main() {
rootCmd := cmd.Command()
rootCmd.AddCommand(cmd.CompletionCommand(), cmd.VersionCommand(gitVersion, gitCommit, gitTreeState, buildDate),
cmd.ListCommand(), cmd.CreateCommand(), cmd.JoinCommand(), cmd.KubectlCommand(), cmd.DeleteCommand(),
cmd.StartCommand(), cmd.StopCommand(), cmd.SSHCommand(), cmd.DescribeCommand())
cmd.StartCommand(), cmd.StopCommand(), cmd.SSHCommand(), cmd.DescribeCommand(), cmd.ServeCommand())

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/gorilla/mux/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/gorilla/mux/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c553c99

Please sign in to comment.