mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-08-27 00:37:20 +00:00
* Add a host collector / analyzer to check if a subnet contains an IP address
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package analyzer
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/pkg/errors"
|
|
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
|
|
)
|
|
|
|
type AnalyzeHostSubnetContainsIP struct {
|
|
hostAnalyzer *troubleshootv1beta2.SubnetContainsIPAnalyze
|
|
}
|
|
|
|
func (a *AnalyzeHostSubnetContainsIP) Title() string {
|
|
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Subnet Contains IP")
|
|
}
|
|
|
|
func (a *AnalyzeHostSubnetContainsIP) IsExcluded() (bool, error) {
|
|
return isExcluded(a.hostAnalyzer.Exclude)
|
|
}
|
|
|
|
func (a *AnalyzeHostSubnetContainsIP) Analyze(
|
|
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
|
|
) ([]*AnalyzeResult, error) {
|
|
_, ipNet, err := net.ParseCIDR(a.hostAnalyzer.CIDR)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to parse CIDR %s", a.hostAnalyzer.CIDR)
|
|
}
|
|
|
|
ip := net.ParseIP(a.hostAnalyzer.IP)
|
|
if ip == nil {
|
|
return nil, errors.Errorf("failed to parse IP address %s", a.hostAnalyzer.IP)
|
|
}
|
|
|
|
contains := fmt.Sprintf("%t", ipNet.Contains(ip))
|
|
|
|
results, err := analyzeHostCollectorResults([]collectedContent{{Data: []byte(contains)}}, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title())
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to analyze Subnet Contains IP")
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func (a *AnalyzeHostSubnetContainsIP) CheckCondition(when string, data []byte) (bool, error) {
|
|
switch when {
|
|
case "true":
|
|
return string(data) == "true", nil
|
|
case "false":
|
|
return string(data) == "false", nil
|
|
}
|
|
|
|
return false, errors.Errorf("unknown condition: %q", when)
|
|
}
|