From c68014cc4aa104884e4fd3a6153d2dadacd5d18a Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 16 Apr 2024 15:33:41 +0800 Subject: [PATCH] fix(host): set container port_mappings metadata when status is ready --- pkg/hostman/guestman/pod.go | 71 +++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/pkg/hostman/guestman/pod.go b/pkg/hostman/guestman/pod.go index 9cc6ccadb85..68176389d4b 100644 --- a/pkg/hostman/guestman/pod.go +++ b/pkg/hostman/guestman/pod.go @@ -104,6 +104,20 @@ func (s *sPodGuestInstance) ImportServer(pendingDelete bool) { } func (s *sPodGuestInstance) DeployFs(ctx context.Context, userCred mcclient.TokenCredential, deployInfo *deployapi.DeployInfo) (jsonutils.JSONObject, error) { + // update port_mappings + podInput, err := s.getPodCreateParams() + if err != nil { + return nil, errors.Wrap(err, "getPodCreateParams") + } + if len(podInput.PortMappings) != 0 { + pms, err := s.getPortMappings(podInput.PortMappings) + if err != nil { + return nil, errors.Wrap(err, "get port mappings") + } + if err := s.setPortMappings(ctx, userCred, s.convertToPodMetadataPortMappings(pms)); err != nil { + return nil, errors.Wrap(err, "set port mappings") + } + } return nil, nil } @@ -413,10 +427,27 @@ func (s *sPodGuestInstance) startPod(ctx context.Context, userCred mcclient.Toke Windows: nil, } - if len(podInput.PortMappings) != 0 { - pms, err := s.getPortMappings(podInput.PortMappings) - if err != nil { - return nil, errors.Wrap(err, "get port mappings") + metaPms, err := s.GetPodMetadataPortMappings() + if err != nil { + return nil, errors.Wrap(err, "GetPodMetadataPortMappings") + } + if len(metaPms) != 0 { + pms := make([]*runtimeapi.PortMapping, len(metaPms)) + for idx := range metaPms { + pm := metaPms[idx] + proto := runtimeapi.Protocol_TCP + switch pm.Protocol { + case computeapi.PodPortMappingProtocolTCP: + proto = runtimeapi.Protocol_TCP + case computeapi.PodPortMappingProtocolUDP: + proto = runtimeapi.Protocol_UDP + } + pms[idx] = &runtimeapi.PortMapping{ + Protocol: proto, + ContainerPort: pm.ContainerPort, + HostPort: pm.HostPort, + HostIp: pm.HostIp, + } } podCfg.PortMappings = pms } @@ -613,13 +644,10 @@ func (s *sPodGuestInstance) getCRIId() string { return s.GetSourceDesc().Metadata[computeapi.POD_METADATA_CRI_ID] } -func (s *sPodGuestInstance) convertToPodMetadataPortMappings(cfg *runtimeapi.PodSandboxConfig) []*computeapi.PodMetadataPortMapping { - if cfg.PortMappings == nil { - return []*computeapi.PodMetadataPortMapping{} - } - ret := make([]*computeapi.PodMetadataPortMapping, len(cfg.PortMappings)) - for idx := range cfg.PortMappings { - pm := cfg.PortMappings[idx] +func (s *sPodGuestInstance) convertToPodMetadataPortMappings(pms []*runtimeapi.PortMapping) []*computeapi.PodMetadataPortMapping { + ret := make([]*computeapi.PodMetadataPortMapping, len(pms)) + for idx := range pms { + pm := pms[idx] var proto computeapi.PodPortMappingProtocol = computeapi.PodPortMappingProtocolTCP if pm.Protocol == runtimeapi.Protocol_UDP { proto = computeapi.PodPortMappingProtocolUDP @@ -634,20 +662,27 @@ func (s *sPodGuestInstance) convertToPodMetadataPortMappings(cfg *runtimeapi.Pod return ret } +func (s *sPodGuestInstance) setPortMappings(ctx context.Context, userCred mcclient.TokenCredential, pms []*computeapi.PodMetadataPortMapping) error { + pmStr := jsonutils.Marshal(pms).String() + s.Desc.Metadata[computeapi.POD_METADATA_PORT_MAPPINGS] = pmStr + session := auth.GetSession(ctx, userCred, options.HostOptions.Region) + if _, err := computemod.Servers.SetMetadata(session, s.GetId(), jsonutils.Marshal(map[string]string{ + computeapi.POD_METADATA_PORT_MAPPINGS: pmStr, + })); err != nil { + return errors.Wrapf(err, "set cri_id of pod %s", s.GetId()) + } + return SaveDesc(s, s.Desc) +} + func (s *sPodGuestInstance) setCRIInfo(ctx context.Context, userCred mcclient.TokenCredential, criId string, cfg *runtimeapi.PodSandboxConfig) error { s.Desc.Metadata[computeapi.POD_METADATA_CRI_ID] = criId cfgStr := jsonutils.Marshal(cfg).String() s.Desc.Metadata[computeapi.POD_METADATA_CRI_CONFIG] = cfgStr - pms := s.convertToPodMetadataPortMappings(cfg) - pmStr := jsonutils.Marshal(pms).String() - s.Desc.Metadata[computeapi.POD_METADATA_PORT_MAPPINGS] = pmStr - session := auth.GetSession(ctx, userCred, options.HostOptions.Region) if _, err := computemod.Servers.SetMetadata(session, s.GetId(), jsonutils.Marshal(map[string]string{ - computeapi.POD_METADATA_CRI_ID: criId, - computeapi.POD_METADATA_CRI_CONFIG: cfgStr, - computeapi.POD_METADATA_PORT_MAPPINGS: pmStr, + computeapi.POD_METADATA_CRI_ID: criId, + computeapi.POD_METADATA_CRI_CONFIG: cfgStr, })); err != nil { return errors.Wrapf(err, "set cri_id of pod %s", s.GetId()) }