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

Check for the permissions instead of using a CLI flag #2787

Merged
merged 43 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
de651f3
Check for the permissions instead of using a CLI flag. #2588
iblancasa Apr 8, 2024
e0bcbd7
Fix bundle
iblancasa Apr 8, 2024
0dd7c54
Fix log message
iblancasa Apr 8, 2024
07545f5
Add E2E tests and fix #2833
iblancasa Apr 9, 2024
e53bd6a
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 9, 2024
14b737c
Fix tests
iblancasa Apr 9, 2024
730e71f
Apply changes requested in code review
iblancasa Apr 9, 2024
a51d162
Fix changelog
iblancasa Apr 9, 2024
9e764e9
Fix yaml
iblancasa Apr 9, 2024
18e2fe6
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 10, 2024
a12aade
Apply changes requested in code review
iblancasa Apr 10, 2024
4fc2724
Fix k8sattributes processor
iblancasa Apr 11, 2024
856440a
Fix test
iblancasa Apr 11, 2024
7347aed
Apply changes requested in code review
iblancasa Apr 11, 2024
85123b3
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 11, 2024
f276b03
Apply changes requested in code review
iblancasa Apr 11, 2024
fb2d5be
Remove checked error
iblancasa Apr 11, 2024
e457ea3
Merge branch 'main' into feature/2588
iblancasa Apr 11, 2024
1116be4
Revert change
iblancasa Apr 11, 2024
9c8a246
Merge branch 'feature/2588' of github.com:iblancasa/opentelemetry-ope…
iblancasa Apr 11, 2024
73afa80
Fix workflow
iblancasa Apr 11, 2024
bd2716c
Fix E2E test
iblancasa Apr 11, 2024
7fc13fb
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 12, 2024
0558a69
Fix bundle
iblancasa Apr 12, 2024
f8ca274
Merge branch 'main' into feature/2588
iblancasa Apr 15, 2024
1213cd9
Merge branch 'main' into feature/2588
iblancasa Apr 15, 2024
b548a41
Apply changes requested in code review
iblancasa Apr 16, 2024
019c516
Fix docs
iblancasa Apr 16, 2024
f0bcc60
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 16, 2024
3852840
Fixes #2862
iblancasa Apr 16, 2024
9b7e653
Fix E2E tests
iblancasa Apr 16, 2024
338eb62
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 17, 2024
08915d2
Apply changes requested in code review
iblancasa Apr 17, 2024
71c0b0a
Merge branch 'main' into feature/2588
iblancasa Apr 18, 2024
df654f2
Merge branch 'main' into feature/2588
iblancasa Apr 18, 2024
83a31eb
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 25, 2024
4bbefe9
Remove kustomization
iblancasa Apr 25, 2024
f1ff151
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 29, 2024
add0827
Merge branch 'main' into feature/2588
iblancasa Apr 29, 2024
6c7640b
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Apr 30, 2024
fafb8c9
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa May 3, 2024
26e8830
Update bundle
iblancasa May 3, 2024
603d6d3
Fix typo
iblancasa May 3, 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/2833-fix-detector-resourcedetectionprocessor.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: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Use the k8snode detector instead of kubernetes for the automatic RBAC creation for the resourcedetector"
iblancasa marked this conversation as resolved.
Show resolved Hide resolved

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

# (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
@@ -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. collector, target allocator, auto-instrumentation, opamp, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Check the permissions for the SA running the OpenTelemetry Operator instead of using the create-rbac-permissions flag.
iblancasa marked this conversation as resolved.
Show resolved Hide resolved

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

# (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:
1 change: 1 addition & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- "1.29"
group:
- e2e
- e2e-automatic-rbac
- e2e-autoscale
- e2e-instrumentation
- e2e-opampbridge
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ generate: controller-gen
e2e: chainsaw
$(CHAINSAW) test --test-dir ./tests/e2e

# end-to-end-test for testing automatic RBAC creation
.PHONY: e2e-automatic-rbac
e2e-automatic-rbac: chainsaw
# Add extra needed permissions
kubectl apply -f hack/rbac-operator-permissions.yaml
kubectl rollout restart deployment opentelemetry-operator-controller-manager -n opentelemetry-operator-system
kubectl rollout status deployment opentelemetry-operator-controller-manager -n opentelemetry-operator-system
go run hack/check-operator-ready.go
$(CHAINSAW) test --test-dir ./tests/e2e-automatic-rbac
# Cleanup
kubectl delete -f hack/rbac-operator-permissions.yaml
kubectl rollout restart deployment opentelemetry-operator-controller-manager -n opentelemetry-operator-system
iblancasa marked this conversation as resolved.
Show resolved Hide resolved

