-
Notifications
You must be signed in to change notification settings - Fork 5
Add first version of createPodSandbox #14
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "name": "podsandbox1", | ||
| "hostname": "ocic_host", | ||
| "log_directory": ".", | ||
| "dns_options": { | ||
| "servers": [ | ||
| "server1.redhat.com", | ||
| "server2.redhat.com" | ||
| ], | ||
| "searches": [ | ||
| "8.8.8.8" | ||
| ] | ||
| }, | ||
| "port_mappings": [ | ||
| { | ||
| "name": "port_map1", | ||
| "protocol": 1, | ||
| "container_port": 80, | ||
| "host_port": 4888, | ||
| "host_ip": "192.168.0.33" | ||
| }, | ||
| { | ||
| "name": "port_map2", | ||
| "protocol": 2, | ||
| "container_port": 81, | ||
| "host_port": 4889, | ||
| "host_ip": "192.168.0.33" | ||
| } | ||
| ], | ||
| "resources": { | ||
| "cpu": { | ||
| "limits": 50000000, | ||
| "requests": 20000000 | ||
| }, | ||
| "memory": { | ||
| "limits": 500000000000, | ||
| "requests": 200000000000 | ||
| } | ||
| }, | ||
| "labels": { | ||
| "group": "test" | ||
| }, | ||
| "annotations": { | ||
| "owner": "hmeng" | ||
| }, | ||
| "linux": { | ||
| "cgroup_parent": "/sys/fs/cgroup/test", | ||
| "namespace_options": { | ||
| "host_network": true, | ||
| "host_pid": true, | ||
| "host_ipc": true | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| pb "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime" | ||
|
|
@@ -37,16 +39,117 @@ func (s *Server) Version(ctx context.Context, req *pb.VersionRequest) (*pb.Versi | |
| func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxRequest) (*pb.CreatePodSandboxResponse, error) { | ||
| var err error | ||
|
|
||
| // TODO: Parametrize as a global argument to ocid | ||
| ocidSandboxDir := "/var/lib/ocid/sandbox" | ||
| podSandboxDir := filepath.Join(ocidSandboxDir, req.GetConfig().GetName()) | ||
| if err := os.MkdirAll(s.sandboxDir, 0755); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // process req.Name | ||
| name := req.GetConfig().GetName() | ||
| if name == "" { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should require name to be set
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I thought so.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can remove the extra code here.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's because is everything is optional in proto for compat with v3
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. Got it. |
||
| return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty") | ||
| } | ||
|
|
||
| podSandboxDir := filepath.Join(s.sandboxDir, name) | ||
| if _, err := os.Stat(podSandboxDir); err == nil { | ||
| return nil, fmt.Errorf("pod sandbox (%s) already exists", podSandboxDir) | ||
| } | ||
|
|
||
| if err := os.MkdirAll(podSandboxDir, 0755); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // creates a spec Generator with the default spec. | ||
| g := generate.New() | ||
|
|
||
| // TODO: Customize the config per the settings in the req | ||
| // process req.Hostname | ||
| hostname := req.GetConfig().GetHostname() | ||
| if hostname != "" { | ||
| g.SetHostname(hostname) | ||
| } | ||
|
|
||
| // process req.LogDirectory | ||
| logDir := req.GetConfig().GetLogDirectory() | ||
| if logDir == "" { | ||
| logDir = fmt.Sprintf("/var/log/ocid/pods/%s", name) | ||
| } | ||
|
|
||
| // TODO: construct /etc/resolv.conf based on dnsOpts. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to construct /etc/resolv.conf to be mounted. (I looked at existing kubelet code).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mrunalp , sounds good.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mind doing this in a later PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mrunalp , great.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hmeng-19 It is okay, I'll merge this and then we can address the TODOs in follow on PRs. |
||
| dnsOpts := req.GetConfig().GetDnsOptions() | ||
| fmt.Println(dnsOpts) | ||
|
|
||
| // TODO: the unit of cpu here is cores. How to map it into specs.Spec.Linux.Resouces.CPU? | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read this document to understand how the conversion works. Basically we need to set shares, quota and period. |
||
| cpu := req.GetConfig().GetResources().GetCpu() | ||
| if cpu != nil { | ||
| limits := cpu.GetLimits() | ||
| requests := cpu.GetRequests() | ||
| fmt.Println(limits) | ||
| fmt.Println(requests) | ||
| } | ||
|
|
||
| memory := req.GetConfig().GetResources().GetMemory() | ||
| if memory != nil { | ||
| // limits sets specs.Spec.Linux.Resouces.Memory.Limit | ||
| limits := memory.GetLimits() | ||
| if limits != 0 { | ||
| g.SetLinuxResourcesMemoryLimit(uint64(limits)) | ||
| } | ||
|
|
||
| // requests sets specs.Spec.Linux.Resouces.Memory.Reservation | ||
| requests := memory.GetRequests() | ||
| if requests != 0 { | ||
| g.SetLinuxResourcesMemoryReservation(uint64(requests)) | ||
| } | ||
| } | ||
|
|
||
| labels := req.GetConfig().GetLabels() | ||
| s.sandboxes = append(s.sandboxes, &sandbox{ | ||
| name: name, | ||
| logDir: logDir, | ||
| labels: labels, | ||
| }) | ||
|
|
||
| annotations := req.GetConfig().GetAnnotations() | ||
| for k, v := range annotations { | ||
| err := g.AddAnnotation(fmt.Sprintf("%s=%s", k, v)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| // TODO: double check cgroupParent. | ||
| cgroupParent := req.GetConfig().GetLinux().GetCgroupParent() | ||
| if cgroupParent != "" { | ||
| g.SetLinuxCgroupsPath(cgroupParent) | ||
| } | ||
|
|
||
| // set up namespaces | ||
| if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostNetwork() == false { | ||
| err := g.AddOrReplaceLinuxNamespace("network", "") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostPid() == false { | ||
| err := g.AddOrReplaceLinuxNamespace("pid", "") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostIpc() == false { | ||
| err := g.AddOrReplaceLinuxNamespace("ipc", "") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| err = g.SaveToFile(filepath.Join(podSandboxDir, "config.json")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return nil, err | ||
| return &pb.CreatePodSandboxResponse{PodSandboxId: &name}, nil | ||
| } | ||
|
|
||
| // StopPodSandbox stops the sandbox. If there are any running containers in the | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refer to #12 if you're dealing with images
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing out.
I have not reached that far yet. Currently, I am just trying to process each field inside
PodSandboxConfig.