Files
troubleshoot/pkg/analyze/host_subnetcontainsip.go
Salah Al Saleh d5a6b19417 Add a host analyzer to check if a subnet contains an IP address (#1735)
* Add a host collector / analyzer to check if a subnet contains an IP address
2025-02-13 13:16:59 -08:00

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)
}