Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/troubleshoot/sample-analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ spec:
- distribution:
outcomes:
- fail:
when: "= docker desktop"
when: "= dockerdesktop"
message: "docker for desktop is not allowed"
# - fail:
# when: "microk8s"
# message: "mickrk8s is not prod"
- fail:
when: "microk8s"
message: "mickrk8s is not prod"
when: "!= openshift"
message: "this should fail on anything other than openshift"
- warn:
when: "!= eks"
message: "YMMV on not eks"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/aws/aws-sdk-go v1.25.18 // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/chzyer/logex v1.1.11-0.20160617073814-96a4d311aa9b // indirect
github.com/coreos/etcd v3.3.15+incompatible // indirect
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 // indirect
Expand Down
61 changes: 44 additions & 17 deletions pkg/analyze/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"encoding/json"
"fmt"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -35,55 +36,84 @@ const (
aks Provider = iota
)

func analyzeDistribution(analyzer *troubleshootv1beta1.Distribution, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
collected, err := getCollectedFileContents("cluster-resources/nodes.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get contents of nodes.json")
func CheckOpenShift(foundProviders *providers, apiResources []*metav1.APIResourceList, provider string) string {
for _, resource := range apiResources {
if strings.Contains(resource.GroupVersion, "openshift") {
foundProviders.openShift = true
return "openShift"
}
}

var nodes []corev1.Node
if err := json.Unmarshal(collected, &nodes); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal node list")
}
return provider
}

func ParseNodesForProviders(nodes []corev1.Node) (providers, string) {
foundProviders := providers{}
foundMaster := false
stringProvider := ""

for _, node := range nodes {
for k, v := range node.ObjectMeta.Labels {
if k == "microk8s.io/cluster" && v == "true" {
foundProviders.microk8s = true
} else if k == "kurl.sh/cluster" && v == "true" {

if k == "kurl.sh/cluster" && v == "true" {
foundProviders.kurl = true
stringProvider = "kurl"
} else if k == "microk8s.io/cluster" && v == "true" {
foundProviders.microk8s = true
stringProvider = "microk8s"
}
if k == "node-role.kubernetes.io/master" {
foundMaster = true
}
if k == "kubernetes.azure.com/role" {
foundProviders.aks = true
stringProvider = "aks"
}
}

if node.Status.NodeInfo.OSImage == "Docker Desktop" {
foundProviders.dockerDesktop = true
stringProvider = "dockerDesktop"
}

if strings.HasPrefix(node.Spec.ProviderID, "digitalocean:") {
foundProviders.digitalOcean = true
stringProvider = "digitalOcean"
}
if strings.HasPrefix(node.Spec.ProviderID, "aws:") {
foundProviders.eks = true
stringProvider = "eks"
}
if strings.HasPrefix(node.Spec.ProviderID, "gce:") {
foundProviders.gke = true
stringProvider = "gke"
}
}

if foundMaster {
// eks does not have masters within the node list
foundProviders.eks = false
if stringProvider == "eks" {
stringProvider = ""
}
}

return foundProviders, stringProvider
}

func analyzeDistribution(analyzer *troubleshootv1beta1.Distribution, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
collected, err := getCollectedFileContents("cluster-resources/nodes.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get contents of nodes.json")
}

var nodes []corev1.Node
if err := json.Unmarshal(collected, &nodes); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal node list")
}

foundProviders, _ := ParseNodesForProviders(nodes)

apiResourcesBytes, err := getCollectedFileContents("cluster-resources/resources.json")
// if the file is not found, that is not a fatal error
// troubleshoot 0.9.15 and earlier did not collect that file
Expand All @@ -92,11 +122,7 @@ func analyzeDistribution(analyzer *troubleshootv1beta1.Distribution, getCollecte
if err := json.Unmarshal(apiResourcesBytes, &apiResources); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal api resource list")
}
for _, resource := range apiResources {
if strings.Contains(resource.GroupVersion, "openshift") {
foundProviders.openShift = true
}
}
_ = CheckOpenShift(&foundProviders, apiResources, "")
}

result := &AnalyzeResult{
Expand Down Expand Up @@ -189,7 +215,8 @@ func compareDistributionConditionalToActual(conditional string, actual providers
}

if len(parts) != 2 {
return false, errors.New("unable to parse conditional")
return false, fmt.Errorf("unable to parse conditional %v\n", parts)
// return false, errors.New("unable to parse conditional")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove

}

normalizedName := mustNormalizeDistributionName(parts[1])
Expand Down