-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
157 lines (134 loc) · 4.16 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package app
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
)
var (
ErrEmpty = errors.New("empty")
)
type containerType string
var (
initContainer containerType = "initContainer"
normalContainer containerType = "container"
ephemeralContainers containerType = "ephemeralContainer"
)
type ContainerState string
// CtrOptions app options
type CtrOptions struct {
configFlags *genericclioptions.ConfigFlags
rawConfig api.Config
userSpecialedPodName string
userSpecifiedNamespace string
version bool
}
// Complete prepare for ctr
func (o *CtrOptions) Complete(cmd *cobra.Command, args []string) error {
var err error
o.rawConfig, err = o.configFlags.ToRawKubeConfigLoader().RawConfig()
if err != nil {
return err
}
// pod name
o.userSpecialedPodName = args[0]
// namespace
o.userSpecifiedNamespace, err = cmd.Flags().GetString("namespace")
if err != nil {
return err
}
if o.userSpecifiedNamespace == "" {
currentCtx := o.rawConfig.Contexts[o.rawConfig.CurrentContext]
if currentCtx == nil {
return errors.New("failed to read kubeconfig configuration")
}
o.userSpecifiedNamespace = currentCtx.Namespace
}
if o.userSpecifiedNamespace == "" {
o.userSpecifiedNamespace = "default"
}
return nil
}
// Run execute
func (o *CtrOptions) Run() {
namespace := o.userSpecifiedNamespace
containers, err := o.containersForPod()
if err != nil {
if errors.Is(err, ErrEmpty) {
fmt.Printf("No container found in %s namespace.\n", namespace)
return
}
klog.V(2).ErrorS(err, "fetch all containers in "+namespace+" namespace")
fmt.Println(err)
return
}
render(containers)
}
func (o *CtrOptions) containersForPod() ([]Container, error) {
restConfig, _ := o.configFlags.ToRESTConfig()
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, err
}
pod, err := clientSet.CoreV1().Pods(o.userSpecifiedNamespace).Get(context.Background(), o.userSpecialedPodName, metav1.GetOptions{})
if err != nil {
return nil, err
}
containerTotal := len(pod.Spec.InitContainers) + len(pod.Spec.Containers) + len(pod.Spec.EphemeralContainers)
if containerTotal == 0 {
return nil, ErrEmpty
}
// container status
statusMap := make(map[string]corev1.ContainerStatus, len(pod.Status.InitContainerStatuses)+len(pod.Status.ContainerStatuses))
for _, containerStatus := range pod.Status.InitContainerStatuses {
statusMap[containerStatus.Name] = containerStatus
}
for _, containerStatus := range pod.Status.ContainerStatuses {
statusMap[containerStatus.Name] = containerStatus
}
for _, containerStatus := range pod.Status.EphemeralContainerStatuses {
statusMap[containerStatus.Name] = containerStatus
}
containerList := make([]Container, 0, containerTotal)
// initContainers
for _, container := range pod.Spec.InitContainers {
containerList = append(containerList, Container{
Type: initContainer,
Name: container.Name,
Image: container.Image,
ImagePullPolicy: string(container.ImagePullPolicy),
Ports: parsePort(container.Ports),
Status: statusMap[container.Name],
})
}
// containers
for _, container := range pod.Spec.Containers {
containerList = append(containerList, Container{
Type: normalContainer,
Name: container.Name,
Image: container.Image,
ImagePullPolicy: string(container.ImagePullPolicy),
Ports: parsePort(container.Ports),
Status: statusMap[container.Name],
})
}
// EphemeralContainers
for _, ephemeral := range pod.Spec.EphemeralContainers {
container := ephemeral.EphemeralContainerCommon
containerList = append(containerList, Container{
Type: ephemeralContainers,
Name: container.Name,
Image: container.Image,
ImagePullPolicy: string(container.ImagePullPolicy),
Ports: parsePort(container.Ports),
Status: statusMap[container.Name],
})
}
return containerList, nil
}