-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automate the creation of the permissions needed by resourcedetection (#…
…2394) * Automate the creation of the permissions requested by resourcedetection Signed-off-by: Israel Blancas <[email protected]> * Add changelog Signed-off-by: Israel Blancas <[email protected]> * Fix merge Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> * Fix lint Signed-off-by: Israel Blancas <[email protected]> * Add feature gate and test Signed-off-by: Israel Blancas <[email protected]> * Add unit tests Signed-off-by: Israel Blancas <[email protected]> * Apply feedback from pull request Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested as part of the Pull Request Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested as part of the Pull Request Signed-off-by: Israel Blancas <[email protected]> --------- Signed-off-by: Israel Blancas <[email protected]>
- Loading branch information
Showing
20 changed files
with
634 additions
and
3 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
.chloggen/2393-automate-permissions-resourcedetection.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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" | ||
) | ||
|
||
// ConfigToRBAC parses the OpenTelemetry Collector configuration and checks what RBAC resources are needed to be created. | ||
func ConfigToRBAC(logger logr.Logger, config map[interface{}]interface{}) []rbacv1.PolicyRule { | ||
var policyRules []rbacv1.PolicyRule | ||
processorsRaw, ok := config["processors"] | ||
if !ok { | ||
logger.V(2).Info("no processors available as part of the configuration") | ||
return policyRules | ||
} | ||
|
||
processors, ok := processorsRaw.(map[interface{}]interface{}) | ||
if !ok { | ||
logger.V(2).Info("processors doesn't contain valid components") | ||
return policyRules | ||
} | ||
|
||
enabledProcessors := getEnabledComponents(config, ComponentTypeProcessor) | ||
|
||
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 | ||
} |
100 changes: 100 additions & 0 deletions
100
internal/manifests/collector/adapters/config_to_rbac_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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 ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
func TestConfigRBAC(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
config string | ||
expectedRules []rbacv1.PolicyRule | ||
}{ | ||
{ | ||
desc: "No processors", | ||
config: `processors: | ||
service: | ||
traces: | ||
processors:`, | ||
expectedRules: ([]rbacv1.PolicyRule)(nil), | ||
}, | ||
{ | ||
desc: "processors no rbac", | ||
config: `processors: | ||
batch: | ||
service: | ||
pipelines: | ||
traces: | ||
processors: [batch]`, | ||
expectedRules: ([]rbacv1.PolicyRule)(nil), | ||
}, | ||
{ | ||
desc: "resourcedetection-processor k8s", | ||
config: `processors: | ||
resourcedetection: | ||
detectors: [kubernetes] | ||
service: | ||
pipelines: | ||
traces: | ||
processors: [resourcedetection]`, | ||
expectedRules: []rbacv1.PolicyRule{ | ||
{ | ||
APIGroups: []string{""}, | ||
Resources: []string{"nodes"}, | ||
Verbs: []string{"get", "list"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "resourcedetection-processor openshift", | ||
config: `processors: | ||
resourcedetection: | ||
detectors: [openshift] | ||
service: | ||
pipelines: | ||
traces: | ||
processors: [resourcedetection]`, | ||
expectedRules: []rbacv1.PolicyRule{ | ||
{ | ||
APIGroups: []string{"config.openshift.io"}, | ||
Resources: []string{"infrastructures", "infrastructures/status"}, | ||
Verbs: []string{"get", "watch", "list"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
var logger = logf.Log.WithName("collector-unit-tests") | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
config, err := ConfigFromString(tt.config) | ||
require.NoError(t, err, tt.desc) | ||
require.NotEmpty(t, config, tt.desc) | ||
|
||
// test | ||
rules := ConfigToRBAC(logger, config) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expectedRules, rules, tt.desc) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.