-
Notifications
You must be signed in to change notification settings - Fork 219
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(disruption): add node notready controller #1755
Closed
mariuskimmina
wants to merge
5
commits into
kubernetes-sigs:main
from
mariuskimmina:node-notready-controller
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
422151e
feat(disruption): add node notready controller
mariuskimmina 688404d
add one more test case
mariuskimmina 5e0ae43
add one more test case
mariuskimmina d3f5112
fix use of nodeclaim in logs to be UpperCamelCase
mariuskimmina 8561436
Add metric for nodeclaim disruption due to being unreachable
mariuskimmina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright The Kubernetes Authors. | ||
|
||
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 notready | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/karpenter/pkg/metrics" | ||
|
||
nodeclaimutil "sigs.k8s.io/karpenter/pkg/utils/nodeclaim" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
v1 "sigs.k8s.io/karpenter/pkg/apis/v1" | ||
) | ||
|
||
// Controller NotReady is a nodeclaim controller that deletes nodeclaims when they have been unreachable for too long | ||
type Controller struct { | ||
kubeClient client.Client | ||
} | ||
|
||
// NewController constructs a nodeclaim disruption controller | ||
func NewController(kubeClient client.Client) *Controller { | ||
return &Controller{ | ||
kubeClient: kubeClient, | ||
} | ||
} | ||
|
||
func (c *Controller) Reconcile(ctx context.Context, nodeClaim *v1.NodeClaim) (reconcile.Result, error) { | ||
if nodeClaim.Spec.UnreachableTimeout.Duration == nil { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
node, err := nodeclaimutil.NodeForNodeClaim(ctx, c.kubeClient, nodeClaim) | ||
if err != nil { | ||
return reconcile.Result{}, nodeclaimutil.IgnoreDuplicateNodeError(nodeclaimutil.IgnoreNodeNotFoundError(err)) | ||
} | ||
|
||
for _, taint := range node.Spec.Taints { | ||
if taint.Key == corev1.TaintNodeUnreachable { | ||
if taint.TimeAdded != nil { | ||
durationSinceTaint := time.Since(taint.TimeAdded.Time) | ||
if durationSinceTaint > *nodeClaim.Spec.UnreachableTimeout.Duration { | ||
// if node is unreachable for too long, delete the nodeclaim | ||
if err := c.kubeClient.Delete(ctx, nodeClaim); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
log.FromContext(ctx).V(0).Error(err, "Failed to delete NodeClaim", "node", node.Name) | ||
return reconcile.Result{}, err | ||
} | ||
log.FromContext(ctx).V(0).Info("Deleted NodeClaim because the node has been unreachable for more than unreachableTimeout", "node", node.Name) | ||
metrics.NodeClaimsDisruptedTotal.With(prometheus.Labels{ | ||
metrics.ReasonLabel: metrics.UnreachableReason, | ||
metrics.NodePoolLabel: nodeClaim.Labels[v1.NodePoolLabelKey], | ||
metrics.CapacityTypeLabel: nodeClaim.Labels[v1.CapacityTypeLabelKey], | ||
}).Inc() | ||
return reconcile.Result{}, nil | ||
} else { | ||
// If the node is unreachable and the time since it became unreachable is less than the configured timeout, | ||
// we requeue to prevent the node from remaining in an unreachable state indefinitely | ||
log.FromContext(ctx).V(1).Info("Node has been unreachable for less than unreachableTimeout, requeueing", "node", node.Name) | ||
return reconcile.Result{RequeueAfter: *nodeClaim.Spec.UnreachableTimeout.Duration}, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (c *Controller) Register(_ context.Context, m manager.Manager) error { | ||
builder := controllerruntime.NewControllerManagedBy(m) | ||
return builder. | ||
Named("nodeclaim.notready"). | ||
For(&v1.NodeClaim{}). | ||
Watches( | ||
&corev1.Node{}, | ||
nodeclaimutil.NodeEventHandler(c.kubeClient), | ||
). | ||
Complete(reconcile.AsReconciler(m.GetClient(), c)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
Copyright The Kubernetes Authors. | ||
|
||
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 notready_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clock "k8s.io/utils/clock/testing" | ||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sigs.k8s.io/karpenter/pkg/apis" | ||
v1 "sigs.k8s.io/karpenter/pkg/apis/v1" | ||
"sigs.k8s.io/karpenter/pkg/controllers/nodeclaim/notready" | ||
"sigs.k8s.io/karpenter/pkg/metrics" | ||
"sigs.k8s.io/karpenter/pkg/operator/options" | ||
"sigs.k8s.io/karpenter/pkg/test" | ||
. "sigs.k8s.io/karpenter/pkg/test/expectations" | ||
"sigs.k8s.io/karpenter/pkg/test/v1alpha1" | ||
. "sigs.k8s.io/karpenter/pkg/utils/testing" | ||
) | ||
|
||
var ctx context.Context | ||
var notReadyController *notready.Controller | ||
var env *test.Environment | ||
var fakeClock *clock.FakeClock | ||
|
||
func TestAPIs(t *testing.T) { | ||
ctx = TestContextWithLogger(t) | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "NotReady Controller Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
fakeClock = clock.NewFakeClock(time.Now()) | ||
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(func(c cache.Cache) error { | ||
return c.IndexField(ctx, &corev1.Node{}, "spec.providerID", func(obj client.Object) []string { | ||
return []string{obj.(*corev1.Node).Spec.ProviderID} | ||
}) | ||
})) | ||
ctx = options.ToContext(ctx, test.Options()) | ||
notReadyController = notready.NewController(env.Client) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
Expect(env.Stop()).To(Succeed(), "Failed to stop environment") | ||
}) | ||
|
||
var _ = BeforeEach(func() { | ||
ctx = options.ToContext(ctx, test.Options()) | ||
fakeClock.SetTime(time.Now()) | ||
}) | ||
|
||
var _ = AfterEach(func() { | ||
ExpectCleanedUp(ctx, env.Client) | ||
}) | ||
|
||
var _ = Describe("NotReady", func() { | ||
var nodePool *v1.NodePool | ||
var nodeClaim *v1.NodeClaim | ||
var node *corev1.Node | ||
BeforeEach(func() { | ||
nodePool = test.NodePool() | ||
nodeClaim, node = test.NodeClaimAndNode(v1.NodeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{v1.NodePoolLabelKey: nodePool.Name}, | ||
}, | ||
Spec: v1.NodeClaimSpec{ | ||
UnreachableTimeout: v1.MustParseNillableDuration("10m"), | ||
}, | ||
}) | ||
metrics.NodeClaimsDisruptedTotal.Reset() | ||
}) | ||
It("should remove NodeClaim when the node has an unreachable taint for over the UnreachableTimeout duration", func() { | ||
node.Spec.Taints = []corev1.Taint{ | ||
{ | ||
Key: corev1.TaintNodeUnreachable, | ||
Effect: corev1.TaintEffectNoSchedule, | ||
TimeAdded: &metav1.Time{Time: fakeClock.Now().Add(-12 * time.Minute)}, | ||
}, | ||
} | ||
ExpectApplied(ctx, env.Client, nodeClaim, node) | ||
ExpectObjectReconciled(ctx, env.Client, notReadyController, nodeClaim) | ||
ExpectMetricCounterValue(metrics.NodeClaimsDisruptedTotal, 1, map[string]string{ | ||
metrics.ReasonLabel: metrics.UnreachableReason, | ||
"nodepool": nodePool.Name, | ||
}) | ||
ExpectNotFound(ctx, env.Client, nodeClaim) | ||
}) | ||
It("should not remove NodeClaim if unreachable taint is less than the UnreachableTimeout duration", func() { | ||
node.Spec.Taints = []corev1.Taint{ | ||
{ | ||
Key: corev1.TaintNodeUnreachable, | ||
Effect: corev1.TaintEffectNoSchedule, | ||
TimeAdded: &metav1.Time{Time: fakeClock.Now().Add(-7 * time.Minute)}, | ||
}, | ||
} | ||
ExpectApplied(ctx, env.Client, nodeClaim, node) | ||
ExpectObjectReconciled(ctx, env.Client, notReadyController, nodeClaim) | ||
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim) | ||
}) | ||
It("should not remove the NodeClaim when UnreachableTimeout is disabled", func() { | ||
nodeClaim.Spec.ExpireAfter = v1.MustParseNillableDuration("Never") | ||
node.Spec.Taints = []corev1.Taint{ | ||
{ | ||
Key: corev1.TaintNodeUnreachable, | ||
Effect: corev1.TaintEffectNoSchedule, | ||
TimeAdded: &metav1.Time{Time: fakeClock.Now().Add(-12 * time.Minute)}, | ||
}, | ||
} | ||
ExpectApplied(ctx, env.Client, nodeClaim) | ||
ExpectObjectReconciled(ctx, env.Client, notReadyController, nodeClaim) | ||
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we (you) split this bump into its own commit / explain more about the context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bump was created by running
make verify
and then presumably bygo generate ./...
- as this is my first time working on karpenter I wasn't sure if I should commit this change. Happy to remove the bumps if it is deemed not necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I would make the autogenerated changes a separate commit, then it is easy to omit if appropriate)