Skip to content

Commit

Permalink
Merge pull request #269 from manavellamnimble/distributionEnhacement
Browse files Browse the repository at this point in the history
Distribution enhacement
  • Loading branch information
divolgin authored Sep 23, 2020
2 parents 58261cd + 0ea4e1d commit 8a30f2d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
38 changes: 34 additions & 4 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 All @@ -19,6 +20,8 @@ type providers struct {
openShift bool
kurl bool
aks bool
ibm bool
minikube bool
}

type Provider int
Expand All @@ -33,6 +36,8 @@ const (
openShift Provider = iota
kurl Provider = iota
aks Provider = iota
ibm Provider = iota
minikube Provider = iota
)

func CheckOpenShift(foundProviders *providers, apiResources []*metav1.APIResourceList, provider string) string {
Expand Down Expand Up @@ -68,6 +73,10 @@ func ParseNodesForProviders(nodes []corev1.Node) (providers, string) {
foundProviders.aks = true
stringProvider = "aks"
}
if k == "minikube.k8s.io/version" {
foundProviders.minikube = true
stringProvider = "minikube"
}
}

if node.Status.NodeInfo.OSImage == "Docker Desktop" {
Expand All @@ -87,6 +96,10 @@ func ParseNodesForProviders(nodes []corev1.Node) (providers, string) {
foundProviders.gke = true
stringProvider = "gke"
}
if strings.HasPrefix(node.Spec.ProviderID, "ibm:") {
foundProviders.ibm = true
stringProvider = "ibm"
}
}

if foundMaster {
Expand All @@ -101,6 +114,7 @@ func ParseNodesForProviders(nodes []corev1.Node) (providers, string) {
}

func analyzeDistribution(analyzer *troubleshootv1beta2.Distribution, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
var unknownDistribution string
collected, err := getCollectedFileContents("cluster-resources/nodes.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get contents of nodes.json")
Expand Down Expand Up @@ -144,7 +158,7 @@ func analyzeDistribution(analyzer *troubleshootv1beta2.Distribution, getCollecte
return result, nil
}

isMatch, err := compareDistributionConditionalToActual(outcome.Fail.When, foundProviders)
isMatch, err := compareDistributionConditionalToActual(outcome.Fail.When, foundProviders, &unknownDistribution)
if err != nil {
return result, errors.Wrap(err, "failed to compare distribution conditional")
}
Expand All @@ -165,7 +179,7 @@ func analyzeDistribution(analyzer *troubleshootv1beta2.Distribution, getCollecte
return result, nil
}

isMatch, err := compareDistributionConditionalToActual(outcome.Warn.When, foundProviders)
isMatch, err := compareDistributionConditionalToActual(outcome.Warn.When, foundProviders, &unknownDistribution)
if err != nil {
return result, errors.Wrap(err, "failed to compare distribution conditional")
}
Expand All @@ -186,7 +200,7 @@ func analyzeDistribution(analyzer *troubleshootv1beta2.Distribution, getCollecte
return result, nil
}

isMatch, err := compareDistributionConditionalToActual(outcome.Pass.When, foundProviders)
isMatch, err := compareDistributionConditionalToActual(outcome.Pass.When, foundProviders, &unknownDistribution)
if err != nil {
return result, errors.Wrap(err, "failed to compare distribution conditional")
}
Expand All @@ -202,10 +216,17 @@ func analyzeDistribution(analyzer *troubleshootv1beta2.Distribution, getCollecte
}
}

result.IsWarn = true
if unknownDistribution != "" {
result.Message = unknownDistribution
} else {
result.Message = "None of the conditionals were met"
}

return result, nil
}

func compareDistributionConditionalToActual(conditional string, actual providers) (bool, error) {
func compareDistributionConditionalToActual(conditional string, actual providers, unknownDistribution *string) (bool, error) {
parts := strings.Split(strings.TrimSpace(conditional), " ")

// we can make this a lot more flexible
Expand All @@ -223,6 +244,7 @@ func compareDistributionConditionalToActual(conditional string, actual providers
normalizedName := mustNormalizeDistributionName(parts[1])

if normalizedName == unknown {
*unknownDistribution += fmt.Sprintf("- Unknown distribution: %s ", parts[1])
return false, nil
}

Expand All @@ -244,6 +266,10 @@ func compareDistributionConditionalToActual(conditional string, actual providers
isMatch = actual.kurl
case aks:
isMatch = actual.aks
case ibm:
isMatch = actual.ibm
case minikube:
isMatch = actual.minikube
}

switch parts[0] {
Expand Down Expand Up @@ -274,6 +300,10 @@ func mustNormalizeDistributionName(raw string) Provider {
return kurl
case "aks":
return aks
case "ibm", "ibmcloud", "ibm cloud":
return ibm
case "minikube":
return minikube
}

return unknown
Expand Down
3 changes: 2 additions & 1 deletion pkg/analyze/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func Test_compareDistributionConditionalToActual(t *testing.T) {
var unknownDistribution string
tests := []struct {
name string
conditional string
Expand Down Expand Up @@ -46,7 +47,7 @@ func Test_compareDistributionConditionalToActual(t *testing.T) {
defer scopetest.End()
req := require.New(t)

actual, err := compareDistributionConditionalToActual(test.conditional, test.input)
actual, err := compareDistributionConditionalToActual(test.conditional, test.input, &unknownDistribution)
req.NoError(err)

assert.Equal(t, test.expected, actual)
Expand Down

0 comments on commit 8a30f2d

Please sign in to comment.