Skip to content

Commit

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

Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Jan 8, 2024
1 parent 3508d67 commit 884e190
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
}

Check warning on line 132 in internal/handler/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/handler.go#L124-L132

Added lines #L124 - L132 were not covered by tests
}
}
}

obj = resource
}

Expand Down

0 comments on commit 884e190

Please sign in to comment.