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

Add GRPC + Rest endpoint to retreive VM Config, Inject and Inherit Secrets and Configmaps to environment config #204

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
580 changes: 370 additions & 210 deletions v3/protos/vm/vm.pb.go

Large diffs are not rendered by default.

35 changes: 23 additions & 12 deletions v3/protos/vm/vm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "google/protobuf/timestamp.proto";
service VMSvc {
rpc CreateVM (CreateVMRequest) returns (google.protobuf.Empty);
rpc GetVM (general.GetRequest) returns (VM);
rpc GetVMConfig (GetVMConfigRequest) returns (VMConfig);
rpc UpdateVM (UpdateVMRequest) returns (google.protobuf.Empty);
rpc UpdateVMStatus (UpdateVMStatusRequest) returns (google.protobuf.Empty);
rpc DeleteVM (general.ResourceId) returns (google.protobuf.Empty);
Expand All @@ -37,20 +38,30 @@ message VM {
google.protobuf.Timestamp deletion_timestamp = 15;
}

message VMConfig {
map<string, string> config = 1;
}

message GetVMConfigRequest {
string id = 1;
google.protobuf.BoolValue withSecrets = 2;
}

message CreateVMRequest {
string id = 1;
string vm_template_id = 2;
string ssh_username = 3;
string protocol = 4;
string secret_name = 5;
string vm_claim_id = 6;
string vm_claim_uid = 7;
string user = 8;
bool provision = 9;
string vm_set_id = 10;
string vm_set_uid = 11;
map<string, string> labels = 12;
repeated string finalizers = 13;
string environment_id = 2;
string vm_template_id = 3;
string ssh_username = 4;
string protocol = 5;
string secret_name = 6;
string vm_claim_id = 7;
string vm_claim_uid = 8;
string user = 9;
bool provision = 10;
string vm_set_id = 11;
string vm_set_uid = 12;
map<string, string> labels = 13;
repeated string finalizers = 14;
}

message UpdateVMRequest {
Expand Down
37 changes: 37 additions & 0 deletions v3/protos/vm/vm_grpc.pb.go

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

2 changes: 1 addition & 1 deletion v3/services/environmentsvc/internal/environmentservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (e EnvironmentServer) ListFunc(w http.ResponseWriter, r *http.Request) {
for _, e := range environmentList.GetEnvironments() {
keys := make(map[string]map[string]string)
for k := range util.ConvertMapStruct(e.GetTemplateMapping(), util.GetRawStringMap) {
keys[k] = map[string]string{} // reset template mapping entries -> @TODO: Figure out why?
keys[k] = map[string]string{} // reset template mapping entries to avoid leaking configuration but keep the ability to see what VMTs are allowed on this environment
}
preparedEnvironments = append(preparedEnvironments, PreparedListEnvironment{e.GetId(), e.GetDisplayName(), e.GetProvider(), keys})
}
Expand Down
43 changes: 13 additions & 30 deletions v3/services/terraformsvc/internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
vmpb "github.com/hobbyfarm/gargantua/v3/protos/vm"
vmclaimpb "github.com/hobbyfarm/gargantua/v3/protos/vmclaim"
vmsetpb "github.com/hobbyfarm/gargantua/v3/protos/vmset"
vmtemplatepb "github.com/hobbyfarm/gargantua/v3/protos/vmtemplate"
k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -47,7 +46,6 @@ type VMController struct {
terraformClient *GrpcTerraformServer
vmClaimClient vmclaimpb.VMClaimSvcClient
vmSetClient vmsetpb.VMSetSvcClient
vmTemplateClient vmtemplatepb.VMTemplateSvcClient
}

func NewVMController(
Expand All @@ -59,7 +57,6 @@ func NewVMController(
terraformClient *GrpcTerraformServer,
vmClaimClient vmclaimpb.VMClaimSvcClient,
vmSetClient vmsetpb.VMSetSvcClient,
vmTemplateClient vmtemplatepb.VMTemplateSvcClient,
ctx context.Context,
) (*VMController, error) {
kubeClient.CoreV1().ConfigMaps("")
Expand All @@ -83,7 +80,6 @@ func NewVMController(
terraformClient: terraformClient,
vmClaimClient: vmClaimClient,
vmSetClient: vmSetClient,
vmTemplateClient: vmTemplateClient,
}
vmController.SetReconciler(vmController)
vmController.SetWorkScheduler(vmController)
Expand Down Expand Up @@ -219,48 +215,35 @@ func (v *VMController) updateAndVerifyVMDeletion(vm *vmpb.VM) (error, bool) {
func (v *VMController) handleProvision(vm *vmpb.VM) (error, bool) {
//Status is ReadyForProvisioning AND No Secret provided (Do not provision VM twice, happens due to vm.status being updated after vm.status)
if vm.Status.Status == string(hfv1.VmStatusRFP) {
vmt, err := v.vmTemplateClient.GetVMTemplate(v.Context, &generalpb.GetRequest{Id: vm.GetVmTemplateId(), LoadFromCache: true})
if err != nil {
glog.Errorf("error getting vmt %v", err)
return err, true
}
env, err := v.environmentClient.GetEnvironment(v.Context, &generalpb.GetRequest{Id: vm.GetStatus().GetEnvironmentId(), LoadFromCache: true})
if err != nil {
glog.Errorf("error getting env %v", err)
return err, true
}

_, exists := env.GetTemplateMapping()[vmt.GetId()]
if !exists {
glog.Errorf("error pulling environment template info %v", err)
// @TODO: Why do we requeue here??? This will fail for each iteration as long as the environment is not updated...
return fmt.Errorf("Error during RFP: environment %s does not support vmt %s.", env.GetId(), vmt.GetId()), true
}

// let's provision the vm
pubKey, privKey, err := util.GenKeyPair()
if err != nil {
glog.Errorf("error generating keypair %v", err)
return err, true
}
config := util.GetVMConfig(env, vmt)
vmConfig, err := v.VMClient.GetVMConfig(v.Context, &vmpb.GetVMConfigRequest{Id: vm.Id, WithSecrets: &wrapperspb.BoolValue{Value: true}})
if err != nil {
glog.Errorf("error getting VM config %v", err)
return err, true
}
config := vmConfig.Config

config["name"] = vm.GetId()
config["public_key"] = pubKey

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

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

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

password, exists := config["password"]
Expand All @@ -280,7 +263,7 @@ func (v *VMController) handleProvision(vm *vmpb.VM) (error, bool) {
r := fmt.Sprintf("%08x", rand.Uint32())
cm := &k8sv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Join([]string{vm.GetId() + "-cm", r}, "-"),
Name: strings.Join([]string{vm.GetId(), "cm", r}, "-"),
OwnerReferences: vmOwnerReference,
},
Data: config,
Expand All @@ -294,7 +277,7 @@ func (v *VMController) handleProvision(vm *vmpb.VM) (error, bool) {

keypair := &k8sv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Join([]string{vm.GetId() + "-secret", r}, "-"),
Name: strings.Join([]string{vm.GetId(), "secret", r}, "-"),
OwnerReferences: vmOwnerReference,
},
Data: map[string][]byte{
Expand Down Expand Up @@ -346,9 +329,9 @@ func (v *VMController) handleProvision(vm *vmpb.VM) (error, bool) {

var updatedFinalizers []string
if vm.GetFinalizers() != nil {
updatedFinalizers = append(vm.GetFinalizers(), "vm.controllers.hobbyfarm.io")
updatedFinalizers = append(vm.GetFinalizers(), "terraform.controllers.hobbyfarm.io")
} else {
updatedFinalizers = []string{"vm.controllers.hobbyfarm.io"}
updatedFinalizers = []string{"terraform.controllers.hobbyfarm.io"}
}
_, err = v.VMClient.UpdateVM(v.Context, &vmpb.UpdateVMRequest{
Id: vm.GetId(),
Expand Down
2 changes: 1 addition & 1 deletion v3/services/terraformsvc/internal/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *GrpcTerraformServer) CreateState(ctx context.Context, req *terraformpb.
}

random := fmt.Sprintf("%08x", rand.Uint32())
id := strings.Join([]string{vmId + "-tfs", random}, "-")
id := strings.Join([]string{vmId, "tfs", random}, "-")

tfs := &tfv1.State{
ObjectMeta: metav1.ObjectMeta{
Expand Down
3 changes: 0 additions & 3 deletions v3/services/terraformsvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
vmpb "github.com/hobbyfarm/gargantua/v3/protos/vm"
vmclaimpb "github.com/hobbyfarm/gargantua/v3/protos/vmclaim"
vmsetpb "github.com/hobbyfarm/gargantua/v3/protos/vmset"
vmtemplatepb "github.com/hobbyfarm/gargantua/v3/protos/vmtemplate"
)

var (
Expand Down Expand Up @@ -55,7 +54,6 @@ func main() {
vmClaimClient := vmclaimpb.NewVMClaimSvcClient(connections[microservices.VMClaim])
vmClient := vmpb.NewVMSvcClient(connections[microservices.VM])
vmSetClient := vmsetpb.NewVMSetSvcClient(connections[microservices.VMSet])
vmTemplateClient := vmtemplatepb.NewVMTemplateSvcClient(connections[microservices.VMTemplate])

gs := microservices.CreateGRPCServer(serviceConfig.ServerCert.Clone())
ts := terraformservice.NewGrpcTerraformServer(hfClient, hfInformerFactory)
Expand All @@ -70,7 +68,6 @@ func main() {
ts,
vmClaimClient,
vmSetClient,
vmTemplateClient,
ctx,
)

Expand Down
17 changes: 9 additions & 8 deletions v3/services/vmclaimsvc/internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,15 @@ func (v *VMClaimController) submitVirtualMachines(vmc *vmclaimpb.VMClaim) (err e
environment := environmentMap[vmName].Environment
dbc := environmentMap[vmName].DynamicBindConfiguration
vm := &vmpb.CreateVMRequest{
Id: genName,
VmTemplateId: vmDetails.Template,
Protocol: "ssh",
SecretName: "",
VmClaimId: vmc.GetId(),
VmClaimUid: vmc.GetUid(),
User: vmc.GetUserId(),
Provision: true,
Id: genName,
EnvironmentId: environment.Id,
VmTemplateId: vmDetails.Template,
Protocol: "ssh",
SecretName: "",
VmClaimId: vmc.GetId(),
VmClaimUid: vmc.GetUid(),
User: vmc.GetUserId(),
Provision: true,
Labels: map[string]string{
"dynamic": "true",
"vmc": vmc.GetId(),
Expand Down
23 changes: 12 additions & 11 deletions v3/services/vmsetsvc/internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,18 @@ func (v *VMSetController) reconcileVirtualMachineSet(vmset *vmsetpb.VMSet) error
}

_, err := v.vmClient.CreateVM(v.Context, &vmpb.CreateVMRequest{
Id: vmName,
VmTemplateId: vmt.GetId(),
SshUsername: sshUser,
Protocol: protocol,
SecretName: "",
User: "",
Provision: provision,
VmSetId: vmset.GetId(),
VmSetUid: vmset.GetUid(),
Labels: vmLabels,
Finalizers: []string{vmSetFinalizer},
Id: vmName,
EnvironmentId: env.GetId(),
VmTemplateId: vmt.GetId(),
SshUsername: sshUser,
Protocol: protocol,
SecretName: "",
User: "",
Provision: provision,
VmSetId: vmset.GetId(),
VmSetUid: vmset.GetUid(),
Labels: vmLabels,
Finalizers: []string{vmSetFinalizer},
})

if err != nil {
Expand Down
Loading
Loading