Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus containr port name #1099

Merged
merged 3 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3600,6 +3600,8 @@ spec:
maximum: 49151
minimum: 1024
type: integer
portName:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also needed in the ScheduledSparkApplication CRD.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liyinan926 Thanks, I updated it

type: string
required:
- jmxExporterJar
type: object
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/sparkoperator.k8s.io/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ type PrometheusSpec struct {
// +kubebuilder:validation:Maximum=49151
// +optional
Port *int32 `json:"port,omitempty"`
// PortName is the port name of prometheus JMX exporter port.
// If not specified, jmx-exporter will be used as the default.
// +optional
PortName *string `json:"portName,omitempty"`
// ConfigFile is the path to the custom Prometheus configuration file provided in the Spark image.
// ConfigFile takes precedence over Configuration, which is shown below.
// +optional
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ const DefaultPrometheusJavaAgentPort int32 = 8090
// DefaultPrometheusPortProtocol is the default protocol used by the Prometheus JMX exporter.
const DefaultPrometheusPortProtocol string = "TCP"

// DefaultPrometheusPortName is the default port name used by the Prometheus JMX exporter.
const DefaultPrometheusPortName string = "jmx-exporter"

const (
// SparkDriverContainerName is name of driver container in spark driver pod
SparkDriverContainerName = "spark-kubernetes-driver"
Expand Down
10 changes: 8 additions & 2 deletions pkg/webhook/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,19 @@ func getPrometheusConfigPatches(pod *corev1.Pod, app *v1beta2.SparkApplication)
port = *app.Spec.Monitoring.Prometheus.Port
}
protocol := config.DefaultPrometheusPortProtocol
portName := config.DefaultPrometheusPortName
if app.Spec.Monitoring.Prometheus.PortName != nil {
portName = *app.Spec.Monitoring.Prometheus.PortName
}

patchOps = append(patchOps, addConfigMapVolume(pod, name, volumeName))
vmPatchOp := addConfigMapVolumeMount(pod, volumeName, mountPath)
if vmPatchOp == nil {
glog.Warningf("could not mount volume %s in path %s", volumeName, mountPath)
return nil
}
patchOps = append(patchOps, *vmPatchOp)
portPatchOp := addContainerPort(pod, port, protocol)
portPatchOp := addContainerPort(pod, port, protocol, portName)
if portPatchOp == nil {
glog.Warningf("could not expose port %d to scrape metrics outside the pod", port)
return nil
Expand All @@ -392,7 +397,7 @@ func getPrometheusConfigPatches(pod *corev1.Pod, app *v1beta2.SparkApplication)
return patchOps
}

func addContainerPort(pod *corev1.Pod, port int32, protocol string) *patchOperation {
func addContainerPort(pod *corev1.Pod, port int32, protocol string, portName string) *patchOperation {
i := findContainer(pod)
if i < 0 {
glog.Warningf("not able to add containerPort %d as Spark container was not found in pod %s", port, pod.Name)
Expand All @@ -401,6 +406,7 @@ func addContainerPort(pod *corev1.Pod, port int32, protocol string) *patchOperat

path := fmt.Sprintf("/spec/containers/%d/ports", i)
containerPort := corev1.ContainerPort{
Name: portName,
ContainerPort: port,
Protocol: corev1.Protocol(protocol),
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ func TestPatchSparkPod_HadoopConfigMap(t *testing.T) {

func TestPatchSparkPod_PrometheusConfigMaps(t *testing.T) {
var appPort int32 = 9999
appPortName := "jmx-exporter"
app := &v1beta2.SparkApplication{
ObjectMeta: metav1.ObjectMeta{
Name: "spark-test",
Expand All @@ -509,6 +510,7 @@ func TestPatchSparkPod_PrometheusConfigMaps(t *testing.T) {
Prometheus: &v1beta2.PrometheusSpec{
JmxExporterJar: "",
Port: &appPort,
PortName: &appPortName,
ConfigFile: nil,
Configuration: nil,
},
Expand Down Expand Up @@ -543,6 +545,7 @@ func TestPatchSparkPod_PrometheusConfigMaps(t *testing.T) {
expectedConfigMapName := config.GetPrometheusConfigMapName(app)
expectedVolumeName := expectedConfigMapName + "-vol"
expectedContainerPort := *app.Spec.Monitoring.Prometheus.Port
expectedContainerPortName := *app.Spec.Monitoring.Prometheus.PortName
assert.Equal(t, 1, len(modifiedPod.Spec.Volumes))
assert.Equal(t, expectedVolumeName, modifiedPod.Spec.Volumes[0].Name)
assert.True(t, modifiedPod.Spec.Volumes[0].ConfigMap != nil)
Expand All @@ -551,6 +554,7 @@ func TestPatchSparkPod_PrometheusConfigMaps(t *testing.T) {
assert.Equal(t, expectedVolumeName, modifiedPod.Spec.Containers[0].VolumeMounts[0].Name)
assert.Equal(t, config.PrometheusConfigMapMountPath, modifiedPod.Spec.Containers[0].VolumeMounts[0].MountPath)
assert.Equal(t, expectedContainerPort, modifiedPod.Spec.Containers[0].Ports[0].ContainerPort)
assert.Equal(t, expectedContainerPortName, modifiedPod.Spec.Containers[0].Ports[0].Name)
assert.Equal(t, corev1.Protocol(config.DefaultPrometheusPortProtocol), modifiedPod.Spec.Containers[0].Ports[0].Protocol)
}

Expand Down