Skip to content

Commit 0e96e1f

Browse files
authored
Check for the permissions instead of using a CLI flag (open-telemetry#2787)
* Check for the permissions instead of using a CLI flag. open-telemetry#2588 Signed-off-by: Israel Blancas <[email protected]> * Fix bundle Signed-off-by: Israel Blancas <[email protected]> * Fix log message Signed-off-by: Israel Blancas <[email protected]> * Add E2E tests and fix open-telemetry#2833 Signed-off-by: Israel Blancas <[email protected]> * Fix tests Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Fix changelog Signed-off-by: Israel Blancas <[email protected]> * Fix yaml Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Fix k8sattributes processor Signed-off-by: Israel Blancas <[email protected]> * Fix test Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Remove checked error Signed-off-by: Israel Blancas <[email protected]> * Revert change Signed-off-by: Israel Blancas <[email protected]> * Fix workflow Signed-off-by: Israel Blancas <[email protected]> * Fix E2E test Signed-off-by: Israel Blancas <[email protected]> * Fix bundle Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Fix docs Signed-off-by: Israel Blancas <[email protected]> * Fixes open-telemetry#2862 Signed-off-by: Israel Blancas <[email protected]> * Fix E2E tests Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Remove kustomization Signed-off-by: Israel Blancas <[email protected]> * Update bundle Signed-off-by: Israel Blancas <[email protected]> * Fix typo Signed-off-by: Israel Blancas <[email protected]> --------- Signed-off-by: Israel Blancas <[email protected]>
1 parent d204609 commit 0e96e1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+989
-127
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Use the k8snode detector instead of kubernetes for the automatic RBAC creation for the resourcedetector"
9+
10+
# One or more tracking issues related to the change
11+
issues: [2833]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "When two Collectors are created with the same name but different namespaces, the ClusterRoleBinding created by the first will be overriden by the second one."
9+
10+
# One or more tracking issues related to the change
11+
issues: [2862]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Automatically enable RBAC creation if operator SA can create clusterroles and bindings. --create-rbac-permissions flag is noop and deprecated now.
9+
10+
# One or more tracking issues related to the change
11+
issues: [2588]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

.github/workflows/e2e.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- "1.29"
2525
group:
2626
- e2e
27+
- e2e-automatic-rbac
2728
- e2e-autoscale
2829
- e2e-instrumentation
2930
- e2e-opampbridge
@@ -40,7 +41,8 @@ jobs:
4041
setup: "add-multi-instrumentation-params prepare-e2e"
4142
- group: e2e-metadata-filters
4243
setup: "add-operator-arg OPERATOR_ARG='--annotations-filter=*filter.out --labels=*filter.out' prepare-e2e"
43-
44+
- group: e2e-automatic-rbac
45+
setup: "add-rbac-permissions-to-operator prepare-e2e"
4446
steps:
4547
- name: Check out code into the Go module directory
4648
uses: actions/checkout@v4

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ config/manager/kustomization.yaml
3838
# test resources
3939
kubeconfig
4040
tests/_build/
41+
config/rbac/extra-permissions-operator/
4142

4243
# autoinstrumentation artifacts
4344
build

Makefile

+14-10
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,15 @@ add-multi-instrumentation-params:
157157
add-image-opampbridge:
158158
@$(MAKE) add-operator-arg OPERATOR_ARG=--operator-opamp-bridge-image=$(OPERATOROPAMPBRIDGE_IMG)
159159

160-
.PHONY: enable-operator-featuregates
161-
enable-operator-featuregates: OPERATOR_ARG = --feature-gates=$(FEATUREGATES)
162-
enable-operator-featuregates: add-operator-arg
160+
.PHONY: add-rbac-permissions-to-operator
161+
add-rbac-permissions-to-operator: manifests kustomize
162+
# Kustomize only allows patches in the folder where the kustomization is located
163+
# This folder is ignored by .gitignore
164+
cp -r tests/e2e-automatic-rbac/extra-permissions-operator/ config/rbac/extra-permissions-operator
165+
cd config/rbac && $(KUSTOMIZE) edit add patch --kind ClusterRole --name manager-role --path extra-permissions-operator/namespaces.yaml
166+
cd config/rbac && $(KUSTOMIZE) edit add patch --kind ClusterRole --name manager-role --path extra-permissions-operator/nodes.yaml
167+
cd config/rbac && $(KUSTOMIZE) edit add patch --kind ClusterRole --name manager-role --path extra-permissions-operator/rbac.yaml
168+
cd config/rbac && $(KUSTOMIZE) edit add patch --kind ClusterRole --name manager-role --path extra-permissions-operator/replicaset.yaml
163169

164170
# Deploy controller in the current Kubernetes context, configured in ~/.kube/config
165171
.PHONY: deploy
@@ -217,6 +223,11 @@ generate: controller-gen
217223
e2e: chainsaw
218224
$(CHAINSAW) test --test-dir ./tests/e2e
219225

226+
# end-to-end-test for testing automatic RBAC creation
227+
.PHONY: e2e-automatic-rbac
228+
e2e-automatic-rbac: chainsaw
229+
$(CHAINSAW) test --test-dir ./tests/e2e-automatic-rbac
230+
220231
# end-to-end-test for testing autoscale
221232
.PHONY: e2e-autoscale
222233
e2e-autoscale: chainsaw
@@ -272,9 +283,6 @@ e2e-upgrade: undeploy chainsaw
272283
.PHONY: prepare-e2e
273284
prepare-e2e: chainsaw set-image-controller add-image-targetallocator add-image-opampbridge container container-target-allocator container-operator-opamp-bridge start-kind cert-manager install-metrics-server install-targetallocator-prometheus-crds load-image-all deploy
274285

275-
.PHONY: prepare-e2e-with-featuregates
276-
prepare-e2e-with-featuregates: chainsaw enable-operator-featuregates prepare-e2e
277-
278286
.PHONY: scorecard-tests
279287
scorecard-tests: operator-sdk
280288
$(OPERATOR_SDK) scorecard -w=5m bundle || (echo "scorecard test failed" && exit 1)
@@ -320,10 +328,6 @@ endif
320328
install-metrics-server:
321329
./hack/install-metrics-server.sh
322330

323-
.PHONY: install-prometheus-operator
324-
install-prometheus-operator:
325-
./hack/install-prometheus-operator.sh
326-
327331
# This only installs the CRDs Target Allocator supports
328332
.PHONY: install-targetallocator-prometheus-crds
329333
install-targetallocator-prometheus-crds:

apis/v1beta1/collector_webhook.go

+1-24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"strings"
2121

2222
"github.com/go-logr/logr"
23-
authorizationv1 "k8s.io/api/authorization/v1"
2423
autoscalingv2 "k8s.io/api/autoscaling/v2"
2524
rbacv1 "k8s.io/api/rbac/v1"
2625
"k8s.io/apimachinery/pkg/runtime"
@@ -359,7 +358,7 @@ func (c CollectorWebhook) validateTargetAllocatorConfig(ctx context.Context, r *
359358
if subjectAccessReviews, err := c.reviewer.CheckPolicyRules(ctx, r.GetNamespace(), r.Spec.TargetAllocator.ServiceAccount, targetAllocatorCRPolicyRules...); err != nil {
360359
return nil, fmt.Errorf("unable to check rbac rules %w", err)
361360
} else if allowed, deniedReviews := rbac.AllSubjectAccessReviewsAllowed(subjectAccessReviews); !allowed {
362-
return warningsGroupedByResource(deniedReviews), nil
361+
return rbac.WarningsGroupedByResource(deniedReviews), nil
363362
}
364363
}
365364

@@ -407,28 +406,6 @@ func checkAutoscalerSpec(autoscaler *AutoscalerSpec) error {
407406
return nil
408407
}
409408

410-
// warningsGroupedByResource is a helper to take the missing permissions and format them as warnings.
411-
func warningsGroupedByResource(reviews []*authorizationv1.SubjectAccessReview) []string {
412-
fullResourceToVerbs := make(map[string][]string)
413-
for _, review := range reviews {
414-
if review.Spec.ResourceAttributes != nil {
415-
key := fmt.Sprintf("%s/%s", review.Spec.ResourceAttributes.Group, review.Spec.ResourceAttributes.Resource)
416-
if len(review.Spec.ResourceAttributes.Group) == 0 {
417-
key = review.Spec.ResourceAttributes.Resource
418-
}
419-
fullResourceToVerbs[key] = append(fullResourceToVerbs[key], review.Spec.ResourceAttributes.Verb)
420-
} else if review.Spec.NonResourceAttributes != nil {
421-
key := fmt.Sprintf("nonResourceURL: %s", review.Spec.NonResourceAttributes.Path)
422-
fullResourceToVerbs[key] = append(fullResourceToVerbs[key], review.Spec.NonResourceAttributes.Verb)
423-
}
424-
}
425-
var warnings []string
426-
for fullResource, verbs := range fullResourceToVerbs {
427-
warnings = append(warnings, fmt.Sprintf("missing the following rules for %s: [%s]", fullResource, strings.Join(verbs, ",")))
428-
}
429-
return warnings
430-
}
431-
432409
func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer) error {
433410
cvw := &CollectorWebhook{
434411
reviewer: reviewer,

bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ metadata:
9999
categories: Logging & Tracing,Monitoring
100100
certified: "false"
101101
containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
102-
createdAt: "2024-04-30T12:37:39Z"
102+
createdAt: "2024-05-03T15:21:44Z"
103103
description: Provides the OpenTelemetry components, including the Collector
104104
operators.operatorframework.io/builder: operator-sdk-v1.29.0
105105
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
@@ -499,6 +499,11 @@ spec:
499499
- --zap-log-level=info
500500
- --zap-time-encoding=rfc3339nano
501501
- --enable-nginx-instrumentation=true
502+
env:
503+
- name: SERVICE_ACCOUNT_NAME
504+
valueFrom:
505+
fieldRef:
506+
fieldPath: spec.serviceAccountName
502507
image: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:0.99.0
503508
livenessProbe:
504509
httpGet:

config/manager/manager.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,10 @@ spec:
5151
requests:
5252
cpu: 100m
5353
memory: 64Mi
54+
env:
55+
- name: SERVICE_ACCOUNT_NAME
56+
valueFrom:
57+
fieldRef:
58+
fieldPath: spec.serviceAccountName
5459
serviceAccountName: controller-manager
5560
terminationGracePeriodSeconds: 10

controllers/opampbridge_controller_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/open-telemetry/opentelemetry-operator/controllers"
3838
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
3939
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
40+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
4041
"github.com/open-telemetry/opentelemetry-operator/internal/config"
4142
)
4243

@@ -48,6 +49,9 @@ var opampBridgeMockAutoDetector = &mockAutoDetect{
4849
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
4950
return prometheus.Available, nil
5051
},
52+
RBACPermissionsFunc: func(ctx context.Context) (rbac.Availability, error) {
53+
return rbac.Available, nil
54+
},
5155
}
5256

5357
func TestNewObjectsOnReconciliation_OpAMPBridge(t *testing.T) {

controllers/opentelemetrycollector_controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
4040
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
4141
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
42+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
4243
"github.com/open-telemetry/opentelemetry-operator/internal/config"
4344
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
4445
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
@@ -239,7 +240,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er
239240
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
240241
Owns(&policyV1.PodDisruptionBudget{})
241242

242-
if r.config.CreateRBACPermissions() {
243+
if r.config.CreateRBACPermissions() == rbac.Available {
243244
builder.Owns(&rbacv1.ClusterRoleBinding{})
244245
builder.Owns(&rbacv1.ClusterRole{})
245246
}

controllers/suite_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import (
5656
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
5757
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
5858
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
59+
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
5960
"github.com/open-telemetry/opentelemetry-operator/internal/config"
6061
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
6162
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/testdata"
@@ -95,6 +96,7 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)
9596
type mockAutoDetect struct {
9697
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
9798
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
99+
RBACPermissionsFunc func(ctx context.Context) (autoRBAC.Availability, error)
98100
}
99101

100102
func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
@@ -111,6 +113,13 @@ func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailabi
111113
return openshift.RoutesNotAvailable, nil
112114
}
113115

116+
func (m *mockAutoDetect) RBACPermissions(ctx context.Context) (autoRBAC.Availability, error) {
117+
if m.RBACPermissionsFunc != nil {
118+
return m.RBACPermissionsFunc(ctx)
119+
}
120+
return autoRBAC.NotAvailable, nil
121+
}
122+
114123
func TestMain(m *testing.M) {
115124
ctx, cancel = context.WithCancel(context.TODO())
116125
defer cancel()

internal/autodetect/main.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
package autodetect
1717

1818
import (
19+
"context"
20+
"fmt"
21+
1922
"k8s.io/client-go/discovery"
2023
"k8s.io/client-go/rest"
2124

2225
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2326
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
27+
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
28+
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
2429
)
2530

2631
var _ AutoDetect = (*autoDetect)(nil)
@@ -29,14 +34,16 @@ var _ AutoDetect = (*autoDetect)(nil)
2934
type AutoDetect interface {
3035
OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error)
3136
PrometheusCRsAvailability() (prometheus.Availability, error)
37+
RBACPermissions(ctx context.Context) (autoRBAC.Availability, error)
3238
}
3339

3440
type autoDetect struct {
35-
dcl discovery.DiscoveryInterface
41+
dcl discovery.DiscoveryInterface
42+
reviewer *rbac.Reviewer
3643
}
3744

3845
// New creates a new auto-detection worker, using the given client when talking to the current cluster.
39-
func New(restConfig *rest.Config) (AutoDetect, error) {
46+
func New(restConfig *rest.Config, reviewer *rbac.Reviewer) (AutoDetect, error) {
4047
dcl, err := discovery.NewDiscoveryClientForConfig(restConfig)
4148
if err != nil {
4249
// it's pretty much impossible to get into this problem, as most of the
@@ -46,7 +53,8 @@ func New(restConfig *rest.Config) (AutoDetect, error) {
4653
}
4754

4855
return &autoDetect{
49-
dcl: dcl,
56+
dcl: dcl,
57+
reviewer: reviewer,
5058
}, nil
5159
}
5260

@@ -83,3 +91,15 @@ func (a *autoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability
8391

8492
return openshift.RoutesNotAvailable, nil
8593
}
94+
95+
func (a *autoDetect) RBACPermissions(ctx context.Context) (autoRBAC.Availability, error) {
96+
w, err := autoRBAC.CheckRBACPermissions(ctx, a.reviewer)
97+
if err != nil {
98+
return autoRBAC.NotAvailable, err
99+
}
100+
if w != nil {
101+
return autoRBAC.NotAvailable, fmt.Errorf("missing permissions: %s", w)
102+
}
103+
104+
return autoRBAC.Available, nil
105+
}

0 commit comments

Comments
 (0)