forked from kubeflow/spark-operator
/
sparkui.go
153 lines (133 loc) · 4.46 KB
/
sparkui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2017 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sparkapplication
import (
"fmt"
"regexp"
"strconv"
"github.com/golang/glog"
apiv1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
clientset "k8s.io/client-go/kubernetes"
"github.com/GoogleCloudPlatform/spark-on-k8s-operator/pkg/apis/sparkoperator.k8s.io/v1beta2"
"github.com/GoogleCloudPlatform/spark-on-k8s-operator/pkg/config"
)
const (
sparkUIPortConfigurationKey = "spark.ui.port"
defaultSparkWebUIPort = "4040"
)
var ingressURLRegex = regexp.MustCompile("{{\\s*[$]appName\\s*}}")
func getSparkUIingressURL(ingressURLFormat string, appName string) string {
return ingressURLRegex.ReplaceAllString(ingressURLFormat, appName)
}
// SparkService encapsulates information about the driver UI service.
type SparkService struct {
serviceName string
servicePort int32
serviceIP string
}
// SparkIngress encapsulates information about the driver UI ingress.
type SparkIngress struct {
ingressName string
ingressURL string
}
func createSparkUIIngress(app *v1beta2.SparkApplication, service SparkService, ingressURLFormat string, kubeClient clientset.Interface) (*SparkIngress, error) {
ingressURL := getSparkUIingressURL(ingressURLFormat, app.GetName())
ingress := extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: getDefaultUIIngressName(app),
Namespace: app.Namespace,
Labels: getResourceLabels(app),
OwnerReferences: []metav1.OwnerReference{*getOwnerReference(app)},
},
Spec: extensions.IngressSpec{
Rules: []extensions.IngressRule{{
Host: ingressURL,
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: []extensions.HTTPIngressPath{{
Backend: extensions.IngressBackend{
ServiceName: service.serviceName,
ServicePort: intstr.IntOrString{
Type: intstr.Int,
IntVal: service.servicePort,
},
},
}},
},
},
}},
},
}
glog.Infof("Creating an Ingress %s for the Spark UI for application %s", ingress.Name, app.Name)
_, err := kubeClient.ExtensionsV1beta1().Ingresses(ingress.Namespace).Create(&ingress)
if err != nil {
return nil, err
}
return &SparkIngress{
ingressName: ingress.Name,
ingressURL: ingressURL,
}, nil
}
func createSparkUIService(
app *v1beta2.SparkApplication,
kubeClient clientset.Interface) (*SparkService, error) {
portStr := getUITargetPort(app)
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, fmt.Errorf("invalid Spark UI port: %s", portStr)
}
service := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: getDefaultUIServiceName(app),
Namespace: app.Namespace,
Labels: getResourceLabels(app),
OwnerReferences: []metav1.OwnerReference{*getOwnerReference(app)},
},
Spec: apiv1.ServiceSpec{
Ports: []apiv1.ServicePort{
{
Name: "spark-driver-ui-port",
Port: int32(port),
},
},
Selector: map[string]string{
config.SparkAppNameLabel: app.Name,
config.SparkRoleLabel: config.SparkDriverRole,
},
Type: apiv1.ServiceTypeClusterIP,
},
}
glog.Infof("Creating a service %s for the Spark UI for application %s", service.Name, app.Name)
service, err = kubeClient.CoreV1().Services(app.Namespace).Create(service)
if err != nil {
return nil, err
}
return &SparkService{
serviceName: service.Name,
servicePort: service.Spec.Ports[0].Port,
serviceIP: service.Spec.ClusterIP,
}, nil
}
// getWebUITargetPort attempts to get the Spark web UI port from configuration property spark.ui.port
// in Spec.SparkConf if it is present, otherwise the default port is returned.
// Note that we don't attempt to get the port from Spec.SparkConfigMap.
func getUITargetPort(app *v1beta2.SparkApplication) string {
port, ok := app.Spec.SparkConf[sparkUIPortConfigurationKey]
if ok {
return port
}
return defaultSparkWebUIPort
}