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

[WIP] [Task] Support the capability for pods, we only have two options, god or nobody #174

Merged
merged 1 commit into from
Jul 25, 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
6 changes: 4 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ For each Pod, we have fileds need to handle.
- vlanTag: the vlan tag for `ifName` interface.
- ipADdress: the IPv4 address of the `ifName` interface.
- netmask: the IPv4 netmask of the `ifName` interface.
7. restartPolicy: the attribute how the pod restart is container, it should be a string and only valid for those following strings.
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

Example:
Expand All @@ -421,7 +422,8 @@ Request Data:
"netmask":"255.255.255.0"
},
"volumes":[
]
],
"capability":true
}
```

Expand Down
1 change: 1 addition & 0 deletions src/entity/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Pod struct {
Volumes []PodVolume `bson:"volumes,omitempty" json:"volumes" validate:"required,dive,required"`
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:"required"`
}

// GetCollection - get model mongo collection name.
Expand Down
26 changes: 22 additions & 4 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"gopkg.in/mgo.v2/bson"
)

var allCapabilities = []corev1.Capability{"NET_ADMIN", "SYS_ADMIN", "NET_RAW"}

// VolumeNamePrefix will set prefix of volumename
const VolumeNamePrefix = "volume-"

Expand Down Expand Up @@ -182,6 +184,20 @@ func generateNetwork(session *mongo.Session, pod *entity.Pod) ([]string, []corev
return nodes, containers, err
}

func generateContainerSecurity(pod *entity.Pod) *corev1.SecurityContext {
if !pod.Capability {
return &corev1.SecurityContext{}
}

privileged := true
return &corev1.SecurityContext{
Privileged: &privileged,
Capabilities: &corev1.Capabilities{
Add: allCapabilities,
},
}
}

func generateAffinity(nodeNames []string) *corev1.Affinity {
if len(nodeNames) == 0 {
return &corev1.Affinity{}
Expand Down Expand Up @@ -230,12 +246,14 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
})

var containers []corev1.Container
securityContext := generateContainerSecurity(pod)
for _, container := range pod.Containers {
containers = append(containers, corev1.Container{
Name: container.Name,
Image: container.Image,
Command: container.Command,
VolumeMounts: volumeMounts,
Name: container.Name,
Image: container.Image,
Command: container.Command,
VolumeMounts: volumeMounts,
SecurityContext: securityContext,
})
}

Expand Down
11 changes: 11 additions & 0 deletions src/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,16 @@ func (suite *PodTestSuite) TestGenerateAffinity() {
suite.Nil(affinity.NodeAffinity)
affinity = generateAffinity([]string{"123"})
suite.NotNil(affinity.NodeAffinity)
}

func (suite *PodTestSuite) TestGenerateContainerSecurityContext() {
pod := &entity.Pod{}
security := generateContainerSecurity(pod)
suite.Nil(security.Privileged)
suite.Nil(security.Capabilities)

pod.Capability = true
security = generateContainerSecurity(pod)
suite.NotNil(security.Privileged)
suite.NotNil(security.Capabilities)
}
1 change: 1 addition & 0 deletions src/server/handler_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (suite *PodTestSuite) TestCreatePod() {
Containers: containers,
Volumes: []entity.PodVolume{},
Networks: []entity.PodNetwork{},
Capability: true,
RestartPolicy: "Never",
}
bodyBytes, err := json.MarshalIndent(pod, "", " ")
Expand Down
3 changes: 2 additions & 1 deletion tests/pod.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
}
],
"volumes":[],
"restaryPolicy":"Always"
"restaryPolicy":"Always",
"capability": true
}