-
Notifications
You must be signed in to change notification settings - Fork 176
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
Add jitter to Default token expiration #260
Changes from 1 commit
611c1f7
04fae22
3eb38ec
e55d30e
ed07698
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
"net/http" | ||
"path/filepath" | ||
"strconv" | ||
|
@@ -417,6 +418,14 @@ func (m *Modifier) buildPodPatchConfig(pod *corev1.Pod) *podPatchConfig { | |
regionalSTS, tokenExpiration := m.Cache.GetCommonConfigurations(pod.Spec.ServiceAccountName, pod.Namespace) | ||
tokenExpiration, containersToSkip := m.parsePodAnnotations(pod, tokenExpiration) | ||
|
||
if tokenExpiration == pkg.DefaultTokenExpiration { | ||
klog.V(4).Infof("Adding jitter to default token expiration") | ||
var err error | ||
tokenExpiration, err = addJitter(tokenExpiration, 5, pkg.MinTokenExpiration, pkg.MaxTokenExpiration) | ||
if err != nil { | ||
klog.Errorf("Error adding jitter to default token expiration: %v", err) | ||
} | ||
} | ||
webhookPodCount.WithLabelValues("container_credentials").Inc() | ||
|
||
return &podPatchConfig{ | ||
|
@@ -479,6 +488,26 @@ func (m *Modifier) buildPodPatchConfig(pod *corev1.Pod) *podPatchConfig { | |
return nil | ||
} | ||
|
||
func addJitter(val int64, jitterPercent int64, min int64, max int64) (int64, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be just simplified to a randomization between min and max, we don't need the val. And jitterPercentage and (min, max) is kinda similar, just to add randomization There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rewrote the function and renamed to |
||
if max < min { | ||
return val, error(fmt.Errorf("max value %d is less than min value %d, cannot add jitter", max, min)) | ||
} | ||
|
||
jitterFactor := float64(jitterPercent) / 100.0 | ||
jitterMin := int64(float64(val) - (float64(val) * jitterFactor)) | ||
if jitterMin < min { | ||
jitterMin = min | ||
} | ||
jitterMax := int64(float64(val) + (float64(val) * jitterFactor)) | ||
if jitterMax > max { | ||
jitterMax = max | ||
} | ||
|
||
valWithJitter := rand.Int63n(jitterMax - jitterMin + 1) + jitterMin | ||
|
||
return valWithJitter, nil | ||
} | ||
|
||
// MutatePod takes a AdmissionReview, mutates the pod, and returns an AdmissionResponse | ||
func (m *Modifier) MutatePod(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse { | ||
badRequest := &v1beta1.AdmissionResponse{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
DefaultTokenExpiration
is used by both IRSAv1 and IRSAv2 (Pod Identity):GetCommonConfigurations
, which is being added from onboarding to IRSAv2 Add support for container credentials method #189I believe we can add the randomization inside the
GetCommonConfigurations
function and not change theDefaultTokenExpiration
for IRSAv1 useThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have updated GetCommonConfigurations the way you are describing in the most recent commit now. Let me know if I missed somethign