Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support tcpCheck in podProbe #1474

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 80 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 @@
"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 @@
"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 @@
exist = true
for j := range ppm.Spec.Probes {
probe := ppm.Spec.Probes[j]
if podProbe.IP == "" {
podProbe.IP = pod.Status.PodIP
}
furykerry marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
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
}
}
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz add ut of Reconcile for tcpsocket and httpget

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

klog.Errorf("Failed to convert tcpSocket probe port, err: %v, pod: %v/%v", err, pod.Namespace, pod.Name)
continue
}
}
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
}
}
podProbe.Probes = append(podProbe.Probes, appsv1alpha1.ContainerProbe{
Name: fmt.Sprintf("%s#%s", ppm.Name, probe.Name),
ContainerName: probe.ContainerName,
Expand All @@ -266,6 +300,43 @@
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)
}
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)
}
// If you need to parse integer values with specific bit sizes, avoid strconv.Atoi,
// and instead use strconv.ParseInt or strconv.ParseUint, which also allow specifying the bit size.
// https://codeql.github.com/codeql-query-help/go/go-incorrect-integer-conversion/
probeNew.Probe.HTTPGet.Port = intstr.FromInt(portInt)
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)
}
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 335 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#L334-L335

Added lines #L334 - L335 were not covered by tests
probeNew.Probe.TCPSocket.Port = intstr.FromInt(portInt)
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
Loading