Skip to content

Commit 3fb6f51

Browse files
authored
refactor: vCenter entity type constants (#360)
## Issue N/A ## Description Make all vCenter entity types constants. Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
1 parent f9b63d8 commit 3fb6f51

18 files changed

Lines changed: 178 additions & 171 deletions

File tree

api/v1alpha1/vspherevalidator_types.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/validator-labs/validator/pkg/plugins"
99
"github.com/validator-labs/validator/pkg/validationrule"
1010

11+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1112
"github.com/validator-labs/validator-plugin-vsphere/pkg/constants"
12-
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
1313
)
1414

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

43-
// Account is the vSphere account to use for authentication
44-
Account *vsphere.Account `json:"account,omitempty" yaml:"account,omitempty"`
43+
// Account is the vCenter account to use for authentication
44+
Account *vcenter.Account `json:"account,omitempty" yaml:"account,omitempty"`
4545
}
4646

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

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

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

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

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

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

158155
// EntityName is the name of the entity to validate
159156
EntityName string `json:"entityName" yaml:"entityName"`

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/vcenter/types.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Package vcenter contains vCenter object types.
2+
package vcenter
3+
4+
// Account contains vCenter account details.
5+
type Account struct {
6+
// Insecure controls whether to validate the vCenter server's certificate.
7+
Insecure bool `json:"insecure" yaml:"insecure"`
8+
9+
// Password is the vCenter password.
10+
Password string `json:"password" yaml:"password"`
11+
12+
// Username is the vCenter username.
13+
Username string `json:"username" yaml:"username"`
14+
15+
// Host is the vCenter URL.
16+
Host string `json:"host" yaml:"host"`
17+
}
18+
19+
// Entity represents a vCenter entity, referenceable via govmomi.
20+
type Entity int
21+
22+
// nolint:revive
23+
const (
24+
Cluster Entity = iota
25+
Datacenter
26+
Datastore
27+
Folder
28+
Host
29+
Network
30+
ResourcePool
31+
VApp
32+
VCenterRoot
33+
VDS
34+
VM
35+
)
36+
37+
// String converts an Entity to a string.
38+
func (e Entity) String() string {
39+
names := []string{
40+
"cluster",
41+
"datacenter",
42+
"datastore",
43+
"folder",
44+
"host",
45+
"network",
46+
"resourcepool",
47+
"vapp",
48+
"",
49+
"vds",
50+
"vm",
51+
}
52+
if e > VM || e < Cluster {
53+
return "Unknown"
54+
}
55+
return names[e]
56+
}

build

chart/validator-plugin-vsphere/crds/validation.spectrocloud.labs_vspherevalidators.yaml

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ spec:
4444
an VsphereValidator.
4545
properties:
4646
account:
47-
description: Account is the vSphere account to use for authentication
47+
description: Account is the vCenter account to use for authentication
4848
properties:
4949
host:
5050
description: Host is the vCenter URL.
5151
type: string
5252
insecure:
53-
description: Insecure controls whether to validate the vSphere
53+
description: Insecure controls whether to validate the vCenter
5454
server's certificate.
5555
type: boolean
5656
password:
@@ -122,11 +122,7 @@ spec:
122122
scope:
123123
description: Scope is the scope of the compute resource validation
124124
rule
125-
enum:
126-
- cluster
127-
- host
128-
- resourcepool
129-
type: string
125+
type: integer
130126
required:
131127
- entityName
132128
- name
@@ -172,19 +168,7 @@ spec:
172168
type: string
173169
entityType:
174170
description: EntityType is the type of the entity to validate
175-
enum:
176-
- cluster
177-
- datacenter
178-
- datastore
179-
- folder
180-
- host
181-
- network
182-
- resourcepool
183-
- vapp
184-
- vcenterroot
185-
- vds
186-
- vm
187-
type: string
171+
type: integer
188172
name:
189173
description: RuleName is the name of the entity privilege validation
190174
rule
@@ -219,14 +203,7 @@ spec:
219203
type: string
220204
entityType:
221205
description: EntityType is the type of the entity to validate
222-
enum:
223-
- cluster
224-
- datacenter
225-
- folder
226-
- host
227-
- resourcepool
228-
- vm
229-
type: string
206+
type: integer
230207
name:
231208
description: RuleName is the name of the tag validation rule
232209
type: string

config/crd/bases/validation.spectrocloud.labs_vspherevalidators.yaml

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ spec:
4444
an VsphereValidator.
4545
properties:
4646
account:
47-
description: Account is the vSphere account to use for authentication
47+
description: Account is the vCenter account to use for authentication
4848
properties:
4949
host:
5050
description: Host is the vCenter URL.
5151
type: string
5252
insecure:
53-
description: Insecure controls whether to validate the vSphere
53+
description: Insecure controls whether to validate the vCenter
5454
server's certificate.
5555
type: boolean
5656
password:
@@ -122,11 +122,7 @@ spec:
122122
scope:
123123
description: Scope is the scope of the compute resource validation
124124
rule
125-
enum:
126-
- cluster
127-
- host
128-
- resourcepool
129-
type: string
125+
type: integer
130126
required:
131127
- entityName
132128
- name
@@ -172,19 +168,7 @@ spec:
172168
type: string
173169
entityType:
174170
description: EntityType is the type of the entity to validate
175-
enum:
176-
- cluster
177-
- datacenter
178-
- datastore
179-
- folder
180-
- host
181-
- network
182-
- resourcepool
183-
- vapp
184-
- vcenterroot
185-
- vds
186-
- vm
187-
type: string
171+
type: integer
188172
name:
189173
description: RuleName is the name of the entity privilege validation
190174
rule
@@ -219,14 +203,7 @@ spec:
219203
type: string
220204
entityType:
221205
description: EntityType is the type of the entity to validate
222-
enum:
223-
- cluster
224-
- datacenter
225-
- folder
226-
- host
227-
- resourcepool
228-
- vm
229-
type: string
206+
type: integer
230207
name:
231208
description: RuleName is the name of the tag validation rule
232209
type: string

