Files
my-rook-config/pkg/operator/k8sutil/job.go
T
Joshua Hoblitt b29e89ef67 k8sutil: remove unused helper functions
Remove dead code from the k8sutil package; none of these symbols had any
callers anywhere in the repo (verified with deadcode, staticcheck U1000,
and repo-wide grep, tests included):

- patcher.go: delete file (Patcher type, NewPatcher, Patch)
- replicaset.go: delete file (DeleteReplicaSet)
- customresource.go: WatchCR (the CustomResource type remains in use)
- deployment.go: WaitForDeploymentImage
- job.go: WaitForJobCompletion
- k8sutil.go: GetK8SVersion
- pod.go: ConfigOverrideMount, ConfigOverrideVolume, GetPodLog
- prometheus.go: DeleteServiceMonitor
- service.go: DeleteService
- volume.go: NodeConfigURI, BinariesMountInfo, YamlToVolumes,
  YamlToVolumeMounts, and the BinariesMountPath const (only used by the
  removed BinariesMountInfo)

Now-unused imports are dropped from each file. The shared
deleteResourceAndWait helper remains (still used by DeleteDeployment).

Signed-off-by: Joshua Hoblitt <josh@hoblitt.com>
2026-06-09 15:05:19 -07:00

106 lines
3.6 KiB
Go

/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package k8sutil for Kubernetes helpers.
package k8sutil
import (
"context"
"fmt"
"time"
batch "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// RunReplaceableJob runs a Kubernetes job with the intention that the job can be replaced by
// another call to this function with the same job name. For example, if a storage operator is
// restarted/updated before the job can complete, the operator's next run of the job should replace
// the previous job if deleteIfFound is set to true.
func RunReplaceableJob(ctx context.Context, clientset kubernetes.Interface, job *batch.Job, deleteIfFound bool) error {
// check if the job was already created and what its status is
existingJob, err := clientset.BatchV1().Jobs(job.Namespace).Get(ctx, job.Name, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
logger.Warningf("failed to detect job %s. %+v", job.Name, err)
} else if err == nil {
// if the job is still running, and the caller has not asked for deletion,
// allow it to continue to completion
if existingJob.Status.Active > 0 && !deleteIfFound {
logger.Infof("Found previous job %s. Status=%+v", job.Name, existingJob.Status)
return nil
}
// delete the job that already exists from a previous run
logger.Infof("Removing previous job %s to start a new one", job.Name)
err := DeleteBatchJob(ctx, clientset, job.Namespace, existingJob.Name, true)
if err != nil {
return fmt.Errorf("failed to remove job %s. %+v", job.Name, err)
}
}
_, err = clientset.BatchV1().Jobs(job.Namespace).Create(ctx, job, metav1.CreateOptions{})
return err
}
// DeleteBatchJob deletes a Kubernetes job.
func DeleteBatchJob(ctx context.Context, clientset kubernetes.Interface, namespace, name string, wait bool) error {
propagation := metav1.DeletePropagationForeground
gracePeriod := int64(0)
options := &metav1.DeleteOptions{GracePeriodSeconds: &gracePeriod, PropagationPolicy: &propagation}
if err := clientset.BatchV1().Jobs(namespace).Delete(ctx, name, *options); err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to remove previous provisioning job for node %s. %+v", name, err)
}
if !wait {
return nil
}
// Retry for the job to be deleted for 90s. A pod can easily take 60s to timeout before
// deletion so we add some buffer to that time.
retries := 30
sleepInterval := 3 * time.Second
for i := 0; i < retries; i++ {
_, err := clientset.BatchV1().Jobs(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
logger.Infof("batch job %s deleted", name)
return nil
}
logger.Infof("batch job %s still exists", name)
time.Sleep(sleepInterval)
}
logger.Warningf("gave up waiting for batch job %s to be deleted", name)
return nil
}
// AddRookVersionLabelToJob adds or updates a label reporting the Rook version which last
// modified a Job.
func AddRookVersionLabelToJob(j *batch.Job) {
if j == nil {
return
}
if j.Labels == nil {
j.Labels = map[string]string{}
}
addRookVersionLabel(j.Labels)
}