@@ -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