122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package k8s_exec
|
|
|
|
import (
|
|
v1 "k8s.io/api/apps/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"strings"
|
|
)
|
|
|
|
type CmiiInterfaceConvert interface {
|
|
}
|
|
|
|
type CmiiPodInterface struct {
|
|
Name string
|
|
Namespace string
|
|
//ReplicaSetName string
|
|
ContainerImageMap map[string]string
|
|
PodIP string
|
|
HostIP string
|
|
NodeName string
|
|
RestartCountMap map[string]int32
|
|
ContainerName string
|
|
Image string
|
|
ImageTag string
|
|
GitBranch string
|
|
GitCommit string
|
|
}
|
|
|
|
type CmiiDeploymentInterface struct {
|
|
Name string
|
|
Namespace string
|
|
AvailableReplicas int32
|
|
Replicas int32
|
|
ContainerImageMap map[string]string
|
|
ContainerName string
|
|
Image string
|
|
ImageTag string
|
|
GitBranch string
|
|
GitCommit string
|
|
StatusOk bool
|
|
}
|
|
|
|
func (deploy CmiiDeploymentInterface) Convert(deployment v1.Deployment) CmiiDeploymentInterface {
|
|
|
|
containers := deployment.Spec.Template.Spec.Containers
|
|
|
|
containerImageMap := make(map[string]string, len(containers))
|
|
if len(containers) > 1 {
|
|
log.WarnF("[CmiiDeploymentInterface Convert] - deployment [%s] [%s] container greater than one !", deployment.Namespace, deployment.Name)
|
|
}
|
|
for _, container := range containers {
|
|
containerImageMap[container.Name] = container.Image
|
|
deploy.Image = container.Image
|
|
deploy.ContainerName = container.Name
|
|
deploy.ImageTag = strings.Split(container.Image, ":")[1]
|
|
|
|
for _, envVar := range container.Env {
|
|
if strings.HasPrefix(envVar.Name, "GIT_BRANCH") {
|
|
deploy.GitBranch = envVar.Value
|
|
}
|
|
|
|
if strings.HasPrefix(envVar.Name, "GIT_COMMIT") {
|
|
deploy.GitCommit = envVar.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
deploy.Name = deployment.Name
|
|
deploy.Namespace = deployment.Namespace
|
|
deploy.AvailableReplicas = deployment.Status.AvailableReplicas
|
|
deploy.Replicas = *deployment.Spec.Replicas
|
|
deploy.ContainerImageMap = containerImageMap
|
|
deploy.StatusOk = deployment.Status.AvailableReplicas == *deployment.Spec.Replicas
|
|
|
|
return deploy
|
|
}
|
|
|
|
func (pod CmiiPodInterface) Convert(podDetail corev1.Pod) CmiiPodInterface {
|
|
|
|
containers := podDetail.Spec.Containers
|
|
containerImageMap := make(map[string]string, len(containers))
|
|
if len(containers) > 1 {
|
|
log.WarnF("[CmiiDeploymentInterface Convert] - pod [%s] [%s] container greater than one !", podDetail.Namespace, podDetail.Name)
|
|
}
|
|
for _, container := range containers {
|
|
containerImageMap[container.Name] = container.Image
|
|
|
|
pod.Image = container.Image
|
|
pod.ContainerName = container.Name
|
|
pod.ImageTag = strings.Split(container.Image, ":")[1]
|
|
|
|
for _, envVar := range container.Env {
|
|
if strings.HasPrefix(envVar.Name, "GIT_BRANCH") {
|
|
pod.GitBranch = envVar.Value
|
|
}
|
|
|
|
if strings.HasPrefix(envVar.Name, "GIT_COMMIT") {
|
|
pod.GitCommit = envVar.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
containerStatuses := podDetail.Status.ContainerStatuses
|
|
containerStatusMap := make(map[string]int32, len(containerStatuses))
|
|
|
|
for _, containerStatus := range containerStatuses {
|
|
containerStatusMap[containerStatus.Name] = containerStatus.RestartCount
|
|
}
|
|
|
|
pod.Name = podDetail.Name
|
|
pod.Namespace = podDetail.Namespace
|
|
pod.ContainerImageMap = containerImageMap
|
|
pod.RestartCountMap = containerStatusMap
|
|
pod.PodIP = podDetail.Status.PodIP
|
|
pod.HostIP = podDetail.Status.HostIP
|
|
pod.NodeName = podDetail.Spec.NodeName
|
|
//if len(podDetail.ObjectMeta.OwnerReferences) > 0 {
|
|
// pod.ReplicaSetName = podDetail.ObjectMeta.OwnerReferences[0].Name
|
|
//}
|
|
|
|
return pod
|
|
}
|