Skip to content

Commit

Permalink
refactor: container meta (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzchaoo authored Apr 12, 2024
1 parent 13d4596 commit 2c02379
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
14 changes: 7 additions & 7 deletions pkg/cri/impl/default_cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,11 @@ func (e *defaultCri) buildCriContainer(criPod *cri.Pod, dc *cri.EngineDetailCont
criContainer.Tz.EnvTz = criContainer.Env["TZ"]

if dc.IsSandbox {
criContainer.Sandbox = true
criContainer.ContainerRole = cri.ContainerRoleSandbox
} else if e.isSidecar(criContainer) {
criContainer.Sidecar = true
criContainer.ContainerRole = cri.ContainerRoleSidecar
} else {
criContainer.MainBiz = true
criContainer.ContainerRole = cri.ContainerRoleBiz
}

if !dc.IsSandbox {
Expand Down Expand Up @@ -606,7 +606,7 @@ func (e *defaultCri) buildCriContainer(criPod *cri.Pod, dc *cri.EngineDetailCont

criPod.All = append(criPod.All, criContainer)

if criContainer.IsRunning() && criContainer.Hacked == cri.HackInit && criContainer.MainBiz {
if criContainer.IsRunning() && criContainer.Hacked == cri.HackInit && criContainer.IsBiz() {
criContainer.Hacked = cri.HackIng

var err error
Expand Down Expand Up @@ -935,7 +935,7 @@ func (e *defaultCri) buildPod(pod *v1.Pod, oldState *internalState, newState *in
if cached != nil && !isContainerChanged(cached.engineContainer, container) {
cached.criContainer.Pod = criPod

if forceUpdateContainerInfo && cached.criContainer.MainBiz {
if forceUpdateContainerInfo && cached.criContainer.IsBiz() {
go e.updateZombieCheck(cached.criContainer)
}

Expand All @@ -962,9 +962,9 @@ func (e *defaultCri) buildPod(pod *v1.Pod, oldState *internalState, newState *in
}

criPod.All = append(criPod.All, cached.criContainer)
if cached.criContainer.Sandbox {
if cached.criContainer.IsSandbox() {
criPod.Sandbox = cached.criContainer
} else if cached.criContainer.Sidecar {
} else if cached.criContainer.IsSidecar() {
criPod.Sidecar = append(criPod.Sidecar, cached.criContainer)
} else {
criPod.Biz = append(criPod.Biz, cached.criContainer)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cri/impl/default_cri_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (e *defaultCri) registerHttpHandlers() {
ret := make([]string, 0)
for _, pod := range state.pods {
for _, container := range pod.All {
if container.Sandbox {
if container.IsSandbox() {
continue
}
if container.Hacked == 1 || container.Hacked == 5 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cri/impl/engine/docker_oom_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (m *OOMManager) listenDockerLoop() {

func (m *OOMManager) handleOOM(msg events.Message) {
ctr, ok := m.CRI.GetContainerByCid(msg.ID)
if !ok || ctr.Sandbox {
if !ok || ctr.IsSandbox() {
// When oom, container and its sandbox all emit oom
return
}
Expand Down
32 changes: 25 additions & 7 deletions pkg/cri/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ const (
AlpineStatusNo
)

const (
// ContainerRoleBiz The target container is the pod's biz(main) container.
ContainerRoleBiz ContainerRole = "biz"
// ContainerRoleSidecar The target container is the pod's sidecar.
ContainerRoleSidecar ContainerRole = "sidecar"
// ContainerRoleSandbox The target container is the pod's sandbox.
ContainerRoleSandbox ContainerRole = "sandbox"
)

// TODO 我们推出一个规范 让用户按我们规范做 就认为它是主容器
var (
ErrMultiBiz = errors.New("multi biz containers")
Expand All @@ -55,7 +64,9 @@ type (
Sandbox *Container `json:"sandbox"`
Hostname string `json:"hostname"`
}
Container struct {
// ContainerRole biz sidecar sandbox
ContainerRole string
Container struct {
// container 可能会依赖底层实现, 因此这里不让它依赖具体的实现类
Id string

Expand Down Expand Up @@ -85,8 +96,7 @@ type (
// docker 的 MergedDir 特有字段, 已经转换到hostfs
MergedDir string

// 是否是一个 sandbox
Sandbox bool
ContainerRole ContainerRole

// 该 container 所属的 pod 的 sandbox cid
SandboxID string
Expand All @@ -96,10 +106,6 @@ type (
// 该容器是否已经被我们hack过
Hacked int

// 是否为一个主容器
MainBiz bool
Sidecar bool

// pouch 场景, 只有登录到 container 里 才能获取 daemonset 的 hostname
Hostname string

Expand Down Expand Up @@ -226,6 +232,18 @@ func (c *Container) GetTzName() string {
return c.Tz.Name
}

func (c *Container) IsBiz() bool {
return c.ContainerRole == ContainerRoleBiz
}

func (c *Container) IsSidecar() bool {
return c.ContainerRole == ContainerRoleSidecar
}

func (c *Container) IsSandbox() bool {
return c.ContainerRole == ContainerRoleSandbox
}

func NoPodError(ns, pod string) error {
return fmt.Errorf("no pod ns=[%s] pod=[%s]", ns, pod)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/k8ssysmetrics/cadvisor/cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func (c *cadvisorSysCollector) calcMetrics(metricTime time.Time, cAdvisorPod *v1
} else if cpi, ok := podCGroupInfo[ctr.Name]; ok {
// pod level cgroup
newStatCache.Metrics = c.collectPodCGroup(ctr, cpi, newStatCache.Metrics, mi, s1, s2, deltaTime, alignTs)
} else if criCtr, ok := c.cri.GetContainerByCid(ctr.Id); ok && criCtr.Sandbox {
} else if criCtr, ok := c.cri.GetContainerByCid(ctr.Id); ok && criCtr.IsSandbox() {
// sandbox: it holds network traffic Metrics
newStatCache.Metrics = c.collectPodSandbox(ctr, newStatCache.Metrics, s1, s2, deltaTime, alignTs)
} else if len(ctr.Subcontainers) == 0 {
Expand Down

0 comments on commit 2c02379

Please sign in to comment.