Skip to content

Commit

Permalink
Refactor/provision (#23)
Browse files Browse the repository at this point in the history
* refactor based on strategy pattern

* update

---------

Co-authored-by: kim.doyoung <[email protected]>
  • Loading branch information
dev-kimdoyoung and kim.doyoung authored Dec 31, 2023
1 parent 6a77fbd commit de5f30e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 49 deletions.
1 change: 1 addition & 0 deletions api/v1/provision_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const (
ProvisionPhaseStop ProvisionPhase = "Stop"
ProvisionPhaseDelete ProvisionPhase = "Delete"
ProvisionPhaseGet ProvisionPhase = "Get"
JobIsSuccess ProvisionPhase = "Success"
)

// ProvisionStatus defines the observed state of Provision
Expand Down
8 changes: 3 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"github.com/cloud-club/Aviator-service/pkg"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -44,7 +45,6 @@ var (

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(vmv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
Expand Down Expand Up @@ -89,10 +89,8 @@ func main() {
os.Exit(1)
}

if err = (&controller.ProvisionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
err = (controller.NewProvisionReconciler(mgr.GetClient(), mgr.GetScheme(), &pkg.NcpService{})).SetupWithManager(mgr)
if err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Provision")
os.Exit(1)
}
Expand Down
9 changes: 1 addition & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ module vm.cloudclub.io

go 1.21.1

toolchain go1.21.4

require (
github.com/cloud-club/Aviator-service v0.0.0-20231217151335-e0cb2c2047aa
github.com/googleapis/gax-go/v2 v2.12.0
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/sirupsen/logrus v1.9.0
google.golang.org/appengine v1.6.7
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/controller-runtime v0.16.3
Expand Down Expand Up @@ -64,9 +59,7 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ const (
apiUrlGet = "https://ncloud.apigw.ntruss.com/vserver/v2/getMemberServerImageInstanceList"
apiUrlStop = "https://ncloud.apigw.ntruss.com/vserver/v2/stopServerInstances"
apiUrlUpdate = "https://ncloud.apigw.ntruss.com/vserver/v2/changeServerInstanceSpec"

ErrorLevelIsFatal = 1
ErrorLevelIsAnError = 2
ErrorLevelIsWarn = 3
ErrorLevelIsInfo = 4
ErrorLevelIsDebug = 5
ErrorLevelIsTrace = 6
)
107 changes: 71 additions & 36 deletions internal/controller/provision_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package controller

import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -30,6 +30,8 @@ import (
vmv1 "vm.cloudclub.io/api/v1"
)

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

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

initProvisionReconcileMap()
return &ProvisionReconciler{
Client: client,
Scheme: scheme,
ncpService: ncpService,
}
}

func initProvisionReconcileMap() {
provisionReconcileMap = make(map[string]func(*ProvisionReconciler, logr.Logger, string, interface{}) error)
provisionReconcileMap["provision"] = provision
provisionReconcileMap["deProvision"] = deProvision
provisionReconcileMap["update"] = update
provisionReconcileMap["get"] = get
provisionReconcileMap["stop"] = stop
}

//+kubebuilder:rbac:groups=vm.cloudclub.io,resources=provisions,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=vm.cloudclub.io,resources=provisions/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=vm.cloudclub.io,resources=provisions/finalizers,verbs=update
Expand All @@ -65,13 +77,13 @@ func NewProvisionReconciler(
func (r *ProvisionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)

log.V(0).Info("Reconciling Provision request", "Request", req)
log.V(ErrorLevelIsInfo).Info("Reconciling Provision request", "Request", req)

original := &vmv1.Provision{}
err := r.Get(ctx, req.NamespacedName, original)
if err != nil {
if errors.IsNotFound(err) {
log.V(0).Info("Provision resource not found. Ignoring reconciliation.")
log.V(ErrorLevelIsInfo).Info("Provision resource not found. Ignoring reconciliation.")
return ctrl.Result{}, nil
}
log.Error(err, "Failed to get Provision resource")
Expand All @@ -80,47 +92,44 @@ func (r *ProvisionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

switch original.Status.Phase {
case "", vmv1.ProvisionPhaseCreate:
log.V(0).Info("Creating a new VM")
// create
err := r.ncpService.Server.Create(apiUrlCreate)
if err != nil {
log.Error(err, "Failed to create VM")
return ctrl.Result{}, err
if v, ok := provisionReconcileMap["provision"]; ok {
if err = v(r, log, apiUrlCreate, nil); err != nil {
log.Error(err, "Failed to create VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseUpdate:
// update
log.V(0).Info("Updating an existing VM")
err := r.ncpService.Server.Update(apiUrlUpdate)
if err != nil {
log.Error(err, "Failed to update VM")
return ctrl.Result{}, err
if v, ok := provisionReconcileMap["update"]; ok {
if err = v(r, log, apiUrlUpdate, nil); err != nil {
log.Error(err, "Failed to update VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseStop:
// delete
log.V(0).Info("Stopping an existing VM")
err := r.ncpService.Server.Stop(apiUrlDelete)
if err != nil {
log.Error(err, "Failed to stop VM")
return ctrl.Result{}, err
if v, ok := provisionReconcileMap["stop"]; ok {
if err = v(r, log, apiUrlStop, nil); err != nil {
log.Error(err, "Failed to stop VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseDelete:
// delete
log.V(0).Info("Deleting an existing VM")
err := r.ncpService.Server.Delete(apiUrlDelete)
if err != nil {
log.Error(err, "Failed to delete VM")
return ctrl.Result{}, err
if v, ok := provisionReconcileMap["deProvision"]; ok {
if err = v(r, log, apiUrlDelete, nil); err != nil {
log.Error(err, "Failed to delete VM")
return ctrl.Result{}, err
}
}
case vmv1.ProvisionPhaseGet:
// get info
log.V(0).Info("Getting information for an existing VM")
err := r.ncpService.Server.Get(apiUrlGet)
if err != nil {
log.Error(err, "Failed to get VM information")
return ctrl.Result{}, err
if v, ok := provisionReconcileMap["get"]; ok {
if err = v(r, log, apiUrlGet, nil); err != nil {
log.Error(err, "Failed to get VM information")
return ctrl.Result{}, err
}
}
default:
log.V(0).Info("No action defined for the current phase")
log.V(ErrorLevelIsAnError).Error(err, "No action defined for the current phase",
"reconcile phase", original.Status.Phase, "namespace", req.NamespacedName)
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
Expand All @@ -132,3 +141,29 @@ func (r *ProvisionReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&vmv1.Provision{}).
Complete(r)
}

func provision(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Creating a new VM")
return r.ncpService.Server.Create(url, payload)
}

func deProvision(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Deleting an existing VM")
return r.ncpService.Server.Delete(url)
}

func update(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Updating an existing VM")
return r.ncpService.Server.Update(url)
}

func stop(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Stopping an existing VM")
//return r.ncpService.Server.Stop(url)
return nil
}

func get(r *ProvisionReconciler, log logr.Logger, url string, payload interface{}) error {
log.V(ErrorLevelIsInfo).Info("Getting information for an existing VM")
return r.ncpService.Server.Get(url)
}

0 comments on commit de5f30e

Please sign in to comment.