...

Source file src/k8s.io/kubernetes/pkg/kubelet/container/container_gc.go

Documentation: k8s.io/kubernetes/pkg/kubelet/container

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package container
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"k8s.io/klog/v2"
    25  )
    26  
    27  // GCPolicy specifies a policy for garbage collecting containers.
    28  type GCPolicy struct {
    29  	// Minimum age at which a container can be garbage collected, zero for no limit.
    30  	MinAge time.Duration
    31  
    32  	// Max number of dead containers any single pod (UID, container name) pair is
    33  	// allowed to have, less than zero for no limit.
    34  	MaxPerPodContainer int
    35  
    36  	// Max number of total dead containers, less than zero for no limit.
    37  	MaxContainers int
    38  }
    39  
    40  // GC manages garbage collection of dead containers.
    41  //
    42  // Implementation is thread-compatible.
    43  type GC interface {
    44  	// Garbage collect containers.
    45  	GarbageCollect(ctx context.Context) error
    46  	// Deletes all unused containers, including containers belonging to pods that are terminated but not deleted
    47  	DeleteAllUnusedContainers(ctx context.Context) error
    48  	// IsContainerFsSeparateFromImageFs tells if writeable layer and read-only layer are separate.
    49  	IsContainerFsSeparateFromImageFs(ctx context.Context) bool
    50  }
    51  
    52  // SourcesReadyProvider knows how to determine if configuration sources are ready
    53  type SourcesReadyProvider interface {
    54  	// AllReady returns true if the currently configured sources have all been seen.
    55  	AllReady() bool
    56  }
    57  
    58  // TODO(vmarmol): Preferentially remove pod infra containers.
    59  type realContainerGC struct {
    60  	// Container runtime
    61  	runtime Runtime
    62  
    63  	// Policy for garbage collection.
    64  	policy GCPolicy
    65  
    66  	// sourcesReadyProvider provides the readiness of kubelet configuration sources.
    67  	sourcesReadyProvider SourcesReadyProvider
    68  }
    69  
    70  // NewContainerGC creates a new instance of GC with the specified policy.
    71  func NewContainerGC(runtime Runtime, policy GCPolicy, sourcesReadyProvider SourcesReadyProvider) (GC, error) {
    72  	if policy.MinAge < 0 {
    73  		return nil, fmt.Errorf("invalid minimum garbage collection age: %v", policy.MinAge)
    74  	}
    75  
    76  	return &realContainerGC{
    77  		runtime:              runtime,
    78  		policy:               policy,
    79  		sourcesReadyProvider: sourcesReadyProvider,
    80  	}, nil
    81  }
    82  
    83  func (cgc *realContainerGC) GarbageCollect(ctx context.Context) error {
    84  	return cgc.runtime.GarbageCollect(ctx, cgc.policy, cgc.sourcesReadyProvider.AllReady(), false)
    85  }
    86  
    87  func (cgc *realContainerGC) DeleteAllUnusedContainers(ctx context.Context) error {
    88  	klog.InfoS("Attempting to delete unused containers")
    89  	return cgc.runtime.GarbageCollect(ctx, cgc.policy, cgc.sourcesReadyProvider.AllReady(), true)
    90  }
    91  
    92  func (cgc *realContainerGC) IsContainerFsSeparateFromImageFs(ctx context.Context) bool {
    93  	resp, err := cgc.runtime.ImageFsInfo(ctx)
    94  	if err != nil {
    95  		return false
    96  	}
    97  	// These fields can be empty if CRI implementation didn't populate.
    98  	if resp.ContainerFilesystems == nil || resp.ImageFilesystems == nil || len(resp.ContainerFilesystems) == 0 || len(resp.ImageFilesystems) == 0 {
    99  		return false
   100  	}
   101  	// KEP 4191 explains that multiple filesystems for images and containers is not
   102  	// supported at the moment.
   103  	// See https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/4191-split-image-filesystem#comment-on-future-extensions
   104  	// for work needed to support multiple filesystems.
   105  	if resp.ContainerFilesystems[0].FsId != nil && resp.ImageFilesystems[0].FsId != nil {
   106  		return resp.ContainerFilesystems[0].FsId.Mountpoint != resp.ImageFilesystems[0].FsId.Mountpoint
   107  	}
   108  	return false
   109  }
   110  

View as plain text