mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #91181 from zvier/master
fix golint errors for `pkg/kubelet/dockershim`
This commit is contained in:
commit
5ec83bc973
@ -100,7 +100,6 @@ pkg/kubelet/cm
|
|||||||
pkg/kubelet/container
|
pkg/kubelet/container
|
||||||
pkg/kubelet/container/testing
|
pkg/kubelet/container/testing
|
||||||
pkg/kubelet/cri/remote
|
pkg/kubelet/cri/remote
|
||||||
pkg/kubelet/dockershim
|
|
||||||
pkg/kubelet/dockershim/libdocker
|
pkg/kubelet/dockershim/libdocker
|
||||||
pkg/kubelet/dockershim/network
|
pkg/kubelet/dockershim/network
|
||||||
pkg/kubelet/dockershim/network/cni/testing
|
pkg/kubelet/dockershim/network/cni/testing
|
||||||
|
@ -166,7 +166,7 @@ func containerToRuntimeAPISandbox(c *dockertypes.Container) (*runtimeapi.PodSand
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkpointToRuntimeAPISandbox(id string, checkpoint DockershimCheckpoint) *runtimeapi.PodSandbox {
|
func checkpointToRuntimeAPISandbox(id string, checkpoint ContainerCheckpoint) *runtimeapi.PodSandbox {
|
||||||
state := runtimeapi.PodSandboxState_SANDBOX_NOTREADY
|
state := runtimeapi.PodSandboxState_SANDBOX_NOTREADY
|
||||||
_, name, namespace, _, _ := checkpoint.GetData()
|
_, name, namespace, _, _ := checkpoint.GetData()
|
||||||
return &runtimeapi.PodSandbox{
|
return &runtimeapi.PodSandbox{
|
||||||
|
@ -16,5 +16,6 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
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
|
// Docker integration using k8s.io/cri-api/pkg/apis/runtime/v1alpha2/api.pb.go
|
||||||
package dockershim
|
package dockershim
|
||||||
|
@ -34,11 +34,13 @@ const (
|
|||||||
schemaVersion = "v1"
|
schemaVersion = "v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DockershimCheckpoint interface {
|
// ContainerCheckpoint provides the interface for process container's checkpoint data
|
||||||
|
type ContainerCheckpoint interface {
|
||||||
checkpointmanager.Checkpoint
|
checkpointmanager.Checkpoint
|
||||||
GetData() (string, string, string, []*PortMapping, bool)
|
GetData() (string, string, string, []*PortMapping, bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Protocol is the type of port mapping protocol
|
||||||
type Protocol string
|
type Protocol string
|
||||||
|
|
||||||
// PortMapping is the port mapping configurations of a sandbox.
|
// PortMapping is the port mapping configurations of a sandbox.
|
||||||
@ -73,7 +75,8 @@ type PodSandboxCheckpoint struct {
|
|||||||
Checksum checksum.Checksum `json:"checksum"`
|
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{
|
return &PodSandboxCheckpoint{
|
||||||
Version: schemaVersion,
|
Version: schemaVersion,
|
||||||
Namespace: namespace,
|
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) {
|
func (cp *PodSandboxCheckpoint) MarshalCheckpoint() ([]byte, error) {
|
||||||
cp.Checksum = checksum.New(*cp.Data)
|
cp.Checksum = checksum.New(*cp.Data)
|
||||||
return json.Marshal(*cp)
|
return json.Marshal(*cp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalCheckpoint decodes the blob data to the PodSandboxCheckpoint instance
|
||||||
func (cp *PodSandboxCheckpoint) UnmarshalCheckpoint(blob []byte) error {
|
func (cp *PodSandboxCheckpoint) UnmarshalCheckpoint(blob []byte) error {
|
||||||
return json.Unmarshal(blob, cp)
|
return json.Unmarshal(blob, cp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VerifyChecksum verifies whether the PodSandboxCheckpoint's data checksum is
|
||||||
|
// the same as calculated checksum
|
||||||
func (cp *PodSandboxCheckpoint) VerifyChecksum() error {
|
func (cp *PodSandboxCheckpoint) VerifyChecksum() error {
|
||||||
return cp.Checksum.Verify(*cp.Data)
|
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) {
|
func (cp *PodSandboxCheckpoint) GetData() (string, string, string, []*PortMapping, bool) {
|
||||||
return cp.Version, cp.Name, cp.Namespace, cp.Data.PortMappings, cp.Data.HostNetwork
|
return cp.Version, cp.Name, cp.Namespace, cp.Data.PortMappings, cp.Data.HostNetwork
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,11 @@ import (
|
|||||||
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
|
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sandboxID = "sandboxid"
|
||||||
|
containerID = "containerid"
|
||||||
|
)
|
||||||
|
|
||||||
// A helper to create a basic config.
|
// A helper to create a basic config.
|
||||||
func makeContainerConfig(sConfig *runtimeapi.PodSandboxConfig, name, image string, attempt uint32, labels, annotations map[string]string) *runtimeapi.ContainerConfig {
|
func makeContainerConfig(sConfig *runtimeapi.PodSandboxConfig, name, image string, attempt uint32, labels, annotations map[string]string) *runtimeapi.ContainerConfig {
|
||||||
return &runtimeapi.ContainerConfig{
|
return &runtimeapi.ContainerConfig{
|
||||||
@ -145,9 +150,8 @@ func TestContainerStatus(t *testing.T) {
|
|||||||
// Create the container.
|
// Create the container.
|
||||||
fClock.SetTime(time.Now().Add(-1 * time.Hour))
|
fClock.SetTime(time.Now().Add(-1 * time.Hour))
|
||||||
expected.CreatedAt = fClock.Now().UnixNano()
|
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)
|
createResp, err := ds.CreateContainer(getTestCTX(), req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
id := createResp.ContainerId
|
id := createResp.ContainerId
|
||||||
@ -156,7 +160,7 @@ func TestContainerStatus(t *testing.T) {
|
|||||||
c, err := fDocker.InspectContainer(id)
|
c, err := fDocker.InspectContainer(id)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, c.Config.Labels[containerTypeLabelKey], containerTypeLabelContainer)
|
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.
|
// Set the id manually since we don't know the id until it's created.
|
||||||
expected.Id = id
|
expected.Id = id
|
||||||
@ -207,8 +211,7 @@ func TestContainerLogPath(t *testing.T) {
|
|||||||
config := makeContainerConfig(sConfig, "pause", "iamimage", 0, nil, nil)
|
config := makeContainerConfig(sConfig, "pause", "iamimage", 0, nil, nil)
|
||||||
config.LogPath = containerLogPath
|
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)
|
createResp, err := ds.CreateContainer(getTestCTX(), req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
id := createResp.ContainerId
|
id := createResp.ContainerId
|
||||||
@ -248,11 +251,9 @@ func TestContainerCreationConflict(t *testing.T) {
|
|||||||
sConfig := makeSandboxConfig("foo", "bar", "1", 0)
|
sConfig := makeSandboxConfig("foo", "bar", "1", 0)
|
||||||
config := makeContainerConfig(sConfig, "pause", "iamimage", 0, map[string]string{}, map[string]string{})
|
config := makeContainerConfig(sConfig, "pause", "iamimage", 0, map[string]string{}, map[string]string{})
|
||||||
containerName := makeContainerName(sConfig, config)
|
containerName := makeContainerName(sConfig, config)
|
||||||
const sandboxId = "sandboxid"
|
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",
|
||||||
const containerId = "containerid"
|
containerName, 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.",
|
noContainerError := fmt.Errorf("Error response from daemon: No such container: %s", containerID)
|
||||||
containerName, containerId)
|
|
||||||
noContainerError := fmt.Errorf("Error response from daemon: No such container: %s", containerId)
|
|
||||||
randomError := fmt.Errorf("random error")
|
randomError := fmt.Errorf("random error")
|
||||||
|
|
||||||
for desc, test := range map[string]struct {
|
for desc, test := range map[string]struct {
|
||||||
@ -299,7 +300,7 @@ func TestContainerCreationConflict(t *testing.T) {
|
|||||||
fDocker.InjectError("remove", test.removeError)
|
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)
|
createResp, err := ds.CreateContainer(getTestCTX(), req)
|
||||||
require.Equal(t, test.expectError, err)
|
require.Equal(t, test.expectError, err)
|
||||||
assert.NoError(t, fDocker.AssertCalls(test.expectCalls))
|
assert.NoError(t, fDocker.AssertCalls(test.expectCalls))
|
||||||
|
@ -67,7 +67,7 @@ func dirSize(path string) (int64, int64, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
inodes += 1
|
inodes++
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
bytes += info.Size()
|
bytes += info.Size()
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ func (d *dockerService) GetContainerLogs(_ context.Context, pod *v1.Pod, contain
|
|||||||
// GetContainerLogTail attempts to read up to MaxContainerTerminationMessageLogLength
|
// 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.
|
// from the end of the log when docker is configured with a log driver other than json-log.
|
||||||
// It reads up to MaxContainerTerminationMessageLogLines lines.
|
// 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)
|
value := int64(kubecontainer.MaxContainerTerminationMessageLogLines)
|
||||||
buf, _ := circbuf.NewBuffer(kubecontainer.MaxContainerTerminationMessageLogLength)
|
buf, _ := circbuf.NewBuffer(kubecontainer.MaxContainerTerminationMessageLogLength)
|
||||||
// Although this is not a full spec pod, dockerLegacyService.GetContainerLogs() currently completely ignores its pod param
|
// 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,
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (ds *dockerService) getContainerStats(containerID string) (*runtimeapi.Cont
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hcsshim_container, err := hcsshim.OpenContainer(containerID)
|
hcsshimContainer, err := hcsshim.OpenContainer(containerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// As we moved from using Docker stats to hcsshim directly, we may query HCS with already exited container IDs.
|
// 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.
|
// 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
|
return nil, nil
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
closeErr := hcsshim_container.Close()
|
closeErr := hcsshimContainer.Close()
|
||||||
if closeErr != nil {
|
if closeErr != nil {
|
||||||
klog.Errorf("Error closing container '%s': %v", containerID, closeErr)
|
klog.Errorf("Error closing container '%s': %v", containerID, closeErr)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
stats, err := hcsshim_container.Statistics()
|
stats, err := hcsshimContainer.Statistics()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ func (d *dockerExitError) ExitStatus() int {
|
|||||||
// NativeExecHandler executes commands in Docker containers using Docker's exec API.
|
// NativeExecHandler executes commands in Docker containers using Docker's exec API.
|
||||||
type NativeExecHandler struct{}
|
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 {
|
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{})
|
done := make(chan struct{})
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
@ -289,16 +289,16 @@ func recoverFromCreationConflictIfNeeded(client libdocker.Interface, createConfi
|
|||||||
|
|
||||||
id := matches[1]
|
id := matches[1]
|
||||||
klog.Warningf("Unable to create pod sandbox due to conflict. Attempting to remove sandbox %q", id)
|
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)
|
klog.V(2).Infof("Successfully removed conflicting container %q", id)
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
}
|
||||||
klog.Errorf("Failed to remove the conflicting container %q: %v", id, rmErr)
|
klog.Errorf("Failed to remove the conflicting container %q: %v", id, rmErr)
|
||||||
// Return if the error is not container not found error.
|
// Return if the error is not container not found error.
|
||||||
if !libdocker.IsContainerNotFoundError(rmErr) {
|
if !libdocker.IsContainerNotFoundError(rmErr) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// randomize the name to avoid conflict.
|
// randomize the name to avoid conflict.
|
||||||
createConfig.Name = randomizeName(createConfig.Name)
|
createConfig.Name = randomizeName(createConfig.Name)
|
||||||
|
@ -34,6 +34,7 @@ import (
|
|||||||
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
|
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultMemorySwap always returns 0 for no memory swap in a sandbox
|
||||||
func DefaultMemorySwap() int64 {
|
func DefaultMemorySwap() int64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultMemorySwap always returns -1 for no memory swap in a sandbox
|
||||||
func DefaultMemorySwap() int64 {
|
func DefaultMemorySwap() int64 {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ import (
|
|||||||
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
|
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultMemorySwap always returns 0 for no memory swap in a sandbox
|
||||||
func DefaultMemorySwap() int64 {
|
func DefaultMemorySwap() int64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user