forked from kubeflow/notebooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented collision handling using ownerReferences
Signed-off-by: Adem Baccara <[email protected]>
- Loading branch information
Showing
4 changed files
with
258 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package helper | ||
|
||
import ( | ||
"errors" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
func CopyStatefulSetFields(from, to *appsv1.StatefulSet) bool { | ||
requireUpdate := false | ||
for k, v := range to.Labels { | ||
if from.Labels[k] != v { | ||
requireUpdate = true | ||
} | ||
} | ||
to.Labels = from.Labels | ||
|
||
for k, v := range to.Annotations { | ||
if from.Annotations[k] != v { | ||
requireUpdate = true | ||
} | ||
} | ||
to.Annotations = from.Annotations | ||
|
||
if *from.Spec.Replicas != *to.Spec.Replicas { | ||
*to.Spec.Replicas = *from.Spec.Replicas | ||
requireUpdate = true | ||
} | ||
|
||
if !reflect.DeepEqual(to.Spec.Template.Spec, from.Spec.Template.Spec) { | ||
requireUpdate = true | ||
} | ||
to.Spec.Template.Spec = from.Spec.Template.Spec | ||
|
||
return requireUpdate | ||
} | ||
|
||
// CopyServiceFields copies the owned fields from one Service to another | ||
func CopyServiceFields(from, to *corev1.Service) bool { | ||
requireUpdate := false | ||
for k, v := range to.Labels { | ||
if from.Labels[k] != v { | ||
requireUpdate = true | ||
} | ||
} | ||
to.Labels = from.Labels | ||
|
||
for k, v := range to.Annotations { | ||
if from.Annotations[k] != v { | ||
requireUpdate = true | ||
} | ||
} | ||
to.Annotations = from.Annotations | ||
|
||
// Don't copy the entire Spec, because we can't overwrite the clusterIp field | ||
|
||
if !reflect.DeepEqual(to.Spec.Selector, from.Spec.Selector) { | ||
requireUpdate = true | ||
} | ||
to.Spec.Selector = from.Spec.Selector | ||
|
||
if !reflect.DeepEqual(to.Spec.Ports, from.Spec.Ports) { | ||
requireUpdate = true | ||
} | ||
to.Spec.Ports = from.Spec.Ports | ||
|
||
return requireUpdate | ||
} | ||
|
||
// StopRetry is returned to tell the Retry function to stop retrying. | ||
type StopRetry struct{ Err error } | ||
|
||
// Error implements the error interface | ||
func (s *StopRetry) Error() string { return s.Err.Error() } | ||
|
||
// Retry will retry the given function until either the maximum attempts is reached or | ||
// a stop error is returned. | ||
func Retry(attempts int, sleep time.Duration, f func() error) error { | ||
if err := f(); err != nil { | ||
var stop *StopRetry | ||
if errors.As(err, &stop) { | ||
return stop.Err | ||
} | ||
// user can pass -1 to retry indefinitely | ||
if attempts--; attempts != 0 { | ||
time.Sleep(sleep) | ||
return Retry(attempts, 2*sleep, f) | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} |