Commit 14940eda authored by Random-Liu's avatar Random-Liu

Add legacy container cleanup

parent c84f3abc
...@@ -75,6 +75,15 @@ func (ds *dockerService) ListContainers(filter *runtimeapi.ContainerFilter) ([]* ...@@ -75,6 +75,15 @@ func (ds *dockerService) ListContainers(filter *runtimeapi.ContainerFilter) ([]*
result = append(result, converted) result = append(result, converted)
} }
// Include legacy containers if there are still legacy containers not cleaned up yet.
if !ds.legacyCleanup.Done() {
legacyContainers, err := ds.ListLegacyContainers(filter)
if err != nil {
return nil, err
}
// Legacy containers are always older, so we can safely append them to the end.
result = append(result, legacyContainers...)
}
return result, nil return result, nil
} }
...@@ -360,6 +369,15 @@ func (ds *dockerService) ContainerStatus(containerID string) (*runtimeapi.Contai ...@@ -360,6 +369,15 @@ func (ds *dockerService) ContainerStatus(containerID string) (*runtimeapi.Contai
ct, st, ft := createdAt.UnixNano(), startedAt.UnixNano(), finishedAt.UnixNano() ct, st, ft := createdAt.UnixNano(), startedAt.UnixNano(), finishedAt.UnixNano()
exitCode := int32(r.State.ExitCode) exitCode := int32(r.State.ExitCode)
// If the container has no containerTypeLabelKey label, treat it as a legacy container.
if _, ok := r.Config.Labels[containerTypeLabelKey]; !ok {
names, labels, err := convertLegacyNameAndLabels([]string{r.Name}, r.Config.Labels)
if err != nil {
return nil, err
}
r.Name, r.Config.Labels = names[0], labels
}
metadata, err := parseContainerName(r.Name) metadata, err := parseContainerName(r.Name)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -244,12 +244,23 @@ func (ds *dockerService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodS ...@@ -244,12 +244,23 @@ func (ds *dockerService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodS
} }
network := &runtimeapi.PodSandboxNetworkStatus{Ip: IP} network := &runtimeapi.PodSandboxNetworkStatus{Ip: IP}
netNS := getNetworkNamespace(r) netNS := getNetworkNamespace(r)
hostNetwork := sharesHostNetwork(r)
// If the sandbox has no containerTypeLabelKey label, treat it as a legacy sandbox.
if _, ok := r.Config.Labels[containerTypeLabelKey]; !ok {
names, labels, err := convertLegacyNameAndLabels([]string{r.Name}, r.Config.Labels)
if err != nil {
return nil, err
}
r.Name, r.Config.Labels = names[0], labels
// Forcibly trigger infra container restart.
hostNetwork = !hostNetwork
}
metadata, err := parseSandboxName(r.Name) metadata, err := parseSandboxName(r.Name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
hostNetwork := sharesHostNetwork(r)
labels, annotations := extractLabels(r.Config.Labels) labels, annotations := extractLabels(r.Config.Labels)
return &runtimeapi.PodSandboxStatus{ return &runtimeapi.PodSandboxStatus{
Id: r.ID, Id: r.ID,
...@@ -318,7 +329,7 @@ func (ds *dockerService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([] ...@@ -318,7 +329,7 @@ func (ds *dockerService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]
c := containers[i] c := containers[i]
converted, err := containerToRuntimeAPISandbox(&c) converted, err := containerToRuntimeAPISandbox(&c)
if err != nil { if err != nil {
glog.V(4).Infof("Unable to convert docker to runtime API sandbox: %v", err) glog.V(4).Infof("Unable to convert docker to runtime API sandbox %+v: %v", c, err)
continue continue
} }
if filterOutReadySandboxes && converted.State == runtimeapi.PodSandboxState_SANDBOX_READY { if filterOutReadySandboxes && converted.State == runtimeapi.PodSandboxState_SANDBOX_READY {
...@@ -353,6 +364,16 @@ func (ds *dockerService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([] ...@@ -353,6 +364,16 @@ func (ds *dockerService) ListPodSandbox(filter *runtimeapi.PodSandboxFilter) ([]
result = append(result, checkpointToRuntimeAPISandbox(id, checkpoint)) result = append(result, checkpointToRuntimeAPISandbox(id, checkpoint))
} }
} }
// Include legacy sandboxes if there are still legacy sandboxes not cleaned up yet.
if !ds.legacyCleanup.Done() {
legacySandboxes, err := ds.ListLegacyPodSandbox(filter)
if err != nil {
return nil, err
}
// Legacy sandboxes are always older, so we can safely append them to the end.
result = append(result, legacySandboxes...)
}
return result, nil return result, nil
} }
......
...@@ -153,7 +153,6 @@ func NewDockerService(client dockertools.DockerInterface, seccompProfileRoot str ...@@ -153,7 +153,6 @@ func NewDockerService(client dockertools.DockerInterface, seccompProfileRoot str
glog.Infof("Setting cgroupDriver to %s", cgroupDriver) glog.Infof("Setting cgroupDriver to %s", cgroupDriver)
} }
ds.cgroupDriver = cgroupDriver ds.cgroupDriver = cgroupDriver
return ds, nil return ds, nil
} }
...@@ -179,6 +178,8 @@ type dockerService struct { ...@@ -179,6 +178,8 @@ type dockerService struct {
// cgroup driver used by Docker runtime. // cgroup driver used by Docker runtime.
cgroupDriver string cgroupDriver string
checkpointHandler CheckpointHandler checkpointHandler CheckpointHandler
// legacyCleanup indicates whether legacy cleanup has finished or not.
legacyCleanup legacyCleanupFlag
} }
// Version returns the runtime name, runtime version and runtime API version // Version returns the runtime name, runtime version and runtime API version
...@@ -241,6 +242,8 @@ type dockerNetworkHost struct { ...@@ -241,6 +242,8 @@ type dockerNetworkHost struct {
// Start initializes and starts components in dockerService. // Start initializes and starts components in dockerService.
func (ds *dockerService) Start() error { func (ds *dockerService) Start() error {
// Initialize the legacy cleanup flag.
ds.LegacyCleanupInit()
return ds.containerManager.Start() return ds.containerManager.Start()
} }
......
...@@ -76,7 +76,6 @@ func makeContainerName(s *runtimeapi.PodSandboxConfig, c *runtimeapi.ContainerCo ...@@ -76,7 +76,6 @@ func makeContainerName(s *runtimeapi.PodSandboxConfig, c *runtimeapi.ContainerCo
s.Metadata.Uid, // 4 sandbox uid s.Metadata.Uid, // 4 sandbox uid
fmt.Sprintf("%d", c.Metadata.Attempt), // 5 fmt.Sprintf("%d", c.Metadata.Attempt), // 5
}, nameDelimiter) }, nameDelimiter)
} }
// randomizeName randomizes the container name. This should only be used when we hit the // randomizeName randomizes the container name. This should only be used when we hit the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment