istio.io/istio@v0.0.0-20240520182934-d79c90f27776/cni/pkg/nodeagent/netns.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package nodeagent
    16  
    17  import "io"
    18  
    19  type NetnsFd interface {
    20  	Fd() uintptr
    21  }
    22  
    23  type Netns interface {
    24  	NetnsFd
    25  	Inode() uint64
    26  }
    27  type NetnsCloser interface {
    28  	io.Closer
    29  	Netns
    30  }
    31  
    32  type NetnsWithFd struct {
    33  	netns io.Closer
    34  	fd    uintptr
    35  	inode uint64
    36  }
    37  
    38  func (n *NetnsWithFd) Close() error {
    39  	if n.netns == nil {
    40  		return nil
    41  	}
    42  
    43  	ret := n.netns.Close()
    44  	// set fd to invalid value, so if something uses it by mistake it will err
    45  	uintZero := uintptr(0)
    46  	n.fd = ^uintZero
    47  	return ret
    48  }
    49  
    50  func (n *NetnsWithFd) Fd() uintptr {
    51  	return n.fd
    52  }
    53  
    54  func (n *NetnsWithFd) Inode() uint64 {
    55  	return n.inode
    56  }