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

Cluster edit(patch) and delete #15

Merged
merged 13 commits into from
Aug 4, 2017
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ ifndef HAS_GLIDE
brew install glide
endif
ifndef HAS_SWAGGER
brew tap go-swagger/go-swagger
brew install go-swagger
endif
2 changes: 1 addition & 1 deletion pkg/api/handlers/show_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type showCluster struct {

func (d *showCluster) Handle(params operations.ShowClusterParams, principal *models.Principal) middleware.Responder {
var tprCluster tprv1.Kluster
if err := d.rt.Clients.TPRClient().Get().Namespace("kubernikus").Resource(tprv1.KlusterResourcePlural).LabelsSelectorParam(accountSelector(principal)).Name(params.Name).Do().Into(&tprCluster); err != nil {
if err := d.rt.Clients.TPRClient().Get().Namespace("kubernikus").Resource(tprv1.KlusterResourcePlural).LabelsSelectorParam(accountSelector(principal)).Name(qualifiedName(params.Name,principal.Account)).Do().Into(&tprCluster); err != nil {
if apierrors.IsNotFound(err) {
return operations.NewShowClusterDefault(404).WithPayload(modelsError(err))
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/api/handlers/terminate_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handlers

import (
"github.com/go-openapi/runtime/middleware"
"github.com/sapcc/kubernikus/pkg/api"
"github.com/sapcc/kubernikus/pkg/api/models"
"github.com/sapcc/kubernikus/pkg/api/rest/operations"

tprv1 "github.com/sapcc/kubernikus/pkg/tpr/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

func NewTerminateCluster(rt *api.Runtime) operations.TerminateClusterHandler {
return &terminateCluster{rt: rt}
}

type terminateCluster struct {
rt *api.Runtime
}

func (d *terminateCluster) Handle(params operations.TerminateClusterParams, principal *models.Principal) middleware.Responder {

_,err := editCluster(d.rt.Clients.TPRClient(),principal,params.Name,func(kluster *tprv1.Kluster){
kluster.Status.State = tprv1.KlusterTerminating
kluster.Status.Message = "Cluster terminating"
})
if err != nil {
if apierrors.IsNotFound(err) {
return operations.NewTerminateClusterDefault(404).WithPayload(modelsError(err))
}
return operations.NewTerminateClusterDefault(0).WithPayload(modelsError(err))
}
return operations.NewTerminateClusterOK()
}
33 changes: 33 additions & 0 deletions pkg/api/handlers/update_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package handlers

import (
"github.com/go-openapi/runtime/middleware"
"github.com/sapcc/kubernikus/pkg/api"
"github.com/sapcc/kubernikus/pkg/api/models"
"github.com/sapcc/kubernikus/pkg/api/rest/operations"

tprv1 "github.com/sapcc/kubernikus/pkg/tpr/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

func NewUpdateCluster(rt *api.Runtime) operations.UpdateClusterHandler {
return &updateCluster{rt: rt}
}

type updateCluster struct {
rt *api.Runtime
}

func (d *updateCluster) Handle(params operations.UpdateClusterParams, principal *models.Principal) middleware.Responder {

_, err := editCluster(d.rt.Clients.TPRClient(), principal, params.Name, func(kluster *tprv1.Kluster) {
//TODO: currently no field to update
})
if err != nil {
if apierrors.IsNotFound(err) {
return operations.NewUpdateClusterDefault(404).WithPayload(modelsError(err))
}
return operations.NewUpdateClusterDefault(0).WithPayload(modelsError(err))
}
return operations.NewUpdateClusterOK()
}
28 changes: 28 additions & 0 deletions pkg/api/handlers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"github.com/go-openapi/swag"
"github.com/sapcc/kubernikus/pkg/api/models"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"

"fmt"
tprv1 "github.com/sapcc/kubernikus/pkg/tpr/v1"
"strings"
)

func modelsError(err error) *models.Error {
Expand All @@ -15,3 +20,26 @@ func modelsError(err error) *models.Error {
func accountSelector(principal *models.Principal) labels.Selector {
return labels.SelectorFromSet(map[string]string{"account": principal.Account})
}

// qualifiedName returns <cluster_name>-<account_id>
func qualifiedName(name string, accountId string) string {
if strings.Contains(name, accountId) {
return name
}
return fmt.Sprintf("%s-%s", name, accountId)
}

func editCluster(tprClient *rest.RESTClient, principal *models.Principal, name string, updateFunc func(k *tprv1.Kluster)) (*tprv1.Kluster, error) {
var kluster, updatedCluster tprv1.Kluster
if err := tprClient.Get().Namespace("kubernikus").Resource(tprv1.KlusterResourcePlural).LabelsSelectorParam(accountSelector(principal)).Name(qualifiedName(name, principal.Account)).Do().Into(&kluster); err != nil {
return nil, err
}

updateFunc(&kluster)

if err := tprClient.Put().Body(&kluster).Namespace("kubernikus").Resource(tprv1.KlusterResourcePlural).LabelsSelectorParam(accountSelector(principal)).Name(qualifiedName(name, principal.Account)).Do().Into(&updatedCluster); err != nil {
return nil, err
}
return &updatedCluster, nil

}
2 changes: 2 additions & 0 deletions pkg/api/rest/configure_kubernikus.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func configureAPI(api *operations.KubernikusAPI) http.Handler {
api.ListClustersHandler = handlers.NewListClusters(rt)
api.CreateClusterHandler = handlers.NewCreateCluster(rt)
api.ShowClusterHandler = handlers.NewShowCluster(rt)
api.TerminateClusterHandler = handlers.NewTerminateCluster(rt)
api.UpdateClusterHandler = handlers.NewUpdateCluster(rt)

api.ServerShutdown = func() {}

Expand Down
57 changes: 57 additions & 0 deletions pkg/api/rest/embedded_spec.go

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

28 changes: 28 additions & 0 deletions pkg/api/rest/operations/kubernikus_api.go

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

73 changes: 73 additions & 0 deletions pkg/api/rest/operations/terminate_cluster.go

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

Loading