0yukali0 commented on code in PR #474:
URL: https://github.com/apache/yunikorn-k8shim/pull/474#discussion_r1028079837
##########
pkg/common/utils/utils.go:
##########
@@ -316,3 +318,113 @@ func GetExtraConfigFromConfigMap(config
map[string]string) map[string]string {
}
return result
}
+
+func GetPodAnnotationValue(pod *v1.Pod, annotationKey string) string {
+ if value, ok := pod.Annotations[annotationKey]; ok {
+ return value
+ }
+ return ""
+}
+
+func GetNameSpaceAnnotationValue(namespace *v1.Namespace, annotationKey
string) string {
+ if value, ok := namespace.Annotations[annotationKey]; ok {
+ return value
+ }
+ return ""
+}
+
+func GetPodLabelValue(pod *v1.Pod, labelKey string) string {
+ if value, ok := pod.Labels[labelKey]; ok {
+ return value
+ }
+ return ""
+}
+
+func GetTaskGroupFromPodSpec(pod *v1.Pod) string {
+ return GetPodAnnotationValue(pod, constants.AnnotationTaskGroupName)
+}
+
+func GetPlaceholderFlagFromPodSpec(pod *v1.Pod) bool {
+ if value := GetPodAnnotationValue(pod,
constants.AnnotationPlaceholderFlag); value != "" {
+ if v, err := strconv.ParseBool(value); err == nil {
+ return v
+ }
+ }
+
+ if value := GetPodLabelValue(pod, constants.LabelPlaceholderFlag);
value != "" {
+ if v, err := strconv.ParseBool(value); err == nil {
+ return v
+ }
+ }
+ return false
+}
+
+func GetTaskGroupsFromAnnotation(pod *v1.Pod) ([]v1alpha1.TaskGroup, error) {
+ taskGroupInfo := GetPodAnnotationValue(pod,
constants.AnnotationTaskGroups)
+ if taskGroupInfo == "" {
+ return nil, nil
+ }
+
+ taskGroups := []v1alpha1.TaskGroup{}
+ err := json.Unmarshal([]byte(taskGroupInfo), &taskGroups)
+ if err != nil {
+ return nil, err
+ }
+ // json.Unmarchal won't return error if name or MinMember is empty, but
will return error if MinResource is empty or error format.
+ for _, taskGroup := range taskGroups {
+ if taskGroup.Name == "" {
+ return nil, fmt.Errorf("can't get taskGroup Name from
pod annotation, %s",
+ taskGroupInfo)
+ }
+ if taskGroup.MinResource == nil {
+ return nil, fmt.Errorf("can't get taskGroup MinResource
from pod annotation, %s",
+ taskGroupInfo)
+ }
+ if taskGroup.MinMember == int32(0) {
+ return nil, fmt.Errorf("can't get taskGroup MinMember
from pod annotation, %s",
+ taskGroupInfo)
+ }
+ if taskGroup.MinMember < int32(0) {
+ return nil, fmt.Errorf("minMember cannot be negative,
%s",
+ taskGroupInfo)
+ }
+ }
+ return taskGroups, nil
+}
+
+// generate appID based on the namespace value,
+// and the max length of the ID is 63 chars.
+func generateAppID(namespace string) string {
+ generatedID := fmt.Sprintf("%s-%s-%s", constants.AutoGenAppPrefix,
namespace, constants.AutoGenAppSuffix)
+ appID := fmt.Sprintf("%.63s", generatedID)
+ return appID
+}
+
+func UpdatePodLabelForAdmissionController(pod *v1.Pod, namespace string)
map[string]string {
+ existingLabels := pod.Labels
+ result := make(map[string]string)
+ for k, v := range existingLabels {
+ result[k] = v
+ }
+
Review Comment:
I think we could use GetPodLabelValue(pod, key) instead of
existingLabels[key] and merge the if conditions in a line.
```
sparkAppID := GetPodLabelValue(pod, constants.SparkLabelAppID)
appID := GetPodLabelValue(pod, constants.LabelApplicationID)
if sparkAppID == "" && appID == "" {
// generatedID steps
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]