Skip to content

Commit

Permalink
[#907] ensure getlog utility is eventually safe
Browse files Browse the repository at this point in the history
  • Loading branch information
gtully committed Apr 26, 2024
1 parent a808a09 commit 66f1970
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var _ = Describe("artemis controller", Label("do"), func() {
})
})

Context("operator logging config test", Label("do-operator-log"), func() {
Context("operator logging config", Label("do-operator-log"), func() {
It("test operator with env var", func() {
if os.Getenv("DEPLOY_OPERATOR") == "true" {
// re-install a new operator to have a fresh log
Expand Down
65 changes: 38 additions & 27 deletions controllers/common_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,34 +616,45 @@ func DeployCustomAddress(targetNamespace string, customFunc func(candidate *brok
}

func GetOperatorLog(ns string) (*string, error) {
cfg, err := config.GetConfig()
Expect(err).To(BeNil())
labelSelector, err := labels.Parse("control-plane=controller-manager,name=" + oprName)
Expect(err).To(BeNil())
clientset, err := kubernetes.NewForConfig(cfg)
Expect(err).To(BeNil())
listOpts := metav1.ListOptions{
LabelSelector: labelSelector.String(),
var cfg *rest.Config
var err error
if cfg, err = config.GetConfig(); err == nil {

var labelSelector labels.Selector
if labelSelector, err = labels.Parse("control-plane=controller-manager,name=" + oprName); err == nil {

var clientset *kubernetes.Clientset
if clientset, err = kubernetes.NewForConfig(cfg); err == nil {

listOpts := metav1.ListOptions{
LabelSelector: labelSelector.String(),
}
var podList *corev1.PodList
if podList, err = clientset.CoreV1().Pods(ns).List(ctx, listOpts); err == nil {

if len(podList.Items) != 1 {
err = fmt.Errorf("expect a single pod")
} else {

operatorPod := podList.Items[0]
podLogOpts := corev1.PodLogOptions{}
req := clientset.CoreV1().Pods(ns).GetLogs(operatorPod.Name, &podLogOpts)

var podLogs io.ReadCloser
if podLogs, err = req.Stream(context.Background()); err == nil {
defer podLogs.Close()
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, podLogs); err == nil {
str := buf.String()
return &str, nil
}
}
}
}
}
}
}
podList, err := clientset.CoreV1().Pods(ns).List(ctx, listOpts)
Expect(err).To(BeNil())
Expect(len(podList.Items)).To(Equal(1))
operatorPod := podList.Items[0]

podLogOpts := corev1.PodLogOptions{}
req := clientset.CoreV1().Pods(ns).GetLogs(operatorPod.Name, &podLogOpts)
podLogs, err := req.Stream(context.Background())
Expect(err).To(BeNil())
defer podLogs.Close()

Expect(err).To(BeNil())

buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
Expect(err).To(BeNil())
str := buf.String()

return &str, nil
return nil, err
}

func NewPriveKey() (*rsa.PrivateKey, error) {
Expand Down

0 comments on commit 66f1970

Please sign in to comment.