Skip to content

Commit

Permalink
CRDs
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 17, 2019
1 parent 6fc6db0 commit 92862ff
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 13 deletions.
42 changes: 42 additions & 0 deletions config/crds/troubleshoot.replicated.com_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,48 @@ spec:
required:
- outcomes
type: object
customResourceDefinition:
properties:
checkName:
type: string
customResourceDefinitionName:
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
- customResourceDefinitionName
type: object
storageClass:
properties:
checkName:
Expand Down
36 changes: 34 additions & 2 deletions config/crds/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func (in *Analyze) DeepCopyInto(out *Analyze) {
*out = new(StorageClass)
(*in).DeepCopyInto(*out)
}
if in.CustomResourceDefinition != nil {
in, out := &in.CustomResourceDefinition, &out.CustomResourceDefinition
*out = new(CustomResourceDefinition)
(*in).DeepCopyInto(*out)
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Analyze.
Expand Down Expand Up @@ -512,6 +517,33 @@ func (in *CollectorStatus) DeepCopy() *CollectorStatus {
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *CustomResourceDefinition) DeepCopyInto(out *CustomResourceDefinition) {
*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 CustomResourceDefinition.
func (in *CustomResourceDefinition) DeepCopy() *CustomResourceDefinition {
if in == nil {
return nil
}
out := new(CustomResourceDefinition)
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
Expand Down Expand Up @@ -807,8 +839,8 @@ func (in *SingleOutcome) DeepCopy() *SingleOutcome {
func (in *StorageClass) DeepCopyInto(out *StorageClass) {
*out = *in
out.AnalyzeMeta = in.AnalyzeMeta
if in.Outcome != nil {
in, out := &in.Outcome, &out.Outcome
if in.Outcomes != nil {
in, out := &in.Outcomes, &out.Outcomes
*out = make([]*Outcome, len(*in))
for i := range *in {
if (*in)[i] != nil {
Expand Down
13 changes: 7 additions & 6 deletions config/samples/troubleshoot_v1beta1_preflight.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ spec:
# message: Can't pull the images
# pass:
# message: Connected to docker registry
# - customResourceDefinitions:
# name: rook
# fail:
# message: You don't have rook installed
# pass:
# message: Found rook!
- customResourceDefinition:
customResourceDefinitionName: rook
outcomes:
- fail:
message: You don't have rook installed
- pass:
message: Found rook!
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apiextensions-apiserver v0.0.0-20190409022649-727a075fdec8
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
sigs.k8s.io/controller-runtime v0.2.0-beta.2
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getCollectedFileContents fun
if analyzer.StorageClass != nil {
return analyzeStorageClass(analyzer.StorageClass, getCollectedFileContents)
}
if analyzer.CustomResourceDefinition != nil {
return analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getCollectedFileContents)
}

return nil, errors.New("invalid analyzer")
}
54 changes: 54 additions & 0 deletions pkg/analyze/crd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package analyzer

import (
"encoding/json"
"fmt"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
)

func analyzeCustomResourceDefinition(analyzer *troubleshootv1beta1.CustomResourceDefinition, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
crdData, err := getCollectedFileContents("cluster-resources/custom-resource-definitions.json")
if err != nil {
return nil, err
}

var crds []apiextensionsv1beta1.CustomResourceDefinition
if err := json.Unmarshal(crdData, &crds); err != nil {
return nil, err
}

title := analyzer.CheckName
if title == "" {
title = fmt.Sprintf("Custom resource definition %s", analyzer.CustomResourceDefinitionName)
}

result := AnalyzeResult{
Title: title,
}

for _, storageClass := range crds {
if storageClass.Name == analyzer.CustomResourceDefinitionName {
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
}
1 change: 0 additions & 1 deletion pkg/analyze/storage_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
storagev1beta1 "k8s.io/api/storage/v1beta1"
// "github.com/replicatedhq/troubleshoot/pkg/collect"
)

func analyzeStorageClass(analyzer *troubleshootv1beta1.StorageClass, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
Expand Down
11 changes: 9 additions & 2 deletions pkg/apis/troubleshoot/v1beta1/analyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ type StorageClass struct {
StorageClassName string `json:"storageClassName" yaml:"storageClassName"`
}

type CustomResourceDefinition struct {
AnalyzeMeta `json:",inline" yaml:",inline"`
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
CustomResourceDefinitionName string `json:"customResourceDefinitionName" yaml:"customResourceDefinitionName"`
}

type AnalyzeMeta struct {
CheckName string `json:"checkName,omitempty" yaml:"checkName,omitempty"`
}

type Analyze struct {
ClusterVersion *ClusterVersion `json:"clusterVersion,omitempty" yaml:"clusterVersion,omitempty"`
StorageClass *StorageClass `json:"storageClass,omitempty" yaml:"storageClass,omitempty"`
ClusterVersion *ClusterVersion `json:"clusterVersion,omitempty" yaml:"clusterVersion,omitempty"`
StorageClass *StorageClass `json:"storageClass,omitempty" yaml:"storageClass,omitempty"`
CustomResourceDefinition *CustomResourceDefinition `json:"customResourceDefinition,omitempty" yaml:"customResourceDefinition,omitempty"`
}
32 changes: 32 additions & 0 deletions pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (in *Analyze) DeepCopyInto(out *Analyze) {
*out = new(StorageClass)
(*in).DeepCopyInto(*out)
}
if in.CustomResourceDefinition != nil {
in, out := &in.CustomResourceDefinition, &out.CustomResourceDefinition
*out = new(CustomResourceDefinition)
(*in).DeepCopyInto(*out)
}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Analyze.
Expand Down Expand Up @@ -528,6 +533,33 @@ func (in *CollectorStatus) DeepCopy() *CollectorStatus {
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *CustomResourceDefinition) DeepCopyInto(out *CustomResourceDefinition) {
*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 CustomResourceDefinition.
func (in *CustomResourceDefinition) DeepCopy() *CustomResourceDefinition {
if in == nil {
return nil
}
out := new(CustomResourceDefinition)
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
Expand Down
3 changes: 1 addition & 2 deletions pkg/collect/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

corev1 "k8s.io/api/core/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsv1beta1clientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -82,7 +81,7 @@ func ClusterResources() error {
clusterResourcesOutput.StorageClasses = storageClasses

// crds
crdClient, err := apiextensionsv1beta1.NewForConfig(cfg)
crdClient, err := apiextensionsv1beta1clientset.NewForConfig(cfg)
if err != nil {
return err
}
Expand Down

0 comments on commit 92862ff

Please sign in to comment.