Skip to content

Commit

Permalink
Better labeling for 3rd party provisioners (#154)
Browse files Browse the repository at this point in the history
* go mod tidy

Signed-off-by: Eamon Bauman <eamon@eamonbauman.com>

* switched to "not internal"

Signed-off-by: Eamon Bauman <eamon@eamonbauman.com>

* changed to use label set instead of "internal"

Signed-off-by: Eamon Bauman <eamon@eamonbauman.com>

* added retry to not compete with 3rd party prov

Signed-off-by: Eamon Bauman <eamon@eamonbauman.com>

* extend provisioner label to static vms

---------

Signed-off-by: Eamon Bauman <eamon@eamonbauman.com>
  • Loading branch information
ebauman authored Aug 29, 2023
1 parent ed48c86 commit bd05307
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 82 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
k8s.io/apimachinery v0.25.2
k8s.io/client-go v12.0.0+incompatible
k8s.io/code-generator v0.25.2
k8s.io/utils v0.0.0-20220922133306-665eaaec4324
)

require (
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
k8s.io/gengo v0.0.0-20211129171323-c02415ce4185 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20220928191237-829ce0c27909 // indirect
k8s.io/utils v0.0.0-20220922133306-665eaaec4324 // indirect
sigs.k8s.io/controller-runtime v0.13.0 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
Expand Down
26 changes: 12 additions & 14 deletions pkg/controllers/tfpcontroller/tfpcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,10 @@ func (t *TerraformProvisionerController) processNextVM() bool {
// returns an error and a boolean of requeue
func (t *TerraformProvisionerController) handleProvision(vm *hfv1.VirtualMachine) (error, bool) {
// VM shall not be provisioned by internal terraform controller
if ! vm.Spec.Provision {
if provisionMethod, ok := vm.ObjectMeta.Labels["hobbyfarm.io/provisioner"]; ok {
if provisionMethod == "external" {
glog.V(8).Infof("vm %s ignored due to external provisioning label", vm.Name)
t.vmWorkqueue.Done(vm.Name)
}
if !vm.Spec.Provision {
if prov, ok := vm.ObjectMeta.Labels["hobbyfarm.io/provisioner"]; ok && prov != "" {
glog.V(8).Infof("vm %s ignored by internal provisioner due to 3rd party provisioning label", vm.Name)
t.vmWorkqueue.Done(vm.Name)
}
glog.V(8).Infof("vm %s was not a provisioned vm", vm.Name)
return nil, false
Expand Down Expand Up @@ -235,18 +233,18 @@ func (t *TerraformProvisionerController) handleProvision(vm *hfv1.VirtualMachine
glog.Errorf("error generating keypair %v", err)
return err, true
}
config := util.GetVMConfig(env,vmt)
config := util.GetVMConfig(env, vmt)

config["name"] = vm.Name
config["public_key"] = pubKey

image, exists := config["image"]
if !exists || image == "" {
if !exists || image == "" {
return fmt.Errorf("image does not exist or is empty in vm config for vmt %s", vmt.Name), true
}

moduleName, exists := config["module"]
if !exists || moduleName == "" {
if !exists || moduleName == "" {
return fmt.Errorf("module name does not exist or is empty in vm config for vmt %s", vmt.Name), true
}

Expand Down Expand Up @@ -297,7 +295,7 @@ func (t *TerraformProvisionerController) handleProvision(vm *hfv1.VirtualMachine
Data: map[string][]byte{
"private_key": []byte(privKey),
"public_key": []byte(pubKey),
"password" : []byte(password),
"password": []byte(password),
},
}

Expand Down Expand Up @@ -348,10 +346,10 @@ func (t *TerraformProvisionerController) handleProvision(vm *hfv1.VirtualMachine

toUpdate.Status.Status = hfv1.VmStatusProvisioned
toUpdate.Status.TFState = tfs.Name

toUpdate, updateErr := t.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).UpdateStatus(t.ctx, toUpdate, metav1.UpdateOptions{})

if(updateErr != nil){
if updateErr != nil {
glog.Errorf("error while updating VirtualMachine status %s", toUpdate.Name)
return updateErr
}
Expand All @@ -362,7 +360,7 @@ func (t *TerraformProvisionerController) handleProvision(vm *hfv1.VirtualMachine

toUpdate, updateErr = t.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).Update(t.ctx, toUpdate, metav1.UpdateOptions{})

if(updateErr != nil){
if updateErr != nil {
glog.Errorf("error while updating VirtualMachine %s", toUpdate.Name)
return updateErr
}
Expand Down Expand Up @@ -465,7 +463,7 @@ func (t *TerraformProvisionerController) handleProvision(vm *hfv1.VirtualMachine

toUpdate, updateErr := t.hfClientSet.HobbyfarmV1().VirtualMachines(util.GetReleaseNamespace()).Update(t.ctx, toUpdate, metav1.UpdateOptions{})

if(updateErr != nil){
if updateErr != nil {
glog.Errorf("error while updating machine: %s", toUpdate.Name)
return updateErr
}
Expand Down
Loading

0 comments on commit bd05307

Please sign in to comment.