Files
troubleshoot/internal/specs/configmaps.go
Evans Mungai 15a4802cd2 feat: Add dry run flag to print support bundle specs to std out (#1337)
* Add dry-run flag

* No traces on dry run

* More refactoring

* More updates to support bundle binary

* More refactoring changes

* Different approach of loading specs from URIs

* Self review

* More changes after review and testing

* fix how we parse oci image uri

* Remove unnecessary comment

* Add missing file

* Fix failing tests

* Better error check for no collectors

* Add default collectors when parsing support bundle specs

* Add missed test fixture

* Download specs with correct headers

* Fix typo
2023-10-10 18:43:32 +01:00

47 lines
1.3 KiB
Go

package specs
import (
"context"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)
func LoadFromConfigMap(ctx context.Context, client kubernetes.Interface, ns string, name string) (map[string]string, error) {
foundConfigMap, err := client.CoreV1().ConfigMaps(ns).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to get configmap")
}
klog.V(1).InfoS("Loaded data from config map", "name",
foundConfigMap.Name, "namespace", foundConfigMap.Namespace,
)
return foundConfigMap.Data, nil
}
func LoadFromConfigMapMatchingLabel(ctx context.Context, client kubernetes.Interface, label string, ns string, key string) ([]string, error) {
var configMapMatchingKey []string
configMaps, err := client.CoreV1().ConfigMaps(ns).List(ctx, metav1.ListOptions{LabelSelector: label})
if err != nil {
return nil, errors.Wrap(err, "failed to search for configmaps in the cluster")
}
for _, configMap := range configMaps.Items {
spec, ok := configMap.Data[key]
if !ok {
continue
}
klog.V(1).InfoS("Loaded spec from config map", "name", configMap.Name,
"namespace", configMap.Namespace, "data key", key, "label selector", label,
)
configMapMatchingKey = append(configMapMatchingKey, string(spec))
}
return configMapMatchingKey, nil
}