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

[chore] simplify extension code #3640

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions internal/components/extensions/healthcheckv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ import (
)

const (
DefaultHealthcheckV1Path = "/"
DefaultHealthcheckV1Port = 13133
defaultHealthcheckV1Path = "/"
defaultHealthcheckV1Port = 13133
)

type healthcheckV1Config struct {
components.SingleEndpointConfig `mapstructure:",squash"`
Path string `mapstructure:"path"`
}

// HealthCheckV1Probe returns the probe configuration for the healthcheck v1 extension.
// healthCheckV1Probe returns the probe configuration for the healthcheck v1 extension.
// Right now no TLS config is parsed.
func HealthCheckV1Probe(logger logr.Logger, config healthcheckV1Config) (*corev1.Probe, error) {
func healthCheckV1Probe(logger logr.Logger, config healthcheckV1Config) (*corev1.Probe, error) {
path := config.Path
if len(path) == 0 {
path = DefaultHealthcheckV1Path
path = defaultHealthcheckV1Path
}
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: intstr.FromInt32(config.GetPortNumOrDefault(logger, DefaultHealthcheckV1Port)),
Port: intstr.FromInt32(config.GetPortNumOrDefault(logger, defaultHealthcheckV1Port)),
},
},
}, nil
Expand Down
14 changes: 6 additions & 8 deletions internal/components/extensions/healthcheckv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package extensions_test
package extensions

import (
"fmt"
Expand All @@ -22,8 +22,6 @@ import (
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/open-telemetry/opentelemetry-operator/internal/components/extensions"
)

func TestHealthCheckV1Probe(t *testing.T) {
Expand Down Expand Up @@ -66,7 +64,7 @@ func TestHealthCheckV1Probe(t *testing.T) {
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.FromInt32(extensions.DefaultHealthcheckV1Port),
Port: intstr.FromInt32(defaultHealthcheckV1Port),
},
},
},
Expand Down Expand Up @@ -102,7 +100,7 @@ func TestHealthCheckV1Probe(t *testing.T) {
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt32(extensions.DefaultHealthcheckV1Port),
Port: intstr.FromInt32(defaultHealthcheckV1Port),
},
},
},
Expand Down Expand Up @@ -136,7 +134,7 @@ func TestHealthCheckV1Probe(t *testing.T) {
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt32(extensions.DefaultHealthcheckV1Port),
Port: intstr.FromInt32(defaultHealthcheckV1Port),
},
},
},
Expand Down Expand Up @@ -165,7 +163,7 @@ func TestHealthCheckV1Probe(t *testing.T) {
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.FromInt32(extensions.DefaultHealthcheckV1Port),
Port: intstr.FromInt32(defaultHealthcheckV1Port),
},
},
},
Expand All @@ -175,7 +173,7 @@ func TestHealthCheckV1Probe(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := extensions.ParserFor("health_check")
parser := ParserFor("health_check")
got, err := parser.GetLivenessProbe(logr.Discard(), tt.args.config)
if !tt.wantErr(t, err, fmt.Sprintf("GetLivenessProbe(%v)", tt.args.config)) {
return
Expand Down
47 changes: 13 additions & 34 deletions internal/components/extensions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import (
)

// registry holds a record of all known receiver parsers.
var registry = make(map[string]components.Parser)

// Register adds a new parser builder to the list of known builders.
func Register(name string, p components.Parser) {
registry[name] = p
}

// IsRegistered checks whether a parser is registered with the given name.
func IsRegistered(name string) bool {
_, ok := registry[name]
return ok
var registry = map[string]components.Parser{
"health_check": components.NewBuilder[healthcheckV1Config]().
WithName("health_check").
WithPort(13133).
WithReadinessGen(healthCheckV1Probe).
WithLivenessGen(healthCheckV1Probe).
WithPortParser(func(logger logr.Logger, name string, defaultPort *corev1.ServicePort, config healthcheckV1Config) ([]corev1.ServicePort, error) {
return components.ParseSingleEndpointSilent(logger, name, defaultPort, &config.SingleEndpointConfig)
}).
MustBuild(),
"jaeger_query": components.NewSinglePortParserBuilder("jaeger_query", 16686).
WithTargetPort(16686).
MustBuild(),
}

// ParserFor returns a parser builder for the given exporter name.
Expand All @@ -43,26 +45,3 @@ func ParserFor(name string) components.Parser {
// We want the default for exporters to fail silently.
return components.NewBuilder[any]().WithName(name).MustBuild()
}

var (
componentParsers = []components.Parser{
components.NewBuilder[healthcheckV1Config]().
WithName("health_check").
WithPort(13133).
WithReadinessGen(HealthCheckV1Probe).
WithLivenessGen(HealthCheckV1Probe).
WithPortParser(func(logger logr.Logger, name string, defaultPort *corev1.ServicePort, config healthcheckV1Config) ([]corev1.ServicePort, error) {
return components.ParseSingleEndpointSilent(logger, name, defaultPort, &config.SingleEndpointConfig)
}).
MustBuild(),
components.NewSinglePortParserBuilder("jaeger_query", 16686).
WithTargetPort(16686).
MustBuild(),
}
)

func init() {
for _, parser := range componentParsers {
Register(parser.ParserType(), parser)
}
}
19 changes: 9 additions & 10 deletions internal/components/extensions/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package extensions_test
package extensions

import (
"testing"
Expand All @@ -21,13 +21,12 @@ import (
"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/internal/components"
"github.com/open-telemetry/opentelemetry-operator/internal/components/extensions"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

func TestParserForReturns(t *testing.T) {
const testComponentName = "test"
parser := extensions.ParserFor(testComponentName)
parser := ParserFor(testComponentName)
assert.Equal(t, "test", parser.ParserType())
assert.Equal(t, "__test", parser.ParserName())
ports, err := parser.Ports(logr.Discard(), testComponentName, map[string]interface{}{
Expand All @@ -39,9 +38,8 @@ func TestParserForReturns(t *testing.T) {

func TestCanRegister(t *testing.T) {
const testComponentName = "test"
extensions.Register(testComponentName, components.NewSinglePortParserBuilder(testComponentName, 9000).MustBuild())
assert.True(t, extensions.IsRegistered(testComponentName))
parser := extensions.ParserFor(testComponentName)
registry[testComponentName] = components.NewSinglePortParserBuilder(testComponentName, 9000).MustBuild()
parser := ParserFor(testComponentName)
assert.Equal(t, "test", parser.ParserType())
assert.Equal(t, "__test", parser.ParserName())
ports, err := parser.Ports(logr.Discard(), testComponentName, map[string]interface{}{})
Expand All @@ -60,11 +58,12 @@ func TestExtensionsComponentParsers(t *testing.T) {
} {
t.Run(tt.exporterName, func(t *testing.T) {
t.Run("is registered", func(t *testing.T) {
assert.True(t, extensions.IsRegistered(tt.exporterName))
_, ok := registry[tt.exporterName]
assert.True(t, ok)
})
t.Run("bad config errors", func(t *testing.T) {
// prepare
parser := extensions.ParserFor(tt.exporterName)
parser := ParserFor(tt.exporterName)

// test throwing in pure junk
_, err := parser.Ports(logr.Discard(), tt.exporterName, func() {})
Expand All @@ -75,7 +74,7 @@ func TestExtensionsComponentParsers(t *testing.T) {

t.Run("assigns the expected port", func(t *testing.T) {
// prepare
parser := extensions.ParserFor(tt.exporterName)
parser := ParserFor(tt.exporterName)

// test
ports, err := parser.Ports(logr.Discard(), tt.exporterName, map[string]interface{}{})
Expand All @@ -93,7 +92,7 @@ func TestExtensionsComponentParsers(t *testing.T) {

t.Run("allows port to be overridden", func(t *testing.T) {
// prepare
parser := extensions.ParserFor(tt.exporterName)
parser := ParserFor(tt.exporterName)

// test
ports, err := parser.Ports(logr.Discard(), tt.exporterName, map[string]interface{}{
Expand Down
Loading