Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make remaining rules implement rule interface #131

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

// 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

Check warning on line 141 in api/v1alpha1/maasvalidator_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/maasvalidator_types.go#L140-L141

Added lines #L140 - L141 were not covered by tests
}

// 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

Check warning on line 165 in api/v1alpha1/maasvalidator_types.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/maasvalidator_types.go#L164-L165

Added lines #L164 - L165 were not covered by tests
}

// 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