Merge pull request #2275 from ArielLima/vm-actions-refactor

DRY implementation to our admin_vm_actions
This commit is contained in:
Ben Vesel 2022-08-22 14:29:54 -04:00 коммит произвёл GitHub
Родитель 942c65d56b 3cf59afef3
Коммит cae9508907
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 73 добавлений и 123 удалений

Просмотреть файл

@ -12,8 +12,6 @@ import (
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)
@ -21,40 +19,16 @@ func (f *frontend) postAdminOpenShiftClusterRedeployVM(w http.ResponseWriter, r
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)
err := f._postAdminOpenShiftClusterRedeployVM(ctx, r, log)
err := f._postAdminOpenShiftClusterRedeployVM(log, ctx, r)
adminReply(log, w, nil, nil, err)
}
func (f *frontend) _postAdminOpenShiftClusterRedeployVM(ctx context.Context, r *http.Request, log *logrus.Entry) error {
func (f *frontend) _postAdminOpenShiftClusterRedeployVM(log *logrus.Entry, ctx context.Context, r *http.Request) error {
vars := mux.Vars(r)
vmName := r.URL.Query().Get("vmName")
err := validateAdminVMName(vmName)
azActionsWrapper, err := f.newAzureActionsWrapper(log, ctx, vmName, strings.TrimPrefix(r.URL.Path, "/admin"), vars)
if err != nil {
return err
}
resourceID := strings.TrimPrefix(r.URL.Path, "/admin")
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s' under resource group '%s' was not found.", vars["resourceType"], vars["resourceName"], vars["resourceGroupName"])
case err != nil:
return err
}
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key)
if err != nil {
return err
}
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc)
if err != nil {
return err
}
return a.VMRedeployAndWait(ctx, vmName)
return f.adminAction.VMRedeployAndWait(ctx, azActionsWrapper.vmName)
}

Просмотреть файл

@ -12,8 +12,6 @@ import (
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)
@ -21,40 +19,17 @@ func (f *frontend) postAdminOpenShiftClusterStartVM(w http.ResponseWriter, r *ht
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)
err := f._postAdminOpenShiftClusterStartVM(ctx, r, log)
err := f._postAdminOpenShiftClusterStartVM(log, ctx, r)
adminReply(log, w, nil, nil, err)
}
func (f *frontend) _postAdminOpenShiftClusterStartVM(ctx context.Context, r *http.Request, log *logrus.Entry) error {
func (f *frontend) _postAdminOpenShiftClusterStartVM(log *logrus.Entry, ctx context.Context, r *http.Request) error {
vars := mux.Vars(r)
vmName := r.URL.Query().Get("vmName")
err := validateAdminVMName(vmName)
azActionsWrapper, err := f.newAzureActionsWrapper(log, ctx, vmName, strings.TrimPrefix(r.URL.Path, "/admin"), vars)
if err != nil {
return err
}
resourceID := strings.TrimPrefix(r.URL.Path, "/admin")
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s' under resource group '%s' was not found.", vars["resourceType"], vars["resourceName"], vars["resourceGroupName"])
case err != nil:
return err
}
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key)
if err != nil {
return err
}
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc)
if err != nil {
return err
}
return a.VMStartAndWait(ctx, vmName)
return f.adminAction.VMStartAndWait(ctx, azActionsWrapper.vmName)
}

Просмотреть файл

@ -12,8 +12,6 @@ import (
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)
@ -21,40 +19,17 @@ func (f *frontend) postAdminOpenShiftClusterStopVM(w http.ResponseWriter, r *htt
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)
err := f._postAdminOpenShiftClusterStopVM(ctx, r, log)
err := f._postAdminOpenShiftClusterStopVM(log, ctx, r)
adminReply(log, w, nil, nil, err)
}
func (f *frontend) _postAdminOpenShiftClusterStopVM(ctx context.Context, r *http.Request, log *logrus.Entry) error {
func (f *frontend) _postAdminOpenShiftClusterStopVM(log *logrus.Entry, ctx context.Context, r *http.Request) error {
vars := mux.Vars(r)
vmName := r.URL.Query().Get("vmName")
err := validateAdminVMName(vmName)
azActionsWrapper, err := f.newAzureActionsWrapper(log, ctx, vmName, strings.TrimPrefix(r.URL.Path, "/admin"), vars)
if err != nil {
return err
}
resourceID := strings.TrimPrefix(r.URL.Path, "/admin")
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s' under resource group '%s' was not found.", vars["resourceType"], vars["resourceName"], vars["resourceGroupName"])
case err != nil:
return err
}
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key)
if err != nil {
return err
}
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc)
if err != nil {
return err
}
return a.VMStopAndWait(ctx, vmName)
return f.adminAction.VMStopAndWait(ctx, azActionsWrapper.vmName)
}

