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

Automate the creation of the permissions needed by resourcedetection #2394

Merged
merged 30 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cb4771a
Automate the creation of the permissions requested by resourcedetection
iblancasa Nov 28, 2023
c566e26
Add changelog
iblancasa Nov 28, 2023
d90814d
Fix merge
iblancasa Nov 28, 2023
d6444ab
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Dec 4, 2023
c695885
Apply changes requested in code review
iblancasa Dec 4, 2023
bae77d2
Merge branch 'main' into task/2393
iblancasa Dec 4, 2023
a977949
Fix lint
iblancasa Dec 5, 2023
921b6eb
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Dec 5, 2023
7aa647a
Merge branch 'task/2393' of github.com:iblancasa/opentelemetry-operat…
iblancasa Dec 5, 2023
6bd7d5f
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Dec 18, 2023
10f9ea2
Add feature gate and test
iblancasa Dec 18, 2023
b3243d2
Merge branch 'main' into task/2393
iblancasa Dec 18, 2023
23531b6
Merge branch 'main' into task/2393
iblancasa Dec 19, 2023
ac325d2
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Dec 20, 2023
1e70aea
Add unit tests
iblancasa Dec 20, 2023
be7042a
Merge branch 'task/2393' of github.com:iblancasa/opentelemetry-operat…
iblancasa Dec 20, 2023
4ef9f74
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Jan 2, 2024
d872607
Apply feedback from pull request
iblancasa Jan 2, 2024
461cd29
Merge branch 'main' into task/2393
iblancasa Jan 4, 2024
af31a85
Merge branch 'main' into task/2393
iblancasa Jan 4, 2024
70badae
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Jan 4, 2024
78acfa0
Merge branch 'task/2393' of github.com:iblancasa/opentelemetry-operat…
iblancasa Jan 8, 2024
ddd8194
Apply changes requested as part of the Pull Request
iblancasa Jan 8, 2024
d83b683
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Jan 8, 2024
29f4ca6
Merge branch 'main' into task/2393
iblancasa Jan 9, 2024
40ee3ef
Merge branch 'main' into task/2393
iblancasa Jan 10, 2024
9fd612d
Merge branch 'task/2393' of github.com:iblancasa/opentelemetry-operat…
iblancasa Jan 11, 2024
c99f53f
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Jan 11, 2024
a3d9d95
Apply changes requested as part of the Pull Request
iblancasa Jan 11, 2024
132328b
Merge branch 'main' into task/2393
iblancasa Jan 11, 2024
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
16 changes: 16 additions & 0 deletions .chloggen/2393-automate-permissions-resourcedetection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Automate the creation of the permissions needed by the resourcedetection processor

