github.com/kubeflow/training-operator@v1.7.0/pkg/controller.v1/control/utils.go (about) 1 /* 2 Copyright 2023 The Kubeflow Authors. 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 control 18 19 import ( 20 "fmt" 21 v1 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 ) 25 26 func ValidateControllerRef(controllerRef *metav1.OwnerReference) error { 27 if controllerRef == nil { 28 return fmt.Errorf("controllerRef is nil") 29 } 30 if len(controllerRef.APIVersion) == 0 { 31 return fmt.Errorf("controllerRef has empty APIVersion") 32 } 33 if len(controllerRef.Kind) == 0 { 34 return fmt.Errorf("controllerRef has empty Kind") 35 } 36 if controllerRef.Controller == nil || !*controllerRef.Controller { 37 return fmt.Errorf("controllerRef.Controller is not set to true") 38 } 39 if controllerRef.BlockOwnerDeletion == nil || !*controllerRef.BlockOwnerDeletion { 40 return fmt.Errorf("controllerRef.BlockOwnerDeletion is not set") 41 } 42 return nil 43 } 44 45 func GetServiceFromTemplate(template *v1.Service, parentObject runtime.Object, controllerRef *metav1.OwnerReference) (*v1.Service, error) { 46 service := template.DeepCopy() 47 if controllerRef != nil { 48 service.OwnerReferences = append(service.OwnerReferences, *controllerRef) 49 } 50 return service, nil 51 }