Show error when editing tenant Yaml (#733)
* Show error when editing tenant Yaml Also adds error line when we prepareError() Signed-off-by: Daniel Valdivia <hola@danielvaldivia.com> * make certain metadata fields editable Signed-off-by: Daniel Valdivia <hola@danielvaldivia.com>
This commit is contained in:
@@ -1888,14 +1888,6 @@ func getTenantYAML(session *models.Principal, params admin_api.GetTenantYAMLPara
|
||||
}
|
||||
|
||||
func getUpdateTenantYAML(session *models.Principal, params admin_api.PutTenantYAMLParams) *models.Error {
|
||||
|
||||
// can we parse the inbound?
|
||||
//var inTenant miniov2.Tenant
|
||||
//err := yaml.Unmarshal([]byte(params.Body.Yaml), &inTenant)
|
||||
//if err != nil {
|
||||
// return prepareError(err)
|
||||
//}
|
||||
|
||||
// https://godoc.org/k8s.io/apimachinery/pkg/runtime#Scheme
|
||||
scheme := runtime.NewScheme()
|
||||
|
||||
@@ -1907,7 +1899,7 @@ func getUpdateTenantYAML(session *models.Principal, params admin_api.PutTenantYA
|
||||
|
||||
tenantObject, _, err := deserializer.Decode([]byte(params.Body.Yaml), nil, &miniov2.Tenant{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return &models.Error{Code: 400, Message: swag.String(err.Error())}
|
||||
}
|
||||
inTenant := tenantObject.(*miniov2.Tenant)
|
||||
// get Kubernetes Client
|
||||
@@ -1921,12 +1913,15 @@ func getUpdateTenantYAML(session *models.Principal, params admin_api.PutTenantYA
|
||||
return prepareError(err)
|
||||
}
|
||||
upTenant := tenant.DeepCopy()
|
||||
// only replace the spec field
|
||||
// only update safe fields: spec, metadata.finalizers, metadata.labels and metadata.annotations
|
||||
upTenant.Labels = inTenant.Labels
|
||||
upTenant.Annotations = inTenant.Annotations
|
||||
upTenant.Finalizers = inTenant.Finalizers
|
||||
upTenant.Spec = inTenant.Spec
|
||||
|
||||
_, err = opClient.MinioV2().Tenants(params.Namespace).Update(params.HTTPRequest.Context(), upTenant, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return prepareError(err)
|
||||
return &models.Error{Code: 400, Message: swag.String(err.Error())}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -3,6 +3,8 @@ package restapi
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/go-openapi/swag"
|
||||
"github.com/minio/console/models"
|
||||
@@ -45,7 +47,9 @@ func prepareError(err ...error) *models.Error {
|
||||
errorCode := int32(500)
|
||||
errorMessage := errorGeneric.Error()
|
||||
if len(err) > 0 {
|
||||
log.Print("original error: ", err[0].Error())
|
||||
frame := getFrame(2)
|
||||
fileParts := strings.Split(frame.File, "/")
|
||||
log.Printf("%s:%d: original error: %s", fileParts[len(fileParts)-1], frame.Line, err[0].Error())
|
||||
if k8sErrors.IsUnauthorized(err[0]) {
|
||||
errorCode = 401
|
||||
errorMessage = errorGenericUnauthorized.Error()
|
||||
@@ -157,3 +161,26 @@ func prepareError(err ...error) *models.Error {
|
||||
}
|
||||
return &models.Error{Code: errorCode, Message: swag.String(errorMessage)}
|
||||
}
|
||||
|
||||
func getFrame(skipFrames int) runtime.Frame {
|
||||
// We need the frame at index skipFrames+2, since we never want runtime.Callers and getFrame
|
||||
targetFrameIndex := skipFrames + 2
|
||||
|
||||
// Set size to targetFrameIndex+2 to ensure we have room for one more caller than we need
|
||||
programCounters := make([]uintptr, targetFrameIndex+2)
|
||||
n := runtime.Callers(0, programCounters)
|
||||
|
||||
frame := runtime.Frame{Function: "unknown"}
|
||||
if n > 0 {
|
||||
frames := runtime.CallersFrames(programCounters[:n])
|
||||
for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ {
|
||||
var frameCandidate runtime.Frame
|
||||
frameCandidate, more = frames.Next()
|
||||
if frameIndex == targetFrameIndex {
|
||||
frame = frameCandidate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return frame
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user