mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
Running collectors without the CRD
This commit is contained in:
@@ -29,6 +29,9 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getCollectedFileContents fun
|
||||
if analyzer.Ingress != nil {
|
||||
return analyzeIngress(analyzer.Ingress, getCollectedFileContents)
|
||||
}
|
||||
if analyzer.Secret != nil {
|
||||
return analyzeSecret(analyzer.Secret, getCollectedFileContents)
|
||||
}
|
||||
|
||||
return nil, errors.New("invalid analyzer")
|
||||
}
|
||||
|
||||
65
pkg/analyze/secret.go
Normal file
65
pkg/analyze/secret.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
"github.com/replicatedhq/troubleshoot/pkg/collect"
|
||||
)
|
||||
|
||||
func analyzeSecret(analyzer *troubleshootv1beta1.AnalyzeSecret, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
|
||||
secretData, err := getCollectedFileContents(fmt.Sprintf("secrets/%s/%s.json", analyzer.Namespace, analyzer.SecretName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var foundSecret collect.FoundSecret
|
||||
if err := json.Unmarshal(secretData, &foundSecret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
title := analyzer.CheckName
|
||||
if title == "" {
|
||||
title = fmt.Sprintf("Secret %s", analyzer.SecretName)
|
||||
}
|
||||
|
||||
result := AnalyzeResult{
|
||||
Title: title,
|
||||
}
|
||||
|
||||
var failOutcome *troubleshootv1beta1.Outcome
|
||||
for _, outcome := range analyzer.Outcomes {
|
||||
if outcome.Fail != nil {
|
||||
failOutcome = outcome
|
||||
}
|
||||
}
|
||||
|
||||
if !foundSecret.SecretExists {
|
||||
result.IsFail = true
|
||||
result.Message = failOutcome.Fail.Message
|
||||
result.URI = failOutcome.Fail.URI
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
if analyzer.Key != "" {
|
||||
if foundSecret.Key != analyzer.Key || !foundSecret.KeyExists {
|
||||
result.IsFail = true
|
||||
result.Message = failOutcome.Fail.Message
|
||||
result.URI = failOutcome.Fail.URI
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
}
|
||||
|
||||
result.IsPass = true
|
||||
for _, outcome := range analyzer.Outcomes {
|
||||
if outcome.Pass != nil {
|
||||
result.Message = outcome.Pass.Message
|
||||
result.URI = outcome.Pass.URI
|
||||
}
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
@@ -36,6 +36,14 @@ type Ingress struct {
|
||||
Namespace string `json:"namespace" yaml:"namespace"`
|
||||
}
|
||||
|
||||
type AnalyzeSecret struct {
|
||||
AnalyzeMeta `json:",inline" yaml:",inline"`
|
||||
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
|
||||
SecretName string `json:"secretName" yaml:"secretName"`
|
||||
Namespace string `json:"namespace" yaml:"namespace"`
|
||||
Key string `json:"key,omitempty" yaml:"key,omitempty"`
|
||||
}
|
||||
|
||||
type AnalyzeMeta struct {
|
||||
CheckName string `json:"checkName,omitempty" yaml:"checkName,omitempty"`
|
||||
}
|
||||
@@ -45,4 +53,5 @@ type Analyze struct {
|
||||
StorageClass *StorageClass `json:"storageClass,omitempty" yaml:"storageClass,omitempty"`
|
||||
CustomResourceDefinition *CustomResourceDefinition `json:"customResourceDefinition,omitempty" yaml:"customResourceDefinition,omitempty"`
|
||||
Ingress *Ingress `json:"ingress,omitempty" yaml:"ingress,omitempty"`
|
||||
Secret *AnalyzeSecret `json:"secret,omitempty" yaml:"secret,omitempty"`
|
||||
}
|
||||
|
||||
@@ -6,7 +6,15 @@ type ClusterInfo struct {
|
||||
type ClusterResources struct {
|
||||
}
|
||||
|
||||
type Secret struct {
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
||||
Key string `json:"key,omitempty" yaml:"key,omitempty"`
|
||||
IncludeValue bool `json:"includeValue,omitempty" yaml:"includeValue,omitempty"`
|
||||
}
|
||||
|
||||
type Collect struct {
|
||||
ClusterInfo *ClusterInfo `json:"clusterInfo,omitempty" yaml:"clusterInfo,omitempty"`
|
||||
ClusterResources *ClusterResources `json:"clusterResources,omitempty" yaml:"clusterResources,omitempty"`
|
||||
Secret *Secret `json:"secret,omitempty" yaml:"secret,omitempty"`
|
||||
}
|
||||
|
||||
@@ -32,10 +32,10 @@ type CollectorStatus struct {
|
||||
// Collector is the Schema for the collectors API
|
||||
// +k8s:openapi-gen=true
|
||||
type Collector struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata,omitempty"`
|
||||
metav1.TypeMeta `json:",inline" yaml:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||
|
||||
Spec []*Collect `json:"spec,omitempty"`
|
||||
Spec []*Collect `json:"spec,omitempty" yaml:"spec,omitempty"`
|
||||
Status CollectorStatus `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,11 @@ func (in *Analyze) DeepCopyInto(out *Analyze) {
|
||||
*out = new(Ingress)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
if in.Secret != nil {
|
||||
in, out := &in.Secret, &out.Secret
|
||||
*out = new(AnalyzeSecret)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Analyze.
|
||||
@@ -74,6 +79,33 @@ func (in *AnalyzeMeta) DeepCopy() *AnalyzeMeta {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *AnalyzeSecret) DeepCopyInto(out *AnalyzeSecret) {
|
||||
*out = *in
|
||||
out.AnalyzeMeta = in.AnalyzeMeta
|
||||
if in.Outcomes != nil {
|
||||
in, out := &in.Outcomes, &out.Outcomes
|
||||
*out = make([]*Outcome, len(*in))
|
||||
for i := range *in {
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(Outcome)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AnalyzeSecret.
|
||||
func (in *AnalyzeSecret) DeepCopy() *AnalyzeSecret {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(AnalyzeSecret)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Analyzer) DeepCopyInto(out *Analyzer) {
|
||||
*out = *in
|
||||
@@ -322,6 +354,11 @@ func (in *Collect) DeepCopyInto(out *Collect) {
|
||||
*out = new(ClusterResources)
|
||||
**out = **in
|
||||
}
|
||||
if in.Secret != nil {
|
||||
in, out := &in.Secret, &out.Secret
|
||||
*out = new(Secret)
|
||||
**out = **in
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Collect.
|
||||
@@ -868,6 +905,21 @@ func (in *PreflightStatus) DeepCopy() *PreflightStatus {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Secret) DeepCopyInto(out *Secret) {
|
||||
*out = *in
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Secret.
|
||||
func (in *Secret) DeepCopy() *Secret {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Secret)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *SingleOutcome) DeepCopyInto(out *SingleOutcome) {
|
||||
*out = *in
|
||||
|
||||
@@ -19,9 +19,13 @@ func (c *Collector) RunCollectorSync() error {
|
||||
|
||||
if collect.ClusterInfo != nil {
|
||||
return ClusterInfo()
|
||||
} else if collect.ClusterResources != nil {
|
||||
}
|
||||
if collect.ClusterResources != nil {
|
||||
return ClusterResources()
|
||||
}
|
||||
if collect.Secret != nil {
|
||||
return Secret(collect.Secret)
|
||||
}
|
||||
|
||||
return errors.New("no spec found to run")
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package preflight
|
||||
package collect
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -15,13 +15,13 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
)
|
||||
|
||||
func CreateCollector(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, preflightJobName string, preflightJobNamespace string, collect *troubleshootv1beta1.Collect, image string, pullPolicy string) (*corev1.ConfigMap, *corev1.Pod, error) {
|
||||
configMap, err := createCollectorSpecConfigMap(client, scheme, ownerRef, preflightJobName, preflightJobNamespace, collect)
|
||||
func CreateCollector(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, jobName string, jobNamespace string, jobType string, collect *troubleshootv1beta1.Collect, image string, pullPolicy string) (*corev1.ConfigMap, *corev1.Pod, error) {
|
||||
configMap, err := createCollectorSpecConfigMap(client, scheme, ownerRef, jobName, jobNamespace, collect)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
pod, err := createCollectorPod(client, scheme, ownerRef, preflightJobName, preflightJobNamespace, collect, configMap, image, pullPolicy)
|
||||
pod, err := createCollectorPod(client, scheme, ownerRef, jobName, jobNamespace, jobType, collect, configMap, image, pullPolicy)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -29,12 +29,11 @@ func CreateCollector(client client.Client, scheme *runtime.Scheme, ownerRef meta
|
||||
return configMap, pod, nil
|
||||
}
|
||||
|
||||
func createCollectorSpecConfigMap(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, preflightJobName string, preflightJobNamespace string, collect *troubleshootv1beta1.Collect) (*corev1.ConfigMap, error) {
|
||||
name := fmt.Sprintf("%s-%s", preflightJobName, idForCollector(collect))
|
||||
|
||||
func createCollectorSpecConfigMap(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, jobName string, jobNamespace string, collect *troubleshootv1beta1.Collect) (*corev1.ConfigMap, error) {
|
||||
name := fmt.Sprintf("%s-%s", jobName, idForCollector(collect))
|
||||
namespacedName := types.NamespacedName{
|
||||
Name: name,
|
||||
Namespace: preflightJobNamespace,
|
||||
Namespace: jobNamespace,
|
||||
}
|
||||
|
||||
found := &corev1.ConfigMap{}
|
||||
@@ -54,7 +53,7 @@ func createCollectorSpecConfigMap(client client.Client, scheme *runtime.Scheme,
|
||||
configMap := corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: preflightJobNamespace,
|
||||
Namespace: jobNamespace,
|
||||
},
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
@@ -76,12 +75,12 @@ func createCollectorSpecConfigMap(client client.Client, scheme *runtime.Scheme,
|
||||
return &configMap, nil
|
||||
}
|
||||
|
||||
func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, preflightJobName string, preflightJobNamespace string, collect *troubleshootv1beta1.Collect, configMap *corev1.ConfigMap, image string, pullPolicy string) (*corev1.Pod, error) {
|
||||
name := fmt.Sprintf("%s-%s", preflightJobName, idForCollector(collect))
|
||||
func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef metav1.Object, jobName string, jobNamespace string, jobType string, collect *troubleshootv1beta1.Collect, configMap *corev1.ConfigMap, image string, pullPolicy string) (*corev1.Pod, error) {
|
||||
name := fmt.Sprintf("%s-%s", jobName, idForCollector(collect))
|
||||
|
||||
namespacedName := types.NamespacedName{
|
||||
Name: name,
|
||||
Namespace: preflightJobNamespace,
|
||||
Namespace: jobNamespace,
|
||||
}
|
||||
|
||||
found := &corev1.Pod{}
|
||||
@@ -101,13 +100,14 @@ func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef m
|
||||
}
|
||||
|
||||
podLabels := make(map[string]string)
|
||||
podLabels["preflight"] = preflightJobName
|
||||
podLabels["troubleshoot-role"] = "preflight"
|
||||
|
||||
podLabels[jobType] = jobName
|
||||
podLabels["troubleshoot-role"] = jobType
|
||||
|
||||
pod := corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: preflightJobNamespace,
|
||||
Namespace: jobNamespace,
|
||||
Labels: podLabels,
|
||||
},
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
@@ -163,13 +163,15 @@ func createCollectorPod(client client.Client, scheme *runtime.Scheme, ownerRef m
|
||||
return &pod, nil
|
||||
}
|
||||
|
||||
// Todo these will overlap with troubleshoot containers running at the same time
|
||||
func idForCollector(collector *troubleshootv1beta1.Collect) string {
|
||||
if collector.ClusterInfo != nil {
|
||||
return "cluster-info"
|
||||
} else if collector.ClusterResources != nil {
|
||||
}
|
||||
if collector.ClusterResources != nil {
|
||||
return "cluster-resources"
|
||||
}
|
||||
|
||||
if collector.Secret != nil {
|
||||
return fmt.Sprintf("secret-%s%s", collector.Secret.Namespace, collector.Secret.Name)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
94
pkg/collect/secret.go
Normal file
94
pkg/collect/secret.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package collect
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/config"
|
||||
)
|
||||
|
||||
type FoundSecret struct {
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Key string `json:"key"`
|
||||
SecretExists bool `json:"secretExists"`
|
||||
KeyExists bool `json:"keyExists"`
|
||||
Value string `json:"value,omitempty"`
|
||||
}
|
||||
type SecretOutput struct {
|
||||
FoundSecret map[string][]byte `json:"secrets/,omitempty"`
|
||||
}
|
||||
|
||||
func Secret(secretCollector *troubleshootv1beta1.Secret) error {
|
||||
cfg, err := config.GetConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := kubernetes.NewForConfig(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
secret, encoded, err := secret(client, secretCollector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
secretOutput := SecretOutput{
|
||||
FoundSecret: map[string][]byte{
|
||||
fmt.Sprintf("%s/%s.json", secret.Namespace, secret.Name): encoded,
|
||||
},
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(secretOutput, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("%s\n", b)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func secret(client *kubernetes.Clientset, secretCollector *troubleshootv1beta1.Secret) (*FoundSecret, []byte, error) {
|
||||
found, err := client.CoreV1().Secrets(secretCollector.Namespace).Get(secretCollector.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
missingSecret := FoundSecret{
|
||||
Namespace: secretCollector.Namespace,
|
||||
Name: secretCollector.Name,
|
||||
SecretExists: false,
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(missingSecret, "", " ")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &missingSecret, b, err
|
||||
}
|
||||
|
||||
keyExists := false
|
||||
if secretCollector.Key != "" {
|
||||
if _, ok := found.Data[secretCollector.Key]; ok {
|
||||
keyExists = true
|
||||
}
|
||||
}
|
||||
|
||||
secret := FoundSecret{
|
||||
Namespace: found.Namespace,
|
||||
Name: found.Name,
|
||||
SecretExists: true,
|
||||
KeyExists: keyExists,
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(secret, "", " ")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &secret, b, nil
|
||||
}
|
||||
@@ -434,6 +434,8 @@ func idForCollector(collector *troubleshootv1beta1.Collect) string {
|
||||
return "cluster-info"
|
||||
} else if collector.ClusterResources != nil {
|
||||
return "cluster-resources"
|
||||
} else if collector.Secret != nil {
|
||||
return fmt.Sprintf("secret-%s%s", collector.Secret.Namespace, collector.Secret.Name)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -2,9 +2,10 @@ package preflightjob
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
"github.com/replicatedhq/troubleshoot/pkg/preflight"
|
||||
collectrunner "github.com/replicatedhq/troubleshoot/pkg/collect"
|
||||
)
|
||||
|
||||
func (r *ReconcilePreflightJob) reconcilePreflightCollectors(instance *troubleshootv1beta1.PreflightJob, preflight *troubleshootv1beta1.Preflight) error {
|
||||
@@ -57,7 +58,7 @@ func (r *ReconcilePreflightJob) reconcileOnePreflightCollector(instance *trouble
|
||||
return nil
|
||||
}
|
||||
|
||||
_, _, err := preflight.CreateCollector(r.Client, r.scheme, instance, instance.Name, instance.Namespace, collect, instance.Spec.Image, instance.Spec.ImagePullPolicy)
|
||||
_, _, err := collectrunner.CreateCollector(r.Client, r.scheme, instance, instance.Name, instance.Namespace, "preflight", collect, instance.Spec.Image, instance.Spec.ImagePullPolicy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -94,6 +95,8 @@ func idForCollector(collector *troubleshootv1beta1.Collect) string {
|
||||
return "cluster-info"
|
||||
} else if collector.ClusterResources != nil {
|
||||
return "cluster-resources"
|
||||
} else if collector.Secret != nil {
|
||||
return fmt.Sprintf("secret-%s%s", collector.Secret.Namespace, collector.Secret.Name)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
@@ -36,8 +35,6 @@ func putCollectorOutput(c *gin.Context) {
|
||||
}
|
||||
|
||||
collectorQueue[collectorID] = body
|
||||
|
||||
fmt.Printf("collectorQueue = %#v\n", collectorQueue)
|
||||
c.Status(201)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user