Skip to content

Commit d5a6b19

Browse files
authored
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
1 parent 716dda2 commit d5a6b19

13 files changed

+626
-0
lines changed

config/crds/troubleshoot.sh_analyzers.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,6 +2746,61 @@ spec:
27462746
required:
27472747
- outcomes
27482748
type: object
2749+
subnetContainsIP:
2750+
properties:
2751+
annotations:
2752+
additionalProperties:
2753+
type: string
2754+
type: object
2755+
checkName:
2756+
type: string
2757+
cidr:
2758+
type: string
2759+
collectorName:
2760+
type: string
2761+
exclude:
2762+
type: BoolString
2763+
ip:
2764+
type: string
2765+
outcomes:
2766+
items:
2767+
properties:
2768+
fail:
2769+
properties:
2770+
message:
2771+
type: string
2772+
uri:
2773+
type: string
2774+
when:
2775+
type: string
2776+
type: object
2777+
pass:
2778+
properties:
2779+
message:
2780+
type: string
2781+
uri:
2782+
type: string
2783+
when:
2784+
type: string
2785+
type: object
2786+
warn:
2787+
properties:
2788+
message:
2789+
type: string
2790+
uri:
2791+
type: string
2792+
when:
2793+
type: string
2794+
type: object
2795+
type: object
2796+
type: array
2797+
strict:
2798+
type: BoolString
2799+
required:
2800+
- cidr
2801+
- ip
2802+
- outcomes
2803+
type: object
27492804
sysctl:
27502805
properties:
27512806
annotations:

config/crds/troubleshoot.sh_hostcollectors.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,61 @@ spec:
895895
required:
896896
- outcomes
897897
type: object
898+
subnetContainsIP:
899+
properties:
900+
annotations:
901+
additionalProperties:
902+
type: string
903+
type: object
904+
checkName:
905+
type: string
906+
cidr:
907+
type: string
908+
collectorName:
909+
type: string
910+
exclude:
911+
type: BoolString
912+
ip:
913+
type: string
914+
outcomes:
915+
items:
916+
properties:
917+
fail:
918+
properties:
919+
message:
920+
type: string
921+
uri:
922+
type: string
923+
when:
924+
type: string
925+
type: object
926+
pass:
927+
properties:
928+
message:
929+
type: string
930+
uri:
931+
type: string
932+
when:
933+
type: string
934+
type: object
935+
warn:
936+
properties:
937+
message:
938+
type: string
939+
uri:
940+
type: string
941+
when:
942+
type: string
943+
type: object
944+
type: object
945+
type: array
946+
strict:
947+
type: BoolString
948+
required:
949+
- cidr
950+
- ip
951+
- outcomes
952+
type: object
898953
sysctl:
899954
properties:
900955
annotations:

config/crds/troubleshoot.sh_hostpreflights.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,61 @@ spec:
895895
required:
896896
- outcomes
897897
type: object
898+
subnetContainsIP:
899+
properties:
900+
annotations:
901+
additionalProperties:
902+
type: string
903+
type: object
904+
checkName:
905+
type: string
906+
cidr:
907+
type: string
908+
collectorName:
909+
type: string
910+
exclude:
911+
type: BoolString
912+
ip:
913+
type: string
914+
outcomes:
915+
items:
916+
properties:
917+
fail:
918+
properties:
919+
message:
920+
type: string
921+
uri:
922+
type: string
923+
when:
924+
type: string
925+
type: object
926+
pass:
927+
properties:
928+
message:
929+
type: string
930+
uri:
931+
type: string
932+
when:
933+
type: string
934+
type: object
935+
warn:
936+
properties:
937+
message:
938+
type: string
939+
uri:
940+
type: string
941+
when:
942+
type: string
943+
type: object
944+
type: object
945+
type: array
946+
strict:
947+
type: BoolString
948+
required:
949+
- cidr
950+
- ip
951+
- outcomes
952+
type: object
898953
sysctl:
899954
properties:
900955
annotations:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19837,6 +19837,61 @@ spec:
1983719837
required:
1983819838
- outcomes
1983919839
type: object
19840+
subnetContainsIP:
19841+
properties:
19842+
annotations:
19843+
additionalProperties:
19844+
type: string
19845+
type: object
19846+
checkName:
19847+
type: string
19848+
cidr:
19849+
type: string
19850+
collectorName:
19851+
type: string
19852+
exclude:
19853+
type: BoolString
19854+
ip:
19855+
type: string
19856+
outcomes:
19857+
items:
19858+
properties:
19859+
fail:
19860+
properties:
19861+
message:
19862+
type: string
19863+
uri:
19864+
type: string
19865+
when:
19866+
type: string
19867+
type: object
19868+
pass:
19869+
properties:
19870+
message:
19871+
type: string
19872+
uri:
19873+
type: string
19874+
when:
19875+
type: string
19876+
type: object
19877+
warn:
19878+
properties:
19879+
message:
19880+
type: string
19881+
uri:
19882+
type: string
19883+
when:
19884+
type: string
19885+
type: object
19886+
type: object
19887+
type: array
19888+
strict:
19889+
type: BoolString
19890+
required:
19891+
- cidr
19892+
- ip
19893+
- outcomes
19894+
type: object
1984019895
sysctl:
1984119896
properties:
1984219897
annotations:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: troubleshoot.sh/v1beta2
2+
kind: HostPreflight
3+
metadata:
4+
name: subnet-contains-ip
5+
spec:
6+
analyzers:
7+
- subnetContainsIP:
8+
cidr: "10.0.0.0/8"
9+
ip: "10.0.0.5"
10+
outcomes:
11+
- fail:
12+
when: "false"
13+
message: The IP address is not within the subnet range
14+
- pass:
15+
when: "true"
16+
message: The IP address is within the subnet range

