@@ -11,25 +11,26 @@ import (
1111 "github.com/replicatedhq/troubleshoot/pkg/constants"
1212 iutils "github.com/replicatedhq/troubleshoot/pkg/interfaceutils"
1313 "gopkg.in/yaml.v2"
14+ "k8s.io/klog/v2"
1415)
1516
1617var Filemap = map [string ]string {
17- "Deployment" : constants .CLUSTER_RESOURCES_DEPLOYMENTS ,
18- "StatefulSet" : constants .CLUSTER_RESOURCES_STATEFULSETS ,
19- "NetworkPolicy" : constants .CLUSTER_RESOURCES_NETWORK_POLICY ,
20- "Pod" : constants .CLUSTER_RESOURCES_PODS ,
21- "Ingress" : constants .CLUSTER_RESOURCES_INGRESS ,
22- "Service" : constants .CLUSTER_RESOURCES_SERVICES ,
23- "ResourceQuota" : constants .CLUSTER_RESOURCES_RESOURCE_QUOTA ,
24- "Job" : constants .CLUSTER_RESOURCES_JOBS ,
25- "PersistentVoumeClaim " : constants .CLUSTER_RESOURCES_PVCS ,
26- "pvc" : constants .CLUSTER_RESOURCES_PVCS ,
27- "ReplicaSet" : constants .CLUSTER_RESOURCES_REPLICASETS ,
28- "Namespace" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_NAMESPACES ),
29- "PersistentVolume" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_PVS ),
30- "pv" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_PVS ),
31- "Node" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_NODES ),
32- "StorageClass" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_STORAGE_CLASS ),
18+ "Deployment" : constants .CLUSTER_RESOURCES_DEPLOYMENTS ,
19+ "StatefulSet" : constants .CLUSTER_RESOURCES_STATEFULSETS ,
20+ "NetworkPolicy" : constants .CLUSTER_RESOURCES_NETWORK_POLICY ,
21+ "Pod" : constants .CLUSTER_RESOURCES_PODS ,
22+ "Ingress" : constants .CLUSTER_RESOURCES_INGRESS ,
23+ "Service" : constants .CLUSTER_RESOURCES_SERVICES ,
24+ "ResourceQuota" : constants .CLUSTER_RESOURCES_RESOURCE_QUOTA ,
25+ "Job" : constants .CLUSTER_RESOURCES_JOBS ,
26+ "PersistentVolumeClaim " : constants .CLUSTER_RESOURCES_PVCS ,
27+ "pvc" : constants .CLUSTER_RESOURCES_PVCS ,
28+ "ReplicaSet" : constants .CLUSTER_RESOURCES_REPLICASETS ,
29+ "Namespace" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_NAMESPACES ),
30+ "PersistentVolume" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_PVS ),
31+ "pv" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_PVS ),
32+ "Node" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_NODES ),
33+ "StorageClass" : fmt .Sprintf ("%s.json" , constants .CLUSTER_RESOURCES_STORAGE_CLASS ),
3334}
3435
3536type AnalyzeClusterResource struct {
@@ -108,15 +109,28 @@ func FindResource(kind string, clusterScoped bool, namespace string, name string
108109}
109110
110111func (a * AnalyzeClusterResource ) analyzeResource (analyzer * troubleshootv1beta2.ClusterResource , getFileContents getCollectedFileContents ) (* AnalyzeResult , error ) {
111-
112112 selected , err := FindResource (analyzer .Kind , analyzer .ClusterScoped , analyzer .Namespace , analyzer .Name , getFileContents )
113113 if err != nil {
114- return nil , errors .Wrap (err , "failed to find resource" )
114+ klog .Errorf ("failed to find resource: %v" , err )
115+ return & AnalyzeResult {
116+ Title : a .Title (),
117+ IconKey : "kubernetes_text_analyze" ,
118+ IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg" ,
119+ IsFail : true ,
120+ Message : "resource does not exist" ,
121+ }, nil
115122 }
116123
117124 actual , err := iutils .GetAtPath (selected , analyzer .YamlPath )
118125 if err != nil {
119- return nil , errors .Wrapf (err , "failed to get object at path: %s" , analyzer .YamlPath )
126+ klog .Errorf ("invalid yaml path: %s for kind: %s: %v" , analyzer .YamlPath , analyzer .Kind , err )
127+ return & AnalyzeResult {
128+ Title : a .Title (),
129+ IconKey : "kubernetes_text_analyze" ,
130+ IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg" ,
131+ IsFail : true ,
132+ Message : "YAML path provided is invalid" ,
133+ }, nil
120134 }
121135
122136 var expected interface {}
@@ -125,15 +139,58 @@ func (a *AnalyzeClusterResource) analyzeResource(analyzer *troubleshootv1beta2.C
125139 return nil , errors .Wrap (err , "failed to parse expected value as yaml doc" )
126140 }
127141
128- result := & AnalyzeResult {
142+ actualYAML , err := yaml .Marshal (actual )
143+ if err != nil {
144+ return nil , errors .Wrap (err , "failed to marshal actual value" )
145+ }
146+
147+ if analyzer .ExpectedValue != "" {
148+ result , err := analyzeValue (expected , actual , analyzer .Outcomes , a .Title ())
149+ if err != nil {
150+ return nil , err
151+ }
152+ if result != nil {
153+ return result , nil
154+ }
155+ } else if analyzer .RegexPattern != "" {
156+ result , err := analyzeRegexPattern (analyzer .RegexPattern , actualYAML , analyzer .Outcomes , a .Title ())
157+ if err != nil {
158+ return nil , err
159+ }
160+ if result != nil {
161+ return result , nil
162+ }
163+ } else if analyzer .RegexGroups != "" {
164+ result , err := analyzeRegexGroups (analyzer .RegexGroups , actualYAML , analyzer .Outcomes , a .Title ())
165+ if err != nil {
166+ return nil , err
167+ }
168+ if result != nil {
169+ return result , nil
170+ }
171+ }
172+
173+ return & AnalyzeResult {
129174 Title : a .Title (),
130175 IconKey : "kubernetes_text_analyze" ,
131176 IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg" ,
132- }
177+ IsFail : true ,
178+ Message : "Invalid analyzer" ,
179+ }, nil
180+ }
181+
182+ func analyzeValue (expected interface {}, actual interface {}, outcomes []* troubleshootv1beta2.Outcome , checkName string ) (* AnalyzeResult , error ) {
183+ var err error
133184
134185 equal := reflect .DeepEqual (actual , expected )
135186
136- for _ , outcome := range analyzer .Outcomes {
187+ result := & AnalyzeResult {
188+ Title : checkName ,
189+ IconKey : "kubernetes_text_analyze" ,
190+ IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16" ,
191+ }
192+
193+ for _ , outcome := range outcomes {
137194 if outcome .Fail != nil {
138195 when := false
139196 if outcome .Fail .When != "" {
@@ -183,7 +240,7 @@ func (a *AnalyzeClusterResource) analyzeResource(analyzer *troubleshootv1beta2.C
183240 }
184241
185242 return & AnalyzeResult {
186- Title : a . Title () ,
243+ Title : checkName ,
187244 IconKey : "kubernetes_text_analyze" ,
188245 IconURI : "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg" ,
189246 IsFail : true ,
0 commit comments