Skip to content

Commit

Permalink
feat: Support different namespace for infra resources
Browse files Browse the repository at this point in the history
  • Loading branch information
scuzhanglei committed May 23, 2023
1 parent 09c3204 commit 0244207
Show file tree
Hide file tree
Showing 12 changed files with 2,721 additions and 2,484 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Except for the [common variables](https://cluster-api.sigs.k8s.io/clusterctl/pro
| KUBERNETES_VERSION | Only support Kubernetes versions that corresponding rootfs image `smartxworks/capch-rootfs-$KUBERNETES_VERSION` exists|
| VIRTINK_INFRA_CLUSTER_SECRET_NAME | The name of secret in the management cluster that contains the kubeconfig of the Virtink infrastructure cluster |
| VIRTINK_INFRA_CLUSTER_SECRET_NAMESPACE | The namespace of secret in the management cluster that contains the kubeconfig of the Virtink infrastructure cluster |
| VIRTINK_INFRA_CLUSTER_RESOURCES_NAMESPACE | The namespace of resources(such as VM, DataVolume) to be created in the infrastructural cluster, make sure it exists before create the workload cluster (default `$NAMESPACE`) |
| POD_NETWORK_CIDR | Range of IP addresses for the pod network (default `192.168.0.0/16`) |
| SERVICE_CIDR | Range of IP address for service VIPs (default `10.96.0.0/12`) |
| VIRTINK_CONTROL_PLANE_SERVICE_TYPE | The type of control plane service (default `NodePort`) |
Expand Down
12 changes: 9 additions & 3 deletions api/v1beta1/virtinkmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ type VirtinkMachineSpec struct {

ProviderID *string `json:"providerID,omitempty"`

VMSpec virtv1alpha1.VirtualMachineSpec `json:"vmSpec"`
VolumeTemplates []VolumeTemplateSource `json:"volumeTemplates,omitempty"`
IPPoolRef *corev1.TypedLocalObjectReference `json:"ipPoolRef,omitempty"`
VirtualMachineTemplate VirtualMachineTemplateSpec `json:"virtualMachineTemplate"`
VolumeTemplates []VolumeTemplateSource `json:"volumeTemplates,omitempty"`
IPPoolRef *corev1.TypedLocalObjectReference `json:"ipPoolRef,omitempty"`
}

type VirtualMachineTemplateSpec struct {
// +kubebuilder:pruning:PreserveUnknownFields
ObjectMeta metav1.ObjectMeta `json:"metadata,omitempty"`
Spec virtv1alpha1.VirtualMachineSpec `json:"spec"`
}

type VolumeTemplateSource struct {
Expand Down
19 changes: 18 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

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

2,173 changes: 1,111 additions & 1,062 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_virtinkmachines.yaml

Large diffs are not rendered by default.

2,357 changes: 1,227 additions & 1,130 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_virtinkmachinetemplates.yaml

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions controllers/virtinkcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ func (r *VirtinkClusterReconciler) reconcile(ctx context.Context, cluster *infra
}
}

infraNamespace := cluster.Namespace
if cluster.Spec.ControlPlaneServiceTemplate.ObjectMeta.Namespace != "" {
infraNamespace = cluster.Spec.ControlPlaneServiceTemplate.ObjectMeta.Namespace
}
if !cluster.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(cluster, finalizer) {
var controlPlaneService corev1.Service
controlPlaneServiceKey := types.NamespacedName{
Name: cluster.Name,
Namespace: cluster.Namespace,
Namespace: infraNamespace,
}
controlPlaneServiceNotFound := false
if err := infraClusterClient.Get(ctx, controlPlaneServiceKey, &controlPlaneService); err != nil {
Expand Down Expand Up @@ -124,7 +128,7 @@ func (r *VirtinkClusterReconciler) reconcile(ctx context.Context, cluster *infra
var controlPlaneService corev1.Service
controlPlaneServiceKey := types.NamespacedName{
Name: cluster.Name,
Namespace: cluster.Namespace,
Namespace: infraNamespace,
}
controlPlaneServiceNotFound := false
if err := infraClusterClient.Get(ctx, controlPlaneServiceKey, &controlPlaneService); err != nil {
Expand Down
18 changes: 14 additions & 4 deletions controllers/virtinkmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,17 @@ func (r *VirtinkMachineReconciler) reconcile(ctx context.Context, machine *infra
}
}

infraNamespace := machine.Namespace
if machine.Spec.VirtualMachineTemplate.ObjectMeta.Namespace != "" {
infraNamespace = machine.Spec.VirtualMachineTemplate.ObjectMeta.Namespace
}

if !machine.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(machine, finalizer) {
var vm virtv1alpha1.VirtualMachine
vmKey := types.NamespacedName{
Name: machine.Name,
Namespace: machine.Namespace,
Namespace: infraNamespace,
}
vmNotFound := false
if err := infraClusterClient.Get(ctx, vmKey, &vm); err != nil {
Expand Down Expand Up @@ -240,7 +245,7 @@ func (r *VirtinkMachineReconciler) reconcile(ctx context.Context, machine *infra
var vm virtv1alpha1.VirtualMachine
vmKey := types.NamespacedName{
Name: machine.Name,
Namespace: machine.Namespace,
Namespace: infraNamespace,
}
vmNotFound := false
if err := infraClusterClient.Get(ctx, vmKey, &vm); err != nil {
Expand Down Expand Up @@ -371,7 +376,7 @@ func (r *VirtinkMachineReconciler) buildVM(ctx context.Context, machine *infrast
Labels: machine.Labels,
Annotations: machine.Annotations,
},
Spec: machine.Spec.VMSpec,
Spec: machine.Spec.VirtualMachineTemplate.Spec,
}

for i := range vm.Spec.Volumes {
Expand Down Expand Up @@ -405,13 +410,18 @@ func (r *VirtinkMachineReconciler) buildVM(ctx context.Context, machine *infrast
}

func (r *VirtinkMachineReconciler) buildDataVolumes(ctx context.Context, machine *infrastructurev1beta1.VirtinkMachine) []*cdiv1beta1.DataVolume {
infraNamespace := machine.Namespace
if machine.Spec.VirtualMachineTemplate.ObjectMeta.Namespace != "" {
infraNamespace = machine.Spec.VirtualMachineTemplate.ObjectMeta.Namespace
}

dataVolumes := []*cdiv1beta1.DataVolume{}
for _, volume := range machine.Spec.VolumeTemplates {
switch {
case volume.DataVolume != nil:
dataVolume := cdiv1beta1.DataVolume{
ObjectMeta: metav1.ObjectMeta{
Namespace: machine.Namespace,
Namespace: infraNamespace,
Name: fmt.Sprintf("%s-%s", machine.Name, volume.DataVolume.Name),
},
Spec: *volume.DataVolume.Spec.DeepCopy(),
Expand Down
41 changes: 31 additions & 10 deletions controllers/virtinkmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

infrastructurev1beta1 "github.com/smartxworks/cluster-api-provider-virtink/api/v1beta1"
Expand All @@ -23,6 +24,7 @@ var _ = Describe("VirtinkMachine controller", func() {
var clusterKey types.NamespacedName
var machineKey types.NamespacedName
var virtinkMachineKey types.NamespacedName
var virtualMachineKey types.NamespacedName
BeforeEach(func() {
By("creating a new VirtinkCluster")
clusterKey = types.NamespacedName{
Expand Down Expand Up @@ -74,6 +76,19 @@ var _ = Describe("VirtinkMachine controller", func() {
Namespace: "default",
Name: "virtink-machine-" + uuid.New().String(),
}

infraNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "infra-namespace",
},
}
Expect(client.IgnoreAlreadyExists(k8sClient.Create(ctx, infraNamespace))).To(Succeed())

virtualMachineKey = types.NamespacedName{
Namespace: infraNamespace.Name,
Name: virtinkMachineKey.Name,
}

virtinkMachine := infrastructurev1beta1.VirtinkMachine{
ObjectMeta: metav1.ObjectMeta{
Name: virtinkMachineKey.Name,
Expand All @@ -83,11 +98,16 @@ var _ = Describe("VirtinkMachine controller", func() {
},
},
Spec: infrastructurev1beta1.VirtinkMachineSpec{
VMSpec: virtv1alpha1.VirtualMachineSpec{
Instance: virtv1alpha1.Instance{
CPU: virtv1alpha1.CPU{
Sockets: uint32(1),
CoresPerSocket: uint32(2),
VirtualMachineTemplate: infrastructurev1beta1.VirtualMachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Namespace: virtualMachineKey.Namespace,
},
Spec: virtv1alpha1.VirtualMachineSpec{
Instance: virtv1alpha1.Instance{
CPU: virtv1alpha1.CPU{
Sockets: uint32(1),
CoresPerSocket: uint32(2),
},
},
},
},
Expand All @@ -109,7 +129,7 @@ var _ = Describe("VirtinkMachine controller", func() {
It("should not create VM", func() {
var vm virtv1alpha1.VirtualMachine
Consistently(func() bool {
return apierrors.IsNotFound(k8sClient.Get(ctx, virtinkMachineKey, &vm))
return apierrors.IsNotFound(k8sClient.Get(ctx, virtualMachineKey, &vm))
}).Should(BeTrue())
})
})
Expand All @@ -128,7 +148,7 @@ var _ = Describe("VirtinkMachine controller", func() {
It("should not create VM", func() {
var vm virtv1alpha1.VirtualMachine
Consistently(func() bool {
return apierrors.IsNotFound(k8sClient.Get(ctx, virtinkMachineKey, &vm))
return apierrors.IsNotFound(k8sClient.Get(ctx, virtualMachineKey, &vm))
}).Should(BeTrue())
})
})
Expand Down Expand Up @@ -156,9 +176,10 @@ var _ = Describe("VirtinkMachine controller", func() {
It("should create a virtink VM", func() {
var vm virtv1alpha1.VirtualMachine
Eventually(func() error {
return k8sClient.Get(ctx, virtinkMachineKey, &vm)
return k8sClient.Get(ctx, virtualMachineKey, &vm)
}, "10s").Should(Succeed())
Expect(vm.Spec.Volumes[0].CloudInit.UserDataBase64).To(Equal(base64.StdEncoding.EncodeToString([]byte("#cloud-init"))))
Expect(vm.Namespace).To(Equal("infra-namespace"))

var virtinkMachine infrastructurev1beta1.VirtinkMachine
Eventually(func() bool {
Expand All @@ -172,7 +193,7 @@ var _ = Describe("VirtinkMachine controller", func() {
It("should delete virtink VM and remove finalizer", func() {
var vm virtv1alpha1.VirtualMachine
Eventually(func() error {
return k8sClient.Get(ctx, virtinkMachineKey, &vm)
return k8sClient.Get(ctx, virtualMachineKey, &vm)
}, "10s").Should(Succeed())

virtinkMachine := infrastructurev1beta1.VirtinkMachine{
Expand All @@ -184,7 +205,7 @@ var _ = Describe("VirtinkMachine controller", func() {
Expect(k8sClient.Delete(ctx, &virtinkMachine)).To(Succeed())

Eventually(func() bool {
return apierrors.IsNotFound(k8sClient.Get(ctx, virtinkMachineKey, &vm))
return apierrors.IsNotFound(k8sClient.Get(ctx, virtualMachineKey, &vm))
}).Should(BeTrue())
})
})
Expand Down
Loading

0 comments on commit 0244207

Please sign in to comment.