Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove legacy resource and kind checks from the CLI #19657

Merged
merged 1 commit into from May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkg/api/meta/pods.go
Expand Up @@ -61,14 +61,15 @@ var resourcesToCheck = map[schema.GroupResource]schema.GroupKind{
extensions.Resource("replicasets"): extensions.Kind("ReplicaSet"),
apps.Resource("statefulsets"): apps.Kind("StatefulSet"),

appsapi.Resource("deploymentconfigs"): appsapi.Kind("DeploymentConfig"),
appsapi.LegacyResource("deploymentconfigs"): appsapi.LegacyKind("DeploymentConfig"),
securityapi.Resource("podsecuritypolicysubjectreviews"): securityapi.Kind("PodSecurityPolicySubjectReview"),
securityapi.LegacyResource("podsecuritypolicysubjectreviews"): securityapi.LegacyKind("PodSecurityPolicySubjectReview"),
securityapi.Resource("podsecuritypolicyselfsubjectreviews"): securityapi.Kind("PodSecurityPolicySelfSubjectReview"),
securityapi.LegacyResource("podsecuritypolicyselfsubjectreviews"): securityapi.LegacyKind("PodSecurityPolicySelfSubjectReview"),
securityapi.Resource("podsecuritypolicyreviews"): securityapi.Kind("PodSecurityPolicyReview"),
securityapi.LegacyResource("podsecuritypolicyreviews"): securityapi.LegacyKind("PodSecurityPolicyReview"),
{Group: "", Resource: "deploymentconfigs"}: {Group: "", Kind: "DeploymentConfig"},
{Group: "", Resource: "podsecuritypolicysubjectreviews"}: {Group: "", Kind: "PodSecurityPolicySubjectReview"},
{Group: "", Resource: "podsecuritypolicyselfsubjectreviews"}: {Group: "", Kind: "PodSecurityPolicySelfSubjectReview"},
{Group: "", Resource: "podsecuritypolicyreviews"}: {Group: "", Kind: "PodSecurityPolicyReview"},

appsapi.Resource("deploymentconfigs"): appsapi.Kind("DeploymentConfig"),
securityapi.Resource("podsecuritypolicysubjectreviews"): securityapi.Kind("PodSecurityPolicySubjectReview"),
securityapi.Resource("podsecuritypolicyselfsubjectreviews"): securityapi.Kind("PodSecurityPolicySelfSubjectReview"),
securityapi.Resource("podsecuritypolicyreviews"): securityapi.Kind("PodSecurityPolicyReview"),
}

// HasPodSpec returns true if the resource is known to have a pod spec.
Expand Down
20 changes: 2 additions & 18 deletions pkg/apps/apis/apps/register.go
Expand Up @@ -26,33 +26,17 @@ func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
func LegacyKind(kind string) schema.GroupKind {
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns back a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// LegacyResource takes an unqualified resource and returns back a Group qualified GroupResource
// LegacyResource takes an unqualified resource and returns back a Group qualified
// GroupResource using legacy API
func LegacyResource(resource string) schema.GroupResource {
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
}

// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
// up the API group and also the legacy API.
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
return gk == Kind(kind) || gk == LegacyKind(kind)
}

// IsResourceOrLegacy checks if the provided GroupResources matches with the given
// resource by looking up the API group and also the legacy API.
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
return gr == Resource(resource) || gr == LegacyResource(resource)
}

