From f9c7b8b019752a2b8ceea6c7942436b5b2734a30 Mon Sep 17 00:00:00 2001 From: PoAn Yang Date: Wed, 22 Nov 2023 10:39:55 +0800 Subject: [PATCH] feat: add GOCOVERDIR for deployment Signed-off-by: PoAn Yang --- .../backing_image_data_source_controller.go | 1 + .../backing_image_manager_controller.go | 2 + controller/engine_image_controller.go | 1 + controller/instance_manager_controller.go | 1 + controller/share_manager_controller.go | 1 + csi/deployment.go | 1 + types/deploy.go | 77 +++++++++++++++++++ 7 files changed, 84 insertions(+) diff --git a/controller/backing_image_data_source_controller.go b/controller/backing_image_data_source_controller.go index dbe578c76e..46031606df 100644 --- a/controller/backing_image_data_source_controller.go +++ b/controller/backing_image_data_source_controller.go @@ -757,6 +757,7 @@ func (c *BackingImageDataSourceController) generateBackingImageDataSourcePodMani podSpec.Annotations[nadAnnot] = types.CreateCniAnnotationFromSetting(storageNetwork) } + types.AddGoCoverDirToPod(podSpec) return podSpec, nil } diff --git a/controller/backing_image_manager_controller.go b/controller/backing_image_manager_controller.go index 918d43eb32..68cf4ae198 100644 --- a/controller/backing_image_manager_controller.go +++ b/controller/backing_image_manager_controller.go @@ -789,6 +789,7 @@ func (c *BackingImageManagerController) createBackingImageManagerPod(bim *longho if err != nil { return err } + if _, err := c.ds.CreatePod(podManifest); err != nil && !apierrors.IsAlreadyExists(err) { return err } @@ -902,6 +903,7 @@ func (c *BackingImageManagerController) generateBackingImageManagerPodManifest(b podSpec.Annotations[nadAnnot] = types.CreateCniAnnotationFromSetting(storageNetwork) } + types.AddGoCoverDirToPod(podSpec) return podSpec, nil } diff --git a/controller/engine_image_controller.go b/controller/engine_image_controller.go index eaf83488d5..6e1c0f616c 100644 --- a/controller/engine_image_controller.go +++ b/controller/engine_image_controller.go @@ -867,6 +867,7 @@ func (ic *EngineImageController) createEngineImageDaemonSetSpec(ei *longhorn.Eng }, } } + types.AddGoCoverDirToDaemonSet(d) return d, nil } diff --git a/controller/instance_manager_controller.go b/controller/instance_manager_controller.go index a8cfe23193..ee6b4fe450 100644 --- a/controller/instance_manager_controller.go +++ b/controller/instance_manager_controller.go @@ -1256,6 +1256,7 @@ func (imc *InstanceManagerController) createInstanceManagerPodSpec(im *longhorn. }, }) } + types.AddGoCoverDirToPod(podSpec) return podSpec, nil } diff --git a/controller/share_manager_controller.go b/controller/share_manager_controller.go index 90924813e2..88f934b7bd 100644 --- a/controller/share_manager_controller.go +++ b/controller/share_manager_controller.go @@ -979,6 +979,7 @@ func (c *ShareManagerController) createPodManifest(sm *longhorn.ShareManager, an if resourceReq != nil { podSpec.Spec.Containers[0].Resources = *resourceReq } + types.AddGoCoverDirToPod(podSpec) return podSpec } diff --git a/csi/deployment.go b/csi/deployment.go index f0b37c83cf..6ba8ad692d 100644 --- a/csi/deployment.go +++ b/csi/deployment.go @@ -508,6 +508,7 @@ func NewPluginDeployment(namespace, serviceAccount, nodeDriverRegistrarImage, li }, } } + types.AddGoCoverDirToDaemonSet(daemonSet) return &PluginDeployment{ daemonSet: daemonSet, diff --git a/types/deploy.go b/types/deploy.go index c2aa6cd017..b9ebf993fc 100644 --- a/types/deploy.go +++ b/types/deploy.go @@ -1,5 +1,12 @@ package types +import ( + "os" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" +) + const ( LonghornManagerDaemonSetName = "longhorn-manager" LonghornUIDeploymentName = "longhorn-ui" @@ -11,3 +18,73 @@ const ( CSISnapshotterName = "csi-snapshotter" CSIPluginName = "longhorn-csi-plugin" ) + +// AddGoCoverDirToPod adds GOCOVERDIR env and host path volume to a pod. +// It's used to collect coverage data from a pod. +func AddGoCoverDirToPod(pod *corev1.Pod) { + if pod == nil || len(pod.Spec.Containers) == 0 { + return + } + + goCoverDir := os.Getenv("GOCOVERDIR") + if goCoverDir == "" { + return + } + + pod.Spec.Containers[0].Env = append( + pod.Spec.Containers[0].Env, + corev1.EnvVar{Name: "GOCOVERDIR", Value: goCoverDir}, + ) + pod.Spec.Containers[0].VolumeMounts = append( + pod.Spec.Containers[0].VolumeMounts, + corev1.VolumeMount{Name: "go-cover-dir", MountPath: goCoverDir}, + ) + hostPathType := corev1.HostPathDirectoryOrCreate + pod.Spec.Volumes = append( + pod.Spec.Volumes, + corev1.Volume{ + Name: "go-cover-dir", + VolumeSource: corev1.VolumeSource{ + HostPath: &corev1.HostPathVolumeSource{ + Path: goCoverDir, + Type: &hostPathType, + }, + }, + }, + ) +} + +// AddGoCoverDirToDaemonSet adds GOCOVERDIR env and host path volume to a daemonset. +// It's used to collect coverage data from a daemonset. +func AddGoCoverDirToDaemonSet(daemonset *appsv1.DaemonSet) { + if daemonset == nil || len(daemonset.Spec.Template.Spec.Containers) == 0 { + return + } + + goCoverDir := os.Getenv("GOCOVERDIR") + if goCoverDir == "" { + return + } + + daemonset.Spec.Template.Spec.Containers[0].Env = append( + daemonset.Spec.Template.Spec.Containers[0].Env, + corev1.EnvVar{Name: "GOCOVERDIR", Value: goCoverDir}, + ) + daemonset.Spec.Template.Spec.Containers[0].VolumeMounts = append( + daemonset.Spec.Template.Spec.Containers[0].VolumeMounts, + corev1.VolumeMount{Name: "go-cover-dir", MountPath: goCoverDir}, + ) + hostPathType := corev1.HostPathDirectoryOrCreate + daemonset.Spec.Template.Spec.Volumes = append( + daemonset.Spec.Template.Spec.Volumes, + corev1.Volume{ + Name: "go-cover-dir", + VolumeSource: corev1.VolumeSource{ + HostPath: &corev1.HostPathVolumeSource{ + Path: goCoverDir, + Type: &hostPathType, + }, + }, + }, + ) +}