Skip to content

Commit

Permalink
fix: fix always update svc (#995)
Browse files Browse the repository at this point in the history
when svc type is nodePort or loadBalancer, the svc will always update

fix #982

Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z authored Jan 8, 2024
1 parent 3508d67 commit 72178c9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,26 @@ func (handler *Handler) CreateOrUpdate(ctx context.Context, scheme *runtime.Sche
return err
}
// Required fields when updating service in k8s 1.21
if storageResource.Spec.ClusterIP != "" {
resource.Spec.ClusterIP = storageResource.Spec.ClusterIP
if resource.Spec.Type == "" || resource.Spec.Type == corev1.ServiceTypeClusterIP {
if storageResource.Spec.ClusterIP != "" {
resource.Spec.ClusterIP = storageResource.Spec.ClusterIP
}
}
// If nodePort is not set, will always update service
if resource.Spec.Type == corev1.ServiceTypeNodePort || resource.Spec.Type == corev1.ServiceTypeLoadBalancer {
nodePortMap := make(map[string]int32)
for _, port := range storageResource.Spec.Ports {
nodePortMap[port.Name] = port.NodePort
}
for i, port := range resource.Spec.Ports {
if port.NodePort == 0 {
if nodePort, ok := nodePortMap[port.Name]; ok {
resource.Spec.Ports[i].NodePort = nodePort
}
}
}
}

obj = resource
}

Expand Down

0 comments on commit 72178c9

Please sign in to comment.