-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8c6421
commit 78b822e
Showing
8 changed files
with
158 additions
and
43 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dao | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// Secret represents a secret K8s resource. | ||
type Secret struct { | ||
Resource | ||
decode bool | ||
} | ||
|
||
// Describe describes a secret that can be encoded or decoded. | ||
func (s *Secret) Describe(path string) (string, error) { | ||
encodedDescription, err := s.Resource.Describe(path) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if !s.decode { | ||
return encodedDescription, nil | ||
} | ||
|
||
o, err := s.GetFactory().Get(s.GVR(), path, true, labels.Everything()) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var secret v1.Secret | ||
err = runtime.DefaultUnstructuredConverter.FromUnstructured(o.(*unstructured.Unstructured).Object, &secret) | ||
|
||
dataEndIndex := strings.Index(encodedDescription, "====") + 4 | ||
|
||
if dataEndIndex == -1 { | ||
return "", fmt.Errorf("unable to find data section in secret description") | ||
} | ||
|
||
// Remove the encoded part from k8s's describe API | ||
// More details about the reasoning of index: https://github.com/kubernetes/kubectl/blob/v0.29.0/pkg/describe/describe.go#L2542 | ||
decodedDescription := string([]rune(encodedDescription)[0:dataEndIndex]) | ||
|
||
for k, v := range secret.Data { | ||
decodedDescription = fmt.Sprintf("%s\n%s:\t%s", decodedDescription, k, string(v)) | ||
} | ||
|
||
return decodedDescription, nil | ||
} | ||
|
||
// SetDecode sets the decode flag. | ||
func (s *Secret) SetDecode(flag bool) { | ||
s.decode = flag | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of K9s | ||
|
||
package render | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/derailed/k9s/internal/client" | ||
"github.com/derailed/tview" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// Secret renders a K8s Secret to screen. | ||
type Secret struct { | ||
Base | ||
} | ||
|
||
// Header returns a header row. | ||
func (Secret) Header(ns string) Header { | ||
h := Header{ | ||
HeaderColumn{Name: "NAMESPACE"}, | ||
HeaderColumn{Name: "NAME"}, | ||
HeaderColumn{Name: "TYPE"}, | ||
HeaderColumn{Name: "DATA", Align: tview.AlignRight}, | ||
HeaderColumn{Name: "AGE", Time: true}, | ||
} | ||
|
||
return h | ||
} | ||
|
||
// Render renders a K8s resource to screen. | ||
func (s Secret) Render(o interface{}, ns string, r *Row) error { | ||
raw, ok := o.(*unstructured.Unstructured) | ||
if !ok { | ||
return fmt.Errorf("expected Deployment, but got %T", o) | ||
} | ||
|
||
var secret v1.Secret | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(raw.Object, &secret) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.ID = client.MetaFQN(secret.ObjectMeta) | ||
r.Fields = Fields{ | ||
secret.Namespace, | ||
secret.Name, | ||
string(secret.Type), | ||
strconv.Itoa(len(secret.Data)), | ||
ToAge(secret.GetCreationTimestamp()), | ||
} | ||
|
||
return nil | ||
} |
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