github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/statemodifiers.go (about)

     1  // Copyright © 2020 Banzai Cloud
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package reconciler
    16  
    17  import (
    18  	corev1 "k8s.io/api/core/v1"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  	"k8s.io/apimachinery/pkg/runtime"
    21  
    22  	"github.com/banzaicloud/operator-tools/pkg/types"
    23  )
    24  
    25  func ServiceIPModifier(current, desired runtime.Object) error {
    26  	if co, ok := current.(*corev1.Service); ok {
    27  		do := desired.(*corev1.Service)
    28  		if do.Spec.ClusterIP != "" {
    29  			return nil
    30  		}
    31  		do.Spec.ClusterIP = co.Spec.ClusterIP
    32  		do.Spec.ClusterIPs = co.Spec.ClusterIPs
    33  	}
    34  
    35  	return nil
    36  }
    37  
    38  func KeepLabelsAndAnnotationsModifer(current, desired runtime.Object) error {
    39  	if desiredMetaObject, ok := desired.(metav1.Object); ok {
    40  		base := types.MetaBase{
    41  			Annotations: desiredMetaObject.GetAnnotations(),
    42  			Labels:      desiredMetaObject.GetLabels(),
    43  		}
    44  		if metaObject, ok := current.DeepCopyObject().(metav1.Object); ok {
    45  			merged := base.Merge(metav1.ObjectMeta{
    46  				Labels:      metaObject.GetLabels(),
    47  				Annotations: metaObject.GetAnnotations(),
    48  			})
    49  			desiredMetaObject.SetAnnotations(merged.Annotations)
    50  			desiredMetaObject.SetLabels(merged.Labels)
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func KeepServiceAccountTokenReferences(current, desired runtime.Object) error {
    58  	if co, ok := current.(*corev1.ServiceAccount); ok {
    59  		do := desired.(*corev1.ServiceAccount)
    60  		do.Secrets = co.Secrets
    61  	}
    62  
    63  	return nil
    64  }