Skip to content

Commit

Permalink
feat(hatchery/k8s): log pod events (#6531)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored Apr 11, 2023
1 parent ca0b143 commit fa3baa1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TARGET_OS = $(if ${OS},${OS},windows darwin linux freebsd)
TARGET_ARCH = $(if ${ARCH},${ARCH},amd64 arm 386 arm64)
VERSION := $(if ${CDS_VERSION},${CDS_VERSION},snapshot)
GIT_DESCRIBE := $(shell git describe --tags)
GIT_VERSION := $(if ${GIT_DESCRIBE},${GIT_DESCRIBE},0.0.0-0-snapshot)
GIT_VERSION := $(if ${GIT_DESCRIBE},${GIT_DESCRIBE:v%=%},0.0.0-0-snapshot)
SHA512 := $(if ifeq ${UNAME} "Darwin",shasum -a 512,sha512sum)

TARGET_ENGINE = engine
Expand Down
29 changes: 29 additions & 0 deletions engine/hatchery/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/rockbears/log"

apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -44,6 +45,34 @@ func (h *HatcheryKubernetes) InitHatchery(ctx context.Context) error {
h.GoRoutines.Run(ctx, "hatchery kubernetes routines", func(ctx context.Context) {
h.routines(ctx)
})

h.GoRoutines.Run(ctx, "hatchery kubernetes watcher", func(ctx context.Context) {
if err := h.WatchPodEvents(ctx); err != nil {
log.ErrorWithStackTrace(ctx, err)
}
})

return nil
}

func (h *HatcheryKubernetes) WatchPodEvents(ctx context.Context) error {
opts := metav1.ListOptions{
FieldSelector: "involvedObject.kind=Pod",
Watch: true,
}
// requires "watch" permission on events in clusterrole
watcher, err := h.kubeClient.Events(ctx, h.Config.Namespace, opts)
if err != nil {
return err
}
watchCh := watcher.ResultChan()
defer watcher.Stop()
for event := range watchCh {
switch x := event.Object.(type) {
case *corev1.Event:
log.Info(ctx, "object: %s, reason: %s, message: %s, component: %s, host: %s", x.ObjectMeta.Name, x.Reason, x.Message, x.Source.Component, x.Source.Host)
}
}
return nil
}

Expand Down
9 changes: 8 additions & 1 deletion engine/hatchery/kubernetes/kubernetes_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rockbears/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -113,6 +114,7 @@ type KubernetesClient interface {
SecretDelete(ctx context.Context, ns string, name string, options metav1.DeleteOptions) error
SecretGet(ctx context.Context, ns string, name string, options metav1.GetOptions) (*corev1.Secret, error)
SecretList(ctx context.Context, ns string, options metav1.ListOptions) (*corev1.SecretList, error)
Events(ctx context.Context, ns string, options metav1.ListOptions) (watch.Interface, error)
}

type kubernetesClient struct {
Expand Down Expand Up @@ -143,7 +145,6 @@ func (k *kubernetesClient) PodDelete(ctx context.Context, ns string, name string

func (k *kubernetesClient) PodList(ctx context.Context, ns string, opts metav1.ListOptions) (*corev1.PodList, error) {
ctx = context.WithValue(ctx, logNS, ns)
log.Info(ctx, "listing pod in namespace %s", ns)
pods, err := k.client.CoreV1().Pods(ns).List(ctx, opts)
return pods, sdk.WrapError(err, "unable to list pods in namespace %s", ns)
}
Expand Down Expand Up @@ -175,3 +176,9 @@ func (k *kubernetesClient) PodGetRawLogs(ctx context.Context, ns string, name st
logs, err := k.client.CoreV1().Pods(ns).GetLogs(name, options).DoRaw(ctx)
return logs, sdk.WrapError(err, "unable to get pod %s raw logs", name)
}

func (k *kubernetesClient) Events(ctx context.Context, ns string, options metav1.ListOptions) (watch.Interface, error) {
ctx = context.WithValue(ctx, logNS, ns)
evt, err := k.client.CoreV1().Events(ns).Watch(ctx, options)
return evt, sdk.WrapError(err, "unable to watch events on ns %s ", ns)
}

0 comments on commit fa3baa1

Please sign in to comment.