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

fix: NPE when adding a new service port #8819

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
22 changes: 14 additions & 8 deletions controllers/apps/transformer_cluster_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,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 {
if i == len(obj.Ports) {
break
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
Loading