Skip to content

Commit 7dd198f

Browse files
committed
envtest expose 'SecureConfig' for user conveinience.
Please note that this just contains secure endpoint itself and its CA certs. User will have to set authentication information by themselves and configure some authn module in kube-apiserver.
1 parent b231ddb commit 7dd198f

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

pkg/envtest/server.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,34 @@ type Environment struct {
8787
// ControlPlane is the ControlPlane including the apiserver and etcd
8888
ControlPlane integration.ControlPlane
8989

90-
// Config can be used to talk to the apiserver. It's automatically
91-
// populated if not set using the standard controller-runtime config
90+
// Config can be used to talk to the apiserver (insecure endpoint).
91+
// It's automatically populated if not set using the standard controller-runtime config
9292
// loading.
9393
Config *rest.Config
9494

95+
// SecureConfig can be used to talk to the apiserver (secure endpoint).
96+
// It's automatically populated if not set using the standard controller-runtime config
97+
// loading. This just contains secure endpoint and tlsconfig (no authn info).
98+
// To use this config, you have to configure kube-apiserver with some authn module(static token, basic auth, etc.)
99+
// and set your authentication info to this config. For example:
100+
//
101+
// // basic authn plugin case
102+
// te := &envtest.Environment{
103+
// KubeAPIServerFlags: append(
104+
// envtest.DefaultKubeAPIServerFlags,
105+
// "--basic-auth-file=my-file", "--authorization-mode=RBAC",
106+
// ),
107+
// }
108+
// te.Start()
109+
//
110+
// cfg := rest.CopyConfig(te.SecureConfig)
111+
// cfg.Username = "myname"
112+
// cfg.Password = "mypassword"
113+
//
114+
// // This client can send a request as "myname" user.
115+
// cli := client.New(cfg)
116+
SecureConfig *rest.Config
117+
95118
// CRDInstallOptions are the options for installing CRDs.
96119
CRDInstallOptions CRDInstallOptions
97120

@@ -249,6 +272,13 @@ func (te *Environment) Start() (*rest.Config, error) {
249272
QPS: 1000.0,
250273
Burst: 2000.0,
251274
}
275+
te.SecureConfig = &rest.Config{
276+
Host: fmt.Sprintf("%s:%d", te.ControlPlane.APIURL().Hostname(), te.ControlPlane.APIServer.SecurePort),
277+
TLSClientConfig: te.ControlPlane.APIServer.TLSClientConfig,
278+
// gotta go fast during tests -- we don't really care about overwhelming our test API server
279+
QPS: 1000.0,
280+
Burst: 2000.0,
281+
}
252282
}
253283

254284
log.V(1).Info("installing CRDs")

pkg/internal/testing/integration/apiserver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"fmt"
55
"io"
66
"io/ioutil"
7-
"k8s.io/client-go/rest"
87
"net/url"
98
"os"
109
"path/filepath"
1110
"time"
1211

12+
"k8s.io/client-go/rest"
13+
1314
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
1415
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/internal"
1516
)
@@ -25,7 +26,7 @@ type APIServer struct {
2526
SecurePort int
2627

2728
// TLSconfig is tls configuration to connect to its secure endpoint.
28-
TlsClientConfig rest.TLSClientConfig
29+
TLSClientConfig rest.TLSClientConfig
2930

3031
// Path is the path to the apiserver binary.
3132
//
@@ -161,7 +162,7 @@ func (s *APIServer) populateAPIServerCerts() error {
161162
return err
162163
}
163164

164-
s.TlsClientConfig = rest.TLSClientConfig{
165+
s.TLSClientConfig = rest.TLSClientConfig{
165166
CAData: ca.CA.CertBytes(),
166167
}
167168

0 commit comments

Comments
 (0)