Skip to content

Commit

Permalink
Added Testing for Repair Policy CP interface
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Nov 18, 2024
1 parent 9493339 commit 3b30caf
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 10 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/aws/karpenter-provider-aws

go 1.23.2

replace sigs.k8s.io/karpenter => github.com/engedaam/karpenter-core v0.0.0-20241117033623-cab6157220e3

require (
github.com/Pallinder/go-randomdata v1.2.0
github.com/PuerkitoBio/goquery v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/engedaam/karpenter-core v0.0.0-20241117033623-cab6157220e3 h1:aONl6Y1vKsXK9ipJjSlulSWVA7HwrN9leTy706mG4dw=
github.com/engedaam/karpenter-core v0.0.0-20241117033623-cab6157220e3/go.mod h1:zolnK/3MxqSPEhEan2VBbzuGdReJPFTbpYWGivwTgic=
github.com/evanphx/json-patch v5.7.0+incompatible h1:vgGkfT/9f8zE6tvSCe74nfpAVDQ2tG6yudJd8LBksgI=
github.com/evanphx/json-patch v5.7.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
Expand Down Expand Up @@ -323,8 +325,6 @@ sigs.k8s.io/controller-runtime v0.19.1 h1:Son+Q40+Be3QWb+niBXAg2vFiYWolDjjRfO8hn
sigs.k8s.io/controller-runtime v0.19.1/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/karpenter v1.0.1-0.20241115180652-995040b20d0a h1:fbD7tYsZCIu3uzgJKO9/XWS6uYPCeWxQmwAvP2jtXRE=
sigs.k8s.io/karpenter v1.0.1-0.20241115180652-995040b20d0a/go.mod h1:zolnK/3MxqSPEhEan2VBbzuGdReJPFTbpYWGivwTgic=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
Expand Down
10 changes: 5 additions & 5 deletions pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,18 @@ func (c *CloudProvider) RepairPolicies() []cloudprovider.RepairPolicy {
return []cloudprovider.RepairPolicy{
// Supported Kubelet fields
{
ConditionType: "Ready",
ConditionType: corev1.NodeReady,
ConditionStatus: corev1.ConditionFalse,
TolerationDuration: 30 * time.Minute,
},
{
ConditionType: "DiskPressure",
ConditionStatus: corev1.ConditionFalse,
ConditionType: corev1.NodeDiskPressure,
ConditionStatus: corev1.ConditionTrue,
TolerationDuration: 30 * time.Minute,
},
{
ConditionType: "MemoryPressure",
ConditionStatus: corev1.ConditionFalse,
ConditionType: corev1.NodeMemoryPressure,
ConditionStatus: corev1.ConditionTrue,
TolerationDuration: 30 * time.Minute,
},
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/fake/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
corecloudprovider "sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/test"

Expand Down Expand Up @@ -91,6 +90,6 @@ func (c *CloudProvider) GetSupportedNodeClasses() []status.Object {
return []status.Object{&v1.EC2NodeClass{}}
}

func (c *CloudProvider) RepairPolicy() []cloudprovider.RepairStatements {
return []cloudprovider.RepairStatements{}
func (c *CloudProvider) RepairPolicies() []corecloudprovider.RepairPolicy {
return []corecloudprovider.RepairPolicy{}
}
30 changes: 30 additions & 0 deletions test/pkg/environment/common/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ func (env *Environment) ExpectUpdated(objects ...client.Object) {
}
}

// ExpectStatusUpdated will update objects in the cluster to match the inputs.
// WARNING: This ignores the resource version check, which can result in
// overwriting changes made by other controllers in the cluster.
// This is useful in ensuring that we can clean up resources by patching
// out finalizers.
// Grab the object before making the updates to reduce the chance of this race.
func (env *Environment) ExpectStatusUpdated(objects ...client.Object) {
GinkgoHelper()
for _, o := range objects {
Eventually(func(g Gomega) {
current := o.DeepCopyObject().(client.Object)
g.Expect(env.Client.Get(env.Context, client.ObjectKeyFromObject(current), current)).To(Succeed())
if current.GetResourceVersion() != o.GetResourceVersion() {
log.FromContext(env).Info(fmt.Sprintf("detected an update to an object (%s) with an outdated resource version, did you get the latest version of the object before patching?", lo.Must(apiutil.GVKForObject(o, env.Client.Scheme()))))
}
o.SetResourceVersion(current.GetResourceVersion())
g.Expect(env.Client.Status().Update(env.Context, o)).To(Succeed())
}).WithTimeout(time.Second * 10).Should(Succeed())
}
}

func ReplaceNodeConditions(node *corev1.Node, conds ...corev1.NodeCondition) *corev1.Node {
keys := sets.New[string](lo.Map(conds, func(c corev1.NodeCondition, _ int) string { return string(c.Type) })...)
node.Status.Conditions = lo.Reject(node.Status.Conditions, func(c corev1.NodeCondition, _ int) bool {
return keys.Has(string(c.Type))
})
node.Status.Conditions = append(node.Status.Conditions, conds...)
return node
}

// ExpectCreatedOrUpdated can update objects in the cluster to match the inputs.
// WARNING: ExpectUpdated ignores the resource version check, which can result in
// overwriting changes made by other controllers in the cluster.
Expand Down
86 changes: 86 additions & 0 deletions test/suites/integration/repair_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 integration_test

import (
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
karpenterv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
coretest "sigs.k8s.io/karpenter/pkg/test"

"github.com/aws/karpenter-provider-aws/test/pkg/environment/common"

. "github.com/onsi/ginkgo/v2"
"github.com/samber/lo"
)

var _ = Describe("Repair Policy", func() {
var selector labels.Selector
var dep *appsv1.Deployment
var numPods int

BeforeEach(func() {
numPods = 1
// Add pods with a do-not-disrupt annotation so that we can check node metadata before we disrupt
dep = coretest.Deployment(coretest.DeploymentOptions{
Replicas: int32(numPods),
PodOptions: coretest.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "my-app",
},
Annotations: map[string]string{
karpenterv1.DoNotDisruptAnnotationKey: "true",
},
},
TerminationGracePeriodSeconds: lo.ToPtr[int64](0),
},
})
selector = labels.SelectorFromSet(dep.Spec.Selector.MatchLabels)
})

DescribeTable("Conditions", func(unhealthyCondition corev1.NodeCondition) {
env.ExpectCreated(nodeClass, nodePool, dep)
pod := env.EventuallyExpectHealthyPodCount(selector, numPods)[0]
node := env.ExpectCreatedNodeCount("==", 1)[0]
env.EventuallyExpectInitializedNodeCount("==", 1)

common.ReplaceNodeConditions(node, unhealthyCondition)
env.ExpectStatusUpdated(node)

env.EventuallyExpectNotFound(pod, node)
env.EventuallyExpectHealthyPodCount(selector, numPods)
},
Entry("Kubelet Readiness", corev1.NodeCondition{
Type: corev1.NodeReady,
Status: corev1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: time.Now().Add(-31 * time.Minute)},
}),
Entry("Kubelet DiskPressure", corev1.NodeCondition{
Type: corev1.NodeDiskPressure,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: time.Now().Add(-31 * time.Minute)},
}),
Entry("Kubelet MemoryPressure", corev1.NodeCondition{
Type: corev1.NodeMemoryPressure,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: time.Now().Add(-31 * time.Minute)},
}),
)
})

0 comments on commit 3b30caf

Please sign in to comment.