Skip to content

Commit

Permalink
test: removing settings and using isNAP flag to gate the karpenter co…
Browse files Browse the repository at this point in the history
…ntroller pod calls
  • Loading branch information
Bryce-Soghigian committed Dec 30, 2024
1 parent 3a9cbc0 commit 45309f8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 97 deletions.
5 changes: 0 additions & 5 deletions test/pkg/environment/azure/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (
//nolint:revive,stylecheck
"fmt"

v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/Azure/karpenter-provider-azure/pkg/apis/v1alpha2"
)

var persistedSettings []v1.EnvVar

var (
CleanableObjects = []client.Object{
Expand All @@ -35,7 +33,6 @@ var (
)

func (env *Environment) BeforeEach() {
persistedSettings = env.ExpectSettings()
env.Environment.BeforeEach()
}

Expand All @@ -48,6 +45,4 @@ func (env *Environment) AfterEach() {
fmt.Println("##[group] E2E SUITE: LOG DUMP")
defer fmt.Println("##[endgroup]")
env.Environment.AfterEach()
// Ensure we reset settings after collecting the controller logs
env.ExpectSettingsReplaced(persistedSettings...)
}
96 changes: 4 additions & 92 deletions test/pkg/environment/common/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"math"
"os"
"strings"
"time"

Expand Down Expand Up @@ -144,78 +145,6 @@ func (env *Environment) ExpectCreatedOrUpdated(objects ...client.Object) {
}
}

func (env *Environment) ExpectSettings() (res []corev1.EnvVar) {
GinkgoHelper()

d := &appsv1.Deployment{}
Expect(env.Client.Get(env.Context, types.NamespacedName{Namespace: "kube-system", Name: "karpenter"}, d)).To(Succeed())
Expect(d.Spec.Template.Spec.Containers).To(HaveLen(1))
return lo.Map(d.Spec.Template.Spec.Containers[0].Env, func(v corev1.EnvVar, _ int) corev1.EnvVar {
return *v.DeepCopy()
})
}

func (env *Environment) ExpectSettingsReplaced(vars ...corev1.EnvVar) {
GinkgoHelper()

d := &appsv1.Deployment{}
Expect(env.Client.Get(env.Context, types.NamespacedName{Namespace: "kube-system", Name: "karpenter"}, d)).To(Succeed())
Expect(d.Spec.Template.Spec.Containers).To(HaveLen(1))

stored := d.DeepCopy()
d.Spec.Template.Spec.Containers[0].Env = vars

if !equality.Semantic.DeepEqual(d, stored) {
By("replacing environment variables for karpenter deployment")
Expect(env.Client.Patch(env.Context, d, client.StrategicMergeFrom(stored))).To(Succeed())
env.EventuallyExpectKarpenterRestarted()
}
}

func (env *Environment) ExpectSettingsOverridden(vars ...corev1.EnvVar) {
GinkgoHelper()

d := &appsv1.Deployment{}
Expect(env.Client.Get(env.Context, types.NamespacedName{Namespace: "kube-system", Name: "karpenter"}, d)).To(Succeed())
Expect(d.Spec.Template.Spec.Containers).To(HaveLen(1))

stored := d.DeepCopy()
for _, v := range vars {
if _, i, ok := lo.FindIndexOf(d.Spec.Template.Spec.Containers[0].Env, func(e corev1.EnvVar) bool {
return e.Name == v.Name
}); ok {
d.Spec.Template.Spec.Containers[0].Env[i] = v
} else {
d.Spec.Template.Spec.Containers[0].Env = append(d.Spec.Template.Spec.Containers[0].Env, v)
}
}
if !equality.Semantic.DeepEqual(d, stored) {
By("overriding environment variables for karpenter deployment")
Expect(env.Client.Patch(env.Context, d, client.StrategicMergeFrom(stored))).To(Succeed())
env.EventuallyExpectKarpenterRestarted()
}
}

func (env *Environment) ExpectSettingsRemoved(vars ...corev1.EnvVar) {
GinkgoHelper()

varNames := sets.New(lo.Map(vars, func(v corev1.EnvVar, _ int) string { return v.Name })...)

d := &appsv1.Deployment{}
Expect(env.Client.Get(env.Context, types.NamespacedName{Namespace: "kube-system", Name: "karpenter"}, d)).To(Succeed())
Expect(d.Spec.Template.Spec.Containers).To(HaveLen(1))

stored := d.DeepCopy()
d.Spec.Template.Spec.Containers[0].Env = lo.Reject(d.Spec.Template.Spec.Containers[0].Env, func(v corev1.EnvVar, _ int) bool {
return varNames.Has(v.Name)
})
if !equality.Semantic.DeepEqual(d, stored) {
By("removing environment variables for karpenter deployment")
Expect(env.Client.Patch(env.Context, d, client.StrategicMergeFrom(stored))).To(Succeed())
env.EventuallyExpectKarpenterRestarted()
}
}

func (env *Environment) ExpectConfigMapExists(key types.NamespacedName) *corev1.ConfigMap {
GinkgoHelper()
cm := &corev1.ConfigMap{}
Expand Down Expand Up @@ -362,26 +291,6 @@ func (env *Environment) ConsistentlyExpectHealthyPods(duration time.Duration, po
}, duration.String()).Should(Succeed())
}

func (env *Environment) EventuallyExpectKarpenterRestarted() {
GinkgoHelper()
By("rolling out the new karpenter deployment")
env.EventuallyExpectRollout("karpenter", "kube-system")
env.ExpectKarpenterLeaseOwnerChanged()
}

func (env *Environment) ExpectKarpenterLeaseOwnerChanged() {
GinkgoHelper()

By("waiting for a new karpenter pod to hold the lease")
pods := env.ExpectKarpenterPods()
Eventually(func(g Gomega) {
name := env.ExpectActiveKarpenterPodName()
g.Expect(lo.ContainsBy(pods, func(p *corev1.Pod) bool {
return p.Name == name
})).To(BeTrue())
}).Should(Succeed())
}

func (env *Environment) EventuallyExpectRollout(name, namespace string) {
GinkgoHelper()
By("restarting the deployment")
Expand Down Expand Up @@ -842,6 +751,8 @@ func (env *Environment) printControllerLogs(options *corev1.PodLogOptions) {
options.SinceTime = lastLogged.DeepCopy()
lastLogged = metav1.Now()
}
isNap := os.Getenv("IS_NAP")
if isNap == "" || isNap == "false" {
pods := env.ExpectKarpenterPods()
for _, pod := range pods {
temp := options.DeepCopy() // local version of the log options
Expand All @@ -861,6 +772,7 @@ func (env *Environment) printControllerLogs(options *corev1.PodLogOptions) {
Expect(err).ToNot(HaveOccurred())
log.FromContext(env.Context).Info(raw.String())
}
}
}

func (env *Environment) EventuallyExpectMinUtilization(resource corev1.ResourceName, comparator string, value float64) {
Expand Down

0 comments on commit 45309f8

Please sign in to comment.