Skip to content

Commit e9e669a

Browse files
authored
Merge pull request kubernetes#107276 from jlsong01/fix_flake_TestQuotaLimitService
Fix flake on TestQuotaLimitService
2 parents 86f863c + 3006aa5 commit e9e669a

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

test/integration/auth/auth_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import (
6262
clientset "k8s.io/client-go/kubernetes"
6363
"k8s.io/client-go/rest"
6464
v1 "k8s.io/client-go/tools/clientcmd/api/v1"
65+
resttransport "k8s.io/client-go/transport"
6566
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
6667
"k8s.io/kubernetes/pkg/apis/autoscaling"
6768
api "k8s.io/kubernetes/pkg/apis/core"
@@ -555,11 +556,9 @@ func TestAuthModeAlwaysDeny(t *testing.T) {
555556
controlPlaneConfig.GenericConfig.Authorization.Authorizer = authorizerfactory.NewAlwaysDenyAuthorizer()
556557
_, s, closeFn := framework.RunAnAPIServer(controlPlaneConfig)
557558
defer closeFn()
558-
559559
ns := framework.CreateTestingNamespace("auth-always-deny", s, t)
560560
defer framework.DeleteTestingNamespace(ns, s, t)
561-
562-
transport := http.DefaultTransport
561+
transport := resttransport.NewBearerAuthRoundTripper(framework.UnprivilegedUserToken, http.DefaultTransport)
563562

564563
for _, r := range getTestRequests(ns.Name) {
565564
bodyBytes := bytes.NewReader([]byte(r.body))

test/integration/controlplane/synthetic_controlplane_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ import (
3838
apierrors "k8s.io/apimachinery/pkg/api/errors"
3939
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4040
"k8s.io/apimachinery/pkg/util/wait"
41+
authauthenticator "k8s.io/apiserver/pkg/authentication/authenticator"
4142
"k8s.io/apiserver/pkg/authentication/group"
4243
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
44+
authenticatorunion "k8s.io/apiserver/pkg/authentication/request/union"
4345
"k8s.io/apiserver/pkg/authentication/user"
4446
"k8s.io/apiserver/pkg/authorization/authorizer"
4547
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
@@ -141,6 +143,15 @@ func TestEmptyList(t *testing.T) {
141143

142144
func initStatusForbiddenControlPlaneConfig() *controlplane.Config {
143145
controlPlaneConfig := framework.NewIntegrationTestControlPlaneConfig()
146+
controlPlaneConfig.GenericConfig.Authentication.Authenticator = authenticatorunion.New(
147+
authauthenticator.RequestFunc(func(req *http.Request) (*authauthenticator.Response, bool, error) {
148+
return &authauthenticator.Response{
149+
User: &user.DefaultInfo{
150+
Name: "unprivileged",
151+
Groups: []string{user.AllAuthenticated},
152+
},
153+
}, true, nil
154+
}))
144155
controlPlaneConfig.GenericConfig.Authorization.Authorizer = authorizerfactory.NewAlwaysDenyAuthorizer()
145156
return controlPlaneConfig
146157
}
@@ -178,7 +189,7 @@ func TestStatus(t *testing.T) {
178189
statusCode: http.StatusForbidden,
179190
reqPath: "/apis",
180191
reason: "Forbidden",
181-
message: `forbidden: User "" cannot get path "/apis": Everything is forbidden.`,
192+
message: `forbidden: User "unprivileged" cannot get path "/apis": Everything is forbidden.`,
182193
},
183194
{
184195
name: "401",

test/integration/framework/controlplane_utils.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ import (
6161
netutils "k8s.io/utils/net"
6262
)
6363

64+
const (
65+
UnprivilegedUserToken = "unprivileged-user"
66+
)
67+
6468
// Config is a struct of configuration directives for NewControlPlaneComponents.
6569
type Config struct {
6670
// If nil, a default is used, partially filled configs will not get populated.
@@ -80,11 +84,16 @@ func (alwaysAllow) Authorize(ctx context.Context, requestAttributes authorizer.A
8084
return authorizer.DecisionAllow, "always allow", nil
8185
}
8286

83-
// alwaysEmpty simulates "no authentication" for old tests
84-
func alwaysEmpty(req *http.Request) (*authauthenticator.Response, bool, error) {
87+
// unsecuredUser simulates requests to the unsecured endpoint for old tests
88+
func unsecuredUser(req *http.Request) (*authauthenticator.Response, bool, error) {
89+
auth := req.Header.Get("Authorization")
90+
if len(auth) != 0 {
91+
return nil, false, nil
92+
}
8593
return &authauthenticator.Response{
8694
User: &user.DefaultInfo{
87-
Name: "",
95+
Name: "system:unsecured",
96+
Groups: []string{user.SystemPrivilegedGroup, user.AllAuthenticated},
8897
},
8998
}, true, nil
9099
}
@@ -171,12 +180,17 @@ func startAPIServerOrDie(controlPlaneConfig *controlplane.Config, incomingServer
171180
tokens[privilegedLoopbackToken] = &user.DefaultInfo{
172181
Name: user.APIServerUser,
173182
UID: uuid.New().String(),
174-
Groups: []string{user.SystemPrivilegedGroup},
183+
Groups: []string{user.SystemPrivilegedGroup, user.AllAuthenticated},
184+
}
185+
tokens[UnprivilegedUserToken] = &user.DefaultInfo{
186+
Name: "unprivileged",
187+
UID: uuid.New().String(),
188+
Groups: []string{user.AllAuthenticated},
175189
}
176190

177191
tokenAuthenticator := authenticatorfactory.NewFromTokens(tokens, controlPlaneConfig.GenericConfig.Authentication.APIAudiences)
178192
if controlPlaneConfig.GenericConfig.Authentication.Authenticator == nil {
179-
controlPlaneConfig.GenericConfig.Authentication.Authenticator = authenticatorunion.New(tokenAuthenticator, authauthenticator.RequestFunc(alwaysEmpty))
193+
controlPlaneConfig.GenericConfig.Authentication.Authenticator = authenticatorunion.New(tokenAuthenticator, authauthenticator.RequestFunc(unsecuredUser))
180194
} else {
181195
controlPlaneConfig.GenericConfig.Authentication.Authenticator = authenticatorunion.New(tokenAuthenticator, controlPlaneConfig.GenericConfig.Authentication.Authenticator)
182196
}

0 commit comments

Comments
 (0)