Skip to content
This repository has been archived by the owner on Jul 30, 2021. It is now read-only.

Commit

Permalink
checkpointer: small cleanups from refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegs committed Oct 31, 2017
1 parent 6e1fa08 commit d3f812d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 62 deletions.
8 changes: 8 additions & 0 deletions pkg/checkpoint/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ func writeManifestIfDifferent(path, name string, data []byte) (bool, error) {
glog.Infof("Writing manifest for %q to %q", name, path)
return true, writeAndAtomicRename(path, data, 0644)
}

func writeAndAtomicRename(path string, data []byte, perm os.FileMode) error {
tmpfile := filepath.Join(filepath.Dir(path), "."+filepath.Base(path))
if err := ioutil.WriteFile(tmpfile, data, perm); err != nil {
return err
}
return os.Rename(tmpfile, path)
}
10 changes: 0 additions & 10 deletions pkg/checkpoint/pod.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package checkpoint

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -163,11 +161,3 @@ func podFullNameToInactiveCheckpointPath(id string) string {
func podFullNameToActiveCheckpointPath(id string) string {
return filepath.Join(activeCheckpointPath, strings.Replace(id, "/", "-", -1)+".json")
}

func writeAndAtomicRename(path string, data []byte, perm os.FileMode) error {
tmpfile := filepath.Join(filepath.Dir(path), "."+filepath.Base(path))
if err := ioutil.WriteFile(tmpfile, data, perm); err != nil {
return err
}
return os.Rename(tmpfile, path)
}
104 changes: 52 additions & 52 deletions pkg/checkpoint/runtime_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,62 +37,11 @@ func newRemoteRuntimeService(endpoint string, connectionTimeout time.Duration) (
}, nil
}

func (r *remoteRuntimeService) getRunningKubeletContainers() ([]*runtimeapi.Container, error) {
filter := &runtimeapi.ContainerFilter{}

// Filter out non-running containers
filter.State = &runtimeapi.ContainerStateValue{
State: runtimeapi.ContainerState_CONTAINER_RUNNING,
}

return r.listContainers(filter)
}

func (r *remoteRuntimeService) getRunningKubeletSandboxes() ([]*runtimeapi.PodSandbox, error) {
filter := &runtimeapi.PodSandboxFilter{}

// Filter out non-running sandboxes
filter.State = &runtimeapi.PodSandboxStateValue{
State: runtimeapi.PodSandboxState_SANDBOX_READY,
}
return r.listPodSandbox(filter)
}

func (r *remoteRuntimeService) listPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.timeout)
defer cancel()

resp, err := r.runtimeClient.ListPodSandbox(ctx, &runtimeapi.ListPodSandboxRequest{
Filter: filter,
})
if err != nil {
glog.Errorf("ListPodSandbox with filter %q from runtime sevice failed: %v", filter, err)
return nil, err
}

return resp.Items, nil
}

func (r *remoteRuntimeService) listContainers(filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.timeout)
defer cancel()

resp, err := r.runtimeClient.ListContainers(ctx, &runtimeapi.ListContainersRequest{
Filter: filter,
})
if err != nil {
glog.Errorf("ListContainers with filter %q from runtime service failed: %v", filter, err)
return nil, err
}

return resp.Containers, nil
}

// localRunningPods uses the CRI shim to retrieve the local container runtime pod state
func (r *remoteRuntimeService) localRunningPods() map[string]*v1.Pod {
pods := make(map[string]*v1.Pod)

// Retrieving sandboxes is likely redudant but is done to maintain sameness with what the kubelet does
// Retrieving sandboxes is likely redundant but is done to maintain sameness with what the kubelet does
sandboxes, err := r.getRunningKubeletSandboxes()
if err != nil {
glog.Errorf("failed to list running sandboxes: %v", err)
Expand Down Expand Up @@ -143,3 +92,54 @@ func (r *remoteRuntimeService) localRunningPods() map[string]*v1.Pod {

return pods
}

func (r *remoteRuntimeService) getRunningKubeletContainers() ([]*runtimeapi.Container, error) {
filter := &runtimeapi.ContainerFilter{}

// Filter out non-running containers
filter.State = &runtimeapi.ContainerStateValue{
State: runtimeapi.ContainerState_CONTAINER_RUNNING,
}

return r.listContainers(filter)
}

func (r *remoteRuntimeService) getRunningKubeletSandboxes() ([]*runtimeapi.PodSandbox, error) {
filter := &runtimeapi.PodSandboxFilter{}

// Filter out non-running sandboxes
filter.State = &runtimeapi.PodSandboxStateValue{
State: runtimeapi.PodSandboxState_SANDBOX_READY,
}
return r.listPodSandbox(filter)
}

func (r *remoteRuntimeService) listPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.timeout)
defer cancel()

resp, err := r.runtimeClient.ListPodSandbox(ctx, &runtimeapi.ListPodSandboxRequest{
Filter: filter,
})
if err != nil {
glog.Errorf("ListPodSandbox with filter %q from runtime sevice failed: %v", filter, err)
return nil, err
}

return resp.Items, nil
}

func (r *remoteRuntimeService) listContainers(filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.timeout)
defer cancel()

resp, err := r.runtimeClient.ListContainers(ctx, &runtimeapi.ListContainersRequest{
Filter: filter,
})
if err != nil {
glog.Errorf("ListContainers with filter %q from runtime service failed: %v", filter, err)
return nil, err
}

return resp.Containers, nil
}

0 comments on commit d3f812d

Please sign in to comment.