Skip to content

Commit

Permalink
images/kube-webhook-certgen/rootfs: pass failure policy by value
Browse files Browse the repository at this point in the history
k8s.k8s.patchWebhookConfigurations() always dereferences it and we do
not do a nil check, so the code may panic in some conditions, so it's
safer to just pass it by value, as it's just a wrapped string.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
  • Loading branch information
invidian committed Sep 16, 2021
1 parent 96d20f1 commit d573087
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion images/kube-webhook-certgen/rootfs/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Patch(ctx context.Context, cfg *PatchConfig) error {

options := k8s.PatchOptions{
CABundle: ca,
FailurePolicyType: &failurePolicy,
FailurePolicyType: failurePolicy,
APIServiceName: cfg.APIServiceName,
}

Expand Down
8 changes: 4 additions & 4 deletions images/kube-webhook-certgen/rootfs/cmd/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func Test_Patch(t *testing.T) {

patcher := testPatcher()
patcher.patchObjects = func(_ context.Context, options k8s.PatchOptions) error {
if options.FailurePolicyType != nil {
return fmt.Errorf("expected policy to be nil. got: %q", *options.FailurePolicyType)
if options.FailurePolicyType != "" {
return fmt.Errorf("expected policy to be nil. got: %q", options.FailurePolicyType)
}

return nil
Expand All @@ -146,8 +146,8 @@ func Test_Patch(t *testing.T) {

patcher := testPatcher()
patcher.patchObjects = func(_ context.Context, options k8s.PatchOptions) error {
if options.FailurePolicyType == nil || *options.FailurePolicyType != "Fail" {
return fmt.Errorf("unexpected policy: %q", *options.FailurePolicyType)
if options.FailurePolicyType == "" || options.FailurePolicyType != "Fail" {
return fmt.Errorf("unexpected policy: %q", options.FailurePolicyType)
}

return nil
Expand Down
20 changes: 10 additions & 10 deletions images/kube-webhook-certgen/rootfs/pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ type PatchOptions struct {
MutatingWebhookConfigurationName string
APIServiceName string
CABundle []byte
FailurePolicyType *admissionv1.FailurePolicyType
FailurePolicyType admissionv1.FailurePolicyType
}

func (k8s *k8s) PatchObjects(ctx context.Context, options PatchOptions) error {
patchMutating := options.MutatingWebhookConfigurationName != ""
patchValidating := options.ValidatingWebhookConfigurationName != ""
patchAPIService := options.APIServiceName != ""

if !patchMutating && !patchValidating && options.FailurePolicyType != nil {
if !patchMutating && !patchValidating && options.FailurePolicyType != "" {
return fmt.Errorf("failurePolicy specified, but no webhook will be patched")
}

Expand Down Expand Up @@ -105,11 +105,11 @@ func (k8s *k8s) patchWebhookConfigurations(
ctx context.Context,
configurationName string,
ca []byte,
failurePolicy *admissionv1.FailurePolicyType,
failurePolicy admissionv1.FailurePolicyType,
patchMutating bool,
patchValidating bool,
) error {
log.Infof("patching webhook configurations '%s' mutating=%t, validating=%t, failurePolicy=%s", configurationName, patchMutating, patchValidating, *failurePolicy)
log.Infof("patching webhook configurations '%s' mutating=%t, validating=%t, failurePolicy=%s", configurationName, patchMutating, patchValidating, failurePolicy)

if patchValidating {
if err := k8s.patchValidating(ctx, configurationName, ca, failurePolicy); err != nil {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (err wrappedError) Unwrap() error {
return err.err
}

func (k8s *k8s) patchValidating(ctx context.Context, configurationName string, ca []byte, failurePolicy *admissionv1.FailurePolicyType) error {
func (k8s *k8s) patchValidating(ctx context.Context, configurationName string, ca []byte, failurePolicy admissionv1.FailurePolicyType) error {
valHook, err := k8s.clientset.
AdmissionregistrationV1().
ValidatingWebhookConfigurations().
Expand All @@ -162,8 +162,8 @@ func (k8s *k8s) patchValidating(ctx context.Context, configurationName string, c
for i := range valHook.Webhooks {
h := &valHook.Webhooks[i]
h.ClientConfig.CABundle = ca
if *failurePolicy != "" {
h.FailurePolicy = failurePolicy
if failurePolicy != "" {
h.FailurePolicy = &failurePolicy
}
}

Expand All @@ -180,7 +180,7 @@ func (k8s *k8s) patchValidating(ctx context.Context, configurationName string, c
return nil
}

func (k8s *k8s) patchMutating(ctx context.Context, configurationName string, ca []byte, failurePolicy *admissionv1.FailurePolicyType) error {
func (k8s *k8s) patchMutating(ctx context.Context, configurationName string, ca []byte, failurePolicy admissionv1.FailurePolicyType) error {
mutHook, err := k8s.clientset.
AdmissionregistrationV1().
MutatingWebhookConfigurations().
Expand All @@ -195,8 +195,8 @@ func (k8s *k8s) patchMutating(ctx context.Context, configurationName string, ca
for i := range mutHook.Webhooks {
h := &mutHook.Webhooks[i]
h.ClientConfig.CABundle = ca
if *failurePolicy != "" {
h.FailurePolicy = failurePolicy
if failurePolicy != "" {
h.FailurePolicy = &failurePolicy
}
}

Expand Down
2 changes: 1 addition & 1 deletion images/kube-webhook-certgen/rootfs/pkg/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestPatchWebhookConfigurations(t *testing.T) {
Webhooks: []admissionv1.ValidatingWebhook{{Name: "v1"}, {Name: "v2"}},
}, metav1.CreateOptions{})

if err := k.patchWebhookConfigurations(context.TODO(), testWebhookName, ca, &fail, true, true); err != nil {
if err := k.patchWebhookConfigurations(context.TODO(), testWebhookName, ca, fail, true, true); err != nil {
t.Fatalf("Unexpected error patching webhooks: %s: %v", err.Error(), errors.Unwrap(err))
}

Expand Down

0 comments on commit d573087

Please sign in to comment.