github.com/kubeflow/training-operator@v1.7.0/pkg/util/labels/labels.go (about)

     1  // Copyright 2021 The Kubeflow Authors
     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 labels
    16  
    17  import (
    18  	"errors"
    19  	"strconv"
    20  
    21  	v1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
    22  )
    23  
    24  func ReplicaIndex(labels map[string]string) (int, error) {
    25  	v, ok := labels[v1.ReplicaIndexLabel]
    26  	if !ok {
    27  		return 0, errors.New("replica index label not found")
    28  	}
    29  	return strconv.Atoi(v)
    30  }
    31  
    32  func SetReplicaIndex(labels map[string]string, idx int) {
    33  	SetReplicaIndexStr(labels, strconv.Itoa(idx))
    34  }
    35  
    36  func SetReplicaIndexStr(labels map[string]string, idx string) {
    37  	labels[v1.ReplicaIndexLabel] = idx
    38  }
    39  
    40  func ReplicaType(labels map[string]string) (v1.ReplicaType, error) {
    41  	v, ok := labels[v1.ReplicaTypeLabel]
    42  	if !ok {
    43  		return "", errors.New("replica type label not found")
    44  	}
    45  	return v1.ReplicaType(v), nil
    46  }
    47  
    48  func SetReplicaType(labels map[string]string, rt string) {
    49  	labels[v1.ReplicaTypeLabel] = rt
    50  }
    51  
    52  func HasKnownLabels(labels map[string]string, groupName string) bool {
    53  	_, has := labels[v1.OperatorNameLabel]
    54  	return has
    55  }
    56  
    57  func SetJobRole(labels map[string]string, role string) {
    58  	labels[v1.JobRoleLabel] = role
    59  }