Skip to content

Commit 6a0a4f2

Browse files
committed
Add Encryption Class to the vcsim test
1 parent e10f947 commit 6a0a4f2

File tree

9 files changed

+205
-1
lines changed

9 files changed

+205
-1
lines changed

test/e2e/ownerrefs_finalizers_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ var (
238238
"VirtualMachineClassBinding": func(_ types.NamespacedName, _ []metav1.OwnerReference) error { return nil },
239239
"VirtualMachineClass": func(_ types.NamespacedName, _ []metav1.OwnerReference) error { return nil },
240240
"VMOperatorDependencies": func(_ types.NamespacedName, _ []metav1.OwnerReference) error { return nil },
241+
"EncryptionClass": func(_ types.NamespacedName, _ []metav1.OwnerReference) error { return nil },
241242
}
242243
}
243244

test/framework/vmoperator/vmoperator.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"k8s.io/apimachinery/pkg/api/meta"
3939
"k8s.io/apimachinery/pkg/api/resource"
4040
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
42+
"k8s.io/apimachinery/pkg/runtime/schema"
4143
"k8s.io/apimachinery/pkg/util/wait"
4244
"k8s.io/klog/v2"
4345
ctrl "sigs.k8s.io/controller-runtime"
@@ -453,6 +455,65 @@ func ReconcileDependencies(ctx context.Context, c client.Client, dependenciesCon
453455
}
454456
}
455457

458+
// Create EncryptionClass in K8s
459+
encryptionClassGVR := schema.GroupVersionResource{
460+
Group: "encryption.vmware.com",
461+
Version: "v1alpha1",
462+
Resource: "encryptionclasses",
463+
}
464+
for _, ec := range config.Spec.EncryptionClasses {
465+
metadata := map[string]interface{}{
466+
"name": ec.Name,
467+
"namespace": config.Namespace,
468+
}
469+
// Add default label if this is the default EncryptionClass
470+
if ec.Default {
471+
metadata["labels"] = map[string]interface{}{
472+
"encryption.vmware.com/default": "true",
473+
}
474+
}
475+
476+
spec := map[string]interface{}{}
477+
spec["keyProvider"] = ec.KeyProvider
478+
spec["keyID"] = ec.KeyID
479+
480+
encryptionClass := &unstructured.Unstructured{
481+
Object: map[string]interface{}{
482+
"apiVersion": "encryption.vmware.com/v1alpha1",
483+
"kind": "EncryptionClass",
484+
"metadata": metadata,
485+
"spec": spec,
486+
},
487+
}
488+
_ = wait.PollUntilContextTimeout(ctx, 250*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (bool, error) {
489+
retryError = nil
490+
existing := &unstructured.Unstructured{}
491+
existing.SetGroupVersionKind(schema.GroupVersionKind{
492+
Group: encryptionClassGVR.Group,
493+
Version: encryptionClassGVR.Version,
494+
Kind: "EncryptionClass",
495+
})
496+
if err := c.Get(ctx, client.ObjectKey{
497+
Name: ec.Name,
498+
Namespace: config.Namespace,
499+
}, existing); err != nil {
500+
if !apierrors.IsNotFound(err) {
501+
retryError = errors.Wrapf(err, "failed to get EncryptionClass %s", ec.Name)
502+
return false, nil
503+
}
504+
if err := c.Create(ctx, encryptionClass); err != nil {
505+
retryError = errors.Wrapf(err, "failed to create EncryptionClass %s", ec.Name)
506+
return false, nil
507+
}
508+
log.Info("Created EncryptionClass", "EncryptionClass", klog.KRef(config.Namespace, ec.Name))
509+
}
510+
return true, nil
511+
})
512+
if retryError != nil {
513+
return retryError
514+
}
515+
}
516+
456517
// Create a ContentLibrary in K8s and in vCenter,
457518
// This requires a set of objects in vCenter(or vcsim) as well as their mapping in K8s
458519
// - vCenter: a Library containing an Item

test/infrastructure/vcsim/api/v1alpha1/vmoperatordependencies_types.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type VMOperatorDependenciesSpec struct {
4545

4646
// VirtualMachineClasses defines a list of VirtualMachineClasses to be bound to the namespace where this object is created.
4747
VirtualMachineClasses []VirtualMachineClass `json:"virtualMachineClasses,omitempty"`
48+
49+
// EncryptionClasses defines a list of EncryptionClasses to be created in the namespace where this object is created.
50+
EncryptionClasses []EncryptionClass `json:"encryptionClasses,omitempty"`
4851
}
4952

5053
// VMOperatorRef provide a reference to the running instance of vm-operator.
@@ -80,6 +83,14 @@ type VirtualMachineClass struct {
8083
Memory resource.Quantity `json:"memory,omitempty"`
8184
}
8285

86+
type EncryptionClass struct {
87+
Name string `json:"name,omitempty"`
88+
KeyProvider string `json:"keyProvider,omitempty"`
89+
KeyID string `json:"keyID,omitempty"`
90+
// Default indicates if this EncryptionClass should be marked as default
91+
Default bool `json:"default,omitempty"`
92+
}
93+
8394
type ContentLibraryItemFilesConfig struct {
8495
Name string `json:"name,omitempty"`
8596
Content []byte `json:"content,omitempty"`
@@ -179,7 +190,7 @@ func (d *VMOperatorDependencies) SetVCenterFromVCenterSimulator(vCenterSimulator
179190
)
180191
}
181192

