Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bugs to trim container id in pod spec #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/metaserver/agent/metric/malachite/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ const (
)

type MalachiteClient struct {
// those fields are for testing
sync.RWMutex
urls map[string]string
relativePathFunc *func(podUID, containerId string) (string, error)

urls map[string]string
fetcher pod.PodFetcher
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/metaserver/agent/metric/malachite/client/client_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/malachite/types"
cgroupcm "github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)

func (c *MalachiteClient) GetAllPodContainersStats(ctx context.Context) (map[string]map[string]*types.MalachiteCgroupInfo, error) {
Expand Down Expand Up @@ -55,18 +56,31 @@ func (c *MalachiteClient) GetPodStats(ctx context.Context, podUID string) (map[s

containersStats := make(map[string]*types.MalachiteCgroupInfo)
for _, containerStatus := range pod.Status.ContainerStatuses {
stats, err := c.GetPodContainerStats(podUID, containerStatus.ContainerID)
containerID := native.TrimContainerIDPrefix(containerStatus.ContainerID)
stats, err := c.GetPodContainerStats(podUID, containerID)
if err != nil {
general.Errorf("GetPodStats err %v", err)
continue
}
containersStats[containerStatus.ContainerID] = stats
containersStats[containerID] = stats
}
return containersStats, nil
}

func (c *MalachiteClient) GetPodContainerStats(podUID, containerID string) (*types.MalachiteCgroupInfo, error) {
cgroupPath := cgroupcm.GetContainerRelativeCgroupPath(podUID, containerID)
var cgroupPath string
var err error

// if relativePathFunc has been set, we should use it
if c.relativePathFunc != nil {
cgroupPath, err = (*c.relativePathFunc)(podUID, containerID)
} else {
cgroupPath, err = cgroupcm.GetContainerRelativeCgroupPath(podUID, containerID)
}
if err != nil {
return nil, fmt.Errorf("GetPodContainerStats %s/%v get-relative-path err %v", podUID, containerID, err)
}

containersStats, err := c.GetCgroupStats(cgroupPath)
if err != nil {
return nil, fmt.Errorf("GetPodContainerStats %s/%v get-status %v err %v", podUID, containerID, cgroupPath, err)
Expand Down
22 changes: 16 additions & 6 deletions pkg/metaserver/agent/metric/malachite/client/client_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package client
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -30,11 +32,10 @@ import (

"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/malachite/types"
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod"
"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
)

func TestGetPodContainerStats(t *testing.T) {
t.Parallel()

cgroupData := map[string]*types.MalachiteCgroupResponse{
"podp-uid1/p1-c-uid1": {
Status: 0,
Expand Down Expand Up @@ -96,11 +97,11 @@ func TestGetPodContainerStats(t *testing.T) {
ContainerStatuses: []v1.ContainerStatus{
{
Name: "p1-c-name1",
ContainerID: "p1-c-uid1",
ContainerID: "docker://p1-c-uid1",
},
{
Name: "p1-c-name2",
ContainerID: "p1-c-uid2",
ContainerID: "containerd://p1-c-uid2",
},
},
},
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestGetPodContainerStats(t *testing.T) {
ContainerStatuses: []v1.ContainerStatus{
{
Name: "p3-c-name1",
ContainerID: "p3-c-uid1",
ContainerID: "containerd://p3-c-uid1",
},
},
},
Expand All @@ -142,7 +143,7 @@ func TestGetPodContainerStats(t *testing.T) {
ContainerStatuses: []v1.ContainerStatus{
{
Name: "p4-c-name1",
ContainerID: "p4-c-uid1",
ContainerID: "containerd://p4-c-uid1",
},
},
},
Expand All @@ -157,6 +158,15 @@ func TestGetPodContainerStats(t *testing.T) {

stats, err := malachiteClient.GetAllPodContainersStats(context.Background())
assert.NoError(t, err)
assert.Equal(t, 0, len(stats))

relativePathFunc := func(podUID, containerId string) (string, error) {
return path.Join(fmt.Sprintf("%s%s", common.PodCgroupPathPrefix, podUID), containerId), nil
}
malachiteClient.relativePathFunc = &relativePathFunc

stats, err = malachiteClient.GetAllPodContainersStats(context.Background())
assert.NoError(t, err)
assert.Equal(t, 3, len(stats))

assert.NotNil(t, stats["p-uid1"]["p1-c-uid1"].V1)
Expand Down
22 changes: 20 additions & 2 deletions pkg/util/cgroup/common/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ func GetKubernetesAnyExistAbsCgroupPath(subsys, suffix string) (string, error) {
return "", fmt.Errorf("failed to find absolute path of suffix: %s, error: %v", suffix, utilerrors.NewAggregate(errs))
}

// GetKubernetesAnyExistRelativeCgroupPath returns any relative cgroup path that exists for kubernetes
func GetKubernetesAnyExistRelativeCgroupPath(suffix string) (string, error) {
var errs []error

k8sCgroupPathLock.RLock()
defer k8sCgroupPathLock.RUnlock()

for _, cgPath := range k8sCgroupPathList.List() {
relativePath := path.Join(cgPath, suffix)
p := GetKubernetesAbsCgroupPath(DefaultSelectedSubsys, relativePath)
if general.IsPathExists(p) {
return relativePath, nil
}
}

return "", fmt.Errorf("failed to find relative path of suffix: %s, error: %v", suffix, utilerrors.NewAggregate(errs))
}

// GetPodAbsCgroupPath returns absolute cgroup path for pod level
func GetPodAbsCgroupPath(subsys, podUID string) (string, error) {
return GetKubernetesAnyExistAbsCgroupPath(subsys, podUID)
Expand All @@ -124,8 +142,8 @@ func GetContainerAbsCgroupPath(subsys, podUID, containerId string) (string, erro
}

// GetContainerRelativeCgroupPath returns relative cgroup path for container level
func GetContainerRelativeCgroupPath(podUID, containerId string) string {
return path.Join(fmt.Sprintf("%s%s", PodCgroupPathPrefix, podUID), containerId)
func GetContainerRelativeCgroupPath(podUID, containerId string) (string, error) {
return GetKubernetesAnyExistRelativeCgroupPath(path.Join(fmt.Sprintf("%s%s", PodCgroupPathPrefix, podUID), containerId))
}

func IsContainerCgroupExist(podUID, containerID string) (bool, error) {
Expand Down