Skip to content

Commit

Permalink
Merge pull request #20024 from zexi/container-ready-pm
Browse files Browse the repository at this point in the history
fix(host): set container port_mappings metadata when status is ready
  • Loading branch information
zexi authored Apr 16, 2024
2 parents 35b58e7 + c68014c commit 554640d
Showing 1 changed file with 53 additions and 18 deletions.
71 changes: 53 additions & 18 deletions pkg/hostman/guestman/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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())
}
Expand Down

0 comments on commit 554640d

Please sign in to comment.