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

Improve probe parsing #3250

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use it
  • Loading branch information
jaronoff97 committed Aug 7, 2024
commit 43c8a4838ef659172579a1c8ce47a666e82b576d
36 changes: 35 additions & 1 deletion apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/go-logr/logr"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"

"github.com/open-telemetry/opentelemetry-operator/internal/components"
"github.com/open-telemetry/opentelemetry-operator/internal/components/exporters"
Expand Down Expand Up @@ -139,9 +140,38 @@ type Config struct {
Service Service `json:"service" yaml:"service"`
}

// getRbacRulesForComponentKinds gets the RBAC Rules for the given ComponentKind(s).
func (c *Config) getRbacRulesForComponentKinds(logger logr.Logger, componentKinds ...ComponentKind) ([]rbacv1.PolicyRule, error) {
var rules []rbacv1.PolicyRule
enabledComponents := c.GetEnabledComponents()
for _, componentKind := range componentKinds {
var retriever components.ParserRetriever
var cfg AnyConfig
switch componentKind {
case KindReceiver:
retriever = receivers.ReceiverFor
cfg = c.Receivers
case KindExporter:
retriever = exporters.ParserFor
cfg = c.Exporters
case KindProcessor:
break
}
for componentName := range enabledComponents[componentKind] {
// TODO: Clean up the naming here and make it simpler to use a retriever.
parser := retriever(componentName)
if parsedRules, err := parser.GetRBACRules(logger, cfg.Object[componentName]); err != nil {
return nil, err
} else {
rules = append(rules, parsedRules...)
}
}
}
return rules, nil
}

// getPortsForComponentKinds gets the ports for the given ComponentKind(s).
func (c *Config) getPortsForComponentKinds(logger logr.Logger, componentKinds ...ComponentKind) ([]corev1.ServicePort, error) {

var ports []corev1.ServicePort
enabledComponents := c.GetEnabledComponents()
for _, componentKind := range componentKinds {
Expand Down Expand Up @@ -187,6 +217,10 @@ func (c *Config) GetAllPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter)
}

func (c *Config) GetAllRbacRules(logger logr.Logger) ([]rbacv1.PolicyRule, error) {
return c.getRbacRulesForComponentKinds(logger, KindReceiver, KindExporter, KindProcessor)
}

// Yaml encodes the current object and returns it as a string.
func (c *Config) Yaml() (string, error) {
var buf bytes.Buffer
Expand Down
26 changes: 4 additions & 22 deletions internal/manifests/collector/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/adapters"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

func ClusterRole(params manifests.Params) (*rbacv1.ClusterRole, error) {
confStr, err := params.OtelCol.Spec.Config.Yaml()
rules, err := params.OtelCol.Spec.Config.GetAllRbacRules(params.Log)
if err != nil {
return nil, err
}

configFromString, err := adapters.ConfigFromString(confStr)
if err != nil {
params.Log.Error(err, "couldn't extract the configuration from the context")
return nil, nil
}
rules := adapters.ConfigToRBAC(params.Log, configFromString)

if len(rules) == 0 {
} else if len(rules) == 0 {
return nil, nil
}

Expand All @@ -60,18 +50,10 @@ func ClusterRole(params manifests.Params) (*rbacv1.ClusterRole, error) {
}

func ClusterRoleBinding(params manifests.Params) (*rbacv1.ClusterRoleBinding, error) {
confStr, err := params.OtelCol.Spec.Config.Yaml()
rules, err := params.OtelCol.Spec.Config.GetAllRbacRules(params.Log)
if err != nil {
return nil, err
}
configFromString, err := adapters.ConfigFromString(confStr)
if err != nil {
params.Log.Error(err, "couldn't extract the configuration from the context")
return nil, nil
}
rules := adapters.ConfigToRBAC(params.Log, configFromString)

if len(rules) == 0 {
} else if len(rules) == 0 {
return nil, nil
}

Expand Down
Loading