-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate discovery repo into xline operator
Signed-off-by: Phoeniix Zhao <[email protected]>
- Loading branch information
1 parent
b5d58a5
commit 70fe44f
Showing
10 changed files
with
190 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
_ "net/http/pprof" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/xline-kv/xline-operator/internal/server" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var port int | ||
|
||
func init() { | ||
zap.ReplaceGlobals(zap.Must(zap.NewProduction())) | ||
flag.IntVar(&port, "port", 10086, "The port that the xline discovery's http service runs on (default 10086)") | ||
flag.Parse() | ||
} | ||
|
||
// discovery_url="my-xline-cluster-discovery.default.svc:10086" | ||
// domain="my-xline-cluster-0.my-xline-cluster.default.svc.cluster.local" | ||
// encoded_domain_url=`echo ${domain}:2380 | base64 | tr "\n" " " | sed "s/ //g"` | ||
// wget -qO- -T 3 http://${discovery_url}/new/${encoded_domain_url} | ||
|
||
func main() { | ||
flag.CommandLine.VisitAll(func(flag *flag.Flag) { | ||
zap.S().Info("FLAG: --%s=%q", flag.Name, flag.Value) | ||
}) | ||
|
||
xcName := os.Getenv("XC_NAME") | ||
if len(xcName) < 1 { | ||
zap.L().Fatal("ENV XC_NAME is not set") | ||
} | ||
|
||
go func() { | ||
addr := fmt.Sprintf("0.0.0.0:%d", port) | ||
zap.S().Infof("starting Xline Discovery server, listening on %s", addr) | ||
discoveryServer := server.NewServer() | ||
discoveryServer.ListenAndServe(addr) | ||
}() | ||
|
||
srv := http.Server{Addr: ":6060"} | ||
sc := make(chan os.Signal, 1) | ||
signal.Notify(sc, | ||
syscall.SIGHUP, | ||
syscall.SIGINT, | ||
syscall.SIGTERM, | ||
syscall.SIGQUIT, | ||
) | ||
|
||
go func() { | ||
sig := <-sc | ||
zap.S().Infof("got signal %s to exit", sig) | ||
if err2 := srv.Shutdown(context.Background()); err2 != nil { | ||
zap.S().Fatal("fail to shutdown the HTTP server", err2) | ||
} | ||
}() | ||
|
||
if err := srv.ListenAndServe(); err != http.ErrServerClosed { | ||
zap.S().Fatal(err) | ||
} | ||
zap.S().Infof("xline-discovery exited!!") | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package constants | ||
|
||
const ( | ||
XlinePort = 2379 | ||
DiscoveryPort = 10086 | ||
OperatorNamespace = "xline-operator-system" | ||
OperatorDeployName = "xline-operator-controller-manager" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/emicklei/go-restful" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Discovery interface { | ||
Discover(string) (string, error) | ||
} | ||
|
||
type Server interface { | ||
ListenAndServe(addr string) | ||
} | ||
|
||
type server struct { | ||
discovery Discovery | ||
container *restful.Container | ||
} | ||
|
||
type discovery struct{} | ||
|
||
func NewDiscovery() Discovery { | ||
return &discovery{} | ||
} | ||
|
||
func (d *discovery) Discover(advertisePeerUrl string) (string, error) { | ||
return fmt.Sprintf("%s", advertisePeerUrl), nil | ||
} | ||
|
||
// NewServer creates a new server. | ||
func NewServer() Server { | ||
s := &server{ | ||
discovery: NewDiscovery(), | ||
container: restful.NewContainer(), | ||
} | ||
s.registerHandlers() | ||
return s | ||
} | ||
|
||
func (s *server) registerHandlers() { | ||
ws := new(restful.WebService) | ||
ws.Route(ws.GET("/new/{advertise-peer-url}").To(s.newHandler)) | ||
s.container.Add(ws) | ||
} | ||
|
||
func (s *server) ListenAndServe(addr string) { | ||
zap.S().Fatal(http.ListenAndServe(addr, s.container.ServeMux)) | ||
} | ||
|
||
func (s *server) newHandler(req *restful.Request, resp *restful.Response) { | ||
encodedAdvertisePeerURL := req.PathParameter("advertise-peer-url") | ||
data, err := base64.StdEncoding.DecodeString(encodedAdvertisePeerURL) | ||
if err != nil { | ||
zap.S().Errorf("failed to decode advertise-peer-url: %s, register-type is: %s", encodedAdvertisePeerURL) | ||
if werr := resp.WriteError(http.StatusInternalServerError, err); werr != nil { | ||
zap.S().Errorf("failed to writeError: %v", werr) | ||
} | ||
return | ||
} | ||
advertisePeerURL := string(data) | ||
|
||
var result string | ||
result, err = s.discovery.Discover(advertisePeerURL) | ||
if err != nil { | ||
zap.S().Errorf("failed to discover: %s, %v", advertisePeerURL, err) | ||
if werr := resp.WriteError(http.StatusInternalServerError, err); werr != nil { | ||
zap.S().Errorf("failed to writeError: %v", werr) | ||
} | ||
return | ||
} | ||
|
||
zap.S().Infof("generated args for %s: %s", advertisePeerURL, result) | ||
if _, err := io.WriteString(resp, result); err != nil { | ||
zap.S().Errorf("failed to writeString: %s, %v", result, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters