Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Aug 27, 2024
1 parent 3dbd815 commit 66fb8f1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
34 changes: 28 additions & 6 deletions pkg/controller/component/available.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package component
import (
"context"
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,21 +31,21 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
"github.com/apecloud/kubeblocks/pkg/kbagent/proto"
)

const (
kbAgentEventFieldPath = "spec.containers{kbagent}"
kbAgentAvailableProbe = "availableProbe"
availableProbe = "availableProbe"
)

type AvailableProbeEventHandler struct{}

func (h *AvailableProbeEventHandler) Handle(cli client.Client, reqCtx intctrlutil.RequestCtx, recorder record.EventRecorder, event *corev1.Event) error {
if event.ReportingController != "kbagent" ||
event.Reason != kbAgentAvailableProbe ||
event.InvolvedObject.FieldPath != kbAgentEventFieldPath {
if event.ReportingController != proto.ProbeEventReportingController ||
event.Reason != availableProbe ||
event.InvolvedObject.FieldPath != proto.ProbeEventFieldPath {
return nil
}

Expand All @@ -55,9 +56,30 @@ func (h *AvailableProbeEventHandler) Handle(cli client.Client, reqCtx intctrluti

// TODO: event about uncertainly results

podName := types.NamespacedName{
Namespace: event.InvolvedObject.Namespace,
Name: event.InvolvedObject.Name,
}
pod := &corev1.Pod{}
if err := cli.Get(reqCtx.Ctx, podName, pod, inDataContextUnspecified()); err != nil {
return err
}
if pod.UID != event.InvolvedObject.UID {
return nil // ignore it
}

if pod.Labels == nil {
return fmt.Errorf("pod %s/%s has no labels", pod.Namespace, pod.Name)
}
clusterName := pod.Labels[constant.AppInstanceLabelKey]
compName := pod.Labels[constant.KBAppComponentLabelKey]
if len(clusterName) == 0 || len(compName) == 0 {
return fmt.Errorf("pod %s/%s has no cluster or component labels", pod.Namespace, pod.Name)
}

compKey := types.NamespacedName{
Namespace: event.InvolvedObject.Namespace,
Name: event.InvolvedObject.Name, // TODO: comp name
Name: constant.GenerateClusterComponentName(clusterName, compName),
}
comp := &appsv1alpha1.Component{}
if err := cli.Get(reqCtx.Ctx, compKey, comp); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func inDataContext() *multicluster.ClientOption {
return multicluster.InDataContext()
}

func inDataContextUnspecified() *multicluster.ClientOption {
return multicluster.InDataContextUnspecified()
}

func IsHostNetworkEnabled(synthesizedComp *SynthesizedComponent) bool {
if !hasHostNetworkCapability(synthesizedComp, nil) {
return false
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/instanceset/pod_role_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (h *PodRoleEventHandler) Handle(cli client.Client, reqCtx intctrlutil.Reque
}

func (h *PodRoleEventHandler) transformKBAgentProbeEvent(logger logr.Logger, event *corev1.Event) *corev1.Event {
if event.ReportingController != "kbagent" || event.Reason != "roleProbe" {
if event.ReportingController != proto.ProbeEventReportingController || event.Reason != "roleProbe" {
return event
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/kbagent/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ type ProbeEvent struct {
Output []byte `json:"output,omitempty"`
Message string `json:"message,omitempty"`
}

const (
ProbeEventFieldPath = "spec.containers{kbagent}"
ProbeEventReportingController = "kbagent"
ProbeEventSourceComponent = "kbagent"
)
4 changes: 2 additions & 2 deletions pkg/kbagent/service/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func (r *probeRunner) sendEvent(probe string, code int32, output []byte, message
prefixLen := min(len(output), 32)
r.logger.Info("send probe event", "code", code, "output", string(output[:prefixLen]), "message", message)

eventMsg := &proto.ProbeEvent{
event := &proto.ProbeEvent{
Probe: probe,
Code: code,
Message: message,
Output: output,
}
msg, err := json.Marshal(&eventMsg)
msg, err := json.Marshal(&event)
if err != nil {
r.logger.Error(err, "failed to marshal probe event")
return
Expand Down
20 changes: 11 additions & 9 deletions pkg/kbagent/util/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
ctlruntime "sigs.k8s.io/controller-runtime"

"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/kbagent/proto"
)

const (
Expand All @@ -47,17 +48,18 @@ func SendEventWithMessage(logger *logr.Logger, reason string, message string) {
event := createEvent(reason, message)
err := sendEvent(event)
if logger != nil && err != nil {
logger.Error(err, "send event failed")
logger.Error(err, fmt.Sprintf("send event failed, reason: %s, message: %s", reason, message))
}
}()
}

func createEvent(reason string, message string) *corev1.Event {
// TODO(v1.0): pod variables
podName := os.Getenv(constant.KBEnvPodName)
podUID := os.Getenv(constant.KBEnvPodUID)
nodeName := os.Getenv(constant.KBEnvNodeName)
namespace := os.Getenv(constant.KBEnvNamespace)
var (
namespace = os.Getenv(constant.KBEnvNamespace)
podName = os.Getenv(constant.KBEnvPodName)
podUID = os.Getenv(constant.KBEnvPodUID)
nodeName = os.Getenv(constant.KBEnvNodeName)
)
return &corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", podName, rand.String(16)),
Expand All @@ -68,18 +70,18 @@ func createEvent(reason string, message string) *corev1.Event {
Namespace: namespace,
Name: podName,
UID: types.UID(podUID),
FieldPath: "spec.containers{kbagent}",
FieldPath: proto.ProbeEventFieldPath,
},
Reason: reason,
Message: message,
Source: corev1.EventSource{
Component: "kbagent",
Component: proto.ProbeEventSourceComponent,
Host: nodeName,
},
FirstTimestamp: metav1.Now(),
LastTimestamp: metav1.Now(),
EventTime: metav1.NowMicro(),
ReportingController: "kbagent",
ReportingController: proto.ProbeEventReportingController,
ReportingInstance: podName,
Action: reason,
Type: "Normal",
Expand Down

0 comments on commit 66fb8f1

Please sign in to comment.