112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
/*
|
|
Copyright 2018 The Kubernetes Authors.
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package util
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
)
|
|
|
|
// Reference: https://github.com/pwittrock/kubebuilder-workshop/blob/master/pkg/util/util.go
|
|
|
|
// CopyStatefulSetFields copies the owned fields from one StatefulSet to another
|
|
// Returns true if the fields copied from don't match to.
|
|
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
|
|
}
|
|
|
|
// Copy configuration related fields to another instance and returns true if there
|
|
// is a diff and thus needs to update.
|
|
func CopyVirtualService(from, to *unstructured.Unstructured) bool {
|
|
fromSpec, found, err := unstructured.NestedMap(from.Object, "spec")
|
|
if !found {
|
|
return false
|
|
}
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
toSpec, found, err := unstructured.NestedMap(to.Object, "spec")
|
|
if !found || err != nil {
|
|
unstructured.SetNestedMap(to.Object, fromSpec, "spec")
|
|
return true
|
|
}
|
|
|
|
requiresUpdate := !reflect.DeepEqual(fromSpec, toSpec)
|
|
if requiresUpdate {
|
|
unstructured.SetNestedMap(to.Object, fromSpec, "spec")
|
|
}
|
|
return requiresUpdate
|
|
}
|