github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/rootlessutil/port_linux.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package rootlessutil
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  
    23  	"github.com/containerd/containerd/errdefs"
    24  	gocni "github.com/containerd/go-cni"
    25  	"github.com/rootless-containers/rootlesskit/v2/pkg/api/client"
    26  	"github.com/rootless-containers/rootlesskit/v2/pkg/port"
    27  )
    28  
    29  func NewRootlessCNIPortManager(client client.Client) (*RootlessCNIPortManager, error) {
    30  	if client == nil {
    31  		return nil, errdefs.ErrInvalidArgument
    32  	}
    33  	pm := &RootlessCNIPortManager{
    34  		Client: client,
    35  	}
    36  	return pm, nil
    37  }
    38  
    39  type RootlessCNIPortManager struct {
    40  	client.Client
    41  }
    42  
    43  func (rlcpm *RootlessCNIPortManager) ExposePort(ctx context.Context, cpm gocni.PortMapping) error {
    44  	// NOTE: When `nerdctl run -p 8080:80` is being launched, cpm.HostPort is set to 8080 and cpm.ContainerPort is set to 80.
    45  	// We want to forward the port 8080 of the parent namespace into the port 8080 of the child namespace (which is the "host"
    46  	// from the point of view of CNI). So we do NOT set sp.ChildPort to cpm.ContainerPort here.
    47  	sp := port.Spec{
    48  		Proto:      cpm.Protocol,
    49  		ParentIP:   cpm.HostIP,
    50  		ParentPort: int(cpm.HostPort),
    51  		ChildPort:  int(cpm.HostPort), // NOT typo of cpm.ContainerPort
    52  	}
    53  	_, err := rlcpm.Client.PortManager().AddPort(ctx, sp)
    54  	return err
    55  }
    56  
    57  func (rlcpm *RootlessCNIPortManager) UnexposePort(ctx context.Context, cpm gocni.PortMapping) error {
    58  	pm := rlcpm.Client.PortManager()
    59  	ports, err := pm.ListPorts(ctx)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	id := -1
    64  	for _, p := range ports {
    65  		sp := p.Spec
    66  		if sp.Proto != cpm.Protocol || sp.ParentPort != int(cpm.HostPort) || sp.ChildPort != int(cpm.HostPort) {
    67  			continue
    68  		}
    69  		spParentIP := net.ParseIP(sp.ParentIP)
    70  		cpmHostIP := net.ParseIP(cpm.HostIP)
    71  		if spParentIP == nil || !spParentIP.Equal(cpmHostIP) {
    72  			continue
    73  		}
    74  		id = p.ID
    75  		break
    76  	}
    77  	if id < 0 {
    78  		// no ID found, return nil for idempotency
    79  		return nil
    80  	}
    81  	return pm.RemovePort(ctx, id)
    82  }