diff --git a/api/v1/provision_types.go b/api/v1/provision_types.go index 97eeaf1..7c0753e 100644 --- a/api/v1/provision_types.go +++ b/api/v1/provision_types.go @@ -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"` @@ -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"` } diff --git a/cmd/main.go b/cmd/main.go index 28544a5..488bb36 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) diff --git a/config/crd/bases/vm.cloudclub.io_provisions.yaml b/config/crd/bases/vm.cloudclub.io_provisions.yaml index dc672d2..716643a 100644 --- a/config/crd/bases/vm.cloudclub.io_provisions.yaml +++ b/config/crd/bases/vm.cloudclub.io_provisions.yaml @@ -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: @@ -105,6 +109,10 @@ spec: serverSpecCode: type: string type: object + serverInstanceNo: + type: string + serverInstanceNoList.1: + type: string subnetNo: type: string vpcNo: diff --git a/config/samples/create_test.yaml b/config/samples/create_test.yaml new file mode 100644 index 0000000..d853677 --- /dev/null +++ b/config/samples/create_test.yaml @@ -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" \ No newline at end of file diff --git a/config/samples/delete_test.yaml b/config/samples/delete_test.yaml new file mode 100644 index 0000000..4720e59 --- /dev/null +++ b/config/samples/delete_test.yaml @@ -0,0 +1,7 @@ +apiVersion: vm.cloudclub.io/v1 +kind: Provision +metadata: + name: provision-sample +spec: + phase: "Delete" + serverInstanceNoList.1: '21836952' \ No newline at end of file diff --git a/config/samples/get_test.yaml b/config/samples/get_test.yaml new file mode 100644 index 0000000..f8e3092 --- /dev/null +++ b/config/samples/get_test.yaml @@ -0,0 +1,7 @@ +apiVersion: vm.cloudclub.io/v1 +kind: Provision +metadata: + name: provision-sample +spec: + phase: "Get" + regionCode: "KR" \ No newline at end of file diff --git a/config/samples/stop_test.yaml b/config/samples/stop_test.yaml new file mode 100644 index 0000000..b15389f --- /dev/null +++ b/config/samples/stop_test.yaml @@ -0,0 +1,7 @@ +apiVersion: vm.cloudclub.io/v1 +kind: Provision +metadata: + name: provision-sample +spec: + phase: "Stop" + serverInstanceNoList.1: '21836952' diff --git a/config/samples/update_test.yaml b/config/samples/update_test.yaml new file mode 100644 index 0000000..29d818f --- /dev/null +++ b/config/samples/update_test.yaml @@ -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' \ No newline at end of file diff --git a/internal/controller/constants.go b/internal/controller/constants.go index 18b2c71..8dd363e 100644 --- a/internal/controller/constants.go +++ b/internal/controller/constants.go @@ -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 ) diff --git a/internal/controller/provision_controller.go b/internal/controller/provision_controller.go index c6f39d7..cd09f86 100644 --- a/internal/controller/provision_controller.go +++ b/internal/controller/provision_controller.go @@ -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" @@ -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 { @@ -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, @@ -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 @@ -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 } @@ -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 }