# One or more tracking issues related to the change
issues: [2393]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ metadata:
categories: Logging & Tracing,Monitoring
certified: "false"
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
createdAt: "2023-11-17T13:24:32Z"
createdAt: "2023-11-28T11:01:16Z"
description: Provides the OpenTelemetry components, including the Collector
operators.operatorframework.io/builder: operator-sdk-v1.29.0
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
Expand Down Expand Up @@ -221,6 +221,15 @@ spec:
- patch
- update
- watch
- apiGroups:
- config.openshift.io
resources:
- infrastructures
- infrastructures/status
verbs:
- get
- list
- watch
- apiGroups:
- coordination.k8s.io
resources:
Expand Down Expand Up @@ -328,6 +337,19 @@ spec:
- patch
- update
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
- clusterrolebindings
- clusterroles
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- route.openshift.io
resources:
Expand Down
22 changes: 22 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ rules:
- patch
- update
- watch
- apiGroups:
- config.openshift.io
resources:
- infrastructures
- infrastructures/status
verbs:
- get
- list
- watch
- apiGroups:
- coordination.k8s.io
resources:
Expand Down Expand Up @@ -174,6 +183,19 @@ rules:
- patch
- update
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
- clusterrolebindings
- clusterroles
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- route.openshift.io
resources:
Expand Down
9 changes: 8 additions & 1 deletion controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyV1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -83,10 +84,12 @@ func NewReconciler(p Params) *OpenTelemetryCollectorReconciler {
// +kubebuilder:rbac:groups=apps,resources=daemonsets;deployments;statefulsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=policy,resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings;clusterroles,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;create;update
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes;routes/custom-host,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=config.openshift.io,resources=infrastructures;infrastructures/status,verbs=get;list;watch
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
// +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=opentelemetry.io,resources=opentelemetrycollectors/finalizers,verbs=get;update;patch
Expand Down Expand Up @@ -138,7 +141,11 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er
Owns(&appsv1.DaemonSet{}).
Owns(&appsv1.StatefulSet{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&policyV1.PodDisruptionBudget{})
Owns(&policyV1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
Owns(&policyV1.PodDisruptionBudget{}).
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
Owns(&rbacv1.ClusterRoleBinding{}).
Owns(&rbacv1.ClusterRole{})

if featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
builder.Owns(&monitoringv1.ServiceMonitor{})
Expand Down
3 changes: 3 additions & 0 deletions internal/manifests/collector/adapters/config_to_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ComponentType int
const (
ComponentTypeReceiver ComponentType = iota
ComponentTypeExporter
ComponentTypeProcessor
)

func (c ComponentType) String() string {
Expand Down Expand Up @@ -94,6 +95,8 @@ func ConfigToComponentPorts(logger logr.Logger, cType ComponentType, config map[
cmptParser, err = exporterParser.For(logger, cmptName, exporter)
case ComponentTypeReceiver:
cmptParser, err = receiverParser.For(logger, cmptName, exporter)
case ComponentTypeProcessor:
logger.V(4).Info("processors don't provide a way to enable associated ports", "name", key)
}

if err != nil {
Expand Down
62 changes: 62 additions & 0 deletions internal/manifests/collector/adapters/config_to_rbac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 adapters

import (
"github.com/go-logr/logr"
rbacv1 "k8s.io/api/rbac/v1"

"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/parser/processor"
)

func ConfigToRBAC(logger logr.Logger, config map[interface{}]interface{}) []rbacv1.PolicyRule {
Copy link
Member

Choose a reason for hiding this comment

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

nit: it's worth adding a comment to a public API

processorsRaw, ok := config["processors"]
if !ok {
logger.V(2).Info("no processors available as part of the configuration")
return nil
}

processors, ok := processorsRaw.(map[interface{}]interface{})
if !ok {
logger.V(2).Info("processors doesn't contain valid components")
return nil
}

enabledProcessors := getEnabledComponents(config, ComponentTypeProcessor)

var policyRules []rbacv1.PolicyRule
for key, val := range processors {
if !enabledProcessors[key] {
continue
}

processorCfg, ok := val.(map[interface{}]interface{})
if !ok {
logger.V(2).Info("processor doesn't seem to be a map of properties", "processor", key)
processorCfg = map[interface{}]interface{}{}
}

processorName := key.(string)
processorParser, err := processor.For(logger, processorName, processorCfg)
if err != nil {
logger.V(2).Info("no parser found for '%s'", processorName)
continue
}

policyRules = append(policyRules, processorParser.GetRBACRules()...)
}

return policyRules
}
2 changes: 2 additions & 0 deletions internal/manifests/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func Build(params manifests.Params) ([]client.Object, error) {
manifests.Factory(HeadlessService),
manifests.Factory(MonitoringService),
manifests.Factory(Ingress),
manifests.FactoryWithoutError(ClusterRole),
manifests.FactoryWithoutError(ClusterRoleBinding),
}...)
if params.OtelCol.Spec.Observability.Metrics.EnableMetrics && featuregate.PrometheusOperatorIsAvailable.IsEnabled() {
manifestFactories = append(manifestFactories, manifests.Factory(ServiceMonitor))
Expand Down
71 changes: 71 additions & 0 deletions internal/manifests/collector/parser/processor/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 parser is for parsing the OpenTelemetry Collector configuration.
package processor

import (
"fmt"
"strings"

"github.com/go-logr/logr"
rbacv1 "k8s.io/api/rbac/v1"
)

type ProcessorParser interface {
Copy link
Member

Choose a reason for hiding this comment

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

nit: document public APIs

ParserName() string
GetRBACRules() []rbacv1.PolicyRule
}

type Builder func(logr.Logger, string, map[interface{}]interface{}) ProcessorParser
Copy link
Member

Choose a reason for hiding this comment

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

nit: document public APIs


// registry holds a record of all known processor parsers.
var registry = make(map[string]Builder)

// BuilderFor returns a parser builder for the given processor name.
func BuilderFor(name string) Builder {
return registry[processorType(name)]
}

// For returns a new parser for the given processor name + config.
func For(logger logr.Logger, name string, config map[interface{}]interface{}) (ProcessorParser, error) {
builder := BuilderFor(name)
if builder == nil {
return nil, fmt.Errorf("no builders for %s", name)
}
return builder(logger, name, config), nil
}

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

// IsRegistered checks whether a parser is registered with the given name.
func IsRegistered(name string) bool {
_, ok := registry[name]
return ok
}

func processorType(name string) string {
// processors have a name like:
// - myprocessor/custom
// - myprocessor
// we extract the "myprocessor" part and see if we have a parser for the processor
if strings.Contains(name, "/") {
return name[:strings.Index(name, "/")]
}

return name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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 processor

import (
"fmt"

"github.com/go-logr/logr"
rbacv1 "k8s.io/api/rbac/v1"
)

var _ ProcessorParser = &ResourceDetectionParser{}

const (
parserNameResourceDetection = "__resourcedetection"
)

// PrometheusExporterParser parses the configuration for OTLP receivers.
type ResourceDetectionParser struct {
config map[interface{}]interface{}
logger logr.Logger
name string
}

// NewPrometheusExporterParser builds a new parser for OTLP receivers.
func NewResourceDetectionParser(logger logr.Logger, name string, config map[interface{}]interface{}) ProcessorParser {
return &ResourceDetectionParser{
logger: logger,
name: name,
config: config,
}
}

// ParserName returns the name of this parser.
func (o *ResourceDetectionParser) ParserName() string {
return parserNameResourceDetection
}

func (o *ResourceDetectionParser) GetRBACRules() []rbacv1.PolicyRule {
var prs []rbacv1.PolicyRule

detectorsCfg, ok := o.config["detectors"]
if !ok {
return prs
}

detectors, ok := detectorsCfg.([]interface{})
if !ok {
return prs
}
for _, d := range detectors {
detectorName := fmt.Sprint(d)
switch detectorName {
case "kubernetes":
policy := rbacv1.PolicyRule{
APIGroups: []string{""},
Resources: []string{"nodes"},
Verbs: []string{"get", "list"},
}
prs = append(prs, policy)
case "openshift":
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
policy := rbacv1.PolicyRule{
APIGroups: []string{"config.openshift.io"},
Resources: []string{"infrastructures", "infrastructures/status"},
Verbs: []string{"get", "watch", "list"},
}
prs = append(prs, policy)
}
}
return prs
}

func init() {
Register("resourcedetection", NewResourceDetectionParser)
}
Loading
Loading