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

Support namespace ExtendedOptions in cluster spec #282

Merged
merged 5 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ linters:
# New line required before return would require a large fraction of the
# code base to need updating, it's not worth the perceived benefit.
- nlreturn
# Opinionated and sometimes wrong.
- paralleltest
disable-all: false
presets:
# bodyclose, errcheck, gosec, govet, scopelint, staticcheck, typecheck
Expand Down
36 changes: 36 additions & 0 deletions pkg/apis/m3dboperator/v1alpha1/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package v1alpha1

import "encoding/json"

// Namespace defines an M3DB namespace or points to a preset M3DB namespace.
type Namespace struct {
// Name is the namespace name.
Expand Down Expand Up @@ -98,6 +100,37 @@ type DownsampleOptions struct {
All bool `json:"all,omitempty"`
}

// DynamicOption is an option the type of which is only known at run time.
type DynamicOption interface {
json.Marshaler
DeepCopyDynamicOption() DynamicOption
}

type dynamicOption struct {
o interface{}
}

// NewDynamicOption creates a new DynamicOption.
func NewDynamicOption(value interface{}) DynamicOption {
return &dynamicOption{value}
}

func (o *dynamicOption) DeepCopyDynamicOption() DynamicOption {
// We can fake DeepCopy by resorting to shallow copy here because
// within this service dynamic options are effectively immutable.
return o
}

func (o *dynamicOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.o)
}

// ExtendedOptions stores the extended namespace options.
type ExtendedOptions struct {
Type string `json:"type,omitempty"`
Options map[string]DynamicOption `json:"options,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen interface types in Kubernetes API types before. I wonder if having this be map[string]json.RawMessage would give you the same benefits (auto-generated DeepCopy implementation, more control over serialization) while being more Kubernetes-friendly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @schallert , json.RawMessage really makes the implementation much simpler. Updated.

}

// NamespaceOptions defines parameters for an M3DB namespace. See
// https://m3db.github.io/m3/operational_guide/namespace_configuration/ for more
// details.
Expand Down Expand Up @@ -131,4 +164,7 @@ type NamespaceOptions struct {

// AggregationOptions sets the aggregation parameters.
AggregationOptions AggregationOptions `json:"aggregationOptions,omitempty"`

// ExtendedOptions stores the extended namespace options.
ExtendedOptions *ExtendedOptions `json:"extendedOptions,omitempty"`
}
45 changes: 45 additions & 0 deletions pkg/apis/m3dboperator/v1alpha1/namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExtendedOptionsDeepCopy(t *testing.T) {
extOpts := &ExtendedOptions{
Type: "testType",
Options: map[string]DynamicOption{
"key1": NewDynamicOption("abc"),
"key2": NewDynamicOption(123),
"key3": NewDynamicOption(map[string]interface{}{
"subKey1": NewDynamicOption("foo"),
"subKey2": NewDynamicOption("bar"),
}),
},
}

optsCopy := extOpts.DeepCopy()

assert.Equal(t, extOpts, optsCopy)
}
32 changes: 32 additions & 0 deletions pkg/apis/m3dboperator/v1alpha1/zz_generated.deepcopy.go

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

40 changes: 40 additions & 0 deletions pkg/m3admin/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
package namespace

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"time"

myspec "github.com/m3db/m3db-operator/pkg/apis/m3dboperator/v1alpha1"

"github.com/gogo/protobuf/jsonpb"
pbtypes "github.com/gogo/protobuf/types"
m3ns "github.com/m3db/m3/src/dbnode/generated/proto/namespace"
"github.com/m3db/m3/src/query/generated/proto/admin"
)
Expand Down Expand Up @@ -95,6 +99,11 @@ func m3dbNamespaceOptsFromSpec(opts *myspec.NamespaceOptions) (*m3ns.NamespaceOp
return nil, err
}

extOpts, err := m3dbExtendedOptsFromSpec(opts.ExtendedOptions)
if err != nil {
return nil, err
}

return &m3ns.NamespaceOptions{
BootstrapEnabled: opts.BootstrapEnabled,
FlushEnabled: opts.FlushEnabled,
Expand All @@ -106,6 +115,7 @@ func m3dbNamespaceOptsFromSpec(opts *myspec.NamespaceOptions) (*m3ns.NamespaceOp
IndexOptions: indexOpts,
ColdWritesEnabled: opts.ColdWritesEnabled,
AggregationOptions: aggOpts,
ExtendedOptions: extOpts,
}, nil
}

Expand Down Expand Up @@ -192,3 +202,33 @@ func m3dbAggregationOptsFromSpec(opts myspec.AggregationOptions) (*m3ns.Aggregat
}, nil

}

func m3dbExtendedOptsFromSpec(opts *myspec.ExtendedOptions) (*m3ns.ExtendedOptions, error) {
if opts == nil {
return nil, nil
}

pbStruct, err := pbStructFromSpec(opts.Options)
if err != nil {
return nil, err
}

return &m3ns.ExtendedOptions{
Type: opts.Type,
Options: pbStruct,
}, nil
}

func pbStructFromSpec(opts map[string]myspec.DynamicOption) (*pbtypes.Struct, error) {
jsonBytes, err := json.Marshal(opts)
if err != nil {
return nil, err
}

pbStruct := &pbtypes.Struct{}
if err := jsonpb.Unmarshal(bytes.NewReader(jsonBytes), pbStruct); err != nil {
return nil, err
}

return pbStruct, nil
}
Loading