Merge pull request #9144 from yousong/feature/yousong-vpc-md

Feature/yousong vpc md
This commit is contained in:
Zexi Li
2020-12-04 10:23:25 +08:00
committed by GitHub
5 changed files with 86 additions and 38 deletions
+11 -3
View File
@@ -64,6 +64,7 @@ type Application struct {
isExiting bool
idleConnsClosed chan struct{}
httpServer *http.Server
}
const (
@@ -467,16 +468,23 @@ func (app *Application) ListenAndServeTLSWithCleanup2(addr string, certFile, key
app.addDefaultHandlers()
AddPProfHandler(app)
}
s := app.initServer(addr)
app.httpServer = app.initServer(addr)
if isMaster {
app.registerCleanShutdown(s, onStop)
app.registerCleanShutdown(app.httpServer, onStop)
}
app.listenAndServeInternal(s, certFile, keyFile)
app.listenAndServeInternal(app.httpServer, certFile, keyFile)
if isMaster {
app.waitCleanShutdown()
}
}
func (app *Application) Stop(ctx context.Context) error {
if app.httpServer != nil {
return app.httpServer.Shutdown(ctx)
}
return nil
}
func (app *Application) listenAndServeInternal(s *http.Server, certFile, keyFile string) {
var err error
if len(certFile) == 0 && len(keyFile) == 0 {
+11 -2
View File
@@ -19,6 +19,7 @@ import (
"path/filepath"
execlient "yunion.io/x/executor/client"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
@@ -109,9 +110,17 @@ func (host *SHostService) RunService() {
host.initHandlers(app)
// Init Metadata handler
go metadata.StartService(
go metadata.Start(
app_common.InitApp(&options.HostOptions.BaseOptions, false),
options.HostOptions.Address, options.HostOptions.Port+1000)
&metadata.Service{
Address: options.HostOptions.Address,
Port: options.HostOptions.Port + 1000,
DescGetter: metadata.DescGetterFunc(func(ip string) jsonutils.JSONObject {
guestDesc, _ := guestman.GetGuestManager().GetGuestNicDesc("", ip, "", "", false)
return guestDesc
}),
},
)
cronManager.AddJobEveryFewDays(
"CleanRecycleDiskFiles", 1, 3, 0, 0, storageman.CleanRecycleDiskfiles, false)
+49 -30
View File
@@ -14,6 +14,7 @@
package metadata
// NOTE keep imports minimal. DO NOT IMPORT guestman
import (
"context"
"encoding/base64"
@@ -23,41 +24,72 @@ import (
"strconv"
"strings"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/netutils"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/hostman/guestman"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
"yunion.io/x/onecloud/pkg/httperrors"
)
func addMetadataHandler(prefix string, app *appsrv.Application) {
func Start(app *appsrv.Application, s *Service) {
s.addHandler(app)
addr := net.JoinHostPort(s.Address, strconv.Itoa(s.Port))
log.Infof("Start metadata service on http://%s", addr)
app.ListenAndServeWithoutCleanup(addr, "", "")
}
type DescGetter interface {
Get(ip string) (guestDesc jsonutils.JSONObject)
}
type DescGetterFunc func(ip string) (guestDesc jsonutils.JSONObject)
func (f DescGetterFunc) Get(ip string) (guestDesc jsonutils.JSONObject) {
return f(ip)
}
type Service struct {
Address string
Port int
DescGetter DescGetter
}
func (s *Service) getGuestNicDesc(r *http.Request) (guestDesc jsonutils.JSONObject) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
panic(errors.Wrapf(err, "SplitHostPort %s", r.RemoteAddr))
}
guestDesc = s.DescGetter.Get(ip)
return
}
func (s *Service) addHandler(app *appsrv.Application) {
prefix := ""
for _, method := range []string{"GET", "HEAD"} {
app.AddHandler(method, fmt.Sprintf("%s/<version:%s>",
prefix, `(latest|\d{4}-\d{2}-\d{2})`), versionOnly)
prefix, `(latest|\d{4}-\d{2}-\d{2})`), s.versionOnly)
}
for _, method := range []string{"GET", "HEAD"} {
app.AddHandler(method, fmt.Sprintf("%s/<version:%s>/user-data",
prefix, `(latest|\d{4}-\d{2}-\d{2})`), userData)
prefix, `(latest|\d{4}-\d{2}-\d{2})`), s.userData)
app.AddHandler(method, fmt.Sprintf("%s/<version:%s>/meta-data",
prefix, `(latest|\d{4}-\d{2}-\d{2})`), metaData)
prefix, `(latest|\d{4}-\d{2}-\d{2})`), s.metaData)
}
}
func versionOnly(ctx context.Context, w http.ResponseWriter, r *http.Request) {
func (s *Service) versionOnly(ctx context.Context, w http.ResponseWriter, r *http.Request) {
hostutils.Response(ctx, w, strings.Join([]string{"meta-data", "user-data"}, "\n"))
}
func userData(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
hostutils.Response(ctx, w, httperrors.NewBadRequestError("Parse Remoteaddr %s error %s", r.RemoteAddr, err.Error()))
return
}
guestDesc, gusetNic := guestman.GetGuestManager().GetGuestNicDesc("", ip, "", "", false)
if guestDesc == nil || gusetNic == nil {
func (s *Service) userData(ctx context.Context, w http.ResponseWriter, r *http.Request) {
guestDesc := s.getGuestNicDesc(r)
if guestDesc == nil {
hostutils.Response(ctx, w, "")
return
}
@@ -78,15 +110,9 @@ func userData(ctx context.Context, w http.ResponseWriter, r *http.Request) {
hostutils.Response(ctx, w, string(userDataDecoded))
}
func metaData(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
hostutils.Response(ctx, w, httperrors.NewBadRequestError("Parse Remoteaddr %s error %s", r.RemoteAddr, err.Error()))
return
}
guestDesc, gusetNic := guestman.GetGuestManager().GetGuestNicDesc("", ip, "", "", false)
if guestDesc == nil || gusetNic == nil {
func (s *Service) metaData(ctx context.Context, w http.ResponseWriter, r *http.Request) {
guestDesc := s.getGuestNicDesc(r)
if guestDesc == nil {
hostutils.Response(ctx, w, "")
return
}
@@ -278,10 +304,3 @@ func metaData(ctx context.Context, w http.ResponseWriter, r *http.Request) {
}
hostutils.Response(ctx, w, httperrors.NewNotFoundError("Resource not handled"))
}
func StartService(app *appsrv.Application, address string, port int) {
addMetadataHandler("", app)
addr := net.JoinHostPort(address, strconv.Itoa(port))
log.Infof("Host Metadata Start listen on %s://%s", "http", addr)
app.ListenAndServeWithoutCleanup(addr, "", "")
}
+3 -3
View File
@@ -266,9 +266,9 @@ func (keeper *OVNNorthboundKeeper) ClaimVpc(ctx context.Context, vpc *agentmodel
func (keeper *OVNNorthboundKeeper) ClaimNetwork(ctx context.Context, network *agentmodels.Network, mtu int) error {
var (
rpMac = mac.HashMac(network.Id, "rp")
dhcpMac = mac.HashMac(network.Id, "dhcp")
mdMac = mac.HashMac(network.Id, "md")
rpMac = mac.HashSubnetRouterPortMac(network.Id)
dhcpMac = mac.HashSubnetDhcpMac(network.Id)
mdMac = mac.HashSubnetMetadataMac(network.Id)
mdIp = "169.254.169.254"
)
netLs := &ovn_nb.LogicalSwitch{
+12
View File
@@ -24,3 +24,15 @@ func HashMac(in ...string) string {
func HashVpcHostDistgwMac(hostId string) string {
return HashMac(hostId)
}
func HashSubnetRouterPortMac(netId string) string {
return HashMac(netId, "rp")
}
func HashSubnetDhcpMac(netId string) string {
return HashMac(netId, "dhcp")
}
func HashSubnetMetadataMac(netId string) string {
return HashMac(netId, "md")
}