Skip to content

Commit c1f9b39

Browse files
author
Salah Aldeen Al Saleh
authored
support reading redactors from configmaps (#335)
1 parent 0a19d35 commit c1f9b39

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
5555
httpClient = http.DefaultClient
5656
}
5757

58-
collectorContent, err := loadSpec(v, arg)
58+
collectorContent, err := loadSupportBundleSpec(v, arg)
5959
if err != nil {
6060
return errors.Wrap(err, "failed to load collector spec")
6161
}
@@ -73,7 +73,7 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
7373

7474
additionalRedactors := &troubleshootv1beta2.Redactor{}
7575
for idx, redactor := range v.GetStringSlice("redactors") {
76-
redactorContent, err := loadSpec(v, redactor)
76+
redactorContent, err := loadRedactorSpec(v, redactor)
7777
if err != nil {
7878
return errors.Wrapf(err, "failed to load redactor spec #%d", idx)
7979
}
@@ -247,13 +247,12 @@ the %s Admin Console to begin analysis.`
247247
return nil
248248
}
249249

250-
func loadSpec(v *viper.Viper, arg string) ([]byte, error) {
251-
var err error
250+
func loadSupportBundleSpec(v *viper.Viper, arg string) ([]byte, error) {
252251
if strings.HasPrefix(arg, "secret/") {
253252
// format secret/namespace-name/secret-name
254253
pathParts := strings.Split(arg, "/")
255254
if len(pathParts) != 3 {
256-
return nil, errors.Errorf("path %s must have 3 components", arg)
255+
return nil, errors.Errorf("secret path %s must have 3 components", arg)
257256
}
258257

259258
spec, err := specs.LoadFromSecret(pathParts[1], pathParts[2], "support-bundle-spec")
@@ -264,6 +263,43 @@ func loadSpec(v *viper.Viper, arg string) ([]byte, error) {
264263
return spec, nil
265264
}
266265

266+
return loadSpec(v, arg)
267+
}
268+
269+
func loadRedactorSpec(v *viper.Viper, arg string) ([]byte, error) {
270+
if strings.HasPrefix(arg, "configmap/") {
271+
// format configmap/namespace-name/configmap-name[/data-key]
272+
pathParts := strings.Split(arg, "/")
273+
if len(pathParts) > 4 {
274+
return nil, errors.Errorf("configmap path %s must have at most 4 components", arg)
275+
}
276+
if len(pathParts) < 3 {
277+
return nil, errors.Errorf("configmap path %s must have at least 3 components", arg)
278+
}
279+
280+
dataKey := "redactor-spec"
281+
if len(pathParts) == 4 {
282+
dataKey = pathParts[3]
283+
}
284+
285+
spec, err := specs.LoadFromConfigMap(pathParts[1], pathParts[2], dataKey)
286+
if err != nil {
287+
return nil, errors.Wrap(err, "failed to get spec from configmap")
288+
}
289+
290+
return spec, nil
291+
}
292+
293+
spec, err := loadSpec(v, arg)
294+
if err != nil {
295+
return nil, errors.Wrap(err, "failed to load spec")
296+
}
297+
298+
return spec, nil
299+
}
300+
301+
func loadSpec(v *viper.Viper, arg string) ([]byte, error) {
302+
var err error
267303
if _, err = os.Stat(arg); err == nil {
268304
b, err := ioutil.ReadFile(arg)
269305
if err != nil {

pkg/specs/configmaps.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package specs
2+
3+
import (
4+
"context"
5+
6+
"github.com/pkg/errors"
7+
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/client-go/kubernetes"
10+
)
11+
12+
func LoadFromConfigMap(namespace string, configMapName string, key string) ([]byte, error) {
13+
config, err := k8sutil.GetRESTConfig()
14+
if err != nil {
15+
return nil, errors.Wrap(err, "failed to convert kube flags to rest config")
16+
}
17+
18+
client, err := kubernetes.NewForConfig(config)
19+
if err != nil {
20+
return nil, errors.Wrap(err, "failed to convert create k8s client")
21+
}
22+
23+
foundConfigMap, err := client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
24+
if err != nil {
25+
return nil, errors.Wrap(err, "failed to get configmap")
26+
}
27+
28+
spec, ok := foundConfigMap.Data[key]
29+
if !ok {
30+
return nil, errors.Errorf("spec not found in configmap %s", configMapName)
31+
}
32+
33+
return []byte(spec), nil
34+
}

0 commit comments

Comments
 (0)