Skip to content

Commit

Permalink
support tcpCheck in podProbe
Browse files Browse the repository at this point in the history
  • Loading branch information
BH4AWS committed Dec 23, 2023
1 parent fa9a9a0 commit 55dcfa1
Show file tree
Hide file tree
Showing 18 changed files with 1,038 additions and 94 deletions.
2 changes: 2 additions & 0 deletions apis/apps/v1alpha1/node_pod_probe_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type PodProbe struct {
Namespace string `json:"namespace"`
// pod uid
UID string `json:"uid"`
// pod ip
IP string `json:"IP"`
// Custom container probe, supports Exec, Tcp, and returns the result to Pod yaml
Probes []ContainerProbe `json:"probes,omitempty"`
}
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/apps.kruise.io_nodepodprobes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ spec:
podProbes:
items:
properties:
IP:
description: pod ip
type: string
name:
description: pod name
type: string
Expand Down Expand Up @@ -221,6 +224,7 @@ spec:
description: pod uid
type: string
required:
- IP
- name
- namespace
- uid
Expand Down
90 changes: 81 additions & 9 deletions pkg/controller/podprobemarker/pod_probe_marker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,10 @@ import (
"reflect"
"strings"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/features"
"github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
"github.com/openkruise/kruise/pkg/util/controllerfinder"
utildiscovery "github.com/openkruise/kruise/pkg/util/discovery"
utilfeature "github.com/openkruise/kruise/pkg/util/feature"
"github.com/openkruise/kruise/pkg/util/ratelimiter"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
kubecontroller "k8s.io/kubernetes/pkg/controller"
Expand All @@ -44,6 +37,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/features"
"github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
"github.com/openkruise/kruise/pkg/util/controllerfinder"
utildiscovery "github.com/openkruise/kruise/pkg/util/discovery"
utilfeature "github.com/openkruise/kruise/pkg/util/feature"
"github.com/openkruise/kruise/pkg/util/ratelimiter"
)

