-
Notifications
You must be signed in to change notification settings - Fork 9
feat(template): add node count template function #3204
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
Merged
JGAntunes
merged 2 commits into
main
from
jgantunes/sc-131029/support-nodecount-template-function-in-v3
Nov 14, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,24 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| // nodeCount returns the total count of Kubernetes nodes in the cluster. | ||
| // It returns 0 if the kubeClient is unavailable or if an error occurs. | ||
| func (e *Engine) nodeCount() int { | ||
| logger := e.logger.WithField("function", "node-count") | ||
| if e.kubeClient == nil { | ||
| logger.Warn("kube client is nil, cannot get node count") | ||
| return 0 | ||
| } | ||
| ctx := context.TODO() | ||
| nodeList := &corev1.NodeList{} | ||
| if err := e.kubeClient.List(ctx, nodeList); err != nil { | ||
| logger.WithError(err).Warn("failed to list nodes") | ||
JGAntunes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return 0 | ||
|
Member
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. can you log warn here
Member
Author
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. Done via - 3ab44cb |
||
| } | ||
| return len(nodeList.Items) | ||
| } | ||
This file contains hidden or 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,133 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
| ) | ||
|
|
||
| func TestEngine_NodeCount(t *testing.T) { | ||
| config := &kotsv1beta1.Config{ | ||
| Spec: kotsv1beta1.ConfigSpec{ | ||
| Groups: []kotsv1beta1.ConfigGroup{}, | ||
| }, | ||
| } | ||
|
|
||
| t.Run("returns 0 when kubeClient is nil", func(t *testing.T) { | ||
| engine := NewEngine(config) | ||
|
|
||
| err := engine.Parse("{{repl NodeCount }}") | ||
| require.NoError(t, err) | ||
| result, err := engine.Execute(nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "0", result) | ||
| }) | ||
|
|
||
| t.Run("returns correct count with multiple nodes", func(t *testing.T) { | ||
| // Set up scheme for fake client | ||
| sch := runtime.NewScheme() | ||
| require.NoError(t, corev1.AddToScheme(sch)) | ||
| require.NoError(t, scheme.AddToScheme(sch)) | ||
|
|
||
| // Create fake nodes | ||
| node1 := &corev1.Node{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "node-1", | ||
| }, | ||
| } | ||
| node2 := &corev1.Node{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "node-2", | ||
| }, | ||
| } | ||
| node3 := &corev1.Node{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "node-3", | ||
| }, | ||
| } | ||
|
|
||
| // Create fake client with nodes | ||
| fakeClient := clientfake.NewClientBuilder(). | ||
| WithScheme(sch). | ||
| WithObjects(node1, node2, node3). | ||
| Build() | ||
|
|
||
| engine := NewEngine(config, WithKubeClient(fakeClient)) | ||
|
|
||
| err := engine.Parse("{{repl NodeCount }}") | ||
| require.NoError(t, err) | ||
| result, err := engine.Execute(nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "3", result) | ||
| }) | ||
|
|
||
| t.Run("can be used in conditional expressions", func(t *testing.T) { | ||
| // Set up scheme for fake client | ||
| sch := runtime.NewScheme() | ||
| require.NoError(t, corev1.AddToScheme(sch)) | ||
| require.NoError(t, scheme.AddToScheme(sch)) | ||
|
|
||
| // Create fake nodes | ||
| node1 := &corev1.Node{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "node-1", | ||
| }, | ||
| } | ||
| node2 := &corev1.Node{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "node-2", | ||
| }, | ||
| } | ||
|
|
||
| // Create fake client with nodes | ||
| fakeClient := clientfake.NewClientBuilder(). | ||
| WithScheme(sch). | ||
| WithObjects(node1, node2). | ||
| Build() | ||
|
|
||
| engine := NewEngine(config, WithKubeClient(fakeClient)) | ||
|
|
||
| // Test conditional expression | ||
| err := engine.Parse("{{repl if gt (NodeCount) 1 }}HA{{repl else }}Standalone{{repl end }}") | ||
| require.NoError(t, err) | ||
| result, err := engine.Execute(nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "HA", result) | ||
| }) | ||
|
|
||
| t.Run("returns 0 when kubeClient List returns error", func(t *testing.T) { | ||
| // Set up scheme for fake client | ||
| sch := runtime.NewScheme() | ||
| require.NoError(t, corev1.AddToScheme(sch)) | ||
| require.NoError(t, scheme.AddToScheme(sch)) | ||
|
|
||
| // Create fake client with interceptor that returns error on List | ||
| fakeClient := clientfake.NewClientBuilder(). | ||
| WithScheme(sch). | ||
| WithInterceptorFuncs(interceptor.Funcs{ | ||
| List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error { | ||
| return fmt.Errorf("simulated list error") | ||
| }, | ||
| }). | ||
| Build() | ||
|
|
||
| engine := NewEngine(config, WithKubeClient(fakeClient)) | ||
|
|
||
| err := engine.Parse("{{repl NodeCount }}") | ||
| require.NoError(t, err) | ||
| result, err := engine.Execute(nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "0", result) | ||
| }) | ||
| } |
This file contains hidden or 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 you log warn here
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.
Done via - 3ab44cb