Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for available node resources before building injector pod #2220

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/pkg/cluster/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import (
// The chunk size for the tarball chunks.
var payloadChunkSize = 1024 * 768

var (
requestedCPU = resource.MustParse(".5")
requestedMemory = resource.MustParse("64Mi")
limitCPU = resource.MustParse("1")
limitMemory = resource.MustParse("256Mi")
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
)

// StartInjectionMadness initializes a Zarf injection into the cluster.
func (c *Cluster) StartInjectionMadness(tmpDir string, imagesDir string, injectorSeedSrcs []string) {
spinner := message.NewProgressSpinner("Attempting to bootstrap the seed image into the cluster")
Expand All @@ -54,7 +61,7 @@ func (c *Cluster) StartInjectionMadness(tmpDir string, imagesDir string, injecto
// Get all the images from the cluster
timeout := 5 * time.Minute
spinner.Updatef("Getting the list of existing cluster images (%s timeout)", timeout.String())
if images, err = c.GetAllImages(timeout); err != nil {
if images, err = c.GetAllImages(timeout, requestedCPU, requestedMemory); err != nil {
spinner.Fatalf(err, "Unable to generate a list of candidate images to perform the registry injection")
}

Expand Down Expand Up @@ -362,12 +369,12 @@ func (c *Cluster) buildInjectionPod(node, image string, payloadConfigmaps []stri
// Keep resources as light as possible as we aren't actually running the container's other binaries
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(".5"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
corev1.ResourceCPU: requestedCPU,
corev1.ResourceMemory: requestedMemory,
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
corev1.ResourceMemory: resource.MustParse("256Mi"),
corev1.ResourceCPU: limitCPU,
corev1.ResourceMemory: limitMemory,
},
},
},
Expand Down
12 changes: 9 additions & 3 deletions src/pkg/k8s/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"time"

"k8s.io/apimachinery/pkg/api/resource"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -19,7 +20,7 @@ type ImageMap map[string]bool
type ImageNodeMap map[string][]string

// GetAllImages returns a list of images and their nodes found in pods in the cluster.
func (k *K8s) GetAllImages(timeoutDuration time.Duration) (ImageNodeMap, error) {
func (k *K8s) GetAllImages(timeoutDuration time.Duration, requestedCPU resource.Quantity, requestedMemory resource.Quantity) (ImageNodeMap, error) {
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
timeout := time.After(timeoutDuration)

for {
Expand All @@ -34,7 +35,7 @@ func (k *K8s) GetAllImages(timeoutDuration time.Duration) (ImageNodeMap, error)
// After delay, try running.
default:
// If no images or an error, log and loop.
if images, err := k.GetImagesWithNodes(corev1.NamespaceAll); len(images) < 1 || err != nil {
if images, err := k.GetImagesWithNodes(corev1.NamespaceAll, requestedCPU, requestedMemory); len(images) < 1 || err != nil {
k.Log("no images found: %w", err)
} else {
// Otherwise, return the image list.
Expand All @@ -46,7 +47,7 @@ func (k *K8s) GetAllImages(timeoutDuration time.Duration) (ImageNodeMap, error)

// GetImagesWithNodes checks for images on schedulable nodes and returns
// a map of these images and their nodes in a given namespace.
func (k *K8s) GetImagesWithNodes(namespace string) (ImageNodeMap, error) {
func (k *K8s) GetImagesWithNodes(namespace string, requestedCPU resource.Quantity, requestedMemory resource.Quantity) (ImageNodeMap, error) {
chrishorton marked this conversation as resolved.
Show resolved Hide resolved
result := make(ImageNodeMap)

pods, err := k.GetPods(namespace)
Expand All @@ -68,6 +69,11 @@ findImages:
return nil, fmt.Errorf("unable to get the node %s", pod.Spec.NodeName)
}

if nodeDetails.Status.Allocatable.Cpu().Cmp(requestedCPU) < 0 ||
nodeDetails.Status.Allocatable.Memory().Cmp(requestedMemory) < 0 {
chrishorton marked this conversation as resolved.
Show resolved Hide resolved
continue findImages
}

for _, taint := range nodeDetails.Spec.Taints {
if taint.Effect == corev1.TaintEffectNoSchedule || taint.Effect == corev1.TaintEffectNoExecute {
continue findImages
Expand Down
Loading