From 230cd6e2b9262fe35c48b7d837f7d0d7b24f2b43 Mon Sep 17 00:00:00 2001 From: Jacob See Date: Fri, 6 May 2022 21:23:32 -0700 Subject: [PATCH 1/2] Add nil check for service & service ports before iterating --- controllers/model/grafanaService.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/controllers/model/grafanaService.go b/controllers/model/grafanaService.go index 8bedf305c..d03cfd6ce 100644 --- a/controllers/model/grafanaService.go +++ b/controllers/model/grafanaService.go @@ -70,9 +70,11 @@ func GetGrafanaPort(cr *v1alpha1.Grafana) int { func getServicePorts(cr *v1alpha1.Grafana, currentState *v1.Service) []v1.ServicePort { intPort := int32(GetGrafanaPort(cr)) nodePort := int32(0) - for _, nPort := range cr.Spec.Service.Ports { - if nPort.Port == 3000 { - nodePort = nPort.NodePort + if cr.Spec.Service != nil && cr.Spec.Service.Ports != nil { + for _, nPort := range cr.Spec.Service.Ports { + if nPort.Port == 3000 { + nodePort = nPort.NodePort + } } } defaultPorts := []v1.ServicePort{ From 47c69635b65af6207b13be957cbacfadca16f465 Mon Sep 17 00:00:00 2001 From: Jacob See Date: Fri, 6 May 2022 21:36:15 -0700 Subject: [PATCH 2/2] Actually, second check was unnecessary. --- controllers/model/grafanaService.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/model/grafanaService.go b/controllers/model/grafanaService.go index d03cfd6ce..c7e1ac7ac 100644 --- a/controllers/model/grafanaService.go +++ b/controllers/model/grafanaService.go @@ -70,7 +70,7 @@ func GetGrafanaPort(cr *v1alpha1.Grafana) int { func getServicePorts(cr *v1alpha1.Grafana, currentState *v1.Service) []v1.ServicePort { intPort := int32(GetGrafanaPort(cr)) nodePort := int32(0) - if cr.Spec.Service != nil && cr.Spec.Service.Ports != nil { + if cr.Spec.Service != nil { for _, nPort := range cr.Spec.Service.Ports { if nPort.Port == 3000 { nodePort = nPort.NodePort