Skip to content

Commit

Permalink
Adding print columns
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Jun 28, 2024
1 parent d33d387 commit a66e844
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
17 changes: 17 additions & 0 deletions kwok/charts/crds/karpenter.sh_nodepools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ spec:
- jsonPath: .spec.template.spec.nodeClassRef.name
name: NodeClass
type: string
- jsonPath: .status.resources.nodes
name: Nodes
type: string
- jsonPath: .status.conditions[?(@.type=="Ready")].status
name: Ready
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
- jsonPath: .spec.weight
name: Weight
priority: 1
type: integer
- jsonPath: .status.resources.cpu
name: CPU
priority: 1
type: string
- jsonPath: .status.resources.memory
name: Memory
priority: 1
type: string
name: v1
schema:
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/crds/karpenter.sh_nodepools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ spec:
- jsonPath: .spec.template.spec.nodeClassRef.name
name: NodeClass
type: string
- jsonPath: .status.resources.nodes
name: Nodes
type: string
- jsonPath: .status.conditions[?(@.type=="Ready")].status
name: Ready
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
- jsonPath: .spec.weight
name: Weight
priority: 1
type: integer
- jsonPath: .status.resources.cpu
name: CPU
priority: 1
type: string
- jsonPath: .status.resources.memory
name: Memory
priority: 1
type: string
name: v1
schema:
Expand Down
7 changes: 6 additions & 1 deletion pkg/apis/v1/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ type ObjectMeta struct {
// +kubebuilder:storageversion
// +kubebuilder:resource:path=nodepools,scope=Cluster,categories=karpenter
// +kubebuilder:printcolumn:name="NodeClass",type="string",JSONPath=".spec.template.spec.nodeClassRef.name",description=""
// +kubebuilder:printcolumn:name="Weight",type="string",JSONPath=".spec.weight",priority=1,description=""
// +kubebuilder:printcolumn:name="Nodes",type="string",JSONPath=".status.resources.nodes",description=""
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
// +kubebuilder:printcolumn:name="Weight",type="integer",JSONPath=".spec.weight",priority=1,description=""
// +kubebuilder:printcolumn:name="CPU",type="string",JSONPath=".status.resources.cpu",priority=1,description=""
// +kubebuilder:printcolumn:name="Memory",type="string",JSONPath=".status.resources.memory",priority=1,description=""
// +kubebuilder:subresource:status
type NodePool struct {
metav1.TypeMeta `json:",inline"`
Expand Down
11 changes: 11 additions & 0 deletions pkg/controllers/nodepool/counter/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package counter

import (
"context"
"fmt"
"time"

"github.com/samber/lo"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -68,6 +70,15 @@ func (c *Controller) Reconcile(ctx context.Context, nodePool *v1beta1.NodePool)
stored := nodePool.DeepCopy()
// Determine resource usage and update nodepool.status.resources
nodePool.Status.Resources = c.resourceCountsFor(v1beta1.NodePoolLabelKey, nodePool.Name)
nodeCount := lo.CountBy(c.cluster.Nodes(), func(n *state.StateNode) bool {
return n.Labels()[v1beta1.NodePoolLabelKey] == nodePool.Name
})
// Only display node count if nodes are owned by the nodepool
if nodeCount != 0 {
nodePool.Status.Resources = resources.MergeInto(nodePool.Status.Resources, v1.ResourceList{
v1.ResourceName("nodes"): resource.MustParse(fmt.Sprintf("%d", nodeCount)),
})
}
if !equality.Semantic.DeepEqual(stored, nodePool) {
if err := c.kubeClient.Status().Patch(ctx, nodePool, client.MergeFrom(stored)); err != nil {
return reconcile.Result{}, client.IgnoreNotFound(err)
Expand Down
39 changes: 28 additions & 11 deletions pkg/controllers/nodepool/counter/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ var _ = Describe("Counter", func() {
ExpectObjectReconciled(ctx, env.Client, nodePoolController, nodePool)
nodePool = ExpectExists(ctx, env.Client, nodePool)

Expect(nodePool.Status.Resources).To(BeEquivalentTo(nodeClaim.Status.Capacity))
expected := nodeClaim.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))

// Change the node capacity to be different than the nodeClaim capacity
node.Status.Capacity = v1.ResourceList{
Expand All @@ -145,7 +147,9 @@ var _ = Describe("Counter", func() {
ExpectObjectReconciled(ctx, env.Client, nodePoolController, nodePool)
nodePool = ExpectExists(ctx, env.Client, nodePool)

Expect(nodePool.Status.Resources).To(BeEquivalentTo(node.Status.Capacity))
expected = node.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))
})
It("should increase the counter when new nodes are created", func() {
ExpectApplied(ctx, env.Client, node, nodeClaim)
Expand All @@ -155,8 +159,12 @@ var _ = Describe("Counter", func() {
nodePool = ExpectExists(ctx, env.Client, nodePool)

// Should equal both the nodeClaim and node capacity
Expect(nodePool.Status.Resources).To(BeEquivalentTo(nodeClaim.Status.Capacity))
Expect(nodePool.Status.Resources).To(BeEquivalentTo(node.Status.Capacity))
expected := nodeClaim.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))
expected = node.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))
})
It("should decrease the counter when an existing node is deleted", func() {
ExpectApplied(ctx, env.Client, node, nodeClaim, node2, nodeClaim2)
Expand All @@ -167,9 +175,10 @@ var _ = Describe("Counter", func() {

// Should equal the sums of the nodeClaims and nodes
resources := v1.ResourceList{
v1.ResourceCPU: resource.MustParse("600m"),
v1.ResourcePods: resource.MustParse("1256"),
v1.ResourceMemory: resource.MustParse("6Gi"),
v1.ResourceCPU: resource.MustParse("600m"),
v1.ResourcePods: resource.MustParse("1256"),
v1.ResourceMemory: resource.MustParse("6Gi"),
v1.ResourceName("nodes"): resource.MustParse("2"),
}
Expect(nodePool.Status.Resources).To(BeEquivalentTo(resources))
Expect(nodePool.Status.Resources).To(BeEquivalentTo(resources))
Expand All @@ -181,8 +190,12 @@ var _ = Describe("Counter", func() {
nodePool = ExpectExists(ctx, env.Client, nodePool)

// Should equal both the nodeClaim and node capacity
Expect(nodePool.Status.Resources).To(BeEquivalentTo(nodeClaim2.Status.Capacity))
Expect(nodePool.Status.Resources).To(BeEquivalentTo(node2.Status.Capacity))
expected := nodeClaim2.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))
expected = node2.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))
})
It("should nil out the counter when all nodes are deleted", func() {
ExpectApplied(ctx, env.Client, node, nodeClaim)
Expand All @@ -192,8 +205,12 @@ var _ = Describe("Counter", func() {
nodePool = ExpectExists(ctx, env.Client, nodePool)

// Should equal both the nodeClaim and node capacity
Expect(nodePool.Status.Resources).To(BeEquivalentTo(nodeClaim.Status.Capacity))
Expect(nodePool.Status.Resources).To(BeEquivalentTo(node.Status.Capacity))
expected := nodeClaim.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))
expected = node.Status.Capacity
expected[v1.ResourceName("nodes")] = resource.MustParse("1")
Expect(nodePool.Status.Resources).To(BeEquivalentTo(expected))

ExpectDeleted(ctx, env.Client, node, nodeClaim)

Expand Down

0 comments on commit a66e844

Please sign in to comment.