Report Additional POD IPs
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -17,8 +17,10 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/containerd/containerd/log"
|
||||
@@ -32,17 +34,36 @@ import (
|
||||
type cniConfigTemplate struct {
|
||||
// PodCIDR is the cidr for pods on the node.
|
||||
PodCIDR string
|
||||
// PodCIDRRanges is the cidr ranges for pods on the node.
|
||||
PodCIDRRanges []string
|
||||
// Routes is a list of routes configured.
|
||||
Routes []string
|
||||
}
|
||||
|
||||
// cniConfigFileName is the name of cni config file generated by containerd.
|
||||
const cniConfigFileName = "10-containerd-net.conflist"
|
||||
const (
|
||||
// cniConfigFileName is the name of cni config file generated by containerd.
|
||||
cniConfigFileName = "10-containerd-net.conflist"
|
||||
// zeroCIDRv6 is the null route for IPv6.
|
||||
zeroCIDRv6 = "::/0"
|
||||
// zeroCIDRv4 is the null route for IPv4.
|
||||
zeroCIDRv4 = "0.0.0.0/0"
|
||||
)
|
||||
|
||||
// UpdateRuntimeConfig updates the runtime config. Currently only handles podCIDR updates.
|
||||
func (c *criService) UpdateRuntimeConfig(ctx context.Context, r *runtime.UpdateRuntimeConfigRequest) (*runtime.UpdateRuntimeConfigResponse, error) {
|
||||
podCIDR := r.GetRuntimeConfig().GetNetworkConfig().GetPodCidr()
|
||||
if podCIDR == "" {
|
||||
podCIDRs := r.GetRuntimeConfig().GetNetworkConfig().GetPodCidr()
|
||||
if podCIDRs == "" {
|
||||
return &runtime.UpdateRuntimeConfigResponse{}, nil
|
||||
}
|
||||
cidrs := strings.Split(podCIDRs, ",")
|
||||
for i := range cidrs {
|
||||
cidrs[i] = strings.TrimSpace(cidrs[i])
|
||||
}
|
||||
routes, err := getRoutes(cidrs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get routes")
|
||||
}
|
||||
|
||||
confTemplate := c.config.NetworkPluginConfTemplate
|
||||
if confTemplate == "" {
|
||||
log.G(ctx).Info("No cni config template is specified, wait for other system components to drop the config.")
|
||||
@@ -70,8 +91,38 @@ func (c *criService) UpdateRuntimeConfig(ctx context.Context, r *runtime.UpdateR
|
||||
return nil, errors.Wrapf(err, "failed to open cni config file %q", confFile)
|
||||
}
|
||||
defer f.Close()
|
||||
if err := t.Execute(f, cniConfigTemplate{PodCIDR: podCIDR}); err != nil {
|
||||
if err := t.Execute(f, cniConfigTemplate{
|
||||
PodCIDR: cidrs[0],
|
||||
PodCIDRRanges: cidrs,
|
||||
Routes: routes,
|
||||
}); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to generate cni config file %q", confFile)
|
||||
}
|
||||
return &runtime.UpdateRuntimeConfigResponse{}, nil
|
||||
}
|
||||
|
||||
// getRoutes generates required routes for the passed in cidrs.
|
||||
func getRoutes(cidrs []string) ([]string, error) {
|
||||
var (
|
||||
routes []string
|
||||
hasV4, hasV6 bool
|
||||
)
|
||||
for _, c := range cidrs {
|
||||
_, cidr, err := net.ParseCIDR(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cidr.IP.To4() != nil {
|
||||
hasV4 = true
|
||||
} else {
|
||||
hasV6 = true
|
||||
}
|
||||
}
|
||||
if hasV4 {
|
||||
routes = append(routes, zeroCIDRv4)
|
||||
}
|
||||
if hasV6 {
|
||||
routes = append(routes, zeroCIDRv6)
|
||||
}
|
||||
return routes, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user