Skip to content

Commit

Permalink
feat: add GOCOVERDIR for deployment
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <poan.yang@suse.com>
  • Loading branch information
FrankYang0529 authored and David Ko committed Nov 22, 2023
1 parent 8aed086 commit f9c7b8b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions controller/backing_image_data_source_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ func (c *BackingImageDataSourceController) generateBackingImageDataSourcePodMani
podSpec.Annotations[nadAnnot] = types.CreateCniAnnotationFromSetting(storageNetwork)
}

types.AddGoCoverDirToPod(podSpec)
return podSpec, nil
}

Expand Down
2 changes: 2 additions & 0 deletions controller/backing_image_manager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -902,6 +903,7 @@ func (c *BackingImageManagerController) generateBackingImageManagerPodManifest(b
podSpec.Annotations[nadAnnot] = types.CreateCniAnnotationFromSetting(storageNetwork)
}

types.AddGoCoverDirToPod(podSpec)
return podSpec, nil
}

Expand Down
1 change: 1 addition & 0 deletions controller/engine_image_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ func (ic *EngineImageController) createEngineImageDaemonSetSpec(ei *longhorn.Eng
},
}
}
types.AddGoCoverDirToDaemonSet(d)

return d, nil
}
Expand Down
1 change: 1 addition & 0 deletions controller/instance_manager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ func (imc *InstanceManagerController) createInstanceManagerPodSpec(im *longhorn.
},
})
}
types.AddGoCoverDirToPod(podSpec)

return podSpec, nil
}
Expand Down
1 change: 1 addition & 0 deletions controller/share_manager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions csi/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func NewPluginDeployment(namespace, serviceAccount, nodeDriverRegistrarImage, li
},
}
}
types.AddGoCoverDirToDaemonSet(daemonSet)

return &PluginDeployment{
daemonSet: daemonSet,
Expand Down
77 changes: 77 additions & 0 deletions types/deploy.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
},
},
},
)
}

0 comments on commit f9c7b8b

Please sign in to comment.