func mustParseIP4Net(s string) ip.IP4Net { _, n, err := net.ParseCIDR(s) if err != nil { panic(err) } return ip.FromIPNet(n) }
func nodeToLease(n api.Node) (l subnet.Lease, err error) { l.Attrs.PublicIP, err = ip.ParseIP4(n.Annotations[backendPublicIPAnnotation]) if err != nil { return l, err } l.Attrs.BackendType = n.Annotations[backendTypeAnnotation] l.Attrs.BackendData = json.RawMessage(n.Annotations[backendDataAnnotation]) _, cidr, err := net.ParseCIDR(n.Spec.PodCIDR) if err != nil { return l, err } l.Subnet = ip.FromIPNet(cidr) l.Expiration = time.Now().Add(24 * time.Hour) return l, nil }
func (ksm *kubeSubnetManager) AcquireLease(ctx context.Context, network string, attrs *subnet.LeaseAttrs) (*subnet.Lease, error) { nobj, found, err := ksm.nodeStore.Store.GetByKey(ksm.nodeName) if err != nil { return nil, err } if !found { return nil, fmt.Errorf("node %q not found", ksm.nodeName) } n, ok := nobj.(*api.Node) if !ok { return nil, fmt.Errorf("nobj was not a *api.Node") } if n.Spec.PodCIDR == "" { return nil, fmt.Errorf("node %q pod cidr not assigned", ksm.nodeName) } bd, err := attrs.BackendData.MarshalJSON() if err != nil { return nil, err } _, cidr, err := net.ParseCIDR(n.Spec.PodCIDR) if err != nil { return nil, err } if n.Annotations[backendDataAnnotation] != string(bd) || n.Annotations[backendTypeAnnotation] != attrs.BackendType || n.Annotations[backendPublicIPAnnotation] != attrs.PublicIP.String() || n.Annotations[subnetKubeManagedAnnotation] != "true" { n.Annotations[backendTypeAnnotation] = attrs.BackendType n.Annotations[backendDataAnnotation] = string(bd) n.Annotations[backendPublicIPAnnotation] = attrs.PublicIP.String() n.Annotations[subnetKubeManagedAnnotation] = "true" n, err = ksm.client.Core().Nodes().Update(n) if err != nil { return nil, err } } return &subnet.Lease{ Subnet: ip.FromIPNet(cidr), Attrs: *attrs, Expiration: time.Now().Add(24 * time.Hour), }, nil }