182-
// Add default storage and vm class for vcsim in not otherwise specified.
193+
// Add default storage and vm class for vcsim if not otherwise specified.
183194
if len(d.Spec.StorageClasses) == 0 {
184195
d.Spec.StorageClasses = []StorageClass{
185196
{
@@ -197,4 +208,15 @@ func (d *VMOperatorDependencies) SetVCenterFromVCenterSimulator(vCenterSimulator
197208
},
198209
}
199210
}
211+
// Add default encryption class for vcsim if not otherwise specified.
212+
if len(d.Spec.EncryptionClasses) == 0 {
213+
d.Spec.EncryptionClasses = []EncryptionClass{
214+
{
215+
Name: "vcsim-default-encryption-class",
216+
KeyProvider: "vcsim-key-provider",
217+
KeyID: "vcsim-key-id",
218+
Default: true,
219+
},
220+
}
221+
}
200222
}

test/infrastructure/vcsim/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.16.1
7+
name: encryptionclasses.encryption.vmware.com
8+
spec:
9+
group: encryption.vmware.com
10+
names:
11+
kind: EncryptionClass
12+
listKind: EncryptionClassList
13+
plural: encryptionclasses
14+
shortNames:
15+
- encclass
16+
singular: encryptionclass
17+
scope: Namespaced
18+
versions:
19+
- additionalPrinterColumns:
20+
- jsonPath: .spec.keyProvider
21+
name: KeyProvider
22+
type: string
23+
- jsonPath: .spec.keyID
24+
name: KeyID
25+
type: string
26+
name: v1alpha1
27+
schema:
28+
openAPIV3Schema:
29+
description: EncryptionClass is the Schema for the encryptionclasses API.
30+
properties:
31+
apiVersion:
32+
description: |-
33+
APIVersion defines the versioned schema of this representation of an object.
34+
Servers should convert recognized schemas to the latest internal value, and
35+
may reject unrecognized values.
36+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
37+
type: string
38+
kind:
39+
description: |-
40+
Kind is a string value representing the REST resource this object represents.
41+
Servers may infer this from the endpoint the client submits requests to.
42+
Cannot be updated.
43+
In CamelCase.
44+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
45+
type: string
46+
metadata:
47+
type: object
48+
spec:
49+
description: EncryptionClassSpec defines the desired state of EncryptionClass.
50+
properties:
51+
keyID:
52+
description: |-
53+
KeyID describes the key used to encrypt/recrypt/decrypt resources.
54+
When omitted, a key will be generated from the specified provider.
55+
type: string
56+
keyProvider:
57+
description: |-
58+
KeyProvider describes the key provider used to encrypt/recrypt/decrypt
59+
resources.
60+
type: string
61+
required:
62+
- keyProvider
63+
type: object
64+
status:
65+
description: EncryptionClassStatus defines the observed state of EncryptionClass.
66+
type: object
67+
type: object
68+
served: true
69+
storage: true
70+
subresources:
71+
status: {}

test/infrastructure/vcsim/config/crd/bases/vcsim.infrastructure.cluster.x-k8s.io_vmoperatordependencies.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ spec:
4343
VMOperatorDependenciesSpec defines the desired state of the VMOperatorDependencies in
4444
the namespace where this object is created.
4545
properties:
46+
encryptionClasses:
47+
description: EncryptionClasses defines a list of EncryptionClasses
48+
to be created in the namespace where this object is created.
49+
items:
50+
properties:
51+
default:
52+
description: Default indicates if this EncryptionClass should
53+
be marked as default
54+
type: boolean
55+
keyID:
56+
type: string
57+
keyProvider:
58+
type: string
59+
name:
60+
type: string
61+
type: object
62+
type: array
4663
operatorRef:
4764
description: OperatorRef provides a reference to the running instance
4865
of vm-operator.

test/infrastructure/vcsim/config/crd/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ resources:
1111
- bases/vcsim.infrastructure.cluster.x-k8s.io_controlplaneendpoints.yaml
1212
- bases/vcsim.infrastructure.cluster.x-k8s.io_envvars.yaml
1313
- bases/vcsim.infrastructure.cluster.x-k8s.io_vmoperatordependencies.yaml
14+
- bases/encryption.vmware.com_encryptionclasses.yaml
1415

1516
patchesStrategicMerge:
1617
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.

test/infrastructure/vcsim/config/rbac/role.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ rules:
5656
- get
5757
- list
5858
- watch
59+
- apiGroups:
60+
- encryption.vmware.com
61+
resources:
62+
- encryptionclasses
63+
verbs:
64+
- create
65+
- get
66+
- list
67+
- update
68+
- watch
5969
- apiGroups:
6070
- infrastructure.cluster.x-k8s.io
6171
resources:

test/infrastructure/vcsim/controllers/vcsim_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type VCenterSimulatorReconciler struct {
7979
// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachineimages,verbs=get;list;watch;create
8080
// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachineimages/status,verbs=get;update;patch
8181
// +kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get;list;watch;create
82+
// +kubebuilder:rbac:groups=encryption.vmware.com,resources=encryptionclasses,verbs=get;list;watch;create;update
8283
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
8384
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create
8485
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create

0 commit comments

Comments
 (0)