From e7174acf079b7e7d618346ad565cf8b571e1d4ab Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Wed, 17 Jul 2019 22:31:54 +0000 Subject: [PATCH] Ingress preflight --- ...roubleshoot.replicated.com_preflights.yaml | 45 ++++++++++++++++ config/crds/zz_generated.deepcopy.go | 32 +++++++++++ .../troubleshoot_v1beta1_preflight.yaml | 15 +++--- go.sum | 1 + pkg/analyze/analyzer.go | 3 ++ pkg/analyze/ingress.go | 54 +++++++++++++++++++ .../troubleshoot/v1beta1/analyzer_shared.go | 8 +++ .../v1beta1/zz_generated.deepcopy.go | 32 +++++++++++ 8 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 pkg/analyze/ingress.go diff --git a/config/crds/troubleshoot.replicated.com_preflights.yaml b/config/crds/troubleshoot.replicated.com_preflights.yaml index 46057225..1b3c81b9 100644 --- a/config/crds/troubleshoot.replicated.com_preflights.yaml +++ b/config/crds/troubleshoot.replicated.com_preflights.yaml @@ -475,6 +475,51 @@ spec: - outcomes - customResourceDefinitionName type: object + ingress: + properties: + checkName: + type: string + ingressName: + type: string + namespace: + type: string + outcomes: + items: + properties: + fail: + properties: + message: + type: string + uri: + type: string + when: + type: string + type: object + pass: + properties: + message: + type: string + uri: + type: string + when: + type: string + type: object + warn: + properties: + message: + type: string + uri: + type: string + when: + type: string + type: object + type: object + type: array + required: + - outcomes + - ingressName + - namespace + type: object storageClass: properties: checkName: diff --git a/config/crds/zz_generated.deepcopy.go b/config/crds/zz_generated.deepcopy.go index f5aee509..f9d23f2b 100644 --- a/config/crds/zz_generated.deepcopy.go +++ b/config/crds/zz_generated.deepcopy.go @@ -26,6 +26,11 @@ func (in *Analyze) DeepCopyInto(out *Analyze) { *out = new(CustomResourceDefinition) (*in).DeepCopyInto(*out) } + if in.Ingress != nil { + in, out := &in.Ingress, &out.Ingress + *out = new(Ingress) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Analyze. @@ -544,6 +549,33 @@ func (in *CustomResourceDefinition) DeepCopy() *CustomResourceDefinition { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Ingress) DeepCopyInto(out *Ingress) { + *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 Ingress. +func (in *Ingress) DeepCopy() *Ingress { + if in == nil { + return nil + } + out := new(Ingress) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Outcome) DeepCopyInto(out *Outcome) { *out = *in diff --git a/config/samples/troubleshoot_v1beta1_preflight.yaml b/config/samples/troubleshoot_v1beta1_preflight.yaml index 81dab210..21f2c934 100644 --- a/config/samples/troubleshoot_v1beta1_preflight.yaml +++ b/config/samples/troubleshoot_v1beta1_preflight.yaml @@ -33,13 +33,14 @@ spec: # message: The top secret secret is missing # pass: # message: You know the secret - # - ingress: - # namespace: default - # name: connect-to-me - # fail: - # message: The ingress isn't ingressing - # pass: - # message: All systems ok on ingress + - ingress: + namespace: default + ingressName: connect-to-me + outcomes: + - fail: + message: The ingress isn't ingressing + - pass: + message: All systems ok on ingress # - imagePullSecret: # name: replicated # namespace: my-app diff --git a/go.sum b/go.sum index a54bb8fb..bad705ba 100644 --- a/go.sum +++ b/go.sum @@ -480,6 +480,7 @@ k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b h1:aBGgKJUM9Hk/3AE8WaZIApnTxG35kbu k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA= k8s.io/api v0.0.0-20190703205437-39734b2a72fe h1:MFaHtAyhZcfBZocN91muHSqnwiF5yfXx7yGoehneNYg= k8s.io/api v0.0.0-20190703205437-39734b2a72fe/go.mod h1:J5EZ0KSEjvyKOBy5BDHSF3zn82madLLWg7nUKaOHZKU= +k8s.io/api v0.0.0-20190717022910-653c86b0609b h1:WiE134uexhvhHw4DjJuYsghv792UCe2xN5SHQOayf28= k8s.io/apiextensions-apiserver v0.0.0-20190228180357-d002e88f6236 h1:JfFtjaElBIgYKCWEtYQkcNrTpW+lMO4GJy8NP6SVQmM= k8s.io/apiextensions-apiserver v0.0.0-20190228180357-d002e88f6236/go.mod h1:IxkesAMoaCRoLrPJdZNZUQp9NfZnzqaVzLhb2VEQzXE= k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8 h1:q1Qvjzs/iEdXF6A1a8H3AKVFDzJNcJn3nXMs6R6qFtA= diff --git a/pkg/analyze/analyzer.go b/pkg/analyze/analyzer.go index c0d2fbdd..ff24a102 100644 --- a/pkg/analyze/analyzer.go +++ b/pkg/analyze/analyzer.go @@ -26,6 +26,9 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getCollectedFileContents fun if analyzer.CustomResourceDefinition != nil { return analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getCollectedFileContents) } + if analyzer.Ingress != nil { + return analyzeIngress(analyzer.Ingress, getCollectedFileContents) + } return nil, errors.New("invalid analyzer") } diff --git a/pkg/analyze/ingress.go b/pkg/analyze/ingress.go new file mode 100644 index 00000000..b88b98f1 --- /dev/null +++ b/pkg/analyze/ingress.go @@ -0,0 +1,54 @@ +package analyzer + +import ( + "encoding/json" + "fmt" + + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" + extensionsv1beta1 "k8s.io/api/extensions/v1beta1" +) + +func analyzeIngress(analyzer *troubleshootv1beta1.Ingress, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { + ingressData, err := getCollectedFileContents("cluster-resources/storage-classes.json") + if err != nil { + return nil, err + } + + var ingresses []extensionsv1beta1.Ingress + if err := json.Unmarshal(ingressData, &ingresses); err != nil { + return nil, err + } + + title := analyzer.CheckName + if title == "" { + title = fmt.Sprintf("Ingress %s", analyzer.IngressName) + } + + result := AnalyzeResult{ + Title: title, + } + + for _, ingress := range ingresses { + if ingress.Name == analyzer.IngressName { + 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 + } + } + + result.IsFail = true + for _, outcome := range analyzer.Outcomes { + if outcome.Fail != nil { + result.Message = outcome.Fail.Message + result.URI = outcome.Fail.URI + } + } + + return &result, nil +} diff --git a/pkg/apis/troubleshoot/v1beta1/analyzer_shared.go b/pkg/apis/troubleshoot/v1beta1/analyzer_shared.go index b4cdfecc..f21db454 100644 --- a/pkg/apis/troubleshoot/v1beta1/analyzer_shared.go +++ b/pkg/apis/troubleshoot/v1beta1/analyzer_shared.go @@ -29,6 +29,13 @@ type CustomResourceDefinition struct { CustomResourceDefinitionName string `json:"customResourceDefinitionName" yaml:"customResourceDefinitionName"` } +type Ingress struct { + AnalyzeMeta `json:",inline" yaml:",inline"` + Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"` + IngressName string `json:"ingressName" yaml:"ingressName"` + Namespace string `json:"namespace" yaml:"namespace"` +} + type AnalyzeMeta struct { CheckName string `json:"checkName,omitempty" yaml:"checkName,omitempty"` } @@ -37,4 +44,5 @@ type Analyze struct { ClusterVersion *ClusterVersion `json:"clusterVersion,omitempty" yaml:"clusterVersion,omitempty"` StorageClass *StorageClass `json:"storageClass,omitempty" yaml:"storageClass,omitempty"` CustomResourceDefinition *CustomResourceDefinition `json:"customResourceDefinition,omitempty" yaml:"customResourceDefinition,omitempty"` + Ingress *Ingress `json:"ingress,omitempty" yaml:"ingress,omitempty"` } diff --git a/pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go b/pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go index c8bdd98d..a2c3ac81 100644 --- a/pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go +++ b/pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go @@ -42,6 +42,11 @@ func (in *Analyze) DeepCopyInto(out *Analyze) { *out = new(CustomResourceDefinition) (*in).DeepCopyInto(*out) } + if in.Ingress != nil { + in, out := &in.Ingress, &out.Ingress + *out = new(Ingress) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Analyze. @@ -560,6 +565,33 @@ func (in *CustomResourceDefinition) DeepCopy() *CustomResourceDefinition { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Ingress) DeepCopyInto(out *Ingress) { + *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 Ingress. +func (in *Ingress) DeepCopy() *Ingress { + if in == nil { + return nil + } + out := new(Ingress) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Outcome) DeepCopyInto(out *Outcome) { *out = *in