Skip to content

Commit

Permalink
Add github actions and linting (#7)
Browse files Browse the repository at this point in the history
* Changes:
- Add .github/workflows
- linting

* Install golangci-lint

* Add timeout for lint in CI
  • Loading branch information
alperencelik authored Oct 19, 2023
1 parent 2cca9b8 commit 13462b4
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 49 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: GolangCI-Lint

on:
pull_request:
types: [opened, edited, synchronize, reopened]

jobs:
lint:
name: Lint
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set Up Go
uses: actions/setup-go@v2
with:
go-version: 1.20.5 # Replace with the Go version you're using

- name: Install golangci-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2
- name: Run Linter
run: golangci-lint run --timeout 10m --verbose
42 changes: 42 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: docker-build

on:
push:
branches:
- "main"

env:
TEST_TAG: alperencelik/kubemox:test
LATEST_TAG: alperencelik/kubemox:latest

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Build and export to Docker
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: ${{ env.TEST_TAG }}
- name: Test
run: |
docker run --rm ${{ env.TEST_TAG }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ env.LATEST_TAG }}
11 changes: 8 additions & 3 deletions internal/controller/proxmox/managedvirtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (r *ManagedVirtualMachineReconciler) Reconcile(ctx context.Context, req ctr
if managedVM.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(managedVM, managedvirtualMachineFinalizerName) {
controllerutil.AddFinalizer(managedVM, managedvirtualMachineFinalizerName)
if err := r.Update(ctx, managedVM); err != nil {
if err = r.Update(ctx, managedVM); err != nil {
log.Log.Info(fmt.Sprintf("Error updating ManagedVirtualMachine %s", managedVM.Name))
}
}
} else {
Expand All @@ -96,7 +97,11 @@ func (r *ManagedVirtualMachineReconciler) Reconcile(ctx context.Context, req ctr
}
// // Update ManagedVM
proxmox.UpdateManagedVM(managedVM.Name, proxmox.GetNodeOfVM(managedVM.Name), managedVM)
r.Update(context.Background(), managedVM)
err = r.Update(context.Background(), managedVM)
if err != nil {
log.Log.Info(fmt.Sprintf("ManagedVM %v could not be updated", managedVM.Name))
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Update ManagedVMStatus
var managedVMStatus proxmoxv1alpha1.ManagedVirtualMachineStatus
nodeName := proxmox.GetNodeOfVM(managedVM.Name)
Expand All @@ -120,7 +125,7 @@ func (r *ManagedVirtualMachineReconciler) SetupWithManager(mgr ctrl.Manager) err
// AllVMs - ControllerVMs = VMs that are not managed by the controller
ManagedVMs := proxmox.SubstractSlices(AllVMs, ControllerVMs)
for _, ManagedVM := range ManagedVMs {
if proxmox.CheckManagedVMExists(ManagedVM) != true {
if !proxmox.CheckManagedVMExists(ManagedVM) {
log.Log.Info(fmt.Sprintf("ManagedVM %v does not exist so creating it", ManagedVM))
managedVM := proxmox.CreateManagedVM(ManagedVM)
err := r.Create(context.Background(), managedVM)
Expand Down
19 changes: 10 additions & 9 deletions internal/controller/proxmox/virtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
if !controllerutil.ContainsFinalizer(vm, virtualMachineFinalizerName) {
controllerutil.AddFinalizer(vm, virtualMachineFinalizerName)
if err := r.Update(ctx, vm); err != nil {
log.Log.Error(err, "Error updating VirtualMachine")
}
}
} else {
Expand Down Expand Up @@ -105,7 +106,7 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
vmName := vm.Spec.Name
nodeName := vm.Spec.NodeName
vmExists := proxmox.CheckVM(vmName, nodeName)
if vmExists == true {
if vmExists {
// If exists, update the VM
vmState := proxmox.GetVMState(vmName, nodeName)
if vmState == "stopped" {
Expand All @@ -118,7 +119,10 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
processedResources[resourceKey] = true
}
proxmox.UpdateVM(vmName, nodeName, vm)
r.Update(context.Background(), vm)
err = r.Update(context.Background(), vm)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
} else {
// If not exists, create the VM
Expand All @@ -140,7 +144,10 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
// If template and created VM has different resources then update the VM with new resources the function itself decides if VM restart needed or not
proxmox.UpdateVM(vmName, nodeName, vm)
r.Update(context.Background(), vm)
err = r.Update(context.Background(), vm)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Update the status of VirtualMachine resource
var Status proxmoxv1alpha1.VirtualMachineStatus
Status.State, Status.ID, Status.Uptime, Status.Node, Status.Name, Status.IPAddress, Status.OSInfo = proxmox.UpdateVMStatus(vmName, nodeName)
Expand Down Expand Up @@ -181,9 +188,3 @@ func isProcessed(resourceKey string) bool {
defer logMutex.Unlock()
return processedResources[resourceKey]
}

func markAsProcessed(resourceKey string) {
logMutex.Lock()
defer logMutex.Unlock()
processedResources[resourceKey] = true
}
19 changes: 13 additions & 6 deletions internal/controller/proxmox/virtualmachineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
client.InNamespace(req.Namespace),
// Change that one to metadata.ownerReference
client.MatchingLabels{"owner": vmSet.Name}); err != nil {
log.Log.Info("Unable to list VMs")
}

resourceKey := fmt.Sprintf("%s/%s", vmSet.Namespace, vmSet.Name)
Expand All @@ -84,7 +85,10 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
if len(vmList.Items) < replicas && vmSet.Status.Condition != "Terminating" {
for i := 1; i <= replicas; i++ {
vmSet.Status.Condition = "Scaling Up"
r.Status().Update(ctx, vmSet)
err := r.Status().Update(ctx, vmSet)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if isProcessed(resourceKey) {
} else {
log.Log.Info(fmt.Sprintf("Creating a new VirtualMachine %s for VirtualMachineSet %s : ", vmSet.Name+"-"+strconv.Itoa(i), vmSet.Name))
Expand All @@ -93,8 +97,7 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
// Get labels of the VMSet
labels := vmSet.ObjectMeta.Labels
labels["owner"] = vmSet.Name
vm := &proxmoxv1alpha1.VirtualMachine{}
vm = &proxmoxv1alpha1.VirtualMachine{
vm := &proxmoxv1alpha1.VirtualMachine{
ObjectMeta: ctrl.ObjectMeta{
Name: vmSet.Name + "-" + strconv.Itoa(i),
Namespace: vmSet.Namespace,
Expand Down Expand Up @@ -130,8 +133,11 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
}
} else if len(vmList.Items) > replicas {
vmSet.Status.Condition = "Scaling Down"
r.Status().Update(ctx, vmSet)
var LastConditionTime time.Time
err = r.Status().Update(ctx, vmSet)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
LastConditionTime := time.Now()
if time.Since(LastConditionTime) < 5*time.Second {
return ctrl.Result{}, client.IgnoreNotFound(err)
} else {
Expand All @@ -154,7 +160,6 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
}
}
}
LastConditionTime = time.Now()
} else {
// Do nothing
// log.Log.Info("VMSet has the same number of VMs as replicas")
Expand Down Expand Up @@ -183,6 +188,7 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
if !controllerutil.ContainsFinalizer(vmSet, virtualMachineSetFinalizerName) {
controllerutil.AddFinalizer(vmSet, virtualMachineSetFinalizerName)
if err := r.Update(ctx, vmSet); err != nil {
log.Log.Info(fmt.Sprintf("Error updating VirtualMachineSet %s", vmSet.Name))
}
}
} else {
Expand All @@ -200,6 +206,7 @@ func (r *VirtualMachineSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
client.InNamespace(req.Namespace),
// Change that one to metadata.ownerReference
client.MatchingLabels{"owner": vmSet.Name}); err != nil {
log.Log.Info("Unable to list VMs")
}
// Delete all VMs owned by this VirtualMachineSet
if len(vmListDel.Items) != 0 {
Expand Down
20 changes: 16 additions & 4 deletions internal/controller/proxmox/virtualmachinesnapshot_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,34 @@ func (r *VirtualMachineSnapshotReconciler) Reconcile(ctx context.Context, req ct
// Set the status to created
if StatusCode == 0 {
vmSnapshot.Spec.Timestamp = metav1.Now()
r.Update(ctx, vmSnapshot)
err = r.Update(ctx, vmSnapshot)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
vmSnapshot.Status.Status = "Created"
r.Status().Update(ctx, vmSnapshot)
err = r.Status().Update(ctx, vmSnapshot)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
} else if vmSnapshot.Status.Status == "Created" {
if StatusCode == 2 {
// Snapshot is already created, return
vmSnapshot.Status.ErrorMessage = "Snapshot is already created"
r.Status().Update(ctx, vmSnapshot)
err := r.Status().Update(ctx, vmSnapshot)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
return ctrl.Result{}, nil
} else {
// Snapshot creation failed, return
if StatusCode == 1 {
vmSnapshot.Status.ErrorMessage = "Snapshot creation failed"
r.Status().Update(ctx, vmSnapshot)
err = r.Status().Update(ctx, vmSnapshot)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
return ctrl.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (r *VirtualMachineSnapshotPolicyReconciler) Reconcile(ctx context.Context,
// Append to vmList
virtualMachineList := &proxmoxv1alpha1.VirtualMachineList{}
if err := r.List(ctx, virtualMachineList, client.InNamespace(namespace), client.MatchingLabels(matchingLabels)); err != nil {
log.Log.Error(err, "unable to list VirtualMachines")
}
MatchingVirtualMachines = append(MatchingVirtualMachines, virtualMachineList.Items...)
}
Expand Down
Loading

0 comments on commit 13462b4

Please sign in to comment.