Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix linting errors #21

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/virtbmc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func main() {
},
},
Action: func(cCtx *cli.Context) error {
ctx := context.WithValue(cCtx.Context, "VMNamespace", cCtx.Args().Get(0))
ctx = context.WithValue(ctx, "VMName", cCtx.Args().Get(1))
ctx := context.WithValue(cCtx.Context, virtbmc.VMNamespaceKey{}, cCtx.Args().Get(0))
ctx = context.WithValue(ctx, virtbmc.VMNameKey{}, cCtx.Args().Get(1))
return run(ctx, options)
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// Update VirtualMachineBMC status
if svc.Spec.ClusterIP == "" {
return ctrl.Result{RequeueAfter: time.Duration(time.Second * 10)}, fmt.Errorf("clusterIP is not ready yet")
return ctrl.Result{RequeueAfter: time.Second * 10}, fmt.Errorf("clusterIP is not ready yet")
}
virtualMachineBMC.Status.Ready = true
virtualMachineBMC.Status.ServiceIP = svc.Spec.ClusterIP
Expand Down
10 changes: 3 additions & 7 deletions internal/controller/virtualmachine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (v *VirtualMachineReconciler) handleFinalizer(ctx context.Context, vm *kube
return nil
}

func (v *VirtualMachineReconciler) constructVirtualMachineBMCForVirtualMachine(vm *kubevirtv1.VirtualMachine) (*virtualmachinev1.VirtualMachineBMC, error) {
func (v *VirtualMachineReconciler) constructVirtualMachineBMCFromVirtualMachine(vm *kubevirtv1.VirtualMachine) *virtualmachinev1.VirtualMachineBMC {
name := fmt.Sprintf("%s-%s", vm.Namespace, vm.Name)

virtualMachineBMC := &virtualmachinev1.VirtualMachineBMC{
Expand All @@ -80,7 +80,7 @@ func (v *VirtualMachineReconciler) constructVirtualMachineBMCForVirtualMachine(v
},
}

return virtualMachineBMC, nil
return virtualMachineBMC
}

//+kubebuilder:rbac:groups=kubevirt.io,resources=virtualmachines,verbs=get;list;watch;update;patch
Expand All @@ -106,11 +106,7 @@ func (v *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}

// Prepare the VirtualMachineBMC
virtualMachineBMC, err := v.constructVirtualMachineBMCForVirtualMachine(&vm)
if err != nil {
log.Error(err, "unable to construct virtualmachinebmc from template")
return ctrl.Result{}, err
}
virtualMachineBMC := v.constructVirtualMachineBMCFromVirtualMachine(&vm)

// Create the VirtualMachineBMC on the cluster
if err := v.Create(ctx, virtualMachineBMC); err != nil && !apierrors.IsAlreadyExists(err) {
Expand Down
20 changes: 6 additions & 14 deletions internal/controller/virtualmachinebmc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
apiGVStr = virtualmachinev1.GroupVersion.String()
)

func (r *VirtualMachineBMCReconciler) constructPodForVirtualMachineBMC(virtualMachineBMC *virtualmachinev1.VirtualMachineBMC) (*corev1.Pod, error) {
func (r *VirtualMachineBMCReconciler) constructPodFromVirtualMachineBMC(virtualMachineBMC *virtualmachinev1.VirtualMachineBMC) *corev1.Pod {
name := fmt.Sprintf("%s-virtbmc", virtualMachineBMC.Name)

pod := &corev1.Pod{
Expand Down Expand Up @@ -81,10 +81,10 @@ func (r *VirtualMachineBMCReconciler) constructPodForVirtualMachineBMC(virtualMa
},
}

return pod, nil
return pod
}

func (r *VirtualMachineBMCReconciler) constructServiceForVirtualMachineBMC(virtualMachineBMC *virtualmachinev1.VirtualMachineBMC) (*corev1.Service, error) {
func (r *VirtualMachineBMCReconciler) constructServiceFromVirtualMachineBMC(virtualMachineBMC *virtualmachinev1.VirtualMachineBMC) *corev1.Service {
name := fmt.Sprintf("%s-virtbmc", virtualMachineBMC.Name)

svc := &corev1.Service{
Expand All @@ -111,7 +111,7 @@ func (r *VirtualMachineBMCReconciler) constructServiceForVirtualMachineBMC(virtu
},
}

return svc, nil
return svc
}

//+kubebuilder:rbac:groups=virtualmachine.kubevirt.io,resources=virtualmachinebmcs,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -142,11 +142,7 @@ func (r *VirtualMachineBMCReconciler) Reconcile(ctx context.Context, req ctrl.Re
}

// Prepare the virtBMC Pod
pod, err := r.constructPodForVirtualMachineBMC(&virtualMachineBMC)
if err != nil {
log.Error(err, "unable to construct pod from template")
return ctrl.Result{}, err
}
pod := r.constructPodFromVirtualMachineBMC(&virtualMachineBMC)
if err := ctrl.SetControllerReference(&virtualMachineBMC, pod, r.Scheme); err != nil {
return ctrl.Result{}, err
}
Expand All @@ -160,11 +156,7 @@ func (r *VirtualMachineBMCReconciler) Reconcile(ctx context.Context, req ctrl.Re
log.V(1).Info("created Pod for VirtualMachineBMC", "pod", pod)

// Prepare the virtBMC Service
svc, err := r.constructServiceForVirtualMachineBMC(&virtualMachineBMC)
if err != nil {
log.Error(err, "unable to construct svc from template")
return ctrl.Result{}, err
}
svc := r.constructServiceFromVirtualMachineBMC(&virtualMachineBMC)
if err := ctrl.SetControllerReference(&virtualMachineBMC, svc, r.Scheme); err != nil {
return ctrl.Result{}, err
}
Expand Down
21 changes: 1 addition & 20 deletions pkg/virtbmc/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,6 @@ genClientset:
return clientset
}

func (b *VirtBMC) getVirtualMachine(namespace, name string) (*kubevirtv1.VirtualMachine, error) {
vm, err := b.kvClient.VirtualMachines(namespace).Get(b.context, name, v1.GetOptions{})
if err != nil {
return nil, err
}
return vm, nil
}

func (b *VirtBMC) getVirtualMachineInstance(namespace, name string) (*kubevirtv1.VirtualMachineInstance, error) {
vmi, err := b.kvClient.VirtualMachineInstances(namespace).Get(b.context, name, v1.GetOptions{})
if err != nil {
return nil, err
}
return vmi, nil
}

func (b *VirtBMC) getVirtualMachinePowerStatus() (bool, error) {
vm, err := b.kvClient.VirtualMachines(b.vmNamespace).Get(b.context, b.vmName, v1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -104,10 +88,7 @@ func (b *VirtBMC) startVirtualMachine() error {
}

func (b *VirtBMC) rebootVirtualMachine() error {
if err := b.kvClient.VirtualMachineInstances(b.vmNamespace).Delete(b.context, b.vmName, v1.DeleteOptions{}); err != nil {
return err
}
return nil
return b.kvClient.VirtualMachineInstances(b.vmNamespace).Delete(b.context, b.vmName, v1.DeleteOptions{})
}

func (b *VirtBMC) setVirtualMachineBootDevice(bd BootDevice) error {
Expand Down
8 changes: 6 additions & 2 deletions pkg/virtbmc/virtbmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
kubevirtv1 "kubevirt.io/kubevirtbmc/pkg/generated/clientset/versioned/typed/core/v1"
)

type VMNameKey struct{}

type VMNamespaceKey struct{}

type Options struct {
KubeconfigPath string
Address string
Expand All @@ -31,8 +35,8 @@ func NewVirtBMC(ctx context.Context, options Options, inCluster bool) (*VirtBMC,
context: ctx,
address: options.Address,
port: options.Port,
vmNamespace: ctx.Value("VMNamespace").(string),
vmName: ctx.Value("VMName").(string),
vmNamespace: ctx.Value(VMNamespaceKey{}).(string),
vmName: ctx.Value(VMNameKey{}).(string),
kvClient: NewK8sClient(options),
sim: ipmi.NewSimulator(net.UDPAddr{
IP: net.ParseIP(options.Address).To4(),
Expand Down