Skip to content

Commit

Permalink
Merge pull request #239 from BarthV/none-mode
Browse files Browse the repository at this point in the history
Add sentinel bootstrap file check modes in CRD
  • Loading branch information
k8s-ci-robot authored May 3, 2023
2 parents 0cd24c7 + 9a7fe21 commit b49257e
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 3 deletions.
15 changes: 15 additions & 0 deletions api/v1alpha1/kubevirtmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,27 @@ type KubevirtMachineSpec struct {
// +optional
ProviderID *string `json:"providerID,omitempty"`

// BootstrapCheckSpec defines how the CAPK controller is checking CAPI Sentinel file inside the VM.
// +optional
BootstrapCheckSpec VirtualMachineBootstrapCheckSpec `json:"virtualMachineBootstrapCheck,omitempty"`

// InfraClusterSecretRef is a reference to a secret with a kubeconfig for external cluster used for infra.
// When nil, this defaults to the value present in the KubevirtCluster object's spec associated with this machine.
// +optional
InfraClusterSecretRef *corev1.ObjectReference `json:"infraClusterSecretRef,omitempty"`
}

// VirtualMachineBootstrapCheckSpec defines how the controller will remotely check CAPI Sentinel file content.
type VirtualMachineBootstrapCheckSpec struct {
// CheckStrategy describes how CAPK controller will validate a successful CAPI bootstrap.
// Following specified method, CAPK will try to retrieve the state of the CAPI Sentinel file from the VM.
// Possible values are: "none" or "ssh" (default is "ssh") and this value is validated by apiserver.
// +optional
// +kubebuilder:validation:Enum=none;ssh
// +kubebuilder:default:=ssh
CheckStrategy string `json:"checkStrategy,omitempty"`
}

