-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service account impersonation support for controllers (#59)
Add support for per namespace service account impersonation in controllers. This enables running controller operations with different service account permissions based on namespace configuration. Key changes: - Add `ServiceAccounts` field to `ResourceGroupSpec` for mapping namespaces to service accounts - Implement dynamic client creation with service account impersonation - Add metrics for tracking impersonation `success`/`failures` and `latency` - Support default service account fallback using `"*"` key e.g usage: ```yaml apiVersion: x.symphony.k8s.aws/v1alpha1 kind: ResourceGroup metadata: name: deploymentservice.x.symphony.k8s.aws spec: # NOTE: this is just an initial implementation, we'll probably # review and redesign this. serviceAccounts: production: prod-sa staging: staging-sa "*": fallback-sa ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
Showing
6 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
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,69 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 instance | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
) | ||
|
||
const ( | ||
// MetricImpersonationTotal is the total number of impersonation requests | ||
// made by the controller | ||
MetricImpersonationTotal = "controller_impersonation_total" | ||
// MetricImpersonationErrors is the total number of errors encountered | ||
// while making impersonation requests | ||
MetricImpersonationErrors = "controller_impersonation_errors_total" | ||
// MetricImpersonationDuration tracks the duration of impersonation operations | ||
MetricImpersonationDuration = "controller_impersonation_duration_seconds" | ||
) | ||
|
||
var ( | ||
impersonationTotal = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: MetricImpersonationTotal, | ||
Help: "Total number of service account impersonation attempts by namespace and result", | ||
}, | ||
[]string{"namespace", "service_account", "result"}, | ||
) | ||
|
||
impersonationErrors = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: MetricImpersonationErrors, | ||
Help: "Total number of service account impersonation errors by category", | ||
}, | ||
[]string{"namespace", "service_account", "error_type"}, | ||
) | ||
|
||
impersonationDuration = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: MetricImpersonationDuration, | ||
Help: "Duration of service account impersonation operations", | ||
Buckets: []float64{0.01, 0.1, 0.5, 1, 2, 5}, | ||
}, | ||
[]string{"namespace", "service_account"}, | ||
) | ||
) | ||
|
||
func recordImpersonateError(namespace, sa string, category errorCategory) { | ||
impersonationErrors.WithLabelValues(namespace, sa, string(category)).Inc() | ||
} | ||
|
||
func init() { | ||
metrics.Registry.MustRegister( | ||
impersonationTotal, | ||
impersonationErrors, | ||
impersonationDuration, | ||
) | ||
} |
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