Skip to content

Commit

Permalink
Merge pull request #105 from integr8ly/fix-node-port
Browse files Browse the repository at this point in the history
fix: do not overwrite existing node port
  • Loading branch information
pb82 committed Jan 1, 2020
2 parents 6eab7bb + 6c0342d commit 742edea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions pkg/controller/grafana/grafana_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ func (r *ReconcileGrafana) getGrafanaAdminUrl(cr *i8ly.Grafana, state *common.Cl
}
}

var servicePort = int32(model.GetGrafanaPort(cr))

// Otherwise rely on the service
if state.GrafanaService != nil && state.GrafanaService.Spec.ClusterIP != "" {
return fmt.Sprintf("http://%v:%d", state.GrafanaService.Spec.ClusterIP,
model.GetGrafanaPort(cr)), nil
servicePort), nil
} else if state.GrafanaService != nil {
return fmt.Sprintf("http://%v:%d", state.GrafanaService.Name,
model.GetGrafanaPort(cr)), nil
servicePort), nil
}

return "", stdErr.New("failed to find admin url")
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/model/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ const (
GrafanaAdminUserEnvVar = "GF_SECURITY_ADMIN_USER"
GrafanaAdminPasswordEnvVar = "GF_SECURITY_ADMIN_PASSWORD"
GrafanaHttpPort int = 3000
GrafanaHttpPortName = "grafana"
)
25 changes: 16 additions & 9 deletions pkg/controller/model/grafanaService.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"strconv"
)

const (
defaultPortName = "grafana"
)

func getServiceLabels(cr *v1alpha1.Grafana) map[string]string {
if cr.Spec.Service == nil {
return nil
Expand Down Expand Up @@ -54,12 +50,12 @@ func GetGrafanaPort(cr *v1alpha1.Grafana) int {
return port
}

func getServicePorts(cr *v1alpha1.Grafana) []v1.ServicePort {
func getServicePorts(cr *v1alpha1.Grafana, currentState *v1.Service) []v1.ServicePort {
intPort := int32(GetGrafanaPort(cr))

defaultPorts := []v1.ServicePort{
{
Name: defaultPortName,
Name: GrafanaHttpPortName,
Protocol: "TCP",
Port: intPort,
TargetPort: intstr.FromString("grafana-http"),
Expand All @@ -70,14 +66,25 @@ func getServicePorts(cr *v1alpha1.Grafana) []v1.ServicePort {
return defaultPorts
}

// Re-assign existing node port
if cr.Spec.Service != nil &&
currentState != nil &&
cr.Spec.Service.Type == v1.ServiceTypeNodePort {
for _, port := range currentState.Spec.Ports {
if port.Name == GrafanaHttpPortName {
defaultPorts[0].NodePort = port.NodePort
}
}
}

if cr.Spec.Service.Ports == nil {
return defaultPorts
}

// Don't allow overriding the default port but allow adding
// additional ports
for _, port := range cr.Spec.Service.Ports {
if port.Name == defaultPortName || port.Port == intPort {
if port.Name == GrafanaHttpPortName || port.Port == intPort {
continue
}
defaultPorts = append(defaultPorts, port)
Expand All @@ -95,7 +102,7 @@ func GrafanaService(cr *v1alpha1.Grafana) *v1.Service {
Annotations: getServiceAnnotations(cr),
},
Spec: v1.ServiceSpec{
Ports: getServicePorts(cr),
Ports: getServicePorts(cr, nil),
Selector: map[string]string{
"app": GrafanaPodLabel,
},
Expand All @@ -109,7 +116,7 @@ func GrafanaServiceReconciled(cr *v1alpha1.Grafana, currentState *v1.Service) *v
reconciled := currentState.DeepCopy()
reconciled.Labels = getServiceLabels(cr)
reconciled.Annotations = getServiceAnnotations(cr)
reconciled.Spec.Ports = getServicePorts(cr)
reconciled.Spec.Ports = getServicePorts(cr, currentState)
reconciled.Spec.Type = getServiceType(cr)
return reconciled
}
Expand Down

0 comments on commit 742edea

Please sign in to comment.