Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple ips #95

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 36 additions & 43 deletions pkg/api/galaxy/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ const (

// For fip crd object which has this label, it's reserved by admin manually. IPAM will not allocate it to pods.
ReserveFIPLabel = "reserved"
)

// ParseExtendedCNIArgs parses extended cni args from pod annotation
func ParseExtendedCNIArgs(args string) (map[string]map[string]json.RawMessage, error) {
argsMap := map[string]map[string]json.RawMessage{}
if err := json.Unmarshal([]byte(args), &argsMap); err != nil {
return nil, fmt.Errorf("failed to unmarshal %s value %s: %v", ExtendedCNIArgsAnnotation, args, err)
}
return argsMap, nil
}

const (
IPInfosKey = "ipinfos"
)

Expand All @@ -56,38 +45,6 @@ type IPInfo struct {
Gateway net.IP `json:"gateway"`
}

// FormatIPInfo formats ipInfos as extended CNI Args annotation value
func FormatIPInfo(ipInfos []IPInfo) (string, error) {
data, err := json.Marshal(ipInfos)
if err != nil {
return "", err
}
raw := json.RawMessage(data)
str, err := json.Marshal(map[string]map[string]*json.RawMessage{CommonCNIArgsKey: {IPInfosKey: &raw}})
return string(str), err
}

// ParseIPInfo pareses ipInfo from annotation
func ParseIPInfo(str string) ([]IPInfo, error) {
m := map[string]map[string]*json.RawMessage{}
if err := json.Unmarshal([]byte(str), &m); err != nil {
return nil, fmt.Errorf("failed to unmarshal %s value %s: %v", ExtendedCNIArgsAnnotation, str, err)
}
commonMap := m[CommonCNIArgsKey]
if commonMap == nil {
return []IPInfo{}, nil
}
ipInfoStr := commonMap[IPInfosKey]
if ipInfoStr == nil {
return []IPInfo{}, nil
}
var ipInfos []IPInfo
if err := json.Unmarshal([]byte(*ipInfoStr), &ipInfos); err != nil {
return nil, fmt.Errorf("failed to unmarshal %s value %s as common/ipInfos: %v", ExtendedCNIArgsAnnotation, str, err)
}
return ipInfos, nil
}

// ReleasePolicy defines floatingip release policy
type ReleasePolicy uint16

Expand Down Expand Up @@ -124,3 +81,39 @@ const (
NameSpace = "floating-ip"
IpType = "ipType"
)

// CniArgs is the cni args in pod annotation
type CniArgs struct {
// RequestIPRange is the requested ip candidates to allocate, one ip per []nets.IPRange
RequestIPRange [][]nets.IPRange `json:"request_ip_range,omitempty"`
// Common is the common args for cni plugins to setup network
Common CommonCniArgs `json:"common"`
}

type CommonCniArgs struct {
IPInfos []IPInfo `json:"ipinfos,omitempty"`
}

// UnmarshalCniArgs unmarshal cni args from input str
func UnmarshalCniArgs(str string) (*CniArgs, error) {
if str == "" {
return nil, nil
}
var cniArgs CniArgs
if err := json.Unmarshal([]byte(str), &cniArgs); err != nil {
return nil, fmt.Errorf("unmarshal pod cni args: %v", err)
}
return &cniArgs, nil
}

// MarshalCniArgs marshal cni args of the given ipInfos
func MarshalCniArgs(ipInfos []IPInfo) (string, error) {
cniArgs := CniArgs{Common: CommonCniArgs{
IPInfos: ipInfos,
}}
data, err := json.Marshal(cniArgs)
if err != nil {
return "", err
}
return string(data), nil
}
51 changes: 0 additions & 51 deletions pkg/api/galaxy/constant/constant_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions pkg/galaxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ func parseExtendedCNIArgs(pod *corev1.Pod) (map[string]map[string]json.RawMessag
if pod.Annotations == nil {
return nil, nil
}
annotation := pod.Annotations[constant.ExtendedCNIArgsAnnotation]
if annotation == "" {
args := pod.Annotations[constant.ExtendedCNIArgsAnnotation]
if args == "" {
return nil, nil
}
argsMap, err := constant.ParseExtendedCNIArgs(annotation)
if err != nil {
return nil, err
argsMap := map[string]map[string]json.RawMessage{}
if err := json.Unmarshal([]byte(args), &argsMap); err != nil {
return nil, fmt.Errorf("failed to unmarshal cni args %s: %v", args, err)
}
return argsMap, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ipam/api/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (c *PoolController) preAllocateIP(req *restful.Request, resp *restful.Respo
httputil.InternalError(resp, err)
return
}
subnetSet, err := c.IPAM.NodeSubnetsByKey("")
subnetSet, err := c.IPAM.NodeSubnetsByKeyAndIPRanges("", nil)
if err != nil {
httputil.InternalError(resp, err)
return
Expand Down
10 changes: 8 additions & 2 deletions pkg/ipam/floatingip/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/util/sets"
"tkestack.io/galaxy/pkg/api/galaxy/constant"
"tkestack.io/galaxy/pkg/utils/nets"
)

var (
Expand All @@ -42,6 +43,8 @@ type IPAM interface {
AllocateSpecificIP(string, net.IP, Attr) error
// AllocateInSubnet allocate subnet of IPs.
AllocateInSubnet(string, *net.IPNet, Attr) (net.IP, error)
// AllocateInSubnetsAndIPRange allocates an ip for each ip range array of the input node subnet.
AllocateInSubnetsAndIPRange(string, *net.IPNet, [][]nets.IPRange, Attr) ([]net.IP, error)
// AllocateInSubnetWithKey allocate a floatingIP in given subnet and key.
AllocateInSubnetWithKey(oldK, newK, subnet string, attr Attr) error
// ReserveIP can reserve a IP entitled by a terminated pod. Attributes **expect policy attr** will be updated.
Expand All @@ -59,10 +62,13 @@ type IPAM interface {
ByPrefix(string) ([]FloatingIP, error)
// ByKeyword returns floatingIP set by a given keyword.
ByKeyword(string) ([]FloatingIP, error)
// ByKeyAndIPRanges finds an ip for each iprange array by key, and returns all fips
ByKeyAndIPRanges(string, [][]nets.IPRange) ([]*FloatingIPInfo, error)
// NodeSubnets returns node's subnet.
NodeSubnet(net.IP) *net.IPNet
// NodeSubnetsByKey returns keys corresponding node subnets which has `key` as a prefix.
NodeSubnetsByKey(key string) (sets.String, error)
// NodeSubnetsByKeyAndIPRanges finds an ip for each iprange array by key, and returns their intersection
// node subnets.
NodeSubnetsByKeyAndIPRanges(key string, ipranges [][]nets.IPRange) (sets.String, error)
// Name returns IPAM's name.
Name() string
// implements metrics Collector interface
Expand Down
Loading