Skip to content

Instantly share code, notes, and snippets.

@shivanshuraj1333
Created November 12, 2021 08:31
Show Gist options
  • Save shivanshuraj1333/10a801d10c8f790b2e12e50cfb451dc7 to your computer and use it in GitHub Desktop.
Save shivanshuraj1333/10a801d10c8f790b2e12e50cfb451dc7 to your computer and use it in GitHub Desktop.
Custom formatter for pods and nodes of package `pkg/scheduler/internal/cache/debugger/formatter.go` in `kubernetes`
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package debugger
import (
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kubernetes/pkg/scheduler/framework"
"strings"
)
type formatPod struct {
pods []*v1.Pod
}
type podInfo struct {
Name, Namespace, NominatedNode string
UID types.UID
Phase v1.PodPhase
}
type formatNode struct {
name string
node *framework.NodeInfo
nominatedPodInfo []*framework.PodInfo
}
type nodeInfo struct {
NodeName string
Deleted bool
RequestedResources, AllocatableResources *framework.Resource
ScheduledPods int
PodInfo []podInfo
NominatedPods int
NominatedPodsInfo []podInfo
}
func (f formatPod) String() string {
var podInfo strings.Builder
for _, v := range f.pods {
podInfo.WriteString(fmt.Sprintf("Name: %v, Namespace: %v, UID: %v, Phase: %v, NominatedNode: %v\n", v.Name, v.Namespace, v.UID, v.Status.Phase, v.Status.NominatedNodeName))
}
return podInfo.String()
}
func (f formatPod) MarshalLog() interface{} {
var podSlice []podInfo
for _, v := range f.pods {
podSlice = append(podSlice, podInfo{
Name: v.Name,
Namespace: v.Namespace,
UID: v.UID,
Phase: v.Status.Phase,
NominatedNode: v.Status.NominatedNodeName,
})
}
return podSlice
}
func (f formatNode) String() string {
var nodeInfo strings.Builder
nodeInfo.WriteString(fmt.Sprintf("\nNodeName: %s\nDeleted %t\nRequestedResources: %+v\nAllocatableResources: %+v\nScheduledPods: %v\nNominatedPods: %v\n",
f.name, f.node.Node() == nil, f.node.Requested, f.node.Allocatable, len(f.node.Pods), len(f.nominatedPodInfo)))
for _, v := range f.node.Pods {
nodeInfo.WriteString(fmt.Sprintf("Name: %v, Namespace: %v, UID: %v, Phase: %v, NominatedNode: %v\n", v.Pod.Name, v.Pod.Namespace, v.Pod.UID, v.Pod.Status.Phase, v.Pod.Status.NominatedNodeName))
}
if len(f.nominatedPodInfo) != 0 {
for _, v := range f.nominatedPodInfo {
nodeInfo.WriteString(fmt.Sprintf("Name: %v, Namespace: %v, UID: %v, Phase: %v, NominatedNode: %v\n", v.Pod.Name, v.Pod.Namespace, v.Pod.UID, v.Pod.Status.Phase, v.Pod.Status.NominatedNodeName))
}
}
return nodeInfo.String()
}
func (f formatNode) MarshalLog() interface{} {
var nodeInfo nodeInfo
nodeInfo.NodeName = f.name
nodeInfo.Deleted = f.node.Node() == nil
nodeInfo.RequestedResources = f.node.Requested
nodeInfo.AllocatableResources = f.node.Allocatable
nodeInfo.ScheduledPods = len(f.node.Pods)
nodeInfo.NominatedPods = len(f.nominatedPodInfo)
for _, v := range f.node.Pods {
nodeInfo.PodInfo = append(nodeInfo.PodInfo, podInfo{
Name: v.Pod.Name,
Namespace: v.Pod.Namespace,
UID: v.Pod.UID,
Phase: v.Pod.Status.Phase,
NominatedNode: v.Pod.Status.NominatedNodeName,
})
}
if nodeInfo.NominatedPods != 0 {
for _, v := range f.nominatedPodInfo {
nodeInfo.NominatedPodsInfo = append(nodeInfo.PodInfo, podInfo{
Name: v.Pod.Name,
Namespace: v.Pod.Namespace,
UID: v.Pod.UID,
Phase: v.Pod.Status.Phase,
NominatedNode: v.Pod.Status.NominatedNodeName,
})
}
}
return nodeInfo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment