-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use options correctly, remove duplication (#3215)
* Refactor to use options correctly, remove duplication * comment and export * remove unnecessary option * rename methods, add test * comment
- Loading branch information
1 parent
c97fcb5
commit f02ae89
Showing
9 changed files
with
261 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// 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 components | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/mitchellh/mapstructure" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
var ( | ||
_ Parser = &GenericParser[SingleEndpointConfig]{} | ||
) | ||
|
||
// GenericParser serves as scaffolding for custom parsing logic by isolating | ||
// functionality to idempotent functions. | ||
type GenericParser[T any] struct { | ||
name string | ||
portParser PortParser[T] | ||
option *Option | ||
} | ||
|
||
func (g *GenericParser[T]) Ports(logger logr.Logger, name string, config interface{}) ([]corev1.ServicePort, error) { | ||
if g.portParser == nil { | ||
return nil, nil | ||
} | ||
var parsed T | ||
if err := mapstructure.Decode(config, &parsed); err != nil { | ||
return nil, err | ||
} | ||
return g.portParser(logger, name, g.option, parsed) | ||
} | ||
|
||
func (g *GenericParser[T]) ParserType() string { | ||
return ComponentType(g.name) | ||
} | ||
|
||
func (g *GenericParser[T]) ParserName() string { | ||
return fmt.Sprintf("__%s", g.name) | ||
} | ||
|
||
func NewGenericParser[T any](name string, port int32, parser PortParser[T], opts ...PortBuilderOption) *GenericParser[T] { | ||
o := NewOption(name, port) | ||
o.Apply(opts...) | ||
return &GenericParser[T]{name: name, portParser: parser, option: o} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// 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 components_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/internal/components" | ||
) | ||
|
||
func TestGenericParser_GetPorts(t *testing.T) { | ||
type args struct { | ||
logger logr.Logger | ||
config interface{} | ||
} | ||
type testCase[T any] struct { | ||
name string | ||
g *components.GenericParser[T] | ||
args args | ||
want []corev1.ServicePort | ||
wantErr assert.ErrorAssertionFunc | ||
} | ||
|
||
tests := []testCase[*components.SingleEndpointConfig]{ | ||
{ | ||
name: "valid config with endpoint", | ||
g: components.NewGenericParser[*components.SingleEndpointConfig]("test", 0, components.ParseSingleEndpoint), | ||
args: args{ | ||
logger: logr.Discard(), | ||
config: map[string]interface{}{ | ||
"endpoint": "http://localhost:8080", | ||
}, | ||
}, | ||
want: []corev1.ServicePort{ | ||
{ | ||
Name: "test", | ||
Port: 8080, | ||
}, | ||
}, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "valid config with listen_address", | ||
g: components.NewGenericParser[*components.SingleEndpointConfig]("test", 0, components.ParseSingleEndpoint), | ||
args: args{ | ||
logger: logr.Discard(), | ||
config: map[string]interface{}{ | ||
"listen_address": "0.0.0.0:9090", | ||
}, | ||
}, | ||
want: []corev1.ServicePort{ | ||
{ | ||
Name: "test", | ||
Port: 9090, | ||
}, | ||
}, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "valid config with listen_address with option", | ||
g: components.NewGenericParser[*components.SingleEndpointConfig]("test", 0, components.ParseSingleEndpoint, components.WithProtocol(corev1.ProtocolUDP)), | ||
args: args{ | ||
logger: logr.Discard(), | ||
config: map[string]interface{}{ | ||
"listen_address": "0.0.0.0:9090", | ||
}, | ||
}, | ||
want: []corev1.ServicePort{ | ||
{ | ||
Name: "test", | ||
Port: 9090, | ||
Protocol: corev1.ProtocolUDP, | ||
}, | ||
}, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "invalid config with no endpoint or listen_address", | ||
g: components.NewGenericParser[*components.SingleEndpointConfig]("test", 0, components.ParseSingleEndpoint), | ||
args: args{ | ||
logger: logr.Discard(), | ||
config: map[string]interface{}{}, | ||
}, | ||
want: []corev1.ServicePort{}, | ||
wantErr: assert.Error, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.g.Ports(tt.args.logger, "test", tt.args.config) | ||
if !tt.wantErr(t, err, fmt.Sprintf("GetRBACRules(%v, %v)", tt.args.logger, tt.args.config)) { | ||
return | ||
} | ||
assert.Equalf(t, tt.want, got, "GetRBACRules(%v, %v)", tt.args.logger, tt.args.config) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.