github.com/argoproj/argo-events@v1.9.1/controllers/common/util.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package common
    18  
    19  import (
    20  	"github.com/argoproj/argo-events/common"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/runtime/schema"
    23  )
    24  
    25  // SetObjectMeta sets ObjectMeta of child resource
    26  func SetObjectMeta(owner, obj metav1.Object, gvk schema.GroupVersionKind) error {
    27  	references := obj.GetOwnerReferences()
    28  	references = append(references,
    29  		*metav1.NewControllerRef(owner, gvk),
    30  	)
    31  	obj.SetOwnerReferences(references)
    32  
    33  	if obj.GetName() == "" && obj.GetGenerateName() == "" {
    34  		obj.SetName(owner.GetName())
    35  	}
    36  	if obj.GetNamespace() == "" {
    37  		obj.SetNamespace(owner.GetNamespace())
    38  	}
    39  
    40  	hash, err := common.GetObjectHash(obj)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	annotations := obj.GetAnnotations()
    45  	if annotations == nil {
    46  		annotations = make(map[string]string)
    47  	}
    48  	annotations[common.AnnotationResourceSpecHash] = hash
    49  	obj.SetAnnotations(annotations)
    50  
    51  	return nil
    52  }