Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Jan 26, 2024
1 parent 23e65c3 commit 8993f3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package constants

const (
ProbeNamePrefix = "pie-probe"
ProvisionProbeNamePrefix = "provision-probe"
MountProbeNamePrefix = "mount-probe"
ProvisionProbeNamePrefix = "provision"
MountProbeNamePrefix = "mount"
ProbeContainerName = "probe"
PodFinalizerName = "pie.topolvm.io/pod"
NodeFinalizerName = "pie.topolvm.io/node"
Expand Down
31 changes: 13 additions & 18 deletions internal/controller/pie/pieprobe_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
for _, node := range availableNodeList.Items {
err = r.createOrUpdatePVC(
ctx,
pieProbe.Namespace,
node.Name,
&pieProbe,
)
Expand Down Expand Up @@ -242,8 +241,8 @@ func getPVCName(nodeName string, pieProbe *piev1alpha1.PieProbe) string {
io.WriteString(sha1, pieProbeName+"\000"+nodeName+"\000"+storageClass)
hashedName := hex.EncodeToString(sha1.Sum(nil))

if len(pieProbeName) > 7 {
pieProbeName = pieProbeName[:7]
if len(pieProbeName) > 10 {
pieProbeName = pieProbeName[:10]
}
if len(nodeName) > 11 {
nodeName = nodeName[:11]
Expand All @@ -256,7 +255,6 @@ func getPVCName(nodeName string, pieProbe *piev1alpha1.PieProbe) string {

func (r *PieProbeReconciler) createOrUpdatePVC(
ctx context.Context,
namespace string,
nodeName string,
pieProbe *piev1alpha1.PieProbe,
) error {
Expand All @@ -266,7 +264,7 @@ func (r *PieProbeReconciler) createOrUpdatePVC(
storageClass := pieProbe.Spec.MonitoringStorageClass

pvc := &corev1.PersistentVolumeClaim{}
pvc.SetNamespace(namespace)
pvc.SetNamespace(pieProbe.GetNamespace())
pvc.SetName(pvcName)

op, err := ctrl.CreateOrUpdate(ctx, r.client, pvc, func() error {
Expand Down Expand Up @@ -301,13 +299,14 @@ func (r *PieProbeReconciler) createOrUpdatePVC(
return nil
}

func makeCronSchedule(storageClass string, nodeNamePtr *string, period int) string {
func makeCronSchedule(pieProbeName string, storageClass string, nodeNamePtr *string, period int) string {
nodeName := ""
if nodeNamePtr != nil {
nodeName = *nodeNamePtr
}

h := crc32.NewIEEE()
h.Write([]byte(pieProbeName))
h.Write([]byte(storageClass))
h.Write([]byte(nodeName))

Expand All @@ -334,16 +333,12 @@ func (r *PieProbeReconciler) createOrUpdateJob(
logger.Info("createOrUpdateJob")
defer logger.Info("createOrUpdateJob Finished")

storageClass := pieProbe.Spec.MonitoringStorageClass
namespace := pieProbe.Namespace
containerImage := pieProbe.Spec.ContainerImage
controllerURL := pieProbe.Spec.ControllerUrl
probePeriod := pieProbe.Spec.ProbePeriod

cronjob := &batchv1.CronJob{}
cronjob.SetNamespace(namespace)
cronjob.SetNamespace(pieProbe.GetNamespace())
cronjob.SetName(getCronJobName(kind, nodeName, pieProbe))

storageClass := pieProbe.Spec.MonitoringStorageClass

op, err := ctrl.CreateOrUpdate(ctx, r.client, cronjob, func() error {
label := map[string]string{
constants.ProbeStorageClassLabelKey: storageClass,
Expand All @@ -355,7 +350,7 @@ func (r *PieProbeReconciler) createOrUpdateJob(
cronjob.SetLabels(label)

cronjob.Spec.ConcurrencyPolicy = batchv1.ForbidConcurrent
cronjob.Spec.Schedule = makeCronSchedule(storageClass, nodeName, probePeriod)
cronjob.Spec.Schedule = makeCronSchedule(pieProbe.GetName(), storageClass, nodeName, pieProbe.Spec.ProbePeriod)

var successfulJobsHistoryLimit = int32(0)
cronjob.Spec.SuccessfulJobsHistoryLimit = &successfulJobsHistoryLimit
Expand All @@ -374,7 +369,7 @@ func (r *PieProbeReconciler) createOrUpdateJob(
volumeName := "genericvol"
container := &cronjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0]
container.Name = constants.ProbeContainerName
container.Image = containerImage
container.Image = pieProbe.Spec.ContainerImage

var userID int64 = 1001
var groupID int64 = 1001
Expand Down Expand Up @@ -423,7 +418,7 @@ func (r *PieProbeReconciler) createOrUpdateJob(
}
container.Args = []string{
"probe",
fmt.Sprintf("--destination-address=%s", controllerURL),
fmt.Sprintf("--destination-address=%s", pieProbe.Spec.ControllerUrl),
"--path=/mounted/",
fmt.Sprintf("--node-name=%s", *nodeName),
fmt.Sprintf("--storage-class=%s", storageClass),
Expand Down Expand Up @@ -492,8 +487,8 @@ func getCronJobName(kind int, nodeNamePtr *string, pieProbe *piev1alpha1.PieProb
io.WriteString(sha1, pieProbeName+"\000"+nodeName+"\000"+storageClass)
hashedName := hex.EncodeToString(sha1.Sum(nil))

if len(pieProbeName) > 7 {
pieProbeName = pieProbeName[:7]
if len(pieProbeName) > 10 {
pieProbeName = pieProbeName[:10]
}
if len(nodeName) > 11 {
nodeName = nodeName[:11]
Expand Down

0 comments on commit 8993f3b

Please sign in to comment.