Skip to content

Commit

Permalink
Merge pull request #207 from donpenney/precache-inherit-env
Browse files Browse the repository at this point in the history
precache: Include env vars from manager pod when creating precache job
  • Loading branch information
openshift-merge-bot[bot] authored Jan 5, 2024
2 parents 20a1f19 + 511e709 commit e18a496
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
32 changes: 31 additions & 1 deletion controllers/prep_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -153,6 +154,29 @@ func readPrecachingList(imageListFile, clusterRegistry, seedRegistry string, ove
return imageList, nil
}

func (r *ImageBasedUpgradeReconciler) getPodEnvVars(ctx context.Context) (envVars []corev1.EnvVar, err error) {
pod := &corev1.Pod{}
if err = r.Client.Get(ctx, types.NamespacedName{Name: os.Getenv("MY_POD_NAME"), Namespace: common.LcaNamespace}, pod); err != nil {
err = fmt.Errorf("failed to get pod info: %w", err)
return
}

for _, container := range pod.Spec.Containers {
if container.Name == "manager" {
for _, envVar := range container.Env {
if envVar.ValueFrom != nil {
// Skipping any valueFrom env variables
continue
}
envVars = append(envVars, envVar)
}
break
}
}

return
}

func (r *ImageBasedUpgradeReconciler) launchPrecaching(ctx context.Context, imageListFile string, ibu *lcav1alpha1.ImageBasedUpgrade) (bool, error) {
clusterRegistry, err := commonUtils.GetReleaseRegistry(ctx, r.Client)
if err != nil {
Expand All @@ -176,8 +200,14 @@ func (r *ImageBasedUpgradeReconciler) launchPrecaching(ctx context.Context, imag
return false, err
}

envVars, err := r.getPodEnvVars(ctx)
if err != nil {
err = fmt.Errorf("failed to get pod env vars: %w", err)
return false, err
}

// Create pre-cache config using default values
config := precache.NewConfig(imageList)
config := precache.NewConfig(imageList, envVars)
err = r.Precache.CreateJob(ctx, config)
if err != nil {
r.Log.Error(err, "Failed to create precaching job")
Expand Down
1 change: 1 addition & 0 deletions docs/precache-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The `Config` struct defines the configuration options for a pre-caching job. The
- `NicePriority`: Nice priority for pre-caching, affecting process scheduling.
- `IoNiceClass`: I/O scheduling class for pre-caching (0: none, 1: realtime, 2: best-effort, 3: idle).
- `IoNicePriority`: I/O nice priority for pre-caching.
- `EnvVars`: A list of container spec environment variables to be set in the job definition.

### 2. ConfigMap Generation

Expand Down
14 changes: 3 additions & 11 deletions internal/precache/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func renderJob(config *Config, log logr.Logger) (*batchv1.Job, error) {
execPrecacheArgs := fmt.Sprintf("nice -n %d ionice -c %d -n %d precache",
nicePriority, ioNiceClass, ioNicePriority)

envVars := []corev1.EnvVar{
precacheEnvVars := append(config.EnvVars, []corev1.EnvVar{
{
Name: EnvPrecacheSpecFile,
Value: filepath.Join(PrecachingSpecFilepath, PrecachingSpecFilename),
Expand All @@ -165,15 +165,7 @@ func renderJob(config *Config, log logr.Logger) (*batchv1.Job, error) {
Name: EnvMaxPullThreads,
Value: strconv.Itoa(numConcurrentPulls),
},
}

envBestEffort := os.Getenv(EnvPrecacheBestEffort)
if envBestEffort == "TRUE" {
envVars = append(envVars, corev1.EnvVar{
Name: EnvPrecacheBestEffort,
Value: "TRUE",
})
}
}...)

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -194,7 +186,7 @@ func renderJob(config *Config, log logr.Logger) (*batchv1.Job, error) {
ImagePullPolicy: corev1.PullAlways,
Command: []string{"sh", "-c", "--"},
Args: []string{execPrecacheArgs},
Env: envVars,
Env: precacheEnvVars,
SecurityContext: &corev1.SecurityContext{
Privileged: &privileged,
RunAsUser: &runAsUser,
Expand Down
6 changes: 3 additions & 3 deletions internal/precache/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestRenderJob(t *testing.T) {
}{
{
name: "Fully specified, valid precaching config",
config: NewConfig([]string{}, "NumConcurrentPulls", 1, "NicePriority", 1, "IoNiceClass", IoNiceClassRealTime, "IoNicePriority", 5),
config: NewConfig([]string{}, []corev1.EnvVar{}, "NumConcurrentPulls", 1, "NicePriority", 1, "IoNiceClass", IoNiceClassRealTime, "IoNicePriority", 5),
expectedError: nil,
expectedArgs: []string{fmt.Sprintf("nice -n 1 ionice -c %d -n 5 precache", IoNiceClassRealTime)},
expectedEnvVars: []corev1.EnvVar{
Expand All @@ -282,7 +282,7 @@ func TestRenderJob(t *testing.T) {
},
{
name: "Partially specified, with some invalid precaching config",
config: NewConfig([]string{}, "NumConcurrentPulls", 10, "NicePriority", 100, "IoNiceClass", IoNiceClassRealTime),
config: NewConfig([]string{}, []corev1.EnvVar{}, "NumConcurrentPulls", 10, "NicePriority", 100, "IoNiceClass", IoNiceClassRealTime),
expectedError: nil,
expectedArgs: []string{fmt.Sprintf("nice -n %d ionice -c %d -n %d precache",
DefaultNicePriority, IoNiceClassRealTime, DefaultIoNicePriority)},
Expand All @@ -295,7 +295,7 @@ func TestRenderJob(t *testing.T) {
},
{
name: "Only image list provided in precaching config",
config: NewConfig([]string{}),
config: NewConfig([]string{}, []corev1.EnvVar{}),
expectedError: nil,
expectedArgs: []string{fmt.Sprintf("nice -n %d ionice -c %d -n %d precache",
DefaultNicePriority, DefaultIoNiceClass, DefaultIoNicePriority)},
Expand Down
10 changes: 9 additions & 1 deletion internal/precache/precache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/openshift-kni/lifecycle-agent/internal/common"

"sigs.k8s.io/controller-runtime/pkg/client"

corev1 "k8s.io/api/core/v1"
)

// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;delete
Expand All @@ -44,12 +46,17 @@ type PHandler struct {
type Config struct {
ImageList []string
NumConcurrentPulls int

// To run pre-caching job with an adjusted niceness, which affects process scheduling.
// Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process).
NicePriority int

// To configure the I/O-scheduling class and priority of a process.
IoNiceClass int // 0: none, 1: realtime, 2: best-effort, 3: idle
IoNicePriority int // priority (0..7) in the specified scheduling class, only for the realtime and best-effort classes

// Allow for environment variables to be passed in
EnvVars []corev1.EnvVar
}

// NewConfig creates a new Config instance with the provided imageList and optional configuration parameters.
Expand All @@ -63,13 +70,14 @@ type Config struct {
// Example usage:
//
// config := NewConfig(imageList, "NumConcurrentPulls", 10, "NicePriority", 5)
func NewConfig(imageList []string, args ...interface{}) *Config {
func NewConfig(imageList []string, envVars []corev1.EnvVar, args ...interface{}) *Config {
instance := &Config{
ImageList: imageList,
NumConcurrentPulls: DefaultMaxConcurrentPulls,
NicePriority: DefaultNicePriority,
IoNiceClass: DefaultIoNiceClass,
IoNicePriority: DefaultIoNicePriority,
EnvVars: envVars,
}

for i := 0; i < len(args); i += 2 {
Expand Down

0 comments on commit e18a496

Please sign in to comment.