internal/controller/vspherevalidator_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/client"
3434

3535
"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
36+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
3637
"github.com/validator-labs/validator-plugin-vsphere/pkg/validate"
37-
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
3838
vapi "github.com/validator-labs/validator/api/v1alpha1"
3939
vres "github.com/validator-labs/validator/pkg/validationresult"
4040
)
@@ -149,7 +149,7 @@ func (r *VsphereValidatorReconciler) secretKeyAuth(req ctrl.Request, validator *
149149
return fmt.Errorf("failed to convert insecureSkipVerify to bool: %w", err)
150150
}
151151

152-
validator.Spec.Auth.Account = &vsphere.Account{
152+
validator.Spec.Auth.Account = &vcenter.Account{
153153
Insecure: skipVerify,
154154
Username: string(username),
155155
Password: string(password),

internal/controller/vspherevalidator_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"k8s.io/apimachinery/pkg/types"
1313

1414
"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
15+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1516
"github.com/validator-labs/validator-plugin-vsphere/pkg/vcsim"
1617
vapi "github.com/validator-labs/validator/api/v1alpha1"
1718
vres "github.com/validator-labs/validator/pkg/validationresult"
@@ -48,7 +49,7 @@ var _ = Describe("VsphereValidator controller", Ordered, func() {
4849
TagValidationRules: []v1alpha1.TagValidationRule{
4950
{
5051
RuleName: "Datacenter k8s-region rule",
51-
EntityType: "datacenter",
52+
EntityType: vcenter.Datacenter,
5253
EntityName: "Datacenter",
5354
Tag: "k8s-region",
5455
},

pkg/validate/validate_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212

1313
"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
14+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1415
"github.com/validator-labs/validator-plugin-vsphere/pkg/vcsim"
1516
)
1617

@@ -33,7 +34,7 @@ func TestValidate(t *testing.T) {
3334
Datacenter: "DC0",
3435
PrivilegeValidationRules: testRules([]privilegeRuleInput{
3536
{
36-
EntityType: "cluster",
37+
EntityType: vcenter.Cluster,
3738
EntityName: "DC0_C0",
3839
Privileges: []string{"Alarm.Acknowledge"},
3940
},
@@ -50,7 +51,7 @@ func TestValidate(t *testing.T) {
5051
Datacenter: "DC0",
5152
PrivilegeValidationRules: testRules([]privilegeRuleInput{
5253
{
53-
EntityType: "cluster",
54+
EntityType: vcenter.Cluster,
5455
EntityName: "DC0_C0",
5556
Privileges: []string{"Nonexistent"},
5657
},
@@ -76,7 +77,7 @@ func TestValidate(t *testing.T) {
7677
}
7778

7879
type privilegeRuleInput struct {
79-
EntityType string
80+
EntityType vcenter.Entity
8081
EntityName string
8182
Privileges []string
8283
}

pkg/validators/computeresources/computeresources.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/apimachinery/pkg/api/resource"
1818

1919
"github.com/validator-labs/validator-plugin-vsphere/api/v1alpha1"
20+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
2021
"github.com/validator-labs/validator-plugin-vsphere/pkg/constants"
2122
"github.com/validator-labs/validator-plugin-vsphere/pkg/vsphere"
2223
vapi "github.com/validator-labs/validator/api/v1alpha1"
@@ -125,11 +126,11 @@ func (c *ValidationService) ReconcileComputeResourceValidationRule(rule v1alpha1
125126

126127
var res *Usage
127128
switch rule.Scope {
128-
case "cluster":
129+
case vcenter.Cluster:
129130
res, err = clusterUsage(ctx, rule, finder)
130-
case "resourcepool":
131+
case vcenter.ResourcePool:
131132
res, err = resourcePoolUsage(ctx, rule, finder, driver)
132-
case "host":
133+
case vcenter.Host:
133134
res, err = hostUsage(ctx, rule, finder)
134135
default:
135136
err = fmt.Errorf("unsupported scope: %s", rule.Scope)
@@ -366,11 +367,11 @@ func getTotalQuantity(quantity string, numberOfNodes int) resource.Quantity {
366367
// GetScopeKey returns a formatted key depending on the scope of a rule
367368
func GetScopeKey(rule v1alpha1.ComputeResourceRule) (string, error) {
368369
switch rule.Scope {
369-
case "cluster":
370+
case vcenter.Cluster:
370371
return fmt.Sprintf("%s-%s", rule.Scope, rule.EntityName), nil
371-
case "host":
372+
case vcenter.Host:
372373
return fmt.Sprintf("%s-%s", rule.Scope, rule.EntityName), nil
373-
case "resourcepool":
374+
case vcenter.ResourcePool:
374375
return fmt.Sprintf("%s-%s", rule.Scope, rule.ClusterName), nil
375376
default:
376377
return "", fmt.Errorf("unsupported scope: %s", rule.Scope)

0 commit comments

Comments
 (0)