Skip to content

Commit

Permalink
Merge pull request #24 from HeeSeoung/master
Browse files Browse the repository at this point in the history
Aviator-Service 개발 완료에 따른 기능 연동
  • Loading branch information
HeeSeoung authored Jan 3, 2024
2 parents e35a5d2 + afee6a9 commit 9a55037
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 28 deletions.
4 changes: 4 additions & 0 deletions api/v1/provision_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type NetworkInterface struct {

// ProvisionSpec defines the desired state of Provision
type ProvisionSpec struct {
RegionCode string `json:"regionCode,omitempty"`
ServerInstanceNo string `json:"serverInstanceNo,omitempty"`
ServerNo string `json:"serverInstanceNoList.1,omitempty"`
AccessControlGroupNoListN string `json:"accessControlGroupNoList,omitempty"`
AssociateWithPublicIp bool `json:"associateWithPublicIp,omitempty"`
BlockDevicePartitionMountPoint string `json:"blockDevicePartitionMountPoint,omitempty"`
Expand All @@ -65,6 +68,7 @@ type ProvisionSpec struct {
SubnetNo string `json:"subnetNo,omitempty"`
VpcNo string `json:"vpcNo,omitempty"`
Server Server `json:"server,omitempty"`
Phase ProvisionPhase `json:"phase,omitempty"`
BlockStorageMapping BlockStorageMapping `json:"blockStorageMapping,omitempty"`
NetworkInterface NetworkInterface `json:"networkInterface,omitempty"`
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ func main() {
os.Exit(1)
}

err = (controller.NewProvisionReconciler(mgr.GetClient(), mgr.GetScheme(), &pkg.NcpService{})).SetupWithManager(mgr)
err = (controller.NewProvisionReconciler(
mgr.GetClient(),
mgr.GetScheme(),
&pkg.NcpService{Server: pkg.NewServerService("6CmrDJ4KaswJ10g25GEP", "OvZ7QHH0Bi3AwGn5rlsD7xoC986bEOiIjdbwMFCo")},
)).SetupWithManager(mgr)
if err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Provision")
os.Exit(1)
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/vm.cloudclub.io_provisions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ spec:
networkInterfaceSubnetNo:
type: string
type: object
phase:
type: string
placementGroupNo:
type: string
raidTypeName:
type: string
regionCode:
type: string
responseFormatType:
type: string
server:
Expand All @@ -105,6 +109,10 @@ spec:
serverSpecCode:
type: string
type: object
serverInstanceNo:
type: string
serverInstanceNoList.1:
type: string
subnetNo:
type: string
vpcNo:
Expand Down
14 changes: 14 additions & 0 deletions config/samples/create_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: vm.cloudclub.io/v1
kind: Provision
metadata:
name: provision-sample
spec:
phase: "Create"
server:
serverImageProductCode: "SW.VSVR.OS.LNX64.CNTOS.0703.B050"
serverProductCode: "SVR.VSVR.HICPU.C002.M004.NET.HDD.B050.G002"
vpcNo: "52833"
subnetNo: "120320"
networkInterface:
networkInterfaceList: 0
accessControlGroupNoList: "148207"
7 changes: 7 additions & 0 deletions config/samples/delete_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: vm.cloudclub.io/v1
kind: Provision
metadata:
name: provision-sample
spec:
phase: "Delete"
serverInstanceNoList.1: '21836952'
7 changes: 7 additions & 0 deletions config/samples/get_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: vm.cloudclub.io/v1
kind: Provision
metadata:
name: provision-sample
spec:
phase: "Get"
regionCode: "KR"
7 changes: 7 additions & 0 deletions config/samples/stop_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: vm.cloudclub.io/v1
kind: Provision
metadata:
name: provision-sample
spec:
phase: "Stop"
serverInstanceNoList.1: '21836952'
9 changes: 9 additions & 0 deletions config/samples/update_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: vm.cloudclub.io/v1
kind: Provision
metadata:
name: provision-sample
spec:
phase: "Update"
server:
serverProductCode: "SVR.VSVR.STAND.C032.M128.NET.HDD.B050.G002"
serverInstanceNoList.1: '21836952'
2 changes: 1 addition & 1 deletion internal/controller/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const (
apiUrlStop = "https://ncloud.apigw.ntruss.com/vserver/v2/stopServerInstances"
apiUrlUpdate = "https://ncloud.apigw.ntruss.com/vserver/v2/changeServerInstanceSpec"
// error level
ErrorLevelIsInfo = 0
ErrorLevelIsFatal = 1
ErrorLevelIsAnError = 2
ErrorLevelIsWarn = 3
ErrorLevelIsInfo = 4
ErrorLevelIsDebug = 5
ErrorLevelIsTrace = 6
)
75 changes: 49 additions & 26 deletions internal/controller/provision_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controller

import (
"context"
"fmt"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -26,11 +28,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

ncputil "github.com/cloud-club/Aviator-service/pkg"
server "github.com/cloud-club/Aviator-service/types/server"

vmv1 "vm.cloudclub.io/api/v1"
)

var provisionReconcileMap map[string]func(*ProvisionReconciler, logr.Logger, string, interface{}) error
var provisionReconcileMap map[string]func(*ProvisionReconciler, logr.Logger, string, *vmv1.Provision, interface{}) error

// ProvisionReconciler reconciles a Provision object
type ProvisionReconciler struct {
Expand All @@ -42,8 +45,8 @@ type ProvisionReconciler struct {
func NewProvisionReconciler(
client client.Client,
scheme *runtime.Scheme,
ncpService *ncputil.NcpService) *ProvisionReconciler {

ncpService *ncputil.NcpService,
) *ProvisionReconciler {
initProvisionReconcileMap()
return &ProvisionReconciler{
Client: client,
Expand All @@ -53,7 +56,7 @@ func NewProvisionReconciler(
}

func initProvisionReconcileMap() {
provisionReconcileMap = make(map[string]func(*ProvisionReconciler, logr.Logger, string, interface{}) error)
provisionReconcileMap = make(map[string]func(*ProvisionReconciler, logr.Logger, string, *vmv1.Provision, interface{}) error)
provisionReconcileMap["provision"] = provision
provisionReconcileMap["deProvision"] = deProvision
provisionReconcileMap["update"] = update
Expand Down Expand Up @@ -89,45 +92,45 @@ func (r *ProvisionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

switch original.Status.Phase {
switch original.Spec.Phase {
case "", vmv1.ProvisionPhaseCreate:
if v, ok := provisionReconcileMap["provision"]; ok {
if err = v(r, log, apiUrlCreate, nil); err != nil {
if err = v(r, log, apiUrlCreate, original, nil); err != nil {
log.Error(err, "Failed to create VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseUpdate:
if v, ok := provisionReconcileMap["update"]; ok {
if err = v(r, log, apiUrlUpdate, nil); err != nil {
if err = v(r, log, apiUrlUpdate, original, nil); err != nil {
log.Error(err, "Failed to update VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseStop:
if v, ok := provisionReconcileMap["stop"]; ok {
if err = v(r, log, apiUrlStop, nil); err != nil {
if err = v(r, log, apiUrlStop, original, nil); err != nil {
log.Error(err, "Failed to stop VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseDelete:
if v, ok := provisionReconcileMap["deProvision"]; ok {
if err = v(r, log, apiUrlDelete, nil); err != nil {
if err = v(r, log, apiUrlDelete, original, nil); err != nil {
log.Error(err, "Failed to delete VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseGet:
if v, ok := provisionReconcileMap["get"]; ok {
if err = v(r, log, apiUrlGet, nil); err != nil {
if err = v(r, log, apiUrlGet, original, nil); err != nil {
log.Error(err, "Failed to get VM information")
return ctrl.Result{}, err
}
}
default:
log.V(ErrorLevelIsAnError).Error(err, "No action defined for the current phase",
"reconcile phase", original.Status.Phase, "namespace", req.NamespacedName)
"reconcile phase", original.Spec.Phase, "namespace", req.NamespacedName)
return ctrl.Result{}, err
}

Expand All @@ -141,32 +144,52 @@ func (r *ProvisionReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func provision(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
func provision(r *ProvisionReconciler, log logr.Logger, url string, original *vmv1.Provision, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Creating a new VM")
//return r.ncpService.Server.Create(url, payload)
return nil
csr := &server.CreateServerRequest{
ServerImageProductCode: original.Spec.Server.ImageProductCode,
VpcNo: original.Spec.VpcNo,
SubnetNo: original.Spec.SubnetNo,
NetworkInterfaceOrder: original.Spec.NetworkInterface.Order,
AccessControlGroupNoListN: original.Spec.AccessControlGroupNoListN,
ServerProductCode: original.Spec.Server.ProductCode,
}
createServerResponse, err := r.ncpService.Server.Create(ncputil.API_URL+ncputil.CREATE_SERVER_INSTANCE_PATH, csr, []int{1, 1})
fmt.Println(createServerResponse)
return err
}

func deProvision(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
func deProvision(r *ProvisionReconciler, log logr.Logger, url string, original *vmv1.Provision, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Deleting an existing VM")
//return r.ncpService.Server.Delete(url)
return nil
dsr := &server.DeleteServerRequest{ServerNo: original.Spec.ServerNo}
deleteServerResponse, err := r.ncpService.Server.Delete(ncputil.API_URL+ncputil.DELETE_SERVER_INSTANCE_PATH, dsr)
fmt.Println(deleteServerResponse)
return err
}

func update(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
func update(r *ProvisionReconciler, log logr.Logger, url string, original *vmv1.Provision, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Updating an existing VM")
//return r.ncpService.Server.Update(url)
return nil
usr := &server.UpdateServerRequest{
ServerInstanceNo: original.Spec.ServerInstanceNo,
ServerProductCode: original.Spec.Server.ProductCode,
}
updateServerResponse, err := r.ncpService.Server.Update(ncputil.API_URL+ncputil.UPDATE_SERVER_INSTANCE_PATH, usr)
fmt.Println(updateServerResponse)
return err
}

func stop(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
func stop(r *ProvisionReconciler, log logr.Logger, url string, original *vmv1.Provision, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Stopping an existing VM")
//return r.ncpService.Server.Stop(url)
return nil
ssr := &server.StopServerRequest{ServerNo: original.Spec.ServerNo}
stopServerResponse, err := r.ncpService.Server.Stop(ncputil.API_URL+ncputil.STOP_SERVER_INSTANCE_PATH, ssr)
fmt.Println(stopServerResponse)
return err
}

func get(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
func get(r *ProvisionReconciler, log logr.Logger, url string, original *vmv1.Provision, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Getting information for an existing VM")
return nil
//return r.ncpService.Server.Get(url)
lsr := &server.ListServerRequest{RegionCode: original.Spec.RegionCode}
serverListResponse, err := r.ncpService.Server.List(ncputil.API_URL+ncputil.GET_SERVER_INSTANCE_PATH, lsr)
fmt.Println(serverListResponse)
return err
}

0 comments on commit 9a55037

Please sign in to comment.