-
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 57b9591
Showing
10 changed files
with
122 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
Submodule discovery
added at
b8dfc7
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