// KubevirtMachineStatus defines the observed state of KubevirtMachine.
type KubevirtMachineStatus struct {
// Ready denotes that the machine is ready
Expand Down
16 changes: 16 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.

Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ spec:
providerID:
description: ProviderID TBD what to use for Kubevirt
type: string
virtualMachineBootstrapCheck:
description: BootstrapCheckSpec defines how the CAPK controller is
checking CAPI Sentinel file inside the VM.
properties:
checkStrategy:
default: ssh
description: 'CheckStrategy describes how CAPK controller will
validate a successful CAPI bootstrap. Following specified method,
CAPK will try to retrieve the state of the CAPI Sentinel file
from the VM. Possible values are: "none" or "ssh" (default is
"ssh") and this value is validated by apiserver.'
enum:
- none
- ssh
type: string
type: object
virtualMachineTemplate:
description: VirtualMachineTemplateSpec defines the desired state
of the kubevirt VM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ spec:
providerID:
description: ProviderID TBD what to use for Kubevirt
type: string
virtualMachineBootstrapCheck:
description: BootstrapCheckSpec defines how the CAPK controller
is checking CAPI Sentinel file inside the VM.
properties:
checkStrategy:
default: ssh
description: 'CheckStrategy describes how CAPK controller
will validate a successful CAPI bootstrap. Following
specified method, CAPK will try to retrieve the state
of the CAPI Sentinel file from the VM. Possible values
are: "none" or "ssh" (default is "ssh") and this value
is validated by apiserver.'
enum:
- none
- ssh
type: string
type: object
virtualMachineTemplate:
description: VirtualMachineTemplateSpec defines the desired
state of the kubevirt VM.
Expand Down
19 changes: 19 additions & 0 deletions pkg/kubevirt/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ func (m *Machine) SupportsCheckingIsBootstrapped() bool {

// IsBootstrapped checks if the VM is bootstrapped with Kubernetes.
func (m *Machine) IsBootstrapped() bool {
// CheckStrategy value is already sanitized by apiserver
switch m.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy {
case "none":
// skip bootstrap check and always returns positively
return true

case "":
fallthrough // ssh is default check strategy, fallthrough
case "ssh":
return m.IsBootstrappedWithSSH()

default:
// Since CRD CheckStrategy field is validated by an enum, this case should never be hit
return false
}
}

// IsBootstrappedWithSSH checks if the VM is bootstrapped with Kubernetes using SSH strategy.
func (m *Machine) IsBootstrappedWithSSH() bool {
if !m.IsReady() || m.sshKeys == nil {
return false
}
Expand Down
76 changes: 73 additions & 3 deletions pkg/kubevirt/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"sigs.k8s.io/cluster-api-provider-kubevirt/api/v1alpha1"
"sigs.k8s.io/cluster-api-provider-kubevirt/pkg/context"
"sigs.k8s.io/cluster-api-provider-kubevirt/pkg/ssh"
"sigs.k8s.io/cluster-api-provider-kubevirt/pkg/testing"
Expand Down Expand Up @@ -64,6 +65,8 @@ var _ = Describe("Without KubeVirt VM running", func() {
virtualMachine := testing.NewVirtualMachine(virtualMachineInstance)

BeforeEach(func() {
kubevirtMachine.Spec.BootstrapCheckSpec = v1alpha1.VirtualMachineBootstrapCheckSpec{}

machineContext = &context.MachineContext{
Context: gocontext.TODO(),
Cluster: cluster,
Expand Down Expand Up @@ -114,8 +117,29 @@ var _ = Describe("Without KubeVirt VM running", func() {
Expect(externalMachine.IsReady()).To(BeFalse())
})

It("IsBootstrapped should return false", func() {
It("default mode: IsBootstrapped should return false", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte{})
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeFalse())
})

It("ssh mode: IsBootstrapped return false", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte{})
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "ssh"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeFalse())
})

It("none mode: IsBootstrapped should be forced to be true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte{})
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "none"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("invalid mode: IsBootstrapped should return false", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte{})
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "impossible-invalid-input"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeFalse())
})
Expand Down Expand Up @@ -176,6 +200,8 @@ var _ = Describe("With KubeVirt VM running", func() {
virtualMachine := testing.NewVirtualMachine(virtualMachineInstance)

BeforeEach(func() {
kubevirtMachine.Spec.BootstrapCheckSpec = v1alpha1.VirtualMachineBootstrapCheckSpec{}

machineContext = &context.MachineContext{
Context: gocontext.TODO(),
Cluster: cluster,
Expand Down Expand Up @@ -234,12 +260,33 @@ var _ = Describe("With KubeVirt VM running", func() {
Expect(externalMachine.IsReady()).To(BeTrue())
})

It("IsBootstrapped should return true", func() {
It("default mode: IsBootstrapped should return true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("ssh mode: IsBootstrapped return true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "ssh"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("none mode: IsBootstrapped should be forced to be true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "none"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("invalid mode: IsBootstrapped should return false", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "impossible-invalid-input"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeFalse())
})

It("SupportsCheckingIsBootstrapped should return true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -319,6 +366,8 @@ var _ = Describe("With KubeVirt VM running externally", func() {
virtualMachine := testing.NewVirtualMachine(virtualMachineInstance)

BeforeEach(func() {
kubevirtMachine.Spec.BootstrapCheckSpec = v1alpha1.VirtualMachineBootstrapCheckSpec{}

machineContext = &context.MachineContext{
Context: gocontext.TODO(),
Cluster: cluster,
Expand Down Expand Up @@ -377,12 +426,33 @@ var _ = Describe("With KubeVirt VM running externally", func() {
Expect(externalMachine.IsReady()).To(BeTrue())
})

It("IsBootstrapped should return true", func() {
It("default mode: IsBootstrapped should return true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("ssh mode: IsBootstrapped return true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "ssh"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("none mode: IsBootstrapped should be forced to be true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "none"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeTrue())
})

It("invalid mode: IsBootstrapped should return false", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
externalMachine.machineContext.KubevirtMachine.Spec.BootstrapCheckSpec.CheckStrategy = "impossible-invalid-input"
Expect(err).NotTo(HaveOccurred())
Expect(externalMachine.IsBootstrapped()).To(BeFalse())
})

It("SupportsCheckingIsBootstrapped should return true", func() {
externalMachine, err := defaultTestMachine(machineContext, namespace, fakeClient, fakeVMCommandExecutor, []byte(sshKey))
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 2 additions & 0 deletions pkg/testing/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func NewKubevirtMachine(kubevirtMachineName, machineName string) *infrav1.Kubevi
Template: &kubevirtv1.VirtualMachineInstanceTemplateSpec{},
},
},

BootstrapCheckSpec: infrav1.VirtualMachineBootstrapCheckSpec{},
},
Status: infrav1.KubevirtMachineStatus{},
}
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster-template-ext-infra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
spec:
runStrategy: Always
Expand Down
4 changes: 4 additions & 0 deletions templates/cluster-template-kccm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down Expand Up @@ -100,6 +102,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down
4 changes: 4 additions & 0 deletions templates/cluster-template-lb-kccm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down Expand Up @@ -100,6 +102,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down
4 changes: 4 additions & 0 deletions templates/cluster-template-lb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down Expand Up @@ -100,6 +102,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down
4 changes: 4 additions & 0 deletions templates/cluster-template-passt-kccm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down Expand Up @@ -105,6 +107,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down
4 changes: 4 additions & 0 deletions templates/cluster-template-persistent-storage-kccm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down Expand Up @@ -110,6 +112,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down
4 changes: 4 additions & 0 deletions templates/cluster-template-persistent-storage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down Expand Up @@ -110,6 +112,8 @@ metadata:
spec:
template:
spec:
virtualMachineBootstrapCheck:
checkStrategy: ssh
virtualMachineTemplate:
metadata:
namespace: "${NAMESPACE}"
Expand Down
Loading

0 comments on commit b49257e

Please sign in to comment.