Files
my-rook-config/pkg/operator/ceph/object/admin.go
T
Satoru Takeuchi a89d35e5b6 ceph: fix improper json parsing in radosgw-admin
Sometimes `radosgw-admin` succeeds after showing logs to stderr. We should
skip non-json strings if parsing output as json.

Here is an example.

```
2021-02-26 04:10:44.190418 I | op-bucket-prov: creating Ceph user "ceph-user-aSzNqgE7"
E0226 04:11:37.901310       8 controller.go:199] error syncing 'logging/loki-bucket': error provisioning bucket: Provision: can't create ceph user: error creating ceph user "ceph-user-aSzNqgE7": failed to unmarshal json. 2021-02-26T04:11:21.425+0000 7f6714be4980  1 robust_notify: If at first you don't succeed: (110) Connection timed out
2021-02-26T04:11:21.426+0000 7f6714be4980  0 ERROR: failed to distribute cache for ceph-hdd-object-store.rgw.meta:users.uid:ceph-user-aSzNqgE7
2021-02-26T04:11:32.168+0000 7f6714be4980  1 robust_notify: If at first you don't succeed: (110) Connection timed out
2021-02-26T04:11:32.168+0000 7f6714be4980  0 ERROR: failed to distribute cache for ceph-hdd-object-store.rgw.meta:users.keys:23Z8GUEXR0TJDO86PSJR
{
    "user_id": "ceph-user-aSzNqgE7",
    "display_name": "ceph-user-aSzNqgE7",
...
    "mfa_ids": []
}: invalid character '-' after top-level value: failed to unmarshal json.
```

In this case, some logs like "robust_notify:..." was shown in stderr.
Unmarsharing was failed due to tried to parse these logs as json.

Signed-off-by: Satoru Takeuchi <satoru.takeuchi@gmail.com>
2021-03-10 16:06:56 +00:00

118 lines
4.2 KiB
Go

/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package object
import (
"fmt"
"regexp"
"time"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/client"
)
// Context holds the context for the object store.
type Context struct {
Context *clusterd.Context
clusterInfo *client.ClusterInfo
Name string
UID string
Endpoint string
Realm string
ZoneGroup string
Zone string
}
// NewContext creates a new object store context.
func NewContext(context *clusterd.Context, clusterInfo *client.ClusterInfo, name string) *Context {
return &Context{Context: context, Name: name, clusterInfo: clusterInfo}
}
func NewMultisiteContext(context *clusterd.Context, clusterInfo *client.ClusterInfo, store *cephv1.CephObjectStore) (*Context, error) {
objContext := &Context{Context: context, Name: store.Name, clusterInfo: clusterInfo}
realmName, zoneGroupName, zoneName, err := getMultisiteForObjectStore(context, store)
if err != nil {
return nil, errors.Wrapf(err, "failed to get realm/zone group/zone for object store %q", store.Name)
}
objContext.Realm = realmName
objContext.ZoneGroup = zoneGroupName
objContext.Zone = zoneName
return objContext, nil
}
func extractJSON(output string) (string, error) {
// `radosgw-admin` sometimes leaves logs to stderr even if it succeeds.
// So we should skip them if parsing output as json.
pattern := regexp.MustCompile("(?ms)^{.*")
match := pattern.Find([]byte(output))
if match == nil {
return "", errors.Errorf("didn't contain json. %s", output)
}
return string(match), nil
}
// RunAdminCommandNoMultisite is for running radosgw-admin commands in scenarios where an object-store has not been created yet or for commands on the realm or zonegroup (ex: radosgw-admin zonegroup get)
// This function times out after a fixed interval if no response is received.
func RunAdminCommandNoMultisite(c *Context, expectJSON bool, args ...string) (string, error) {
command, args := client.FinalizeCephCommandArgs("radosgw-admin", c.clusterInfo, args, c.Context.ConfigDir)
timeout, err := time.ParseDuration(fmt.Sprintf("%ss", client.CephConnectionTimeout))
if err != nil {
return "", errors.Wrap(err, "failed to parse CephConnectionTimeout")
}
// start the rgw admin command
output, err := c.Context.Executor.ExecuteCommandWithTimeout(timeout, command, args...)
if err != nil {
return output, err
}
if expectJSON {
match, err := extractJSON(output)
if err != nil {
return output, errors.Wrap(err, "failed to parse as JSON")
}
output = match
}
return output, nil
}
// This function is for running radosgw-admin commands in scenarios where an object-store has been created and the Context has been updated with the appropriate realm, zone group, and zone.
func runAdminCommand(c *Context, expectJSON bool, args ...string) (string, error) {
// If the objectStoreName is not passed in the storage class
// This means we are pointing to an external cluster so these commands are not needed
// simply because the external cluster mode does not support that yet
//
// The following conditions tries to determine if the cluster is external
// When connecting to an external cluster, the Ceph user is different than client.admin
// This is not perfect though since "client.admin" is somehow supported...
if c.Name != "" && c.clusterInfo.CephCred.Username == client.AdminUsername {
options := []string{
fmt.Sprintf("--rgw-realm=%s", c.Realm),
fmt.Sprintf("--rgw-zonegroup=%s", c.ZoneGroup),
fmt.Sprintf("--rgw-zone=%s", c.Zone),
}
return RunAdminCommandNoMultisite(c, expectJSON, append(args, options...)...)
}
return RunAdminCommandNoMultisite(c, expectJSON, args...)
}