-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpod.go
72 lines (61 loc) · 1.39 KB
/
pod.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
package cmd
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os"
"time"
"github.com/controlplaneio/kubectl-kubesec/v2/pkg/kubesec"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var podCmd = &cobra.Command{
Use: `pod [name]`,
Short: "Scans pod object",
Example: ` pod podinfo --namespace=default`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("pod name is required")
}
name := args[0]
kubeClient, err := newKubeClient(kubeconfig)
if err != nil {
return err
}
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
fmt.Println("scanning pod", name, "in namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
pod, err := kubeClient.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pod.TypeMeta = metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
}
err = serializer.Encode(pod, writer)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := writer.Flush(); err != nil {
return err
}
kc := kubesec.NewClient(scanURL, scanTimeOut)
rs, err := kc.ScanDefinition(buffer)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := rs.Dump(os.Stdout); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
}