11package collect
22
33import (
4+ "encoding/base64"
45 "encoding/json"
56 "fmt"
7+ "strings"
68
79 "github.com/replicatedhq/troubleshoot/pkg/redact"
810 corev1 "k8s.io/api/core/v1"
@@ -20,6 +22,7 @@ type ClusterResourcesOutput struct {
2022 Ingress map [string ][]byte `json:"cluster-resources/ingress,omitempty"`
2123 StorageClasses []byte `json:"cluster-resources/storage-classes.json,omitempty"`
2224 CustomResourceDefinitions []byte `json:"cluster-resources/custom-resource-definitions.json,omitempty"`
25+ ImagePullSecrets map [string ][]byte `json:"cluster-resources/image-pull-secrets,omitempty"`
2326}
2427
2528func ClusterResources (redact bool ) error {
@@ -92,6 +95,13 @@ func ClusterResources(redact bool) error {
9295 }
9396 clusterResourcesOutput .CustomResourceDefinitions = customResourceDefinitions
9497
98+ // imagepullsecrets
99+ imagePullSecrets , err := imagePullSecrets (client , namespaceNames )
100+ if err != nil {
101+ return err
102+ }
103+ clusterResourcesOutput .ImagePullSecrets = imagePullSecrets
104+
95105 if redact {
96106 clusterResourcesOutput , err = clusterResourcesOutput .Redact ()
97107 if err != nil {
@@ -231,6 +241,51 @@ func crds(client *apiextensionsv1beta1clientset.ApiextensionsV1beta1Client) ([]b
231241 return b , nil
232242}
233243
244+ func imagePullSecrets (client * kubernetes.Clientset , namespaces []string ) (map [string ][]byte , error ) {
245+ imagePullSecrets := make (map [string ][]byte )
246+
247+ // better than vendoring in.... kubernetes
248+ type DockerConfigEntry struct {
249+ Auth string `json:"auth"`
250+ }
251+ type DockerConfigJSON struct {
252+ Auths map [string ]DockerConfigEntry `json:"auths"`
253+ }
254+
255+ for _ , namespace := range namespaces {
256+ secrets , err := client .CoreV1 ().Secrets (namespace ).List (metav1.ListOptions {})
257+ if err != nil {
258+ return nil , err
259+ }
260+
261+ for _ , secret := range secrets .Items {
262+ if secret .Type == corev1 .SecretTypeDockerConfigJson {
263+ dockerConfigJSON := DockerConfigJSON {}
264+ if err := json .Unmarshal (secret .Data [corev1 .DockerConfigJsonKey ], & dockerConfigJSON ); err != nil {
265+ return nil , err
266+ }
267+
268+ for registry , registryAuth := range dockerConfigJSON .Auths {
269+ decoded , err := base64 .StdEncoding .DecodeString (registryAuth .Auth )
270+ if err != nil {
271+ return nil , err
272+ }
273+
274+ registryAndUsername := make (map [string ]string )
275+ registryAndUsername [registry ] = strings .Split (string (decoded ), ":" )[0 ]
276+ b , err := json .Marshal (registryAndUsername )
277+ if err != nil {
278+ return nil , err
279+ }
280+ imagePullSecrets [fmt .Sprintf ("%s/%s.json" , namespace , secret .Name )] = b
281+ }
282+ }
283+ }
284+ }
285+
286+ return imagePullSecrets , nil
287+ }
288+
234289func (c * ClusterResourcesOutput ) Redact () (* ClusterResourcesOutput , error ) {
235290 namespaces , err := redact .Redact (c .Namespaces )
236291 if err != nil {
@@ -268,5 +323,6 @@ func (c *ClusterResourcesOutput) Redact() (*ClusterResourcesOutput, error) {
268323 Ingress : ingress ,
269324 StorageClasses : storageClasses ,
270325 CustomResourceDefinitions : crds ,
326+ ImagePullSecrets : c .ImagePullSecrets ,
271327 }, nil
272328}
0 commit comments