# end-to-end-test for testing autoscale
.PHONY: e2e-autoscale
e2e-autoscale: chainsaw
Expand Down
26 changes: 1 addition & 25 deletions apis/v1alpha1/collector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package v1alpha1
import (
"context"
"fmt"
"strings"

"github.com/go-logr/logr"
v1 "k8s.io/api/authorization/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -378,7 +376,7 @@ func (c CollectorWebhook) validateTargetAllocatorConfig(ctx context.Context, r *
if subjectAccessReviews, err := c.reviewer.CheckPolicyRules(ctx, r.GetNamespace(), r.Spec.TargetAllocator.ServiceAccount, targetAllocatorCRPolicyRules...); err != nil {
return nil, fmt.Errorf("unable to check rbac rules %w", err)
} else if allowed, deniedReviews := rbac.AllSubjectAccessReviewsAllowed(subjectAccessReviews); !allowed {
return warningsGroupedByResource(deniedReviews), nil
return rbac.WarningsGroupedByResource(deniedReviews), nil
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -426,28 +424,6 @@ func checkAutoscalerSpec(autoscaler *AutoscalerSpec) error {
return nil
}

// warningsGroupedByResource is a helper to take the missing permissions and format them as warnings.
func warningsGroupedByResource(reviews []*v1.SubjectAccessReview) []string {
fullResourceToVerbs := make(map[string][]string)
for _, review := range reviews {
if review.Spec.ResourceAttributes != nil {
key := fmt.Sprintf("%s/%s", review.Spec.ResourceAttributes.Group, review.Spec.ResourceAttributes.Resource)
if len(review.Spec.ResourceAttributes.Group) == 0 {
key = review.Spec.ResourceAttributes.Resource
}
fullResourceToVerbs[key] = append(fullResourceToVerbs[key], review.Spec.ResourceAttributes.Verb)
} else if review.Spec.NonResourceAttributes != nil {
key := fmt.Sprintf("nonResourceURL: %s", review.Spec.NonResourceAttributes.Path)
fullResourceToVerbs[key] = append(fullResourceToVerbs[key], review.Spec.NonResourceAttributes.Verb)
}
}
var warnings []string
for fullResource, verbs := range fullResourceToVerbs {
warnings = append(warnings, fmt.Sprintf("missing the following rules for %s: [%s]", fullResource, strings.Join(verbs, ",")))
}
return warnings
}

func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer) error {
cvw := &CollectorWebhook{
reviewer: reviewer,
Expand Down
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: "2024-04-05T17:37:15Z"
createdAt: "2024-04-08T14:26:03Z"
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 @@ -413,6 +413,11 @@ spec:
- --zap-time-encoding=rfc3339nano
- --feature-gates=+operator.autoinstrumentation.go
- --enable-nginx-instrumentation=true
env:
- name: SERVICE_ACCOUNT_NAME
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
image: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:0.97.1
livenessProbe:
httpGet:
Expand Down
5 changes: 5 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ spec:
requests:
cpu: 100m
memory: 64Mi
env:
- name: SERVICE_ACCOUNT_NAME
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
serviceAccountName: controller-manager
terminationGracePeriodSeconds: 10
4 changes: 4 additions & 0 deletions controllers/opampbridge_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/controllers"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
)

Expand All @@ -48,6 +49,9 @@ var opampBridgeMockAutoDetector = &mockAutoDetect{
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
return prometheus.Available, nil
},
RBACPermissionsFunc: func(ctx context.Context) (rbac.Availability, error) {
return rbac.Available, nil
},
}

func TestNewObjectsOnReconciliation_OpAMPBridge(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
Expand Down Expand Up @@ -245,7 +246,7 @@ func (r *OpenTelemetryCollectorReconciler) SetupWithManager(mgr ctrl.Manager) er
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Owns(&policyV1.PodDisruptionBudget{})

if r.config.CreateRBACPermissions() {
if r.config.CreateRBACPermissions() == rbac.Available {
builder.Owns(&rbacv1.ClusterRoleBinding{})
builder.Owns(&rbacv1.ClusterRole{})
}
Expand Down
9 changes: 9 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
autoRbac "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/testdata"
Expand Down Expand Up @@ -94,6 +95,7 @@ var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)
type mockAutoDetect struct {
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
RBACPermissionsFunc func(ctx context.Context) (autoRbac.Availability, error)
}

func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
Expand All @@ -110,6 +112,13 @@ func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailabi
return openshift.RoutesNotAvailable, nil
}

func (m *mockAutoDetect) RBACPermissions(ctx context.Context) (autoRbac.Availability, error) {
if m.RBACPermissionsFunc != nil {
return m.RBACPermissionsFunc(ctx)
}
return autoRbac.NotAvailable, nil
}

func TestMain(m *testing.M) {
ctx, cancel = context.WithCancel(context.TODO())
defer cancel()
Expand Down
67 changes: 67 additions & 0 deletions hack/rbac-operator-permissions.yaml
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: generate-processors-rbac
rules:
- apiGroups:
- ""
resources:
- namespaces
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
- clusterrolebindings
- clusterroles
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps
resources:
- replicasets
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- replicasets
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: generate-processors-rbac
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: generate-processors-rbac
subjects:
- kind: ServiceAccount
name: opentelemetry-operator-controller-manager
namespace: opentelemetry-operator-system
26 changes: 23 additions & 3 deletions internal/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
package autodetect

import (
"context"
"fmt"

"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
autoRbac "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
)

var _ AutoDetect = (*autoDetect)(nil)
Expand All @@ -29,14 +34,16 @@ var _ AutoDetect = (*autoDetect)(nil)
type AutoDetect interface {
OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error)
PrometheusCRsAvailability() (prometheus.Availability, error)
RBACPermissions(ctx context.Context) (autoRbac.Availability, error)
}

type autoDetect struct {
dcl discovery.DiscoveryInterface
dcl discovery.DiscoveryInterface
reviewer *rbac.Reviewer
}

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

return &autoDetect{
dcl: dcl,
dcl: dcl,
reviewer: reviewer,
}, nil
}

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

return openshift.RoutesNotAvailable, nil
}

func (a *autoDetect) RBACPermissions(ctx context.Context) (autoRbac.Availability, error) {
w, err := autoRbac.CheckRbacPermissions(ctx, a.reviewer)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we make the naming convention consistent? I.e. RBAC vs Rbac (I think the all caps probs makes more sense)

if err != nil {
return autoRbac.NotAvailable, err
}
if w != nil {
return autoRbac.NotAvailable, fmt.Errorf("missing permissions: %s", w)
}

return autoRbac.Available, nil
}
Loading
Loading