func init() {
Expand Down Expand Up @@ -234,15 +236,47 @@ func (r *ReconcilePodProbeMarker) updateNodePodProbes(ppm *appsv1alpha1.PodProbe
exist = true
for j := range ppm.Spec.Probes {
probe := ppm.Spec.Probes[j]
if podProbe.IP == "" {
podProbe.IP = pod.Status.PodIP
}
if probe.Probe.TCPSocket != nil {
probe, err = convertTcpSocketProbeCheckPort(probe, pod)
if err != nil {
klog.Errorf("Failed to convert tcpSocket probe port, err: %v, pod: %v/%v", err, pod.Namespace, pod.Name)
continue

Check warning on line 246 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L243-L246

Added lines #L243 - L246 were not covered by tests
}
}
if probe.Probe.HTTPGet != nil {
probe, err = convertHttpGetProbeCheckPort(probe, pod)
if err != nil {
klog.Errorf("Failed to convert httpGet probe port, err: %v, pod: %v/%v", err, pod.Namespace, pod.Name)
continue

Check warning on line 253 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L250-L253

Added lines #L250 - L253 were not covered by tests
}
}
setPodContainerProbes(podProbe, probe, ppm.Name)
}
break
}
}
if !exist {
podProbe := appsv1alpha1.PodProbe{Name: pod.Name, Namespace: pod.Namespace, UID: string(pod.UID)}
podProbe := appsv1alpha1.PodProbe{Name: pod.Name, Namespace: pod.Namespace, UID: string(pod.UID), IP: pod.Status.PodIP}
for j := range ppm.Spec.Probes {
probe := ppm.Spec.Probes[j]
// look up a port in a container by name & convert container name port
if probe.Probe.TCPSocket != nil {
probe, err = convertTcpSocketProbeCheckPort(probe, pod)
if err != nil {
klog.Errorf("Failed to convert tcpSocket probe port, err: %v, pod: %v/%v", err, pod.Namespace, pod.Name)
continue

Check warning on line 270 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L267-L270

Added lines #L267 - L270 were not covered by tests
}
}
if probe.Probe.HTTPGet != nil {
probe, err = convertHttpGetProbeCheckPort(probe, pod)
if err != nil {
klog.Errorf("Failed to convert httpGet probe port, err: %v, pod: %v/%v", err, pod.Namespace, pod.Name)
continue

Check warning on line 277 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L274-L277

Added lines #L274 - L277 were not covered by tests
}
}
podProbe.Probes = append(podProbe.Probes, appsv1alpha1.ContainerProbe{
Name: fmt.Sprintf("%s#%s", ppm.Name, probe.Name),
ContainerName: probe.ContainerName,
Expand All @@ -266,6 +300,44 @@ func (r *ReconcilePodProbeMarker) updateNodePodProbes(ppm *appsv1alpha1.PodProbe
return nil
}

func convertHttpGetProbeCheckPort(probe appsv1alpha1.PodContainerProbe, pod *corev1.Pod) (appsv1alpha1.PodContainerProbe, error) {
probeNew := probe.DeepCopy()
if probe.Probe.HTTPGet.Port.Type == intstr.Int {
return *probeNew, nil
}
container := util.GetPodContainerByName(probe.ContainerName, pod)
if container == nil {
return *probeNew, fmt.Errorf("Failed to get container by name: %v in pod: %v/%v", probe.ContainerName, pod.Namespace, pod.Name)
}

Check warning on line 311 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L310-L311

Added lines #L310 - L311 were not covered by tests
portInt, err := util.ExtractPort(probe.Probe.HTTPGet.Port, *container)
if err != nil {
return *probeNew, fmt.Errorf("Failed to extract port for container: %v in pod: %v/%v", container.Name, pod.Namespace, pod.Name)
}

Check warning on line 315 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L314-L315

Added lines #L314 - L315 were not covered by tests
probeNew.Probe.HTTPGet.Port = intstr.IntOrString{
Type: intstr.Int, IntVal: int32(portInt), StrVal: "",

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an integer with architecture-dependent bit size from
strconv.Atoi
to a lower bit size type int32 without an upper bound check.
}
return *probeNew, nil
}

func convertTcpSocketProbeCheckPort(probe appsv1alpha1.PodContainerProbe, pod *corev1.Pod) (appsv1alpha1.PodContainerProbe, error) {
probeNew := probe.DeepCopy()
if probe.Probe.TCPSocket.Port.Type == intstr.Int {
return *probeNew, nil
}
container := util.GetPodContainerByName(probe.ContainerName, pod)
if container == nil {
return *probeNew, fmt.Errorf("Failed to get container by name: %v in pod: %v/%v", probe.ContainerName, pod.Namespace, pod.Name)
}

Check warning on line 330 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L329-L330

Added lines #L329 - L330 were not covered by tests
portInt, err := util.ExtractPort(probe.Probe.TCPSocket.Port, *container)
if err != nil {
return *probeNew, fmt.Errorf("Failed to extract port for container: %v in pod: %v/%v", container.Name, pod.Namespace, pod.Name)
}

Check warning on line 334 in pkg/controller/podprobemarker/pod_probe_marker_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/podprobemarker/pod_probe_marker_controller.go#L333-L334

Added lines #L333 - L334 were not covered by tests
probeNew.Probe.TCPSocket.Port = intstr.IntOrString{
Type: intstr.Int, IntVal: int32(portInt), StrVal: "",

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an integer with architecture-dependent bit size from
strconv.Atoi
to a lower bit size type int32 without an upper bound check.
}
return *probeNew, nil
}

func setPodContainerProbes(podProbe *appsv1alpha1.PodProbe, probe appsv1alpha1.PodContainerProbe, ppmName string) {
newProbe := appsv1alpha1.ContainerProbe{
Name: fmt.Sprintf("%s#%s", ppmName, probe.Name),
Expand Down
Loading

0 comments on commit 55dcfa1

Please sign in to comment.