-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
168 lines (147 loc) · 4.53 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"flag"
"fmt"
"io"
v12 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
"log"
"os"
"strings"
"sync"
)
func getDeployment(r io.Reader) *v12.Deployment {
d := yaml.NewYAMLOrJSONDecoder(r, 100000)
var dep v12.Deployment
err := d.Decode(&dep)
if err != nil {
log.Fatalf("Failed to decode deployment file: %#v\n", err)
}
return &dep
}
func getCustomResource(r io.Reader) *unstructured.Unstructured {
d := yaml.NewYAMLOrJSONDecoder(r, 100000)
var u unstructured.Unstructured
err := d.Decode(&u)
if err != nil {
log.Fatalf("Failed to decode the cr: %#v\n", err)
}
return &u
}
func getReader(filepath string) *os.File {
f, err := os.Open(filepath)
if err != nil {
log.Fatalf("Could not open file: %#v\n", err)
return nil
}
return f
}
func main() {
operatorYAML := flag.String("deployment-filepath", "./deploy/operator.yaml", "filepath of ansible-operator deployment file. Defaults to `./deploy/operator.yaml`")
operatorNamespace := flag.String("namespace", "default", "The namespace in which operator is running. Defaults to `default`")
crYAML := flag.String("cr-filepath", "./deploy/cr.yaml", "filepath of the cr yaml. Defaults to ./deploy/cr.yaml")
jobID := flag.String("job-id", "latest", "The job id for which logs needs to be displayed")
kubeconfig := flag.String("kubeconfig", "~/.kube/config", "filepath to the kubeconfig")
flag.Parse()
deployment := getDeployment(getReader(*operatorYAML))
cr := getCustomResource(getReader(*crYAML))
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
log.Fatalf("Failed to create config: %#v\n", err)
}
restClient, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create clientset: %+v", err)
}
labelSelector := deployment.Spec.Selector
watchInterface, err := restClient.CoreV1().Pods(*operatorNamespace).Watch(metav1.ListOptions{LabelSelector: labels.Set(labelSelector.MatchLabels).String()})
if err != nil {
log.Fatalf("Failed to watch the pods: %+v\n", err)
}
waitChan := make(chan bool)
var pod *v1.Pod
var wg sync.WaitGroup
wg.Add(2)
go func() {
for {
select {
case e := <-watchInterface.ResultChan():
if e.Type == watch.Added {
pod, _ = e.Object.(*v1.Pod)
fmt.Printf("Pod %+v added\n", pod.Name)
waitChan <- true
}
}
}
wg.Done()
}()
<-waitChan
fmt.Println("Signal received")
func() {
c, err := containerToAttachTo("", pod)
if err != nil {
log.Fatalf("Failed to get the container: %+v", err)
}
command := "cat /tmp/ansible-operator/runner/" + cr.GroupVersionKind().GroupVersion().String() + "/" + cr.GroupVersionKind().Kind + "/" + *operatorNamespace + "/" + cr.GetName() + "/artifacts/" + *jobID + "/stdout"
req := restClient.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec")
s := runtime.NewScheme()
if err := v1.AddToScheme(s); err != nil {
panic(err)
}
parameterCodec := runtime.NewParameterCodec(s)
req.VersionedParams(&v1.PodExecOptions{
Command: strings.Fields(command),
Container: c.Name,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: false,
}, parameterCodec)
fmt.Println("Request URL:", req.URL().String())
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
log.Fatalf("Failed to get the executor: %+v", err)
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
log.Fatalf("Failed to run the command: %+v", err)
}
}()
return
}
// containerToAttach returns a reference to the container to attach to, given
// by name or the first container if name is empty.
func containerToAttachTo(container string, pod *v1.Pod) (*v1.Container, error) {
if len(container) > 0 {
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].Name == container {
return &pod.Spec.Containers[i], nil
}
}
for i := range pod.Spec.InitContainers {
if pod.Spec.InitContainers[i].Name == container {
return &pod.Spec.InitContainers[i], nil
}
}
return nil, fmt.Errorf("container not found (%s)", container)
}
return &pod.Spec.Containers[0], nil
}