diff --git a/hack/.golint_failures b/hack/.golint_failures index 8cf89a0814c..f7d9fe879fd 100644 --- a/hack/.golint_failures +++ b/hack/.golint_failures @@ -102,7 +102,6 @@ pkg/kubelet/cm pkg/kubelet/container pkg/kubelet/container/testing pkg/kubelet/cri/remote -pkg/kubelet/dockershim pkg/kubelet/dockershim/libdocker pkg/kubelet/dockershim/network pkg/kubelet/dockershim/network/cni/testing diff --git a/pkg/kubelet/dockershim/convert.go b/pkg/kubelet/dockershim/convert.go index 1400d123bed..0ca99cf7262 100644 --- a/pkg/kubelet/dockershim/convert.go +++ b/pkg/kubelet/dockershim/convert.go @@ -166,7 +166,7 @@ func containerToRuntimeAPISandbox(c *dockertypes.Container) (*runtimeapi.PodSand }, nil } -func checkpointToRuntimeAPISandbox(id string, checkpoint DockershimCheckpoint) *runtimeapi.PodSandbox { +func checkpointToRuntimeAPISandbox(id string, checkpoint ContainerCheckpoint) *runtimeapi.PodSandbox { state := runtimeapi.PodSandboxState_SANDBOX_NOTREADY _, name, namespace, _, _ := checkpoint.GetData() return &runtimeapi.PodSandbox{ diff --git a/pkg/kubelet/dockershim/doc.go b/pkg/kubelet/dockershim/doc.go index 2e9fda933f3..152464bc85c 100644 --- a/pkg/kubelet/dockershim/doc.go +++ b/pkg/kubelet/dockershim/doc.go @@ -16,5 +16,6 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package dockershim implements a container runtime interface // Docker integration using k8s.io/cri-api/pkg/apis/runtime/v1alpha2/api.pb.go package dockershim diff --git a/pkg/kubelet/dockershim/docker_checkpoint.go b/pkg/kubelet/dockershim/docker_checkpoint.go index 4d881f7771a..387b934c6dc 100644 --- a/pkg/kubelet/dockershim/docker_checkpoint.go +++ b/pkg/kubelet/dockershim/docker_checkpoint.go @@ -34,11 +34,13 @@ const ( schemaVersion = "v1" ) -type DockershimCheckpoint interface { +// ContainerCheckpoint provides the interface for process container's checkpoint data +type ContainerCheckpoint interface { checkpointmanager.Checkpoint GetData() (string, string, string, []*PortMapping, bool) } +// Protocol is the type of port mapping protocol type Protocol string // PortMapping is the port mapping configurations of a sandbox. @@ -73,7 +75,8 @@ type PodSandboxCheckpoint struct { Checksum checksum.Checksum `json:"checksum"` } -func NewPodSandboxCheckpoint(namespace, name string, data *CheckpointData) DockershimCheckpoint { +// NewPodSandboxCheckpoint inits a PodSandboxCheckpoint with the given args +func NewPodSandboxCheckpoint(namespace, name string, data *CheckpointData) ContainerCheckpoint { return &PodSandboxCheckpoint{ Version: schemaVersion, Namespace: namespace, @@ -82,19 +85,24 @@ func NewPodSandboxCheckpoint(namespace, name string, data *CheckpointData) Docke } } +// MarshalCheckpoint encodes the PodSandboxCheckpoint instance to a json object func (cp *PodSandboxCheckpoint) MarshalCheckpoint() ([]byte, error) { cp.Checksum = checksum.New(*cp.Data) return json.Marshal(*cp) } +// UnmarshalCheckpoint decodes the blob data to the PodSandboxCheckpoint instance func (cp *PodSandboxCheckpoint) UnmarshalCheckpoint(blob []byte) error { return json.Unmarshal(blob, cp) } +// VerifyChecksum verifies whether the PodSandboxCheckpoint's data checksum is +// the same as calculated checksum func (cp *PodSandboxCheckpoint) VerifyChecksum() error { return cp.Checksum.Verify(*cp.Data) } +// GetData gets the PodSandboxCheckpoint's version and some net information func (cp *PodSandboxCheckpoint) GetData() (string, string, string, []*PortMapping, bool) { return cp.Version, cp.Name, cp.Namespace, cp.Data.PortMappings, cp.Data.HostNetwork } diff --git a/pkg/kubelet/dockershim/docker_container_test.go b/pkg/kubelet/dockershim/docker_container_test.go index 7834da3315c..61b30a6c996 100644 --- a/pkg/kubelet/dockershim/docker_container_test.go +++ b/pkg/kubelet/dockershim/docker_container_test.go @@ -34,6 +34,11 @@ import ( containertest "k8s.io/kubernetes/pkg/kubelet/container/testing" ) +const ( + sandboxID = "sandboxid" + containerID = "containerid" +) + // A helper to create a basic config. func makeContainerConfig(sConfig *runtimeapi.PodSandboxConfig, name, image string, attempt uint32, labels, annotations map[string]string) *runtimeapi.ContainerConfig { return &runtimeapi.ContainerConfig{ @@ -145,9 +150,8 @@ func TestContainerStatus(t *testing.T) { // Create the container. fClock.SetTime(time.Now().Add(-1 * time.Hour)) expected.CreatedAt = fClock.Now().UnixNano() - const sandboxId = "sandboxid" - req := &runtimeapi.CreateContainerRequest{PodSandboxId: sandboxId, Config: config, SandboxConfig: sConfig} + req := &runtimeapi.CreateContainerRequest{PodSandboxId: sandboxID, Config: config, SandboxConfig: sConfig} createResp, err := ds.CreateContainer(getTestCTX(), req) require.NoError(t, err) id := createResp.ContainerId @@ -156,7 +160,7 @@ func TestContainerStatus(t *testing.T) { c, err := fDocker.InspectContainer(id) require.NoError(t, err) assert.Equal(t, c.Config.Labels[containerTypeLabelKey], containerTypeLabelContainer) - assert.Equal(t, c.Config.Labels[sandboxIDLabelKey], sandboxId) + assert.Equal(t, c.Config.Labels[sandboxIDLabelKey], sandboxID) // Set the id manually since we don't know the id until it's created. expected.Id = id @@ -207,8 +211,7 @@ func TestContainerLogPath(t *testing.T) { config := makeContainerConfig(sConfig, "pause", "iamimage", 0, nil, nil) config.LogPath = containerLogPath - const sandboxId = "sandboxid" - req := &runtimeapi.CreateContainerRequest{PodSandboxId: sandboxId, Config: config, SandboxConfig: sConfig} + req := &runtimeapi.CreateContainerRequest{PodSandboxId: sandboxID, Config: config, SandboxConfig: sConfig} createResp, err := ds.CreateContainer(getTestCTX(), req) require.NoError(t, err) id := createResp.ContainerId @@ -248,11 +251,9 @@ func TestContainerCreationConflict(t *testing.T) { sConfig := makeSandboxConfig("foo", "bar", "1", 0) config := makeContainerConfig(sConfig, "pause", "iamimage", 0, map[string]string{}, map[string]string{}) containerName := makeContainerName(sConfig, config) - const sandboxId = "sandboxid" - const containerId = "containerid" - conflictError := fmt.Errorf("Error response from daemon: Conflict. The name \"/%s\" is already in use by container %q. You have to remove (or rename) that container to be able to reuse that name.", - containerName, containerId) - noContainerError := fmt.Errorf("Error response from daemon: No such container: %s", containerId) + conflictError := fmt.Errorf("Error response from daemon: Conflict. The name \"/%s\" is already in use by container %q. You have to remove (or rename) that container to be able to reuse that name", + containerName, containerID) + noContainerError := fmt.Errorf("Error response from daemon: No such container: %s", containerID) randomError := fmt.Errorf("random error") for desc, test := range map[string]struct { @@ -299,7 +300,7 @@ func TestContainerCreationConflict(t *testing.T) { fDocker.InjectError("remove", test.removeError) } - req := &runtimeapi.CreateContainerRequest{PodSandboxId: sandboxId, Config: config, SandboxConfig: sConfig} + req := &runtimeapi.CreateContainerRequest{PodSandboxId: sandboxID, Config: config, SandboxConfig: sConfig} createResp, err := ds.CreateContainer(getTestCTX(), req) require.Equal(t, test.expectError, err) assert.NoError(t, fDocker.AssertCalls(test.expectCalls)) diff --git a/pkg/kubelet/dockershim/docker_image_linux.go b/pkg/kubelet/dockershim/docker_image_linux.go index b46d04b4937..1c386d3ead4 100644 --- a/pkg/kubelet/dockershim/docker_image_linux.go +++ b/pkg/kubelet/dockershim/docker_image_linux.go @@ -67,7 +67,7 @@ func dirSize(path string) (int64, int64, error) { if err != nil { return err } - inodes += 1 + inodes++ if !info.IsDir() { bytes += info.Size() } diff --git a/pkg/kubelet/dockershim/docker_legacy_service.go b/pkg/kubelet/dockershim/docker_legacy_service.go index 7e58333bff7..6fae022c9bd 100644 --- a/pkg/kubelet/dockershim/docker_legacy_service.go +++ b/pkg/kubelet/dockershim/docker_legacy_service.go @@ -94,7 +94,7 @@ func (d *dockerService) GetContainerLogs(_ context.Context, pod *v1.Pod, contain // GetContainerLogTail attempts to read up to MaxContainerTerminationMessageLogLength // from the end of the log when docker is configured with a log driver other than json-log. // It reads up to MaxContainerTerminationMessageLogLines lines. -func (d *dockerService) GetContainerLogTail(uid kubetypes.UID, name, namespace string, containerId kubecontainer.ContainerID) (string, error) { +func (d *dockerService) GetContainerLogTail(uid kubetypes.UID, name, namespace string, containerID kubecontainer.ContainerID) (string, error) { value := int64(kubecontainer.MaxContainerTerminationMessageLogLines) buf, _ := circbuf.NewBuffer(kubecontainer.MaxContainerTerminationMessageLogLength) // Although this is not a full spec pod, dockerLegacyService.GetContainerLogs() currently completely ignores its pod param @@ -105,7 +105,7 @@ func (d *dockerService) GetContainerLogTail(uid kubetypes.UID, name, namespace s Namespace: namespace, }, } - err := d.GetContainerLogs(context.Background(), pod, containerId, &v1.PodLogOptions{TailLines: &value}, buf, buf) + err := d.GetContainerLogs(context.Background(), pod, containerID, &v1.PodLogOptions{TailLines: &value}, buf, buf) if err != nil { return "", err } diff --git a/pkg/kubelet/dockershim/docker_stats_windows.go b/pkg/kubelet/dockershim/docker_stats_windows.go index 510362dede7..646bfbd5aa1 100644 --- a/pkg/kubelet/dockershim/docker_stats_windows.go +++ b/pkg/kubelet/dockershim/docker_stats_windows.go @@ -33,7 +33,7 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont return nil, err } - hcsshim_container, err := hcsshim.OpenContainer(containerID) + hcsshimContainer, err := hcsshim.OpenContainer(containerID) if err != nil { // As we moved from using Docker stats to hcsshim directly, we may query HCS with already exited container IDs. // That will typically happen with init-containers in Exited state. Docker still knows about them but the HCS does not. @@ -44,13 +44,13 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont return nil, nil } defer func() { - closeErr := hcsshim_container.Close() + closeErr := hcsshimContainer.Close() if closeErr != nil { klog.Errorf("Error closing container '%s': %v", containerID, closeErr) } }() - stats, err := hcsshim_container.Statistics() + stats, err := hcsshimContainer.Statistics() if err != nil { return nil, err } diff --git a/pkg/kubelet/dockershim/exec.go b/pkg/kubelet/dockershim/exec.go index 0d864edfe3a..31e00bda18b 100644 --- a/pkg/kubelet/dockershim/exec.go +++ b/pkg/kubelet/dockershim/exec.go @@ -60,6 +60,7 @@ func (d *dockerExitError) ExitStatus() int { // NativeExecHandler executes commands in Docker containers using Docker's exec API. type NativeExecHandler struct{} +// ExecInContainer executes the cmd in container using the Docker's exec API func (*NativeExecHandler) ExecInContainer(client libdocker.Interface, container *dockertypes.ContainerJSON, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize, timeout time.Duration) error { done := make(chan struct{}) defer close(done) diff --git a/pkg/kubelet/dockershim/helpers.go b/pkg/kubelet/dockershim/helpers.go index b13dbceb889..33e30bffac0 100644 --- a/pkg/kubelet/dockershim/helpers.go +++ b/pkg/kubelet/dockershim/helpers.go @@ -33,7 +33,7 @@ import ( dockernat "github.com/docker/go-connections/nat" "k8s.io/klog/v2" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" utilerrors "k8s.io/apimachinery/pkg/util/errors" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" "k8s.io/kubernetes/pkg/credentialprovider" @@ -289,15 +289,15 @@ func recoverFromCreationConflictIfNeeded(client libdocker.Interface, createConfi id := matches[1] klog.Warningf("Unable to create pod sandbox due to conflict. Attempting to remove sandbox %q", id) - if rmErr := client.RemoveContainer(id, dockertypes.ContainerRemoveOptions{RemoveVolumes: true}); rmErr == nil { + rmErr := client.RemoveContainer(id, dockertypes.ContainerRemoveOptions{RemoveVolumes: true}) + if rmErr == nil { klog.V(2).Infof("Successfully removed conflicting container %q", id) return nil, err - } else { - klog.Errorf("Failed to remove the conflicting container %q: %v", id, rmErr) - // Return if the error is not container not found error. - if !libdocker.IsContainerNotFoundError(rmErr) { - return nil, err - } + } + klog.Errorf("Failed to remove the conflicting container %q: %v", id, rmErr) + // Return if the error is not container not found error. + if !libdocker.IsContainerNotFoundError(rmErr) { + return nil, err } // randomize the name to avoid conflict. diff --git a/pkg/kubelet/dockershim/helpers_linux.go b/pkg/kubelet/dockershim/helpers_linux.go index e18692150a5..d2296266292 100644 --- a/pkg/kubelet/dockershim/helpers_linux.go +++ b/pkg/kubelet/dockershim/helpers_linux.go @@ -30,10 +30,11 @@ import ( "github.com/blang/semver" dockertypes "github.com/docker/docker/api/types" dockercontainer "github.com/docker/docker/api/types/container" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" ) +// DefaultMemorySwap always returns 0 for no memory swap in a sandbox func DefaultMemorySwap() int64 { return 0 } diff --git a/pkg/kubelet/dockershim/helpers_unsupported.go b/pkg/kubelet/dockershim/helpers_unsupported.go index 09b2d491409..00c6efe79ec 100644 --- a/pkg/kubelet/dockershim/helpers_unsupported.go +++ b/pkg/kubelet/dockershim/helpers_unsupported.go @@ -27,6 +27,7 @@ import ( "k8s.io/klog/v2" ) +// DefaultMemorySwap always returns -1 for no memory swap in a sandbox func DefaultMemorySwap() int64 { return -1 } diff --git a/pkg/kubelet/dockershim/helpers_windows.go b/pkg/kubelet/dockershim/helpers_windows.go index e8681485f83..3a54bb7c4e5 100644 --- a/pkg/kubelet/dockershim/helpers_windows.go +++ b/pkg/kubelet/dockershim/helpers_windows.go @@ -32,6 +32,7 @@ import ( kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis" ) +// DefaultMemorySwap always returns 0 for no memory swap in a sandbox func DefaultMemorySwap() int64 { return 0 }