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

[TASK] Support the PodNetworkType and NodeAffinity filed in the pod entity. #205

Merged
merged 8 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ For each Pod, we have fileds need to handle.
7. capability: the power of the container, if it's ture, it will get almost all capability and act as a privileged=true.
8. restartPolicy: the attribute how the pod restart is container, it should be a string and only valid for those following strings.
- Always,OnFailure,Never
9. hostNetwork: the bool option to run the Pod in the host network namespace, if it's true, all values in the networks will be ignored.
9. networkType: the string options for network type, support "host", "custom" and "cluster".
10. nodeAffinity: the string array to indicate whchi nodes I want my Pod can run in.


Example:
Expand All @@ -642,7 +643,10 @@ Request Data:
},
"volumes":[
],
"capability":true
"capability":true,
"networkType":"host",
"nodeAffinity":[]
]
}
```

Expand Down
12 changes: 10 additions & 2 deletions src/entity/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import (
"gopkg.in/mgo.v2/bson"
)

// PodCollectionName's const
const (
// PodCollectionName's const
PodCollectionName string = "pods"
// The network type for the Pod
// host means the pod use the hostNetwork (share the network with the host machine)
PodHostNetwork = "host"
// cluster means use the cluster Network, maybe the flannel network
PodClusterNetwork = "cluster"
// custom means the custom netwokr we created before, it support the OVS and DPDK network for additional network interface card
PodCustomNetwork = "custom"
)

// Container is the structure for init Container info
Expand Down Expand Up @@ -53,7 +60,8 @@ type Pod struct {
Networks []PodNetwork `bson:"networks,omitempty" json:"networks" validate:"required,dive,required"`
RestartPolicy string `bson:"restartPolicy" json:"restartPolicy" validate:"required,eq=Always|eq=OnFailure|eq=Never`
Capability bool `bson:"capability" json:"capability" validate:"-"`
HostNetwork bool `bson:"hostNetwork" json:"hostNetwork" validate:"-"`
NetworkType string `bson:"networkType" json:"networkType" validate:"required,eq=host|cluster|custom`
NodeAffinity []string `bson:"nodeAffinity" json:"nodeAffinity" validate:"required"`
}

// GetCollection - get model mongo collection name.
Expand Down
24 changes: 17 additions & 7 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,26 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
return err
}