// Adds the list of known types to api.Scheme.
func addLegacyKnownTypes(scheme *runtime.Scheme) error {
types := []runtime.Object{
Expand Down
12 changes: 0 additions & 12 deletions pkg/authorization/apis/authorization/register.go
Expand Up @@ -42,18 +42,6 @@ func LegacyResource(resource string) schema.GroupResource {
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
}

// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
// up the API group and also the legacy API.
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
return gk == Kind(kind) || gk == LegacyKind(kind)
}

// IsResourceOrLegacy checks if the provided GroupResources matches with the given
// resource by looking up the API group and also the legacy API.
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
return gr == Resource(resource) || gr == LegacyResource(resource)
}

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down
7 changes: 6 additions & 1 deletion pkg/build/admission/jenkinsbootstrapper/admission.go
Expand Up @@ -63,7 +63,12 @@ func (a *jenkinsBootstrapper) Admit(attributes admission.Attributes) error {
return nil
}
gr := attributes.GetResource().GroupResource()
if !buildapi.IsResourceOrLegacy("buildconfigs", gr) && !buildapi.IsResourceOrLegacy("builds", gr) {
switch gr {
case buildapi.Resource("buildconfigs"),
buildapi.Resource("builds"),
buildapi.LegacyResource("buildconfigs"),
buildapi.LegacyResource("builds"):
default:
return nil
}
if !needsJenkinsTemplate(attributes.GetObject()) {
Expand Down
26 changes: 17 additions & 9 deletions pkg/build/admission/strategyrestrictions/admission.go
Expand Up @@ -49,12 +49,17 @@ func NewBuildByStrategy() admission.Interface {

func (a *buildByStrategy) Admit(attr admission.Attributes) error {
gr := attr.GetResource().GroupResource()
if !buildapi.IsResourceOrLegacy("buildconfigs", gr) && !buildapi.IsResourceOrLegacy("builds", gr) {
return nil
}
// Explicitly exclude the builds/details subresource because it's only
// updating commit info and cannot change build type.
if buildapi.IsResourceOrLegacy("builds", gr) && attr.GetSubresource() == "details" {
switch gr {
case buildapi.Resource("buildconfigs"),
buildapi.LegacyResource("buildconfigs"):
case buildapi.Resource("builds"),
buildapi.LegacyResource("builds"):
// Explicitly exclude the builds/details subresource because it's only
// updating commit info and cannot change build type.
if attr.GetSubresource() == "details" {
return nil
}
default:
return nil
}

Expand Down Expand Up @@ -177,8 +182,9 @@ func (a *buildByStrategy) checkBuildConfigAuthorization(buildConfig *buildapi.Bu

func (a *buildByStrategy) checkBuildRequestAuthorization(req *buildapi.BuildRequest, attr admission.Attributes) error {
gr := attr.GetResource().GroupResource()
switch {
case buildapi.IsResourceOrLegacy("builds", gr):
switch gr {
case buildapi.Resource("builds"),
buildapi.LegacyResource("builds"):
build, err := a.buildClient.Build().Builds(attr.GetNamespace()).Get(req.Name, metav1.GetOptions{})
if err != nil {
return admission.NewForbidden(attr, err)
Expand All @@ -188,7 +194,9 @@ func (a *buildByStrategy) checkBuildRequestAuthorization(req *buildapi.BuildRequ
return admission.NewForbidden(attr, err)
}
return a.checkBuildAuthorization(internalBuild, attr)
case buildapi.IsResourceOrLegacy("buildconfigs", gr):

case buildapi.Resource("buildconfigs"),
buildapi.LegacyResource("buildconfigs"):
buildConfig, err := a.buildClient.Build().BuildConfigs(attr.GetNamespace()).Get(req.Name, metav1.GetOptions{})
if err != nil {
return admission.NewForbidden(attr, err)
Expand Down
18 changes: 0 additions & 18 deletions pkg/build/apis/build/register.go
Expand Up @@ -27,33 +27,15 @@ func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
func LegacyKind(kind string) schema.GroupKind {
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns back a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// LegacyResource takes an unqualified resource and returns back a Group qualified GroupResource
func LegacyResource(resource string) schema.GroupResource {
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
}

// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
// up the API group and also the legacy API.
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
return gk == Kind(kind) || gk == LegacyKind(kind)
}

// IsResourceOrLegacy checks if the provided GroupResources matches with the given
// resource by looking up the API group and also the legacy API.
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
return gr == Resource(resource) || gr == LegacyResource(resource)
}

// addKnownTypes adds types to API group
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/registry/buildconfig/webhook.go
Expand Up @@ -110,7 +110,7 @@ func (w *WebHookHandler) ProcessWebHook(writer http.ResponseWriter, req *http.Re

plugin, ok := w.plugins[hookType]
if !ok {
return errors.NewNotFound(buildapi.LegacyResource("buildconfighook"), hookType)
return errors.NewNotFound(buildapi.Resource("buildconfighook"), hookType)
}

config, err := w.buildConfigClient.BuildConfigs(apirequest.NamespaceValue(ctx)).Get(name, metav1.GetOptions{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/registry/buildconfig/webhook_test.go
Expand Up @@ -404,7 +404,7 @@ func TestInvokeWebhookMissingPlugin(t *testing.T) {
t.Errorf("Unexpected error: %v", err)
}
if !responder.called ||
!strings.Contains(responder.err.Error(), `buildconfighook "missingplugin" not found`) {
!strings.Contains(responder.err.Error(), `buildconfighook.build.openshift.io "missingplugin" not found`) {
t.Errorf("Expected BadRequest, got %s, expected error %s!", responder.err.Error(), `buildconfighook.build.openshift.io "missingplugin" not found`)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/image/admission/admission.go
Expand Up @@ -106,7 +106,7 @@ func (a *imageLimitRangerPlugin) SupportsAttributes(attr admission.Attributes) b
return false
}
gk := attr.GetKind().GroupKind()
return imageapi.IsKindOrLegacy("ImageStreamMapping", gk)
return imageapi.Kind("ImageStreamMapping") == gk || imageapi.LegacyKind("ImageStreamMapping") == gk
}

// SupportsLimit provides a check to see if the limitRange is applicable to image objects.
Expand Down
12 changes: 0 additions & 12 deletions pkg/image/apis/image/register.go
Expand Up @@ -40,18 +40,6 @@ func LegacyResource(resource string) schema.GroupResource {
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
}

// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
// up the API group and also the legacy API.
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
return gk == Kind(kind) || gk == LegacyKind(kind)
}

// IsResourceOrLegacy checks if the provided GroupResources matches with the given
// resource by looking up the API group and also the legacy API.
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
return gr == Resource(resource) || gr == LegacyResource(resource)
}

// Adds the list of known types to api.Scheme.
func addLegacyKnownTypes(scheme *runtime.Scheme) error {
types := []runtime.Object{
Expand Down
22 changes: 0 additions & 22 deletions pkg/network/apis/network/register.go
Expand Up @@ -27,33 +27,11 @@ func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
func LegacyKind(kind string) schema.GroupKind {
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns back a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// LegacyResource takes an unqualified resource and returns back a Group qualified GroupResource
func LegacyResource(resource string) schema.GroupResource {
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
}

// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
// up the API group and also the legacy API.
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
return gk == Kind(kind) || gk == LegacyKind(kind)
}

// IsResourceOrLegacy checks if the provided GroupResources matches with the given
// resource by looking up the API group and also the legacy API.
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
return gr == Resource(resource) || gr == LegacyResource(resource)
}

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down
22 changes: 0 additions & 22 deletions pkg/oauth/apis/oauth/register.go
Expand Up @@ -27,33 +27,11 @@ func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
func LegacyKind(kind string) schema.GroupKind {
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns back a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// Resource takes an unqualified resource and returns back a Group qualified GroupResource
func LegacyResource(resource string) schema.GroupResource {
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
}

// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
// up the API group and also the legacy API.
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
return gk == Kind(kind) || gk == LegacyKind(kind)
}

// IsResourceOrLegacy checks if the provided GroupResources matches with the given
// resource by looking up the API group and also the legacy API.
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
return gr == Resource(resource) || gr == LegacyResource(resource)
}

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/admin/policy/reconcile_clusterroles.go
Expand Up @@ -133,7 +133,7 @@ func (o *ReconcileClusterRolesOptions) Complete(cmd *cobra.Command, f *clientcmd
if err != nil {
return err
}
if !authorizationapi.IsResourceOrLegacy("clusterroles", resource) {
if authorizationapi.Resource("clusterroles") != resource {
return fmt.Errorf("%v is not a valid resource type for this command", resource)
}
if len(name) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/oc/cli/cmd/cancelbuild.go
Expand Up @@ -154,16 +154,16 @@ func (o *CancelBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
return err
}

switch {
case buildapi.IsResourceOrLegacy("buildconfigs", resource):
switch resource {
case buildapi.Resource("buildconfigs"):
list, err := buildutil.BuildConfigBuilds(o.BuildLister, o.Namespace, name, nil)
if err != nil {
return err
}
for _, b := range list {
o.BuildNames = append(o.BuildNames, b.Name)
}
case buildapi.IsResourceOrLegacy("builds", resource):
case buildapi.Resource("builds"):
o.BuildNames = append(o.BuildNames, strings.TrimSpace(name))
default:
return fmt.Errorf("invalid resource provided: %v", resource)
Expand Down
8 changes: 6 additions & 2 deletions pkg/oc/cli/cmd/logs.go
Expand Up @@ -156,7 +156,10 @@ func (o *OpenShiftLogsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command
gr := resource.GroupResource()
// TODO: podLogOptions should be included in our own logOptions objects.
switch {
case buildapi.IsResourceOrLegacy("build", gr), buildapi.IsResourceOrLegacy("buildconfig", gr):
case buildapi.Resource("build") == gr,
buildapi.Resource("builds") == gr,
buildapi.Resource("buildconfig") == gr,
buildapi.Resource("buildconfigs") == gr:
bopts := &buildapi.BuildLogOptions{
Follow: podLogOptions.Follow,
Previous: podLogOptions.Previous,
Expand All @@ -171,7 +174,8 @@ func (o *OpenShiftLogsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command
}
o.Options = bopts

case appsapi.IsResourceOrLegacy("deploymentconfig", gr):
case appsapi.Resource("deploymentconfig") == gr,
appsapi.Resource("deploymentconfigs") == gr:
dopts := &appsapi.DeploymentLogOptions{
Container: podLogOptions.Container,
Follow: podLogOptions.Follow,
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/cmd/newapp.go
Expand Up @@ -715,7 +715,7 @@ func retryBuildConfig(info *resource.Info, err error) runtime.Object {
buildapi.GitLabWebHookBuildTriggerType: {},
buildapi.BitbucketWebHookBuildTriggerType: {},
}
if buildapi.IsKindOrLegacy("BuildConfig", info.Mapping.GroupVersionKind.GroupKind()) && isInvalidTriggerError(err) {
if buildapi.Kind("BuildConfig") == info.Mapping.GroupVersionKind.GroupKind() && isInvalidTriggerError(err) {
bc, ok := info.Object.(*buildapi.BuildConfig)
if !ok {
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/oc/cli/cmd/startbuild.go
Expand Up @@ -256,10 +256,10 @@ func (o *StartBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, c
if err != nil {
return err
}
switch {
case buildapi.IsResourceOrLegacy("buildconfigs", resource):
switch resource {
case buildapi.Resource("buildconfigs"):
// no special handling required
case buildapi.IsResourceOrLegacy("builds", resource):
case buildapi.Resource("builds"):
if len(o.ListWebhooks) == 0 {
return fmt.Errorf("use --from-build to rerun your builds")
}
Expand All @@ -269,7 +269,7 @@ func (o *StartBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, c
}

// when listing webhooks, allow --from-build to lookup a build config
if buildapi.IsResourceOrLegacy("builds", resource) && len(o.ListWebhooks) > 0 {
if buildapi.Resource("builds") == resource && len(o.ListWebhooks) > 0 {
build, err := o.BuildClient.Builds(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion pkg/oc/cli/describe/deployments.go
Expand Up @@ -293,7 +293,13 @@ func printDeploymentConfigSpec(kc kclientset.Interface, dc appsapi.DeploymentCon

// Autoscaling info
// FIXME: The CrossVersionObjectReference should specify the Group
printAutoscalingInfo([]schema.GroupResource{appsapi.Resource("DeploymentConfig"), appsapi.LegacyResource("DeploymentConfig")}, dc.Namespace, dc.Name, kc, w)
printAutoscalingInfo(
[]schema.GroupResource{
appsapi.Resource("DeploymentConfig"),
// this needs to remain as long as HPA supports putting in the "wrong" DC scheme
appsapi.LegacyResource("DeploymentConfig"),
},
dc.Namespace, dc.Name, kc, w)

// Triggers
printTriggers(spec.Triggers, w)
Expand Down
2 changes: 1 addition & 1 deletion pkg/oc/cli/util/clientcmd/factory.go
Expand Up @@ -341,7 +341,7 @@ func (f *Factory) PodForResource(resource string, timeout time.Duration) (string
return "", err
}
return pod.Name, nil
case appsapi.Resource("deploymentconfigs"), appsapi.LegacyResource("deploymentconfigs"):
case appsapi.Resource("deploymentconfigs"):
appsClient, err := appsclientinternal.NewForConfig(clientConfig)
if err != nil {
return "", err
Expand Down