github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/networking/netinfo/netinfo.go (about)

     1  // Copyright 2015 The rkt 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 netinfo
    16  
    17  import (
    18  	"encoding/json"
    19  	"net"
    20  	"os"
    21  	"path/filepath"
    22  	"syscall"
    23  
    24  	"github.com/containernetworking/cni/pkg/types"
    25  )
    26  
    27  const filename = "net-info.json"
    28  
    29  // A type and some structure to represent rkt's view of a *runtime*
    30  // network instance.
    31  //
    32  // Each instance represents a network configuration that has been enabled,
    33  // along with runtime information from the network plugin.
    34  //
    35  // This information is also serialized in the pod's runtime directory so that
    36  // `rkt list` and other stage0 programs can access the runtime state.
    37  type NetInfo struct {
    38  	NetName    string          `json:"netName"`
    39  	ConfPath   string          `json:"netConf"`
    40  	PluginPath string          `json:"pluginPath"`
    41  	IfName     string          `json:"ifName"`
    42  	IP         net.IP          `json:"ip"`
    43  	Args       string          `json:"args"`
    44  	Mask       net.IP          `json:"mask"` // we used IP instead of IPMask because support for json serialization (we don't need specific functionalities)
    45  	HostIP     net.IP          `json:"-"`
    46  	IP4        *types.IPConfig `json:"-"`
    47  	DNS        types.DNS       `json:"-"`
    48  }
    49  
    50  func LoadAt(cdirfd int) ([]NetInfo, error) {
    51  	fd, err := syscall.Openat(cdirfd, filename, syscall.O_RDONLY, 0)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	f := os.NewFile(uintptr(fd), filename)
    57  
    58  	var info []NetInfo
    59  	err = json.NewDecoder(f).Decode(&info)
    60  	return info, err
    61  }
    62  
    63  func Save(root string, info []NetInfo) error {
    64  	f, err := os.Create(filepath.Join(root, filename))
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer f.Close()
    69  
    70  	return json.NewEncoder(f).Encode(info)
    71  }
    72  
    73  // MergeCNIResult will incorporate the result of a CNI plugin's execution
    74  func (ni *NetInfo) MergeCNIResult(result types.Result) {
    75  	ni.IP = result.IP4.IP.IP
    76  	ni.Mask = net.IP(result.IP4.IP.Mask)
    77  	ni.HostIP = result.IP4.Gateway
    78  	ni.IP4 = result.IP4
    79  	ni.DNS = result.DNS
    80  }