nodeNames := []string{}
nodeAffinity := pod.NodeAffinity
initContainers := []corev1.Container{}
hostNetwork := false
if pod.HostNetwork {
switch pod.NetworkType {
case entity.PodHostNetwork:
hostNetwork = true
} else {
nodeNames, initContainers, err = generateNetwork(session, pod)
if err != nil {
return err
case entity.PodCustomNetwork:
tmp := []string{}
tmp, initContainers, err = generateNetwork(session, pod)
if len(tmp) != 0 {
nodeAffinity = utils.Intersection(nodeAffinity, tmp)
}
case entity.PodClusterNetwork:
//For cluster network, we won't set the nodeAffinity and any network options.
default:
err = fmt.Errorf("UnSupported Pod NetworkType %s", pod.NetworkType)
}

if err != nil {
return err
}

volumes = append(volumes, corev1.Volume{
Expand Down Expand Up @@ -273,7 +283,7 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
InitContainers: initContainers,
Containers: containers,
Volumes: volumes,
Affinity: generateAffinity(nodeNames),
Affinity: generateAffinity(nodeAffinity),
RestartPolicy: corev1.RestartPolicy(pod.RestartPolicy),
HostNetwork: hostNetwork,
},
Expand Down
99 changes: 96 additions & 3 deletions src/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ func (suite *PodTestSuite) TestCreatePod() {

podName := namesgenerator.GetRandomName(0)
pod := &entity.Pod{
ID: bson.NewObjectId(),
Name: podName,
Containers: containers,
ID: bson.NewObjectId(),
Name: podName,
Containers: containers,
NetworkType: entity.PodHostNetwork,
}

err := CreatePod(suite.sp, pod)
Expand Down Expand Up @@ -347,3 +348,95 @@ func (suite *PodTestSuite) TestGenerateContainerSecurityContext() {
suite.NotNil(security.Privileged)
suite.NotNil(security.Capabilities)
}

func (suite *PodTestSuite) TestCreatePodWithNetworkTypes() {

networkName := namesgenerator.GetRandomName(0)
bName := namesgenerator.GetRandomName(0)
network := entity.Network{
ID: bson.NewObjectId(),
Name: networkName,
BridgeName: bName,
Nodes: []entity.Node{
{Name: "node1"},
{Name: "node2"},
{Name: "node3"},
},
}
session := suite.sp.Mongo.NewSession()
defer session.Close()

session.Insert(entity.NetworkCollectionName, network)
defer session.Remove(entity.NetworkCollectionName, "name", network.Name)

//For each case, we need to check the
//HostNetwork object
//NodeAffinity object
//Create Success
testCases := []struct {
caseName string
pod *entity.Pod
rHostNetwork bool //result of hostNetwork
rNodeAffinity []string //result of rNodeAffinity
}{
{
"hostNetwork",
&entity.Pod{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Containers: []entity.Container{},
NetworkType: entity.PodHostNetwork,
NodeAffinity: []string{"node1", "node2"},
},
true,
[]string{"node1", "node2"},
},
{
"clusterNetwork",
&entity.Pod{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Containers: []entity.Container{},
NetworkType: entity.PodClusterNetwork,
NodeAffinity: []string{"node1", "node2"},
},
false,
[]string{"node1", "node2"},
},
{
"customNetwork",
&entity.Pod{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Containers: []entity.Container{},
NetworkType: entity.PodCustomNetwork,
Networks: []entity.PodNetwork{
{
Name: networkName,
},
},
NodeAffinity: []string{"node1", "node5"},
},
false,
[]string{"node1"},
},
}

for _, tc := range testCases {
suite.T().Run(tc.caseName, func(t *testing.T) {
err := CreatePod(suite.sp, tc.pod)
suite.NoError(err)

pod, err := suite.sp.KubeCtl.GetPod(tc.pod.Name, tc.pod.Namespace)
suite.NotNil(pod)
suite.NoError(err)

suite.Equal(tc.rHostNetwork, pod.Spec.HostNetwork)
suite.Equal(tc.rNodeAffinity, pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Values)

err = DeletePod(suite.sp, tc.pod)
suite.NoError(err)

})
}
}
15 changes: 10 additions & 5 deletions src/server/handler_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (suite *PodTestSuite) TestCreatePod() {
Networks: []entity.PodNetwork{},
Capability: true,
RestartPolicy: "Never",
HostNetwork: false,
NetworkType: entity.PodHostNetwork,
NodeAffinity: []string{},
}
bodyBytes, err := json.MarshalIndent(pod, "", " ")
suite.NoError(err)
Expand Down Expand Up @@ -152,10 +153,14 @@ func (suite *PodTestSuite) TestDeletePod() {
}
tName := namesgenerator.GetRandomName(0)
pod := entity.Pod{
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
Capability: true,
RestartPolicy: "Never",
NetworkType: entity.PodHostNetwork,
NodeAffinity: []string{},
}

err := p.CreatePod(suite.sp, &pod)
Expand Down
3 changes: 2 additions & 1 deletion tests/01-multiple-interface/pod.info
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
"volumes":[],
"restaryPolicy":"Always",
"capability": true,
"hostNetwork":false
"networkType": "custom",
"nodeAffinity": []
}
3 changes: 2 additions & 1 deletion tests/02-hoet-network/pod.info
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"volumes":[],
"restaryPolicy":"Always",
"capability": true,
"hostNetwork": true
"networkType": "host",
"nodeAffinity": []
}