Skip to content

Commit 8e62152

Browse files
committed
Add Client.Reader for webhooks and modify logs in webhooks.go
1 parent 95c6a70 commit 8e62152

2 files changed

Lines changed: 71 additions & 27 deletions

File tree

pkg/apiserver/apiserver.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ type Config struct {
8989

9090
// PorchServer contains state for a Kubernetes cluster master/api server.
9191
type PorchServer struct {
92-
GenericAPIServer *genericapiserver.GenericAPIServer
93-
coreClient client.WithWatch
94-
cache cachetypes.Cache
95-
periodicRepoSyncFrequency time.Duration
96-
ListTimeoutPerRepository time.Duration
97-
repoOperationRetryAttempts int
92+
GenericAPIServer *genericapiserver.GenericAPIServer
93+
coreClient client.WithWatch
94+
clientReader client.Reader
95+
cache cachetypes.Cache
96+
periodicRepoSyncFrequency time.Duration
97+
ListTimeoutPerRepository time.Duration
98+
repoOperationRetryAttempts int
9899
}
99100

100101
type completedConfig struct {
@@ -175,6 +176,42 @@ func (c completedConfig) getCoreClient() (client.WithWatch, error) {
175176
return coreClient, nil
176177
}
177178

179+
func (c completedConfig) getClientReader() (client.Reader, error) {
180+
restConfig, err := c.getRestConfig()
181+
if err != nil {
182+
return nil, err
183+
}
184+
185+
// set high qps/burst limits for parallel webhook operations
186+
restConfig.QPS = 200
187+
restConfig.Burst = 400
188+
189+
scheme := runtime.NewScheme()
190+
if err := configapi.AddToScheme(scheme); err != nil {
191+
return nil, fmt.Errorf("error building scheme: %w", err)
192+
}
193+
194+
if err := porchapi.AddToScheme(scheme); err != nil {
195+
return nil, fmt.Errorf("error building scheme: %w", err)
196+
}
197+
198+
if err := corev1.AddToScheme(scheme); err != nil {
199+
return nil, fmt.Errorf("error building scheme: %w", err)
200+
}
201+
if err := internalapi.AddToScheme(scheme); err != nil {
202+
return nil, fmt.Errorf("error building scheme: %w", err)
203+
}
204+
205+
clientReader, err := client.New(restConfig, client.Options{
206+
Scheme: scheme,
207+
})
208+
if err != nil {
209+
return nil, fmt.Errorf("error building client reader: %w", err)
210+
}
211+
212+
return clientReader, nil
213+
}
214+
178215
func (c completedConfig) getCoreV1Client() (*corev1client.CoreV1Client, error) {
179216
restConfig, err := c.getRestConfig()
180217
if err != nil {
@@ -203,6 +240,11 @@ func (c completedConfig) New(ctx context.Context) (*PorchServer, error) {
203240
return nil, fmt.Errorf("failed to build client for core apiserver: %w", err)
204241
}
205242

243+
clientReader, err := c.getClientReader()
244+
if err != nil {
245+
return nil, fmt.Errorf("failed to build client reader: %w", err)
246+
}
247+
206248
coreV1Client, err := c.getCoreV1Client()
207249
if err != nil {
208250
return nil, err
@@ -279,11 +321,12 @@ func (c completedConfig) New(ctx context.Context) (*PorchServer, error) {
279321
s := &PorchServer{
280322
GenericAPIServer: genericServer,
281323
coreClient: coreClient,
324+
clientReader: clientReader,
282325
cache: cacheImpl,
283326
// Set background job periodic frequency the same as repo sync frequency.
284-
periodicRepoSyncFrequency: c.ExtraConfig.CacheOptions.RepoSyncFrequency,
285-
ListTimeoutPerRepository: c.ExtraConfig.ListTimeoutPerRepository,
286-
repoOperationRetryAttempts: c.ExtraConfig.CacheOptions.RepoOperationRetryAttempts,
327+
periodicRepoSyncFrequency: c.ExtraConfig.CacheOptions.RepoSyncFrequency,
328+
ListTimeoutPerRepository: c.ExtraConfig.ListTimeoutPerRepository,
329+
repoOperationRetryAttempts: c.ExtraConfig.CacheOptions.RepoOperationRetryAttempts,
287330
}
288331

289332
// Install the groups.
@@ -305,7 +348,7 @@ func (s *PorchServer) Run(ctx context.Context) error {
305348
// but for now we keep backward compatiblity
306349
certStorageDir, found := os.LookupEnv("CERT_STORAGE_DIR")
307350
if found && strings.TrimSpace(certStorageDir) != "" {
308-
if err := setupWebhooks(ctx, s.coreClient); err != nil {
351+
if err := setupWebhooks(ctx, s.clientReader); err != nil {
309352
klog.Errorf("%v\n", err)
310353
return err
311354
}

pkg/apiserver/webhooks.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func webhookServiceName(ctx context.Context) (serviceName, serviceNamespace stri
162162
return
163163
}
164164

165-
func setupWebhooks(ctx context.Context, porchClient client.Client) error {
165+
func setupWebhooks(ctx context.Context, clientReader client.Reader) error {
166166
cfg := newWebhookConfig(ctx)
167167
if !cfg.CertManWebhook {
168168
caBytes, err := createCerts(cfg)
@@ -174,7 +174,7 @@ func setupWebhooks(ctx context.Context, porchClient client.Client) error {
174174
}
175175
}
176176

177-
if err := runWebhookServer(ctx, cfg, porchClient); err != nil {
177+
if err := runWebhookServer(ctx, cfg, clientReader); err != nil {
178178
return err
179179
}
180180
return nil
@@ -342,7 +342,7 @@ func createValidatingWebhook(ctx context.Context, cfg *WebhookConfig, caCert []b
342342
Name: repositoryCfgName,
343343
},
344344
Webhooks: []admissionregistrationv1.ValidatingWebhook{{
345-
Name: "porchrepositorywebhook.google.com",
345+
Name: "porchrepositorywebhook.nephio.org",
346346
ClientConfig: admissionregistrationv1.WebhookClientConfig{
347347
CABundle: caCert,
348348
},
@@ -352,8 +352,8 @@ func createValidatingWebhook(ctx context.Context, cfg *WebhookConfig, caCert []b
352352
admissionregistrationv1.Update,
353353
},
354354
Rule: admissionregistrationv1.Rule{
355-
APIGroups: []string{"config.porch.kpt.dev"},
356-
APIVersions: []string{"v1alpha1"},
355+
APIGroups: []string{configapi.GroupVersion.Group},
356+
APIVersions: []string{configapi.GroupVersion.Version},
357357
Resources: []string{"repositories"},
358358
},
359359
}},
@@ -468,7 +468,7 @@ func getCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) {
468468
return &cert, nil
469469
}
470470

471-
func runWebhookServer(ctx context.Context, cfg *WebhookConfig, porchClient client.Client) error {
471+
func runWebhookServer(ctx context.Context, cfg *WebhookConfig, clientReader client.Reader) error {
472472
certFile := filepath.Join(cfg.CertStorageDir, "tls.crt")
473473
keyFile := filepath.Join(cfg.CertStorageDir, "tls.key")
474474
// load the cert for the first time
@@ -483,10 +483,10 @@ func runWebhookServer(ctx context.Context, cfg *WebhookConfig, porchClient clien
483483
}
484484
klog.Infoln("Starting webhook server")
485485
http.HandleFunc(cfg.Path, func(w http.ResponseWriter, r *http.Request) {
486-
validateDeletion(w, r, porchClient)
486+
validateDeletion(w, r, clientReader)
487487
})
488488
http.HandleFunc(cfg.RepositoryPath, func(w http.ResponseWriter, r *http.Request) {
489-
validateRepository(w, r, porchClient)
489+
validateRepository(w, r, clientReader)
490490
})
491491
server := http.Server{
492492
Addr: fmt.Sprintf(":%d", cfg.Port),
@@ -506,7 +506,7 @@ func runWebhookServer(ctx context.Context, cfg *WebhookConfig, porchClient clien
506506

507507
}
508508

509-
func validateDeletion(w http.ResponseWriter, r *http.Request, porchClient client.Client) {
509+
func validateDeletion(w http.ResponseWriter, r *http.Request, clientReader client.Reader) {
510510
klog.Infoln("received request to validate deletion")
511511

512512
admissionReviewRequest, err := decodeAdmissionReview(r)
@@ -525,7 +525,7 @@ func validateDeletion(w http.ResponseWriter, r *http.Request, porchClient client
525525

526526
// Get the package revision using the name and namespace from the request.
527527
pr := porchapi.PackageRevision{}
528-
if err := porchClient.Get(context.Background(), client.ObjectKey{
528+
if err := clientReader.Get(context.Background(), client.ObjectKey{
529529
Namespace: admissionReviewRequest.Request.Namespace,
530530
Name: admissionReviewRequest.Request.Name,
531531
}, &pr); err != nil {
@@ -646,14 +646,17 @@ func getEnvInt32(key string, defaultValue int32) int32 {
646646
return int32(i64) // this is safe because of the size parameter of the ParseInt call
647647
}
648648

649-
func validateRepository(w http.ResponseWriter, r *http.Request, porchClient client.Client) {
650-
klog.Infoln("received request to validate repository")
649+
func validateRepository(w http.ResponseWriter, r *http.Request, clientReader client.Reader) {
651650
admissionReviewRequest, err := decodeAdmissionReview(r)
652651
if err != nil {
653652
writeErr(fmt.Sprintf("error decoding admission review: %v", err), &w)
654653
return
655654
}
656655

656+
operation := strings.ToLower(string(admissionReviewRequest.Request.Operation))
657+
repoName := fmt.Sprintf("%s/%s", admissionReviewRequest.Request.Namespace, admissionReviewRequest.Request.Name)
658+
klog.Infof("received request to validate repository %s for %s", operation, repoName)
659+
657660
if admissionReviewRequest.Request.Resource.Resource != "repositories" {
658661
writeErr(fmt.Sprintf("unexpected resource: %s", admissionReviewRequest.Request.Resource.Resource), &w)
659662
return
@@ -681,7 +684,7 @@ func validateRepository(w http.ResponseWriter, r *http.Request, porchClient clie
681684
}
682685

683686
var repoList configapi.RepositoryList
684-
if err := porchClient.List(context.Background(), &repoList); err != nil {
687+
if err := clientReader.List(context.Background(), &repoList); err != nil {
685688
klog.Errorf("failed to list repositories: %v", err)
686689
writeErr(fmt.Sprintf("could not list repositories: %v", err), &w)
687690
return
@@ -715,8 +718,7 @@ func validateRepository(w http.ResponseWriter, r *http.Request, porchClient clie
715718
w.Header().Set("Content-Type", "application/json")
716719
_, err = w.Write(responseBytes)
717720
if err != nil {
718-
errMsg := fmt.Sprintf("error writing response: %v", err)
719-
writeErr(errMsg, &w)
721+
klog.Errorf("error writing response: %v", err)
720722
return
721723
}
722724
}
@@ -825,7 +827,6 @@ func writeModificationResponse(message, reason string, admissionReviewRequest *a
825827
(*w).Header().Set("Content-Type", "application/json")
826828
_, err = (*w).Write(responseBytes)
827829
if err != nil {
828-
errMsg := fmt.Sprintf("error writing response: %v", err)
829-
writeErr(errMsg, w)
830+
klog.Errorf("error writing response: %v", err)
830831
}
831832
}

0 commit comments

Comments
 (0)