Skip to content

Commit 0130ac1

Browse files
authored
Merge pull request kubernetes#130309 from HaraldNordgren/sort_secrets
kubectl: sort secrets alphabetically to avoid random order
2 parents 7397a0f + 4e3026f commit 0130ac1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,21 @@ 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
377+
Namespace: foo
378+
Labels: <none>
379+
Annotations: <none>
380+
381+
Type:
382+
383+
Data
384+
====
385+
password: 16 bytes
386+
username: 8 bytes
387+
`
388+
389+
assert.Equal(t, expectedOut, out)
375390
}
376391

377392
func TestDescribeNamespace(t *testing.T) {

0 commit comments

Comments
 (0)