support the 'includeValue' flag for secrets
@@ -34,16 +34,20 @@ func Secret(ctx *Context, secretCollector *troubleshootv1beta1.Secret) ([]byte,
Errors: make(map[string][]byte),
}
- secret, encoded, err := secret(client, secretCollector)
+ if secretCollector.Namespace == "" {
+ secretCollector.Namespace = "default"
+ }
+
+ path, encoded, err := secret(client, secretCollector)
if err != nil {
errorBytes, err := marshalNonNil([]string{err.Error()})
if err != nil {
return nil, err
}
- secretOutput.Errors[fmt.Sprintf("%s.json", secret.Name)] = errorBytes
+ secretOutput.Errors[path] = errorBytes
}
if encoded != nil {
- secretOutput.FoundSecret[fmt.Sprintf("%s.json", secret.Name)] = encoded
+ secretOutput.FoundSecret[path] = encoded
if ctx.Redact {
secretOutput, err = secretOutput.Redact()
if err != nil {
@@ -60,7 +64,12 @@ func Secret(ctx *Context, secretCollector *troubleshootv1beta1.Secret) ([]byte,
return b, nil
}
-func secret(client *kubernetes.Clientset, secretCollector *troubleshootv1beta1.Secret) (*FoundSecret, []byte, error) {
+func secret(client *kubernetes.Clientset, secretCollector *troubleshootv1beta1.Secret) (string, []byte, error) {
+ path := fmt.Sprintf("%s/%s.json", secretCollector.Namespace, secretCollector.SecretName)
+ if secretCollector.Key != "" {
+ path = fmt.Sprintf("%s/%s/%s.json", secretCollector.Namespace, secretCollector.SecretName, secretCollector.Key)
+ }
+
found, err := client.CoreV1().Secrets(secretCollector.Namespace).Get(secretCollector.SecretName, metav1.GetOptions{})
if err != nil {
missingSecret := FoundSecret{
@@ -71,16 +80,20 @@ func secret(client *kubernetes.Clientset, secretCollector *troubleshootv1beta1.S
b, marshalErr := json.MarshalIndent(missingSecret, "", " ")
if marshalErr != nil {
- return nil, nil, marshalErr
+ return path, nil, marshalErr
}
- return &missingSecret, b, err
+ return path, b, err
}
keyExists := false
+ keyData := ""
if secretCollector.Key != "" {
- if _, ok := found.Data[secretCollector.Key]; ok {
+ if val, ok := found.Data[secretCollector.Key]; ok {
keyExists = true
+ if secretCollector.IncludeValue {
+ keyData = string(val)
+ }
}
}
@@ -89,14 +102,15 @@ func secret(client *kubernetes.Clientset, secretCollector *troubleshootv1beta1.S
Name: found.Name,
SecretExists: true,
KeyExists: keyExists,
+ Value: keyData,
}
b, err := json.MarshalIndent(secret, "", " ")
if err != nil {
- return nil, nil, err
+ return path, nil, err
}
- return &secret, b, nil
+ return path, b, nil
}
func (s *SecretOutput) Redact() (*SecretOutput, error) {
