mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-20 01:23:48 +00:00
fix golint issues in pkg/kubelet/container
This commit is contained in:
@@ -49,7 +49,7 @@ func getTestPodIDAndStatus(numContainers int) (types.UID, *PodStatus) {
|
||||
status = &PodStatus{ID: id}
|
||||
}
|
||||
for i := 0; i < numContainers; i++ {
|
||||
status.ContainerStatuses = append(status.ContainerStatuses, &ContainerStatus{Name: string(i)})
|
||||
status.ContainerStatuses = append(status.ContainerStatuses, &Status{Name: string(i)})
|
||||
}
|
||||
return id, status
|
||||
}
|
||||
|
@@ -23,8 +23,8 @@ import (
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// ContainerGCPolicy specifies a policy for garbage collecting containers.
|
||||
type ContainerGCPolicy struct {
|
||||
// GCPolicy specifies a policy for garbage collecting containers.
|
||||
type GCPolicy struct {
|
||||
// Minimum age at which a container can be garbage collected, zero for no limit.
|
||||
MinAge time.Duration
|
||||
|
||||
@@ -36,10 +36,10 @@ type ContainerGCPolicy struct {
|
||||
MaxContainers int
|
||||
}
|
||||
|
||||
// ContainerGC manages garbage collection of dead containers.
|
||||
// GC manages garbage collection of dead containers.
|
||||
//
|
||||
// Implementation is thread-compatible.
|
||||
type ContainerGC interface {
|
||||
type GC interface {
|
||||
// Garbage collect containers.
|
||||
GarbageCollect() error
|
||||
// Deletes all unused containers, including containers belonging to pods that are terminated but not deleted
|
||||
@@ -58,14 +58,14 @@ type realContainerGC struct {
|
||||
runtime Runtime
|
||||
|
||||
// Policy for garbage collection.
|
||||
policy ContainerGCPolicy
|
||||
policy GCPolicy
|
||||
|
||||
// sourcesReadyProvider provides the readiness of kubelet configuration sources.
|
||||
sourcesReadyProvider SourcesReadyProvider
|
||||
}
|
||||
|
||||
// NewContainerGC creates a new instance of ContainerGC with the specified policy.
|
||||
func NewContainerGC(runtime Runtime, policy ContainerGCPolicy, sourcesReadyProvider SourcesReadyProvider) (ContainerGC, error) {
|
||||
// NewContainerGC creates a new instance of GC with the specified policy.
|
||||
func NewContainerGC(runtime Runtime, policy GCPolicy, sourcesReadyProvider SourcesReadyProvider) (GC, error) {
|
||||
if policy.MinAge < 0 {
|
||||
return nil, fmt.Errorf("invalid minimum garbage collection age: %v", policy.MinAge)
|
||||
}
|
||||
|
@@ -107,9 +107,9 @@ func HashContainer(container *v1.Container) uint64 {
|
||||
return uint64(hash.Sum32())
|
||||
}
|
||||
|
||||
// EnvVarsToMap constructs a map of environment name to value from a slice
|
||||
// envVarsToMap constructs a map of environment name to value from a slice
|
||||
// of env vars.
|
||||
func EnvVarsToMap(envs []EnvVar) map[string]string {
|
||||
func envVarsToMap(envs []EnvVar) map[string]string {
|
||||
result := map[string]string{}
|
||||
for _, env := range envs {
|
||||
result[env.Name] = env.Value
|
||||
@@ -117,9 +117,9 @@ func EnvVarsToMap(envs []EnvVar) map[string]string {
|
||||
return result
|
||||
}
|
||||
|
||||
// V1EnvVarsToMap constructs a map of environment name to value from a slice
|
||||
// v1EnvVarsToMap constructs a map of environment name to value from a slice
|
||||
// of env vars.
|
||||
func V1EnvVarsToMap(envs []v1.EnvVar) map[string]string {
|
||||
func v1EnvVarsToMap(envs []v1.EnvVar) map[string]string {
|
||||
result := map[string]string{}
|
||||
for _, env := range envs {
|
||||
result[env.Name] = env.Value
|
||||
@@ -132,7 +132,7 @@ func V1EnvVarsToMap(envs []v1.EnvVar) map[string]string {
|
||||
// container environment definitions. This does *not* include valueFrom substitutions.
|
||||
// TODO: callers should use ExpandContainerCommandAndArgs with a fully resolved list of environment.
|
||||
func ExpandContainerCommandOnlyStatic(containerCommand []string, envs []v1.EnvVar) (command []string) {
|
||||
mapping := expansion.MappingFuncFor(V1EnvVarsToMap(envs))
|
||||
mapping := expansion.MappingFuncFor(v1EnvVarsToMap(envs))
|
||||
if len(containerCommand) != 0 {
|
||||
for _, cmd := range containerCommand {
|
||||
command = append(command, expansion.Expand(cmd, mapping))
|
||||
@@ -144,7 +144,7 @@ func ExpandContainerCommandOnlyStatic(containerCommand []string, envs []v1.EnvVa
|
||||
// ExpandContainerVolumeMounts expands the subpath of the given VolumeMount by replacing variable references with the values of given EnvVar.
|
||||
func ExpandContainerVolumeMounts(mount v1.VolumeMount, envs []EnvVar) (string, error) {
|
||||
|
||||
envmap := EnvVarsToMap(envs)
|
||||
envmap := envVarsToMap(envs)
|
||||
missingKeys := sets.NewString()
|
||||
expanded := expansion.Expand(mount.SubPathExpr, func(key string) string {
|
||||
value, ok := envmap[key]
|
||||
@@ -162,7 +162,7 @@ func ExpandContainerVolumeMounts(mount v1.VolumeMount, envs []EnvVar) (string, e
|
||||
|
||||
// ExpandContainerCommandAndArgs expands the given Container's command by replacing variable references `with the values of given EnvVar.
|
||||
func ExpandContainerCommandAndArgs(container *v1.Container, envs []EnvVar) (command []string, args []string) {
|
||||
mapping := expansion.MappingFuncFor(EnvVarsToMap(envs))
|
||||
mapping := expansion.MappingFuncFor(envVarsToMap(envs))
|
||||
|
||||
if len(container.Command) != 0 {
|
||||
for _, cmd := range container.Command {
|
||||
@@ -262,11 +262,11 @@ func ConvertPodStatusToRunningPod(runtimeName string, podStatus *PodStatus) Pod
|
||||
}
|
||||
|
||||
// SandboxToContainerState converts runtimeapi.PodSandboxState to
|
||||
// kubecontainer.ContainerState.
|
||||
// kubecontainer.State.
|
||||
// This is only needed because we need to return sandboxes as if they were
|
||||
// kubecontainer.Containers to avoid substantial changes to PLEG.
|
||||
// TODO: Remove this once it becomes obsolete.
|
||||
func SandboxToContainerState(state runtimeapi.PodSandboxState) ContainerState {
|
||||
func SandboxToContainerState(state runtimeapi.PodSandboxState) State {
|
||||
switch state {
|
||||
case runtimeapi.PodSandboxState_SANDBOX_READY:
|
||||
return ContainerStateRunning
|
||||
|
@@ -24,7 +24,7 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||
@@ -43,7 +43,7 @@ func TestEnvVarsToMap(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
varMap := EnvVarsToMap(vars)
|
||||
varMap := envVarsToMap(vars)
|
||||
|
||||
if e, a := len(vars), len(varMap); e != a {
|
||||
t.Errorf("Unexpected map length; expected: %d, got %d", e, a)
|
||||
@@ -414,7 +414,7 @@ func TestShouldContainerBeRestarted(t *testing.T) {
|
||||
ID: pod.UID,
|
||||
Name: pod.Name,
|
||||
Namespace: pod.Namespace,
|
||||
ContainerStatuses: []*ContainerStatus{
|
||||
ContainerStatuses: []*Status{
|
||||
{
|
||||
Name: "alive",
|
||||
State: ContainerStateRunning,
|
||||
|
@@ -94,7 +94,7 @@ type Runtime interface {
|
||||
// If evictNonDeletedPods is set to true, containers and sandboxes belonging to pods
|
||||
// that are terminated, but not deleted will be evicted. Otherwise, only deleted pods will be GC'd.
|
||||
// TODO: Revisit this method and make it cleaner.
|
||||
GarbageCollect(gcPolicy ContainerGCPolicy, allSourcesReady bool, evictNonDeletedPods bool) error
|
||||
GarbageCollect(gcPolicy GCPolicy, allSourcesReady bool, evictNonDeletedPods bool) error
|
||||
// Syncs the running pod into the desired pod.
|
||||
SyncPod(pod *v1.Pod, podStatus *PodStatus, pullSecrets []v1.Secret, backOff *flowcontrol.Backoff) PodSyncResult
|
||||
// KillPod kills all the containers of a pod. Pod may be nil, running pod must not be.
|
||||
@@ -147,13 +147,13 @@ type ImageService interface {
|
||||
ImageStats() (*ImageStats, error)
|
||||
}
|
||||
|
||||
// ContainerAttacher interface allows to attach a container.
|
||||
type ContainerAttacher interface {
|
||||
// Attacher interface allows to attach a container.
|
||||
type Attacher interface {
|
||||
AttachContainer(id ContainerID, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize) (err error)
|
||||
}
|
||||
|
||||
// ContainerCommandRunner interface allows to run command in a container.
|
||||
type ContainerCommandRunner interface {
|
||||
// CommandRunner interface allows to run command in a container.
|
||||
type CommandRunner interface {
|
||||
// RunInContainer synchronously executes the command in the container, and returns the output.
|
||||
// If the command completes with a non-0 exit code, a k8s.io/utils/exec.ExitError will be returned.
|
||||
RunInContainer(id ContainerID, cmd []string, timeout time.Duration) ([]byte, error)
|
||||
@@ -250,18 +250,18 @@ func (id DockerID) ContainerID() ContainerID {
|
||||
}
|
||||
}
|
||||
|
||||
// ContainerState represents the state of a container
|
||||
type ContainerState string
|
||||
// State represents the state of a container
|
||||
type State string
|
||||
|
||||
const (
|
||||
// ContainerStateCreated indicates a container that has been created (e.g. with docker create) but not started.
|
||||
ContainerStateCreated ContainerState = "created"
|
||||
ContainerStateCreated State = "created"
|
||||
// ContainerStateRunning indicates a currently running container.
|
||||
ContainerStateRunning ContainerState = "running"
|
||||
ContainerStateRunning State = "running"
|
||||
// ContainerStateExited indicates a container that ran and completed ("stopped" in other contexts, although a created container is technically also "stopped").
|
||||
ContainerStateExited ContainerState = "exited"
|
||||
ContainerStateExited State = "exited"
|
||||
// ContainerStateUnknown encompasses all the states that we currently don't care about (like restarting, paused, dead).
|
||||
ContainerStateUnknown ContainerState = "unknown"
|
||||
ContainerStateUnknown State = "unknown"
|
||||
)
|
||||
|
||||
// Container provides the runtime information for a container, such as ID, hash,
|
||||
@@ -282,7 +282,7 @@ type Container struct {
|
||||
// not managed by kubelet.
|
||||
Hash uint64
|
||||
// State is the state of the container.
|
||||
State ContainerState
|
||||
State State
|
||||
}
|
||||
|
||||
// PodStatus represents the status of the pod and its containers.
|
||||
@@ -297,20 +297,20 @@ type PodStatus struct {
|
||||
// All IPs assigned to this pod
|
||||
IPs []string
|
||||
// Status of containers in the pod.
|
||||
ContainerStatuses []*ContainerStatus
|
||||
ContainerStatuses []*Status
|
||||
// Status of the pod sandbox.
|
||||
// Only for kuberuntime now, other runtime may keep it nil.
|
||||
SandboxStatuses []*runtimeapi.PodSandboxStatus
|
||||
}
|
||||
|
||||
// ContainerStatus represents the status of a container.
|
||||
type ContainerStatus struct {
|
||||
// Status represents the status of a container.
|
||||
type Status struct {
|
||||
// ID of the container.
|
||||
ID ContainerID
|
||||
// Name of the container.
|
||||
Name string
|
||||
// Status of the container.
|
||||
State ContainerState
|
||||
State State
|
||||
// Creation time of the container.
|
||||
CreatedAt time.Time
|
||||
// Start time of the container.
|
||||
@@ -337,7 +337,7 @@ type ContainerStatus struct {
|
||||
|
||||
// FindContainerStatusByName returns container status in the pod status with the given name.
|
||||
// When there are multiple containers' statuses with the same name, the first match will be returned.
|
||||
func (podStatus *PodStatus) FindContainerStatusByName(containerName string) *ContainerStatus {
|
||||
func (podStatus *PodStatus) FindContainerStatusByName(containerName string) *Status {
|
||||
for _, containerStatus := range podStatus.ContainerStatuses {
|
||||
if containerStatus.Name == containerName {
|
||||
return containerStatus
|
||||
@@ -347,8 +347,8 @@ func (podStatus *PodStatus) FindContainerStatusByName(containerName string) *Con
|
||||
}
|
||||
|
||||
// GetRunningContainerStatuses returns container status of all the running containers in a pod
|
||||
func (podStatus *PodStatus) GetRunningContainerStatuses() []*ContainerStatus {
|
||||
runningContainerStatuses := []*ContainerStatus{}
|
||||
func (podStatus *PodStatus) GetRunningContainerStatuses() []*Status {
|
||||
runningContainerStatuses := []*Status{}
|
||||
for _, containerStatus := range podStatus.ContainerStatuses {
|
||||
if containerStatus.State == ContainerStateRunning {
|
||||
runningContainerStatuses = append(runningContainerStatuses, containerStatus)
|
||||
@@ -643,7 +643,7 @@ func ParsePodFullName(podFullName string) (string, string, error) {
|
||||
type Option func(Runtime)
|
||||
|
||||
// SortContainerStatusesByCreationTime sorts the container statuses by creation time.
|
||||
type SortContainerStatusesByCreationTime []*ContainerStatus
|
||||
type SortContainerStatusesByCreationTime []*Status
|
||||
|
||||
func (s SortContainerStatusesByCreationTime) Len() int { return len(s) }
|
||||
func (s SortContainerStatusesByCreationTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
@@ -39,10 +39,6 @@ var (
|
||||
ErrRunContainer = errors.New("RunContainerError")
|
||||
// ErrKillContainer returned when runtime failed to kill any of pod's containers.
|
||||
ErrKillContainer = errors.New("KillContainerError")
|
||||
// ErrVerifyNonRoot returned if the container or image will run as the root user.
|
||||
ErrVerifyNonRoot = errors.New("VerifyNonRootError")
|
||||
// ErrRunInitContainer returned when container init failed.
|
||||
ErrRunInitContainer = errors.New("RunInitContainerError")
|
||||
// ErrCreatePodSandbox returned when runtime failed to create a sandbox for pod.
|
||||
ErrCreatePodSandbox = errors.New("CreatePodSandboxError")
|
||||
// ErrConfigPodSandbox returned when runetime failed to get pod sandbox config from pod.
|
||||
@@ -51,13 +47,6 @@ var (
|
||||
ErrKillPodSandbox = errors.New("KillPodSandboxError")
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrSetupNetwork returned when network setup failed.
|
||||
ErrSetupNetwork = errors.New("SetupNetworkError")
|
||||
// ErrTeardownNetwork returned when network tear down failed.
|
||||
ErrTeardownNetwork = errors.New("TeardownNetworkError")
|
||||
)
|
||||
|
||||
// SyncAction indicates different kind of actions in SyncPod() and KillPod(). Now there are only actions
|
||||
// about start/kill container and setup/teardown network.
|
||||
type SyncAction string
|
||||
|
@@ -348,7 +348,7 @@ func (f *FakeRuntime) RemoveImage(image kubecontainer.ImageSpec) error {
|
||||
return f.Err
|
||||
}
|
||||
|
||||
func (f *FakeRuntime) GarbageCollect(gcPolicy kubecontainer.ContainerGCPolicy, ready bool, evictNonDeletedPods bool) error {
|
||||
func (f *FakeRuntime) GarbageCollect(gcPolicy kubecontainer.GCPolicy, ready bool, evictNonDeletedPods bool) error {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
|
||||
@@ -406,7 +406,7 @@ type FakeContainerCommandRunner struct {
|
||||
Cmd []string
|
||||
}
|
||||
|
||||
var _ kubecontainer.ContainerCommandRunner = &FakeContainerCommandRunner{}
|
||||
var _ kubecontainer.CommandRunner = &FakeContainerCommandRunner{}
|
||||
|
||||
func (f *FakeContainerCommandRunner) RunInContainer(containerID kubecontainer.ContainerID, cmd []string, timeout time.Duration) ([]byte, error) {
|
||||
// record invoked values
|
||||
|
@@ -137,7 +137,7 @@ func (r *Mock) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWr
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (r *Mock) GarbageCollect(gcPolicy kubecontainer.ContainerGCPolicy, ready bool, evictNonDeletedPods bool) error {
|
||||
func (r *Mock) GarbageCollect(gcPolicy kubecontainer.GCPolicy, ready bool, evictNonDeletedPods bool) error {
|
||||
args := r.Called(gcPolicy, ready, evictNonDeletedPods)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
Reference in New Issue
Block a user