pkg/analyze/host_analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
4545
return &AnalyzeHostIPV4Interfaces{analyzer.IPV4Interfaces}, true
4646
case analyzer.SubnetAvailable != nil:
4747
return &AnalyzeHostSubnetAvailable{analyzer.SubnetAvailable}, true
48+
case analyzer.SubnetContainsIP != nil:
49+
return &AnalyzeHostSubnetContainsIP{analyzer.SubnetContainsIP}, true
4850
case analyzer.FilesystemPerformance != nil:
4951
return &AnalyzeHostFilesystemPerformance{analyzer.FilesystemPerformance}, true
5052
case analyzer.Certificate != nil:

pkg/analyze/host_subnetcontainsip.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package analyzer
2+
3+
import (
4+
"fmt"
5+
"net"
6+
7+
"github.com/pkg/errors"
8+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
9+
)
10+
11+
type AnalyzeHostSubnetContainsIP struct {
12+
hostAnalyzer *troubleshootv1beta2.SubnetContainsIPAnalyze
13+
}
14+
15+
func (a *AnalyzeHostSubnetContainsIP) Title() string {
16+
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Subnet Contains IP")
17+
}
18+
19+
func (a *AnalyzeHostSubnetContainsIP) IsExcluded() (bool, error) {
20+
return isExcluded(a.hostAnalyzer.Exclude)
21+
}
22+
23+
func (a *AnalyzeHostSubnetContainsIP) Analyze(
24+
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
25+
) ([]*AnalyzeResult, error) {
26+
_, ipNet, err := net.ParseCIDR(a.hostAnalyzer.CIDR)
27+
if err != nil {
28+
return nil, errors.Wrapf(err, "failed to parse CIDR %s", a.hostAnalyzer.CIDR)
29+
}
30+
31+
ip := net.ParseIP(a.hostAnalyzer.IP)
32+
if ip == nil {
33+
return nil, errors.Errorf("failed to parse IP address %s", a.hostAnalyzer.IP)
34+
}
35+
36+
contains := fmt.Sprintf("%t", ipNet.Contains(ip))
37+
38+
results, err := analyzeHostCollectorResults([]collectedContent{{Data: []byte(contains)}}, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title())
39+
if err != nil {
40+
return nil, errors.Wrap(err, "failed to analyze Subnet Contains IP")
41+
}
42+
43+
return results, nil
44+
}
45+
46+
func (a *AnalyzeHostSubnetContainsIP) CheckCondition(when string, data []byte) (bool, error) {
47+
switch when {
48+
case "true":
49+
return string(data) == "true", nil
50+
case "false":
51+
return string(data) == "false", nil
52+
}
53+
54+
return false, errors.Errorf("unknown condition: %q", when)
55+
}

0 commit comments

Comments
 (0)