fix(code): remove nil-pointer dereference errors BE-12817 (#2259)

This commit is contained in:
andres-portainer
2026-04-08 19:36:06 -03:00
committed by GitHub
parent 769ea73cec
commit c21d043183
7 changed files with 52 additions and 9 deletions
+7
View File
@@ -430,6 +430,13 @@ func (kcl *KubeClient) GetApplicationFromServiceSelector(pods []corev1.Pod, serv
updateOwnerReferenceToDeployment(&pod, replicaSets)
}
if len(pod.OwnerReferences) == 0 {
return &models.K8sApplication{
Name: pod.Name,
Kind: "Pod",
}, nil
}
return &models.K8sApplication{
Name: pod.OwnerReferences[0].Name,
Kind: pod.OwnerReferences[0].Kind,
+6 -1
View File
@@ -79,11 +79,16 @@ func (kcl *KubeClient) parseCronJob(cronJob batchv1.CronJob, jobsList *batchv1.J
suspend = *cronJob.Spec.Suspend
}
var command string
if len(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers) > 0 {
command = strings.Join(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Command, " ")
}
return models.K8sCronJob{
Id: string(cronJob.UID),
Name: cronJob.Name,
Namespace: cronJob.Namespace,
Command: strings.Join(cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Command, " "),
Command: command,
Schedule: cronJob.Spec.Schedule,
Timezone: timezone,
Suspend: suspend,
+4 -2
View File
@@ -190,8 +190,10 @@ func parseIngress(ingress netv1.Ingress) models.K8sIngressInfo {
if p.PathType != nil {
path.PathType = string(*p.PathType)
}
path.ServiceName = p.Backend.Service.Name
path.Port = int(p.Backend.Service.Port.Number)
if p.Backend.Service != nil {
path.ServiceName = p.Backend.Service.Name
path.Port = int(p.Backend.Service.Port.Number)
}
result.Paths = append(result.Paths, path)
}
}
+22 -4
View File
@@ -11,6 +11,7 @@ import (
models "github.com/portainer/portainer/api/http/models/kubernetes"
"github.com/rs/zerolog/log"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@@ -84,15 +85,32 @@ func (kcl *KubeClient) parseJob(job batchv1.Job) models.K8sJob {
status, failedReason := determineJobStatus(job)
podName := getJobPodName(kcl, job)
var command string
var container corev1.Container
if len(job.Spec.Template.Spec.Containers) > 0 {
command = strings.Join(job.Spec.Template.Spec.Containers[0].Command, " ")
container = job.Spec.Template.Spec.Containers[0]
}
var backoffLimit int32
if job.Spec.BackoffLimit != nil {
backoffLimit = *job.Spec.BackoffLimit
}
var completions int32
if job.Spec.Completions != nil {
completions = *job.Spec.Completions
}
return models.K8sJob{
ID: string(job.UID),
Namespace: job.Namespace,
Name: job.Name,
PodName: podName,
Command: strings.Join(job.Spec.Template.Spec.Containers[0].Command, " "),
Container: job.Spec.Template.Spec.Containers[0],
BackoffLimit: *job.Spec.BackoffLimit,
Completions: *job.Spec.Completions,
Command: command,
Container: container,
BackoffLimit: backoffLimit,
Completions: completions,
StartTime: times.start,
FinishTime: times.finish,
Duration: times.duration,
+6 -1
View File
@@ -128,10 +128,15 @@ func (kcl *KubeClient) CreateUserShellPod(ctx context.Context, serviceAccountNam
return nil, errors.Wrap(err, "aborting pod creation; error waiting for shell pod ready status")
}
var containerName string
if len(shellPod.Spec.Containers) > 0 {
containerName = shellPod.Spec.Containers[0].Name
}
podData := &portainer.KubernetesShellPod{
Namespace: shellPod.Namespace,
PodName: shellPod.Name,
ContainerName: shellPod.Spec.Containers[0].Name,
ContainerName: containerName,
ShellExecCommand: "env TERM=xterm-256color /bin/bash",
}
+3 -1
View File
@@ -294,7 +294,9 @@ func getGroupsByUser(userDN string, conn *ldap.Conn, settings []portainer.LDAPGr
for _, entry := range sr.Entries {
for _, attr := range entry.Attributes {
groups = append(groups, attr.Values[0])
if len(attr.Values) > 0 {
groups = append(groups, attr.Values[0])
}
}
}
}
+4
View File
@@ -40,6 +40,10 @@ func GetEndpointIDFromEdgeKey(edgeKey string) (int, error) {
keyInfo := strings.Split(string(decodedKey), "|")
if len(keyInfo) != 4 {
return 0, errors.New("invalid key format")
}
return strconv.Atoi(keyInfo[3])
}