Skip to content

Commit

Permalink
refactor: make remaining rules implement rule interface (#131)
Browse files Browse the repository at this point in the history
## Description
Forgot two of the four rules when doing
#130. This
does the rest.

Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
  • Loading branch information
mattwelke authored and ahmad-ibra committed Aug 23, 2024
1 parent 381e3ab commit ed0bc5b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
32 changes: 30 additions & 2 deletions api/v1alpha1/maasvalidator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,50 @@ type DNSRecord struct {

// UpstreamDNSRule provides rules for validating the external DNS server
type UpstreamDNSRule struct {
validationrule.ManuallyNamed `json:"-"`

// Unique name for the rule
Name string `json:"name" yaml:"name"`
RuleName string `json:"name" yaml:"name"`
// The minimum expected number of upstream DNS servers
NumDNSServers int `json:"numDNSServers" yaml:"numDNSServers"`
}

var _ validationrule.Interface = (*UpstreamDNSRule)(nil)

// Name returns the name of the UpstreamDNSRule.
func (r UpstreamDNSRule) Name() string {
return r.RuleName
}

// SetName sets the name of the UpstreamDNSRule.
func (r *UpstreamDNSRule) SetName(name string) {
r.RuleName = name
}

// ResourceAvailabilityRule provides rules for validating resource availability
type ResourceAvailabilityRule struct {
validationrule.ManuallyNamed `json:"-"`

// Unique name for the rule
Name string `json:"name" yaml:"name"`
RuleName string `json:"name" yaml:"name"`
// The availability zone to validate
AZ string `json:"az" yaml:"az"`
// The list of resources to validate
Resources []Resource `json:"resources" yaml:"resources"`
}

var _ validationrule.Interface = (*ResourceAvailabilityRule)(nil)

// Name returns the name of the RBAC rule.
func (r ResourceAvailabilityRule) Name() string {
return r.RuleName
}

// SetName sets the name of the RBAC rule.
func (r *ResourceAvailabilityRule) SetName(name string) {
r.RuleName = name
}

// Resource defines a compute resource
type Resource struct {
// Minimum desired number of machines
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/controller/maasvalidator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ var _ = Describe("MaaSValidator controller", Ordered, func() {
}},
},
UpstreamDNSRules: []v1alpha1.UpstreamDNSRule{
{Name: "Upstream DNS", NumDNSServers: 1},
{RuleName: "Upstream DNS", NumDNSServers: 1},
},
ResourceAvailabilityRules: []v1alpha1.ResourceAvailabilityRule{
{Name: "az1 2 machines", AZ: "az1", Resources: []v1alpha1.Resource{
{RuleName: "az1 2 machines", AZ: "az1", Resources: []v1alpha1.Resource{
{NumMachines: 2, NumCPU: 2, Disk: 20, RAM: 4},
}},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/validators/dns/upstream_dns_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewUpstreamDNSRulesService(log logr.Logger, api api.MAASServer) *UpstreamDN
// ReconcileMaasInstanceUpstreamDNSRule reconciles a MAAS instance upstream DNS rule
func (s *UpstreamDNSRulesService) ReconcileMaasInstanceUpstreamDNSRule(rule v1alpha1.UpstreamDNSRule) (*types.ValidationRuleResult, error) {

vr := utils.BuildValidationResult(rule.Name, constants.ValidationTypeUDNS)
vr := utils.BuildValidationResult(rule.Name(), constants.ValidationTypeUDNS)

details, errs := s.findDNSServers(rule.NumDNSServers)

Expand Down
6 changes: 3 additions & 3 deletions pkg/validators/dns/upstream_dns_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestReconcileMaasInstanceUpstreamDNSRule(t *testing.T) {
},
),
upstreamDNSRules: []v1alpha1.UpstreamDNSRule{
{Name: "Upstream DNS rule 1", NumDNSServers: 1},
{RuleName: "Upstream DNS rule 1", NumDNSServers: 1},
},
errors: nil,
details: []string{"Found 1 DNS server(s)"},
Expand All @@ -50,7 +50,7 @@ func TestReconcileMaasInstanceUpstreamDNSRule(t *testing.T) {
upstreamDNS: "8.8.8.8",
}),
upstreamDNSRules: []v1alpha1.UpstreamDNSRule{
{Name: "Upstream DNS rule 2", NumDNSServers: 2},
{RuleName: "Upstream DNS rule 2", NumDNSServers: 2},
},
errors: []string{"expected 2 DNS server(s), got 1"},
details: nil,
Expand All @@ -63,7 +63,7 @@ func TestReconcileMaasInstanceUpstreamDNSRule(t *testing.T) {
upstreamDNS: "",
}),
upstreamDNSRules: []v1alpha1.UpstreamDNSRule{
{Name: "Upstream DNS rule 3", NumDNSServers: 1},
{RuleName: "Upstream DNS rule 3", NumDNSServers: 1},
},
errors: []string{"expected 1 DNS server(s), got 0"},
details: nil,
Expand Down
2 changes: 1 addition & 1 deletion pkg/validators/res/resource_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *ResourceRulesService) ReconcileMaasInstanceResourceRule(rule v1alpha1.R
errs := make([]error, 0)
details := make([]string, 0)

vr := utils.BuildValidationResult(rule.Name, constants.ValidationTypeResource)
vr := utils.BuildValidationResult(rule.Name(), constants.ValidationTypeResource)

// do not process an AZ more than once
if seen[rule.AZ] {
Expand Down
6 changes: 3 additions & 3 deletions pkg/validators/res/resource_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestReconcileMaasInstanceImageRule(t *testing.T) {
},
),
resources: []v1alpha1.ResourceAvailabilityRule{
{Name: "AZ1 rule 1", AZ: "az1", Resources: []v1alpha1.Resource{
{RuleName: "AZ1 rule 1", AZ: "az1", Resources: []v1alpha1.Resource{
{NumMachines: 3, NumCPU: 16, RAM: 16, Disk: 100, Pool: "pool1", Tags: []string{"tag1", "tag2"}},
},
},
Expand All @@ -63,7 +63,7 @@ func TestReconcileMaasInstanceImageRule(t *testing.T) {
},
}),
resources: []v1alpha1.ResourceAvailabilityRule{
{Name: "AZ1 rule 2", AZ: "az1", Resources: []v1alpha1.Resource{
{RuleName: "AZ1 rule 2", AZ: "az1", Resources: []v1alpha1.Resource{
{NumMachines: 2, NumCPU: 16, RAM: 16, Disk: 100, Pool: "pool1", Tags: []string{"tag1", "tag2"}},
}},
},
Expand All @@ -78,7 +78,7 @@ func TestReconcileMaasInstanceImageRule(t *testing.T) {
MachinesList: []entity.Machine{},
}),
resources: []v1alpha1.ResourceAvailabilityRule{
{Name: "AZ1 rule 2", AZ: "az1", Resources: []v1alpha1.Resource{
{RuleName: "AZ1 rule 2", AZ: "az1", Resources: []v1alpha1.Resource{
{NumMachines: 1, NumCPU: 16, RAM: 16, Disk: 100, Pool: "pool1", Tags: []string{"tag1", "tag2"}},
}},
},
Expand Down

0 comments on commit ed0bc5b

Please sign in to comment.