diff --git a/pkg/appsrv/appsrv.go b/pkg/appsrv/appsrv.go index 525c2293a0..81092eb4fe 100644 --- a/pkg/appsrv/appsrv.go +++ b/pkg/appsrv/appsrv.go @@ -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 { diff --git a/pkg/hostman/host_services.go b/pkg/hostman/host_services.go index e429a80650..9e9d491a88 100644 --- a/pkg/hostman/host_services.go +++ b/pkg/hostman/host_services.go @@ -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) diff --git a/pkg/hostman/metadata/metadatahandler.go b/pkg/hostman/metadata/metadatahandler.go index a8f941e488..98ff87bfe0 100644 --- a/pkg/hostman/metadata/metadatahandler.go +++ b/pkg/hostman/metadata/metadatahandler.go @@ -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/", - 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//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//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, "", "") -} diff --git a/pkg/vpcagent/ovn/keeper.go b/pkg/vpcagent/ovn/keeper.go index 3617b0be64..92912800dd 100644 --- a/pkg/vpcagent/ovn/keeper.go +++ b/pkg/vpcagent/ovn/keeper.go @@ -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{ diff --git a/pkg/vpcagent/ovn/mac/mac.go b/pkg/vpcagent/ovn/mac/mac.go index 5015932f60..573b222482 100644 --- a/pkg/vpcagent/ovn/mac/mac.go +++ b/pkg/vpcagent/ovn/mac/mac.go @@ -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") +}