Skip to content

Commit

Permalink
run collectors from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 17, 2019
1 parent 64502c5 commit 279afcb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
93 changes: 92 additions & 1 deletion cmd/preflight/cli/run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cli

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
Expand All @@ -19,8 +21,11 @@ import (
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
Expand Down Expand Up @@ -191,6 +196,11 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
restClient := clientset.CoreV1().RESTClient()

// deploy an object that "owns" everything to aid in cleanup
owner := corev1.ConfigMap{
Expand Down Expand Up @@ -221,14 +231,95 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
desiredCollectors = ensureCollectorInList(desiredCollectors, troubleshootv1beta1.Collect{ClusterInfo: &troubleshootv1beta1.ClusterInfo{}})
desiredCollectors = ensureCollectorInList(desiredCollectors, troubleshootv1beta1.Collect{ClusterResources: &troubleshootv1beta1.ClusterResources{}})

podsCreated := make([]*corev1.Pod, 0, 0)
podsDeleted := make([]*corev1.Pod, 0, 0)

resyncPeriod := time.Second
ctx := context.Background()
watchList := cache.NewListWatchFromClient(restClient, "pods", "", fields.Everything())
_, controller := cache.NewInformer(watchList, &corev1.Pod{}, resyncPeriod,
cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
newPod, ok := newObj.(*corev1.Pod)
if !ok {
return
}
oldPod, ok := oldObj.(*corev1.Pod)
if !ok {
return
}
labels := newPod.Labels
troubleshootRole, ok := labels["troubleshoot-role"]
if !ok || troubleshootRole != "preflight" {
return
}
preflightName, ok := labels["preflight"]
if !ok || preflightName != preflight.Name {
return
}

if oldPod.Status.Phase == newPod.Status.Phase {
return
}

if newPod.Status.Phase != corev1.PodSucceeded {
return
}

podLogOpts := corev1.PodLogOptions{}

req := clientset.CoreV1().Pods(newPod.Namespace).GetLogs(newPod.Name, &podLogOpts)
podLogs, err := req.Stream()
if err != nil {
fmt.Println("get stream")
return
}
defer podLogs.Close()

buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
fmt.Println("copy logs")
return
}

fmt.Printf(buf.String())

if err := client.Delete(context.Background(), newPod); err != nil {
fmt.Println("delete pod")
}
podsDeleted = append(podsDeleted, newPod)
},
})
go func() {
controller.Run(ctx.Done())
}()

s := runtime.NewScheme()
s.AddKnownTypes(schema.GroupVersion{Group: "", Version: "v1"}, &corev1.ConfigMap{})
for _, collector := range desiredCollectors {
_, _, err := preflightrunner.CreateCollector(client, s, &owner, preflight.Name, v.GetString("namespace"), collector, v.GetString("image"), v.GetString("pullpolicy"))
_, pod, err := preflightrunner.CreateCollector(client, s, &owner, preflight.Name, v.GetString("namespace"), collector, v.GetString("image"), v.GetString("pullpolicy"))
if err != nil {
return err
}
podsCreated = append(podsCreated, pod)
}

start := time.Now()
for {
if start.Add(time.Second * 30).Before(time.Now()) {
fmt.Println("timeout running preflight")
return err
}

if len(podsDeleted) == len(podsCreated) {
break
}

time.Sleep(time.Millisecond * 200)
}

ctx.Done()
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/preflight/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef m
imagePullPolicy = corev1.PullPolicy(pullPolicy)
}

podLabels := make(map[string]string)
podLabels["preflight"] = preflightJobName
podLabels["troubleshoot-role"] = "preflight"

pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: preflightJobNamespace,
Labels: podLabels,
},
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand Down

0 comments on commit 279afcb

Please sign in to comment.