kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
9.04k stars 2.94k forks source link

Simulate unauthorized for fake client #1195

Closed dgoradia closed 1 year ago

dgoradia commented 1 year ago

Hello,

Trying to run some unit tests to simulate unauthorized access using the fake client (k8s.io/client-go/kubernetes/fake). I've tried using PrependReactor but it's unclear:

kclient := fake.NewSimpleClientset()
kclient.(*fake.Clientset).PrependReactor("get", "selfsubjectaccessreviews", func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
    return true, &authv1.SelfSubjectAccessReview{}, errors.New("Unauthorized")
})
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
    Lock: &resourcelock.LeaseLock{
        LeaseMeta: v1.ObjectMeta{
            Name:      leaseName,
            Namespace: namespace,
        },
        Client: kclient.CoordinationV1(),
        LockConfig: resourcelock.ResourceLockConfig{
            Identity: id,
        },
    },
    ReleaseOnCancel: true,
    LeaseDuration:   60 * time.Second,
    RenewDeadline:   15 * time.Second,
    RetryPeriod:     5 * time.Second,
    Callbacks: leaderelection.LeaderCallbacks{
        OnStartedLeading: func(context.Context) {
            fmt.Println("started")
        },
        OnStoppedLeading: func() {
            <-ctx.Done()
            fmt.Println("leader lost")
        },
        OnNewLeader: func(identity string) {
            if identity == id {
                return
            }
            fmt.Println("new leader elected")
        },
    },
})

Also tried

kclient.CoordinationV1().(*fakecoord.FakeCoordinationV1).
            PrependReactor("get", "selfsubjectaccessreviews", func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
    return true, &authv1.SelfSubjectAccessReview{}, errors.New("Unauthorized")
})

The the AddReactor variant.

Either way the a leader is successfully elected. I'm expecting the client to return unauthorized instead.

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-ci-robot commented 1 year ago

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to [this](https://github.com/kubernetes/client-go/issues/1195#issuecomment-1535830975): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue with `/reopen` >- Mark this issue as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close not-planned > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.