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

fix: webhook panics due to logging #2232

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all 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
21 changes: 9 additions & 12 deletions internal/webhook/sparkpod_defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func addVolume(pod *corev1.Pod, volume corev1.Volume) error {
func addVolumeMount(pod *corev1.Pod, mount corev1.VolumeMount) error {
i := findContainer(pod)
if i < 0 {
logger.Info("not able to add VolumeMount %s as Spark container was not found in pod %s", mount.Name, pod.Name)
return fmt.Errorf("failed to add volumeMounts as Spark container not found")
}
Comment on lines 192 to 195
Copy link
Contributor

Choose a reason for hiding this comment

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

why not modify findContainer to return a tuple (int, error) instead ?
And call it earlier in the code instead of every patch operation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the logic of findContainer can be optimized as you said, maybe we can do it in a separated PR.


pod.Spec.Containers[i].VolumeMounts = append(pod.Spec.Containers[i].VolumeMounts, mount)
Expand Down Expand Up @@ -228,7 +228,7 @@ func addEnvFrom(pod *corev1.Pod, app *v1beta2.SparkApplication) error {

i := findContainer(pod)
if i < 0 {
return fmt.Errorf("not able to add EnvFrom as Spark container was not found in pod")
return fmt.Errorf("failed to add envFrom as Spark container not found")
}

pod.Spec.Containers[i].EnvFrom = append(pod.Spec.Containers[i].EnvFrom, envFrom...)
Expand All @@ -238,7 +238,7 @@ func addEnvFrom(pod *corev1.Pod, app *v1beta2.SparkApplication) error {
func addEnvironmentVariable(pod *corev1.Pod, name, value string) error {
i := findContainer(pod)
if i < 0 {
return fmt.Errorf("not able to add environment variable as Spark container was not found")
return fmt.Errorf("failed to add env as Spark container not found")
}

pod.Spec.Containers[i].Env = append(pod.Spec.Containers[i].Env, corev1.EnvVar{
Expand Down Expand Up @@ -345,13 +345,11 @@ func addPrometheusConfig(pod *corev1.Pod, app *v1beta2.SparkApplication) error {
}

if err := addConfigMapVolumeMount(pod, volumeName, mountPath); err != nil {
logger.Info("could not mount volume %s in path %s", volumeName, mountPath)
return err
return fmt.Errorf("failed to mount volume %s in path %s: %v", volumeName, mountPath, err)
}

if err := addContainerPort(pod, promPort, promProtocol, promPortName); err != nil {
logger.Info("could not expose port %d to scrape metrics outside the pod", promPort)
return err
return fmt.Errorf("failed to expose port %d to scrape metrics outside the pod: %v", promPort, err)
}

return nil
Expand All @@ -368,7 +366,7 @@ func addContainerPorts(pod *corev1.Pod, app *v1beta2.SparkApplication) error {

for _, p := range ports {
if err := addContainerPort(pod, p.ContainerPort, p.Protocol, p.Name); err != nil {
logger.Info("could not expose port named %s", p.Name)
return fmt.Errorf("failed to expose port named %s: %v", p.Name, err)
}
}
return nil
Expand All @@ -377,7 +375,7 @@ func addContainerPorts(pod *corev1.Pod, app *v1beta2.SparkApplication) error {
func addContainerPort(pod *corev1.Pod, port int32, protocol string, portName string) error {
i := findContainer(pod)
if i < 0 {
return fmt.Errorf("not able to add containerPort %d as Spark container was not found in pod", port)
return fmt.Errorf("failed to add containerPort %d as Spark container not found", port)
}

containerPort := corev1.ContainerPort{
Expand Down Expand Up @@ -605,7 +603,7 @@ func addGPU(pod *corev1.Pod, app *v1beta2.SparkApplication) error {

i := findContainer(pod)
if i < 0 {
return fmt.Errorf("not able to add GPU as Spark container was not found in pod %s", pod.Name)
return fmt.Errorf("failed to add GPU as Spark container was not found in pod %s", pod.Name)
}
if pod.Spec.Containers[i].Resources.Limits == nil {
pod.Spec.Containers[i].Resources.Limits = make(corev1.ResourceList)
Expand Down Expand Up @@ -672,8 +670,7 @@ func addPodLifeCycleConfig(pod *corev1.Pod, app *v1beta2.SparkApplication) error
}
}
if i == len(pod.Spec.Containers) {
logger.Info("Spark container %s not found in pod %s", containerName, pod.Name)
return nil
return fmt.Errorf("Spark container %s not found in pod %s", containerName, pod.Name)
}

pod.Spec.Containers[i].Lifecycle = lifeCycle
Expand Down