Skip to content

Commit

Permalink
Reduce the number of hardwired string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed Aug 23, 2024
1 parent 6b75964 commit ae138e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
15 changes: 7 additions & 8 deletions pkg/apiserver/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

"github.com/fsnotify/fsnotify"

"github.com/nephio-project/porch/api/porch/v1alpha1"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/util/porch"
"github.com/nephio-project/porch/pkg/util"
admissionv1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -293,9 +293,9 @@ func createValidatingWebhook(ctx context.Context, cfg *WebhookConfig, caCert []b
Rules: []admissionregistrationv1.RuleWithOperations{{Operations: []admissionregistrationv1.OperationType{
admissionregistrationv1.Delete},
Rule: admissionregistrationv1.Rule{
APIGroups: []string{"porch.kpt.dev"},
APIVersions: []string{"v1alpha1"},
Resources: []string{"packagerevisions"},
APIGroups: []string{porchapi.SchemeGroupVersion.Group},
APIVersions: []string{porchapi.SchemeGroupVersion.Version},
Resources: []string{porchapi.PackageRevisionGVR.Resource},
},
}},
AdmissionReviewVersions: []string{"v1", "v1beta1"},
Expand Down Expand Up @@ -439,8 +439,7 @@ func validateDeletion(w http.ResponseWriter, r *http.Request) {
}

// Verify that we have a PackageRevision resource
pkgRevGVK := metav1.GroupVersionResource{Group: "porch.kpt.dev", Version: "v1alpha1", Resource: "packagerevisions"}
if admissionReviewRequest.Request.Resource != pkgRevGVK {
if admissionReviewRequest.Request.Resource != util.SchemaToMetaGVR(porchapi.PackageRevisionGVR) {
errMsg := fmt.Sprintf("did not receive PackageRevision, got %s", admissionReviewRequest.Request.Resource.Resource)
writeErr(errMsg, &w)
return
Expand All @@ -453,7 +452,7 @@ func validateDeletion(w http.ResponseWriter, r *http.Request) {
writeErr(errMsg, &w)
return
}
pr := v1alpha1.PackageRevision{}
pr := porchapi.PackageRevision{}
if err := porchClient.Get(context.Background(), client.ObjectKey{
Namespace: admissionReviewRequest.Request.Namespace,
Name: admissionReviewRequest.Request.Name,
Expand All @@ -462,7 +461,7 @@ func validateDeletion(w http.ResponseWriter, r *http.Request) {
}

admissionResponse := &admissionv1.AdmissionResponse{}
if pr.Spec.Lifecycle == v1alpha1.PackageRevisionLifecyclePublished {
if pr.Spec.Lifecycle == porchapi.PackageRevisionLifecyclePublished {
admissionResponse.Allowed = false
admissionResponse.Result = &metav1.Status{
Status: "Failure",
Expand Down
16 changes: 14 additions & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
"fmt"
"os"

porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
registrationapi "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand Down Expand Up @@ -76,15 +79,24 @@ func GetPorchApiServiceKey(ctx context.Context) (client.ObjectKey, error) {
}

apiSvc := registrationapi.APIService{}
apiSvcName := porchapi.SchemeGroupVersion.Version + "." + porchapi.SchemeGroupVersion.Group
err = c.Get(ctx, client.ObjectKey{
Name: "v1alpha1.porch.kpt.dev",
Name: apiSvcName,
}, &apiSvc)
if err != nil {
return client.ObjectKey{}, fmt.Errorf("failed to get APIService 'v1alpha1.porch.kpt.dev': %w", err)
return client.ObjectKey{}, fmt.Errorf("failed to get APIService %q: %w", apiSvcName, err)
}

return client.ObjectKey{
Namespace: apiSvc.Spec.Service.Namespace,
Name: apiSvc.Spec.Service.Name,
}, nil
}

func SchemaToMetaGVR(gvr schema.GroupVersionResource) metav1.GroupVersionResource {
return metav1.GroupVersionResource{
Group: gvr.Group,
Version: gvr.Version,
Resource: gvr.Resource,
}
}
4 changes: 2 additions & 2 deletions test/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (t *TestSuite) IsPorchServerInCluster() bool {
porch := aggregatorv1.APIService{}
ctx := context.TODO()
t.GetF(ctx, client.ObjectKey{
Name: "v1alpha1.porch.kpt.dev",
Name: porchapi.SchemeGroupVersion.Version + "." + porchapi.SchemeGroupVersion.Group,
}, &porch)
service := coreapi.Service{}
t.GetF(ctx, client.ObjectKey{
Expand All @@ -173,7 +173,7 @@ func (t *TestSuite) IsTestRunnerInCluster() bool {
porch := aggregatorv1.APIService{}
ctx := context.TODO()
t.GetF(ctx, client.ObjectKey{
Name: "v1alpha1.porch.kpt.dev",
Name: porchapi.SchemeGroupVersion.Version + "." + porchapi.SchemeGroupVersion.Group,
}, &porch)
service := coreapi.Service{}
err := t.client.Get(ctx, client.ObjectKey{
Expand Down

0 comments on commit ae138e7

Please sign in to comment.