-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor ParsePodAnnotations into annotations package
- Loading branch information
Showing
5 changed files
with
216 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2020 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://www.apache.org/licenses/LICENSE-2.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 annotations | ||
|
||
import ( | ||
"encoding/csv" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/aws/amazon-eks-pod-identity-webhook/pkg" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type PodAnnotations struct { | ||
tokenExpiration *int64 | ||
containersToSkip map[string]bool | ||
} | ||
|
||
func (a *PodAnnotations) GetContainersToSkip() map[string]bool { | ||
return a.containersToSkip | ||
} | ||
|
||
func (a *PodAnnotations) GetTokenExpiration(fallback int64) int64 { | ||
if a.tokenExpiration == nil { | ||
return fallback | ||
} else { | ||
return *a.tokenExpiration | ||
} | ||
} | ||
|
||
// parsePodAnnotations parses the pod annotations that can influence mutation: | ||
// - tokenExpiration. Overrides the given service account annotation/flag-level | ||
// setting. | ||
// - containersToSkip. A Pod specific setting since certain containers within a | ||
// specific pod might need to be opted-out of mutation | ||
func ParsePodAnnotations(pod *corev1.Pod, annotationDomain string) *PodAnnotations { | ||
return &PodAnnotations{ | ||
tokenExpiration: parseTokenExpiration(annotationDomain, pod), | ||
containersToSkip: parseContainersToSkip(annotationDomain, pod), | ||
} | ||
} | ||
|
||
// parseContainersToSkip returns the containers of a pod to skip mutating | ||
func parseContainersToSkip(annotationDomain string, pod *corev1.Pod) map[string]bool { | ||
skippedNames := map[string]bool{} | ||
skipContainersKey := annotationDomain + "/" + SkipContainersAnnotation | ||
|
||
value, ok := pod.Annotations[skipContainersKey] | ||
if !ok { | ||
return nil | ||
} | ||
r := csv.NewReader(strings.NewReader(value)) | ||
// error means we don't skip any | ||
podNames, err := r.Read() | ||
if err != nil { | ||
klog.Infof("Could not parse skip containers annotation on pod %s/%s: %v", pod.Namespace, pod.Name, err) | ||
return nil | ||
} | ||
for _, name := range podNames { | ||
skippedNames[name] = true | ||
} | ||
return skippedNames | ||
} | ||
|
||
func parseTokenExpiration(annotationDomain string, pod *corev1.Pod) *int64 { | ||
expirationKey := annotationDomain + "/" + TokenExpirationAnnotation | ||
expirationStr, ok := pod.Annotations[expirationKey] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
expiration, err := strconv.ParseInt(expirationStr, 10, 64) | ||
if err != nil { | ||
klog.V(4).Infof("Found invalid value for token expiration on the pod annotation: %s, falling back to the default: %v", expirationStr, err) | ||
return nil | ||
} | ||
|
||
val := pkg.ValidateMinTokenExpiration(expiration) | ||
return &val | ||
} |
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,99 @@ | ||
/* | ||
Copyright 2020 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://www.apache.org/licenses/LICENSE-2.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 annotations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func TestParsePodAnnotations(t *testing.T) { | ||
podNoAnnotations := ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: balajilovesoreos` | ||
testcases := []struct { | ||
name string | ||
pod string | ||
|
||
expectedContainersToSkip map[string]bool | ||
|
||
fallbackExpiration int64 | ||
expectedExpiration int64 | ||
}{ | ||
{ | ||
name: "sidecar-containers", | ||
pod: ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: balajilovesoreos | ||
annotations: | ||
testing.eks.amazonaws.com/skip-containers: "sidecar,foo" | ||
`, | ||
expectedContainersToSkip: map[string]bool{"sidecar": true, "foo": true}, | ||
}, | ||
{ | ||
name: "token-expiration", | ||
pod: ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: balajilovesoreos | ||
annotations: | ||
testing.eks.amazonaws.com/token-expiration: "1234" | ||
`, | ||
fallbackExpiration: 4567, | ||
expectedExpiration: 1234, | ||
}, | ||
{ | ||
name: "token-expiration fallback", | ||
pod: podNoAnnotations, | ||
fallbackExpiration: 4567, | ||
expectedExpiration: 4567, | ||
}, | ||
{ | ||
name: "token-expiration round up to min value", | ||
pod: ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: balajilovesoreos | ||
annotations: | ||
testing.eks.amazonaws.com/token-expiration: "0" | ||
`, | ||
fallbackExpiration: 4567, | ||
expectedExpiration: 600, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var pod *corev1.Pod | ||
|
||
err := yaml.Unmarshal([]byte(tc.pod), &pod) | ||
assert.NoError(t, err) | ||
|
||
actual := ParsePodAnnotations(pod, "testing.eks.amazonaws.com") | ||
assert.Equal(t, tc.expectedContainersToSkip, actual.GetContainersToSkip()) | ||
assert.Equal(t, tc.expectedExpiration, actual.GetTokenExpiration(tc.fallbackExpiration)) | ||
}) | ||
} | ||
} |
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