@@ -19,6 +19,7 @@ package apiclient
19
19
import (
20
20
"context"
21
21
"errors"
22
+ "fmt"
22
23
"strings"
23
24
24
25
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
@@ -90,19 +91,25 @@ func CreateOrUpdateSecret(client clientset.Interface, secret *corev1.Secret) err
90
91
func CreateOrUpdateService (client clientset.Interface , service * corev1.Service ) error {
91
92
_ , err := client .CoreV1 ().Services (service .GetNamespace ()).Create (context .TODO (), service , metav1.CreateOptions {})
92
93
if err != nil {
93
- if apierrors .IsAlreadyExists (err ) {
94
- _ , err := client .CoreV1 ().Services (service .GetNamespace ()).Update (context .TODO (), service , metav1.UpdateOptions {})
95
- return err
94
+ if ! apierrors .IsAlreadyExists (err ) {
95
+ // Ignore if the Service is invalid with this error message:
96
+ // Service "apiserver" is invalid: provided Port is already allocated.
97
+ if apierrors .IsInvalid (err ) && strings .Contains (err .Error (), errAllocated .Error ()) {
98
+ klog .V (2 ).ErrorS (err , "failed to create or update serivce" , "service" , klog .KObj (service ))
99
+ return nil
100
+ }
101
+ return fmt .Errorf ("unable to create Service: %v" , err )
96
102
}
97
103
98
- // Ignore if the Service is invalid with this error message:
99
- // Service "apiserver" is invalid: provided Port is already allocated.
100
- if apierrors .IsInvalid (err ) && strings .Contains (err .Error (), errAllocated .Error ()) {
101
- klog .V (2 ).ErrorS (err , "failed to create or update serivce" , "service" , klog .KObj (service ))
102
- return nil
104
+ older , err := client .CoreV1 ().Services (service .GetNamespace ()).Get (context .TODO (), service .GetName (), metav1.GetOptions {})
105
+ if err != nil {
106
+ return err
103
107
}
104
108
105
- return err
109
+ service .ResourceVersion = older .ResourceVersion
110
+ if _ , err := client .CoreV1 ().Services (service .GetNamespace ()).Update (context .TODO (), service , metav1.UpdateOptions {}); err != nil {
111
+ return fmt .Errorf ("unable to update Service: %v" , err )
112
+ }
106
113
}
107
114
108
115
klog .V (5 ).InfoS ("Successfully created or updated service" , "service" , service .GetName ())
0 commit comments