Skip to content

Commit 7d6f865

Browse files
kubectl: sort secrets alphabetically to avoid random order
1 parent b15dfce commit 7d6f865

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

staging/src/k8s.io/kubectl/pkg/describe/describe.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"crypto/x509"
2323
"fmt"
2424
"io"
25+
"maps"
2526
"net"
2627
"net/url"
2728
"reflect"
29+
"slices"
2830
"sort"
2931
"strconv"
3032
"strings"
@@ -2580,12 +2582,12 @@ func describeSecret(secret *corev1.Secret) (string, error) {
25802582
w.Write(LEVEL_0, "\nType:\t%s\n", secret.Type)
25812583

25822584
w.Write(LEVEL_0, "\nData\n====\n")
2583-
for k, v := range secret.Data {
2585+
for _, k := range slices.Sorted(maps.Keys(secret.Data)) {
25842586
switch {
25852587
case k == corev1.ServiceAccountTokenKey && secret.Type == corev1.SecretTypeServiceAccountToken:
2586-
w.Write(LEVEL_0, "%s:\t%s\n", k, string(v))
2588+
w.Write(LEVEL_0, "%s:\t%s\n", k, string(secret.Data[k]))
25872589
default:
2588-
w.Write(LEVEL_0, "%s:\t%d bytes\n", k, len(v))
2590+
w.Write(LEVEL_0, "%s:\t%d bytes\n", k, len(secret.Data[k]))
25892591
}
25902592
}
25912593

staging/src/k8s.io/kubectl/pkg/describe/describe_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ func TestDescribeSecret(t *testing.T) {
372372
if strings.Contains(out, "YWRtaW4=") || strings.Contains(out, "MWYyZDFlMmU2N2Rm") {
373373
t.Errorf("sensitive data should not be shown, unexpected out: %s", out)
374374
}
375+
376+
expectedOut := "Name: bar\nNamespace: foo\nLabels: <none>\nAnnotations: <none>\n\nType: \n\nData\n====\npassword: 16 bytes\nusername: 8 bytes\n"
377+
assert.Equal(t, expectedOut, out)
375378
}
376379

377380
func TestDescribeNamespace(t *testing.T) {

0 commit comments

Comments
 (0)