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

refactor: remove err return and add unit tests for handle errors #1091

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 5 additions & 9 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ func (m *podMutator) Handle(ctx context.Context, req admission.Request) (respons
pod.Spec.Containers = m.mutateContainers(pod.Spec.Containers, clientID, tenantID, skipContainers)

// add the projected service account token volume to the pod if not exists
if err = addProjectedServiceAccountTokenVolume(pod, serviceAccountTokenExpiration, m.audience); err != nil {
logger.Error("failed to add projected service account volume", err)
return admission.Errored(http.StatusBadRequest, err)
}
addProjectedServiceAccountTokenVolume(pod, serviceAccountTokenExpiration, m.audience)

marshaledPod, err := json.Marshal(pod)
if err != nil {
Expand Down Expand Up @@ -401,7 +398,7 @@ func addProjectedTokenVolumeMount(container corev1.Container) corev1.Container {
return container
}

func addProjectedServiceAccountTokenVolume(pod *corev1.Pod, serviceAccountTokenExpiration int64, audience string) error {
func addProjectedServiceAccountTokenVolume(pod *corev1.Pod, serviceAccountTokenExpiration int64, audience string) {
// add the projected service account token volume to the pod if not exists
for _, volume := range pod.Spec.Volumes {
if volume.Projected == nil {
Expand All @@ -412,7 +409,7 @@ func addProjectedServiceAccountTokenVolume(pod *corev1.Pod, serviceAccountTokenE
continue
}
if pvs.ServiceAccountToken.Path == TokenFilePathName {
return nil
return
}
}
}
Expand All @@ -436,9 +433,8 @@ func addProjectedServiceAccountTokenVolume(pod *corev1.Pod, serviceAccountTokenE
},
},
},
})

return nil
},
)
}

// getAzureAuthorityHost returns the active directory endpoint to use for requesting
Expand Down
31 changes: 27 additions & 4 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,8 @@ func TestAddProjectedServiceAccountTokenVolume(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := addProjectedServiceAccountTokenVolume(test.pod, serviceAccountTokenExpiry, DefaultAudience)
if err != nil {
t.Fatalf("expected err to be nil, got: %v", err)
}
addProjectedServiceAccountTokenVolume(test.pod, serviceAccountTokenExpiry, DefaultAudience)

if !reflect.DeepEqual(test.pod.Spec.Volumes, test.expectedVolume) {
t.Fatalf("expected: %v, got: %v", test.pod.Spec.Volumes, test.expectedVolume)
}
Expand Down Expand Up @@ -1295,13 +1293,38 @@ func TestHandleError(t *testing.T) {
clientObjects []client.Object
expectedErr string
}{
{
name: "failed to decode pod",
object: runtime.RawExtension{Raw: []byte("invalid")},
clientObjects: serviceAccounts,
expectedErr: `couldn't get version/kind`,
},
{
name: "service account not found",
object: runtime.RawExtension{Raw: newPodRaw("pod", "ns1", "sa", map[string]string{UseWorkloadIdentityLabel: "true"}, nil, true)},
expectedErr: `serviceaccounts "sa" not found`,
},
{
name: "pod has host network",
object: runtime.RawExtension{Raw: newPodRaw("pod", "ns1", "sa",
map[string]string{UseWorkloadIdentityLabel: "true"}, map[string]string{InjectProxySidecarAnnotation: "true"}, true)},
clientObjects: serviceAccounts,
expectedErr: "hostNetwork is set to true, cannot inject proxy sidecar",
},
{
name: "invalid proxy port",
object: runtime.RawExtension{Raw: newPodRaw("pod", "ns1", "sa", map[string]string{UseWorkloadIdentityLabel: "true"},
map[string]string{InjectProxySidecarAnnotation: "true", ProxySidecarPortAnnotation: "invalid"}, false)},
clientObjects: serviceAccounts,
expectedErr: `failed to parse proxy sidecar port: strconv.ParseInt: parsing "invalid": invalid syntax`,
},
{
name: "invalid sa token expiry",
object: runtime.RawExtension{Raw: newPodRaw("pod", "ns1", "sa", map[string]string{UseWorkloadIdentityLabel: "true"},
map[string]string{ServiceAccountTokenExpiryAnnotation: "invalid"}, false)},
clientObjects: serviceAccounts,
expectedErr: `strconv.ParseInt: parsing "invalid": invalid syntax`,
},
}

for _, test := range tests {
Expand Down
Loading