Skip to content

Commit

Permalink
refactor: vCenter entity type constants (#360)
Browse files Browse the repository at this point in the history
## Issue
N/A

## Description
Make all vCenter entity types constants.

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson authored Sep 6, 2024
1 parent f9b63d8 commit 3fb6f51
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 171 deletions.
15 changes: 6 additions & 9 deletions api/v1alpha1/vspherevalidator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/validator-labs/validator/pkg/plugins"
"github.com/validator-labs/validator/pkg/validationrule"

"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
"github.com/validator-labs/validator-plugin-vsphere/pkg/constants"
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
)

// VsphereValidatorSpec defines the desired state of VsphereValidator
Expand Down Expand Up @@ -40,8 +40,8 @@ type VsphereAuth struct {
// SecretName is the name of the secret containing the vSphere credentials
SecretName string `json:"secretName,omitempty" yaml:"secretName,omitempty"`

// Account is the vSphere account to use for authentication
Account *vsphere.Account `json:"account,omitempty" yaml:"account,omitempty"`
// Account is the vCenter account to use for authentication
Account *vcenter.Account `json:"account,omitempty" yaml:"account,omitempty"`
}

// NTPValidationRule defines the NTP validation rule
Expand Down Expand Up @@ -81,8 +81,7 @@ type ComputeResourceRule struct {
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName"`

// Scope is the scope of the compute resource validation rule
// +kubebuilder:validation:Enum=cluster;host;resourcepool
Scope string `json:"scope" yaml:"scope"`
Scope vcenter.Entity `json:"scope" yaml:"scope"`

// EntityName is the name of the entity to validate
EntityName string `json:"entityName" yaml:"entityName"`
Expand Down Expand Up @@ -117,8 +116,7 @@ type PrivilegeValidationRule struct {
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName,omitempty"`

// EntityType is the type of the entity to validate
// +kubebuilder:validation:Enum=cluster;datacenter;datastore;folder;host;network;resourcepool;vapp;vcenterroot;vds;vm
EntityType string `json:"entityType" yaml:"entityType"`
EntityType vcenter.Entity `json:"entityType" yaml:"entityType"`

// EntityName is the name of the entity to validate
EntityName string `json:"entityName" yaml:"entityName"`
Expand Down Expand Up @@ -152,8 +150,7 @@ type TagValidationRule struct {
ClusterName string `json:"clusterName,omitempty" yaml:"clusterName"`

// EntityType is the type of the entity to validate
// +kubebuilder:validation:Enum=cluster;datacenter;folder;host;resourcepool;vm
EntityType string `json:"entityType" yaml:"entityType"`
EntityType vcenter.Entity `json:"entityType" yaml:"entityType"`

// EntityName is the name of the entity to validate
EntityName string `json:"entityName" yaml:"entityName"`
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

56 changes: 56 additions & 0 deletions api/vcenter/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Package vcenter contains vCenter object types.
package vcenter

// Account contains vCenter account details.
type Account struct {
// Insecure controls whether to validate the vCenter server's certificate.
Insecure bool `json:"insecure" yaml:"insecure"`

// Password is the vCenter password.
Password string `json:"password" yaml:"password"`

// Username is the vCenter username.
Username string `json:"username" yaml:"username"`

// Host is the vCenter URL.
Host string `json:"host" yaml:"host"`
}

// Entity represents a vCenter entity, referenceable via govmomi.
type Entity int

// nolint:revive
const (
Cluster Entity = iota
Datacenter
Datastore
Folder
Host
Network
ResourcePool
VApp
VCenterRoot
VDS
VM
)

// String converts an Entity to a string.
func (e Entity) String() string {
names := []string{
"cluster",
"datacenter",
"datastore",
"folder",
"host",
"network",
"resourcepool",
"vapp",
"",
"vds",
"vm",
}
if e > VM || e < Cluster {
return "Unknown"
}
return names[e]
}
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ spec:
an VsphereValidator.
properties:
account:
description: Account is the vSphere account to use for authentication
description: Account is the vCenter account to use for authentication
properties:
host:
description: Host is the vCenter URL.
type: string
insecure:
description: Insecure controls whether to validate the vSphere
description: Insecure controls whether to validate the vCenter
server's certificate.
type: boolean
password:
Expand Down Expand Up @@ -122,11 +122,7 @@ spec:
scope:
description: Scope is the scope of the compute resource validation
rule
enum:
- cluster
- host
- resourcepool
type: string
type: integer
required:
- entityName
- name
Expand Down Expand Up @@ -172,19 +168,7 @@ spec:
type: string
entityType:
description: EntityType is the type of the entity to validate
enum:
- cluster
- datacenter
- datastore
- folder
- host
- network
- resourcepool
- vapp
- vcenterroot
- vds
- vm
type: string
type: integer
name:
description: RuleName is the name of the entity privilege validation
rule
Expand Down Expand Up @@ -219,14 +203,7 @@ spec:
type: string
entityType:
description: EntityType is the type of the entity to validate
enum:
- cluster
- datacenter
- folder
- host
- resourcepool
- vm
type: string
type: integer
name:
description: RuleName is the name of the tag validation rule
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ spec:
an VsphereValidator.
properties:
account:
description: Account is the vSphere account to use for authentication
description: Account is the vCenter account to use for authentication
properties:
host:
description: Host is the vCenter URL.
type: string
insecure:
description: Insecure controls whether to validate the vSphere
description: Insecure controls whether to validate the vCenter
server's certificate.
type: boolean
password:
Expand Down Expand Up @@ -122,11 +122,7 @@ spec:
scope:
description: Scope is the scope of the compute resource validation
rule
enum:
- cluster
- host
- resourcepool
type: string
type: integer
required:
- entityName
- name
Expand Down Expand Up @@ -172,19 +168,7 @@ spec:
type: string
entityType:
description: EntityType is the type of the entity to validate
enum:
- cluster
- datacenter
- datastore
- folder
- host
- network
- resourcepool
- vapp
- vcenterroot
- vds
- vm
type: string
type: integer
name:
description: RuleName is the name of the entity privilege validation
rule
Expand Down Expand Up @@ -219,14 +203,7 @@ spec:
type: string
entityType:
description: EntityType is the type of the entity to validate
enum:
- cluster
- datacenter
- folder
- host
- resourcepool
- vm
type: string
type: integer
name:
description: RuleName is the name of the tag validation rule
type: string
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/vspherevalidator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
"github.com/validator-labs/validator-plugin-vsphere/pkg/validate"
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
vapi "github.com/validator-labs/validator/api/v1alpha1"
vres "github.com/validator-labs/validator/pkg/validationresult"
)
Expand Down Expand Up @@ -149,7 +149,7 @@ func (r *VsphereValidatorReconciler) secretKeyAuth(req ctrl.Request, validator *
return fmt.Errorf("failed to convert insecureSkipVerify to bool: %w", err)
}

validator.Spec.Auth.Account = &vsphere.Account{
validator.Spec.Auth.Account = &vcenter.Account{
Insecure: skipVerify,
Username: string(username),
Password: string(password),
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/vspherevalidator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/types"

"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
"github.com/validator-labs/validator-plugin-vsphere/pkg/vcsim"
vapi "github.com/validator-labs/validator/api/v1alpha1"
vres "github.com/validator-labs/validator/pkg/validationresult"
Expand Down Expand Up @@ -48,7 +49,7 @@ var _ = Describe("VsphereValidator controller", Ordered, func() {
TagValidationRules: []v1alpha1.TagValidationRule{
{
RuleName: "Datacenter k8s-region rule",
EntityType: "datacenter",
EntityType: vcenter.Datacenter,
EntityName: "Datacenter",
Tag: "k8s-region",
},
Expand Down
7 changes: 4 additions & 3 deletions pkg/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
"github.com/validator-labs/validator-plugin-vsphere/pkg/vcsim"
)

Expand All @@ -33,7 +34,7 @@ func TestValidate(t *testing.T) {
Datacenter: "DC0",
PrivilegeValidationRules: testRules([]privilegeRuleInput{
{
EntityType: "cluster",
EntityType: vcenter.Cluster,
EntityName: "DC0_C0",
Privileges: []string{"Alarm.Acknowledge"},
},
Expand All @@ -50,7 +51,7 @@ func TestValidate(t *testing.T) {
Datacenter: "DC0",
PrivilegeValidationRules: testRules([]privilegeRuleInput{
{
EntityType: "cluster",
EntityType: vcenter.Cluster,
EntityName: "DC0_C0",
Privileges: []string{"Nonexistent"},
},
Expand All @@ -76,7 +77,7 @@ func TestValidate(t *testing.T) {
}

type privilegeRuleInput struct {
EntityType string
EntityType vcenter.Entity
EntityName string
Privileges []string
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/validators/computeresources/computeresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"

"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
"github.com/validator-labs/validator-plugin-vsphere/pkg/constants"
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
vapi "github.com/validator-labs/validator/api/v1alpha1"
Expand Down Expand Up @@ -125,11 +126,11 @@ func (c *ValidationService) ReconcileComputeResourceValidationRule(rule v1alpha1

var res *Usage
switch rule.Scope {
case "cluster":
case vcenter.Cluster:
res, err = clusterUsage(ctx, rule, finder)
case "resourcepool":
case vcenter.ResourcePool:
res, err = resourcePoolUsage(ctx, rule, finder, driver)
case "host":
case vcenter.Host:
res, err = hostUsage(ctx, rule, finder)
default:
err = fmt.Errorf("unsupported scope: %s", rule.Scope)
Expand Down Expand Up @@ -366,11 +367,11 @@ func getTotalQuantity(quantity string, numberOfNodes int) resource.Quantity {
// GetScopeKey returns a formatted key depending on the scope of a rule
func GetScopeKey(rule v1alpha1.ComputeResourceRule) (string, error) {
switch rule.Scope {
case "cluster":
case vcenter.Cluster:
return fmt.Sprintf("%s-%s", rule.Scope, rule.EntityName), nil
case "host":
case vcenter.Host:
return fmt.Sprintf("%s-%s", rule.Scope, rule.EntityName), nil
case "resourcepool":
case vcenter.ResourcePool:
return fmt.Sprintf("%s-%s", rule.Scope, rule.ClusterName), nil
default:
return "", fmt.Errorf("unsupported scope: %s", rule.Scope)
Expand Down
Loading

0 comments on commit 3fb6f51

Please sign in to comment.