|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/kubeflow/spark-operator/api/v1beta2" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func Int32Pointer(a int32) *int32 { |
| 13 | + return &a |
| 14 | +} |
| 15 | +func LabelsForSpark() map[string]string { |
| 16 | + return map[string]string{"version": "3.0.1"} |
| 17 | +} |
| 18 | +func BoolPointer(a bool) *bool { |
| 19 | + return &a |
| 20 | +} |
| 21 | + |
| 22 | +func StringPointer(a string) *string { |
| 23 | + return &a |
| 24 | +} |
| 25 | + |
| 26 | +func GetAppNamespace(app *v1beta2.SparkApplication) string { |
| 27 | + namespace := "default" |
| 28 | + if app.Namespace != "" { |
| 29 | + namespace = app.Namespace |
| 30 | + } else { |
| 31 | + spakConfNamespace, namespaceExists := app.Spec.SparkConf[SparkAppNamespaceKey] |
| 32 | + if namespaceExists { |
| 33 | + namespace = spakConfNamespace |
| 34 | + } |
| 35 | + } |
| 36 | + return namespace |
| 37 | +} |
| 38 | + |
| 39 | +// Helper func to get driver pod name from Spark Application CRD instance |
| 40 | +func GetDriverPodName(app *v1beta2.SparkApplication) string { |
| 41 | + name := app.Spec.Driver.PodName |
| 42 | + if name != nil && len(*name) > 0 { |
| 43 | + return *name |
| 44 | + } |
| 45 | + sparkConf := app.Spec.SparkConf |
| 46 | + if sparkConf[SparkDriverPodNameKey] != "" { |
| 47 | + return sparkConf[SparkDriverPodNameKey] |
| 48 | + } |
| 49 | + return fmt.Sprintf("%s-driver", app.Name) |
| 50 | +} |
| 51 | +func GetDriverPort(sparkConfKeyValuePairs map[string]string) int { |
| 52 | + //Checking if port information is passed in the spec, and using same |
| 53 | + // or using the default ones |
| 54 | + driverPortToBeUsed := DefaultDriverPort |
| 55 | + driverPort, valueExists := sparkConfKeyValuePairs[SparkDriverPort] |
| 56 | + if valueExists { |
| 57 | + driverPortSupplied, err := strconv.Atoi(driverPort) |
| 58 | + if err != nil { |
| 59 | + panic("Driver Port not parseable - hence failing the spark submit" + fmt.Sprint(err)) |
| 60 | + } else { |
| 61 | + driverPortToBeUsed = driverPortSupplied |
| 62 | + } |
| 63 | + } |
| 64 | + return driverPortToBeUsed |
| 65 | +} |
| 66 | + |
| 67 | +// Helper func to get Owner references to be added to Spark Application resources - pod, service, configmap |
| 68 | +func GetOwnerReference(app *v1beta2.SparkApplication) *metav1.OwnerReference { |
| 69 | + controller := false |
| 70 | + return &metav1.OwnerReference{ |
| 71 | + APIVersion: v1beta2.SchemeGroupVersion.String(), |
| 72 | + Kind: reflect.TypeOf(v1beta2.SparkApplication{}).Name(), |
| 73 | + Name: app.Name, |
| 74 | + UID: app.UID, |
| 75 | + Controller: &controller, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func GetMemoryOverheadFactor(app *v1beta2.SparkApplication) string { |
| 80 | + var memoryOverheadFactor string |
| 81 | + if app.Spec.Type == v1beta2.SparkApplicationTypeJava || app.Spec.Type == v1beta2.SparkApplicationTypeScala { |
| 82 | + memoryOverheadFactor = JavaScalaMemoryOverheadFactor |
| 83 | + } else { |
| 84 | + |
| 85 | + memoryOverheadFactor = OtherLanguageMemoryOverheadFactor |
| 86 | + } |
| 87 | + return memoryOverheadFactor |
| 88 | +} |
| 89 | +func CheckSparkConf(sparkConf map[string]string, configKey string) bool { |
| 90 | + valueExists := false |
| 91 | + _, valueExists = sparkConf[configKey] |
| 92 | + return valueExists |
| 93 | +} |
| 94 | +func Int64Pointer(a int64) *int64 { |
| 95 | + return &a |
| 96 | +} |
0 commit comments