Просмотреть файл

@ -17,7 +17,6 @@ import (
kruntime "k8s.io/apimachinery/pkg/runtime"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)
@ -25,34 +24,14 @@ func (f *frontend) postAdminOpenShiftClusterVMResize(w http.ResponseWriter, r *h
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)
err := f._postAdminOpenShiftClusterVMResize(ctx, r, log)
err := f._postAdminOpenShiftClusterVMResize(log, ctx, r)
adminReply(log, w, nil, nil, err)
}
func (f *frontend) _postAdminOpenShiftClusterVMResize(ctx context.Context, r *http.Request, log *logrus.Entry) error {
func (f *frontend) _postAdminOpenShiftClusterVMResize(log *logrus.Entry, ctx context.Context, r *http.Request) error {
vars := mux.Vars(r)
vmName := r.URL.Query().Get("vmName")
err := validateAdminVMName(vmName)
if err != nil {
return err
}
resourceID := strings.TrimPrefix(r.URL.Path, "/admin")
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "",
"The Resource '%s/%s' under resource group '%s' was not found.",
vars["resourceType"], vars["resourceName"], vars["resourceGroupName"])
case err != nil:
return err
}
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key)
azActionsWrapper, err := f.newAzureActionsWrapper(log, ctx, vmName, strings.TrimPrefix(r.URL.Path, "/admin"), vars)
if err != nil {
return err
}
@ -63,12 +42,7 @@ func (f *frontend) _postAdminOpenShiftClusterVMResize(ctx context.Context, r *ht
return err
}
a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc)
if err != nil {
return err
}
k, err := f.kubeActionsFactory(log, f.env, doc.OpenShiftCluster)
k, err := f.kubeActionsFactory(log, f.env, azActionsWrapper.doc.OpenShiftCluster)
if err != nil {
return err
}
@ -95,7 +69,7 @@ func (f *frontend) _postAdminOpenShiftClusterVMResize(ctx context.Context, r *ht
continue
}
if strings.EqualFold(vmName, node.ObjectMeta.Name) {
if strings.EqualFold(azActionsWrapper.vmName, node.ObjectMeta.Name) {
nodeExists = true
break
}
@ -104,8 +78,8 @@ func (f *frontend) _postAdminOpenShiftClusterVMResize(ctx context.Context, r *ht
if !nodeExists {
return api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "",
`"The master node '%s' under resource group '%s' was not found."`,
vmName, vars["resourceGroupName"])
azActionsWrapper.vmName, vars["resourceGroupName"])
}
return a.VMResize(ctx, vmName, vmSize)
return f.adminAction.VMResize(ctx, azActionsWrapper.vmName, vmSize)
}

Просмотреть файл

@ -24,9 +24,7 @@ func (f *frontend) getAdminOpenShiftClusterVMResizeOptions(w http.ResponseWriter
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)
b, err := f._getAdminOpenShiftClusterVMResizeOptions(ctx, r, log)
adminReply(log, w, nil, b, err)
}

Просмотреть файл

@ -0,0 +1,53 @@
package frontend
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
)
type azVmActionsWrapper struct {
vmName string
doc *api.OpenShiftClusterDocument
}
func (f *frontend) newAzureActionsWrapper(log *logrus.Entry, ctx context.Context, vmName, resourceID string, vars map[string]string) (azVmActionsWrapper, error) {
err := validateAdminVMName(vmName)
if err != nil {
return azVmActionsWrapper{}, err
}
if err != nil {
return azVmActionsWrapper{}, err
}
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return azVmActionsWrapper{}, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "",
"The Resource '%s/%s' under resource group '%s' was not found.",
vars["resourceType"], vars["resourceName"], vars["resourceGroupName"])
case err != nil:
return azVmActionsWrapper{}, err
}
subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key)
if err != nil {
return azVmActionsWrapper{}, err
}
f.adminAction, err = f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc)
if err != nil {
return azVmActionsWrapper{}, err
}
return azVmActionsWrapper{
vmName: vmName,
doc: doc,
}, nil
}

Просмотреть файл

@ -60,6 +60,7 @@ type frontend struct {
kubeActionsFactory kubeActionsFactory
azureActionsFactory azureActionsFactory
ocEnricherFactory ocEnricherFactory
adminAction adminactions.AzureActions
l net.Listener
s *http.Server