Skip to content

Commit

Permalink
fix: NPE when adding a new service port
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Jan 16, 2025
1 parent c7b4541 commit b298eaf
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions controllers/apps/transformer_cluster_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,27 @@ func createOrUpdateService(ctx graph.TransformContext, dag *graph.DAG, graphCli
}

func resolveServiceDefaultFields(obj, objCopy *corev1.ServiceSpec) {
// TODO: how about the order changed?
servicePorts := make(map[int32]corev1.ServicePort)
for i, port := range obj.Ports {
servicePorts[port.Port] = obj.Ports[i]
}
for i, port := range objCopy.Ports {
servicePort, ok := servicePorts[port.Port]
if !ok {
continue // new port added
}
// if the service type is NodePort or LoadBalancer, and the nodeport is not set, we should use the nodeport of the exist service
if (objCopy.Type == corev1.ServiceTypeNodePort || objCopy.Type == corev1.ServiceTypeLoadBalancer) && port.NodePort == 0 && obj.Ports[i].NodePort != 0 {
objCopy.Ports[i].NodePort = obj.Ports[i].NodePort
if (objCopy.Type == corev1.ServiceTypeNodePort || objCopy.Type == corev1.ServiceTypeLoadBalancer) &&
port.NodePort == 0 &&
servicePort.NodePort != 0 {
objCopy.Ports[i].NodePort = servicePort.NodePort
}
if port.TargetPort.IntVal != 0 {
continue
}
port.TargetPort = obj.Ports[i].TargetPort
if reflect.DeepEqual(port, obj.Ports[i]) {
objCopy.Ports[i].TargetPort = obj.Ports[i].TargetPort
port.TargetPort = servicePort.TargetPort
if reflect.DeepEqual(port, servicePort) {
objCopy.Ports[i].TargetPort = servicePort.TargetPort
}
}
if len(objCopy.ClusterIP) == 0 {
Expand Down

0 comments on commit b298eaf

Please sign in to comment.