Skip to content

Commit 8af9b16

Browse files
Merge pull request openshift#19657 from deads2k/cli-35-legacy-01
remove legacy resource and kind checks from the CLI
2 parents b1249d9 + 616ac9a commit 8af9b16

File tree

38 files changed

+127
-336
lines changed

38 files changed

+127
-336
lines changed

pkg/api/meta/pods.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ var resourcesToCheck = map[schema.GroupResource]schema.GroupKind{
6161
extensions.Resource("replicasets"): extensions.Kind("ReplicaSet"),
6262
apps.Resource("statefulsets"): apps.Kind("StatefulSet"),
6363

64-
appsapi.Resource("deploymentconfigs"): appsapi.Kind("DeploymentConfig"),
65-
appsapi.LegacyResource("deploymentconfigs"): appsapi.LegacyKind("DeploymentConfig"),
66-
securityapi.Resource("podsecuritypolicysubjectreviews"): securityapi.Kind("PodSecurityPolicySubjectReview"),
67-
securityapi.LegacyResource("podsecuritypolicysubjectreviews"): securityapi.LegacyKind("PodSecurityPolicySubjectReview"),
68-
securityapi.Resource("podsecuritypolicyselfsubjectreviews"): securityapi.Kind("PodSecurityPolicySelfSubjectReview"),
69-
securityapi.LegacyResource("podsecuritypolicyselfsubjectreviews"): securityapi.LegacyKind("PodSecurityPolicySelfSubjectReview"),
70-
securityapi.Resource("podsecuritypolicyreviews"): securityapi.Kind("PodSecurityPolicyReview"),
71-
securityapi.LegacyResource("podsecuritypolicyreviews"): securityapi.LegacyKind("PodSecurityPolicyReview"),
64+
{Group: "", Resource: "deploymentconfigs"}: {Group: "", Kind: "DeploymentConfig"},
65+
{Group: "", Resource: "podsecuritypolicysubjectreviews"}: {Group: "", Kind: "PodSecurityPolicySubjectReview"},
66+
{Group: "", Resource: "podsecuritypolicyselfsubjectreviews"}: {Group: "", Kind: "PodSecurityPolicySelfSubjectReview"},
67+
{Group: "", Resource: "podsecuritypolicyreviews"}: {Group: "", Kind: "PodSecurityPolicyReview"},
68+
69+
appsapi.Resource("deploymentconfigs"): appsapi.Kind("DeploymentConfig"),
70+
securityapi.Resource("podsecuritypolicysubjectreviews"): securityapi.Kind("PodSecurityPolicySubjectReview"),
71+
securityapi.Resource("podsecuritypolicyselfsubjectreviews"): securityapi.Kind("PodSecurityPolicySelfSubjectReview"),
72+
securityapi.Resource("podsecuritypolicyreviews"): securityapi.Kind("PodSecurityPolicyReview"),
7273
}
7374

7475
// HasPodSpec returns true if the resource is known to have a pod spec.

pkg/apps/apis/apps/register.go

+2-18
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,17 @@ func Kind(kind string) schema.GroupKind {
2626
return SchemeGroupVersion.WithKind(kind).GroupKind()
2727
}
2828

29-
// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
30-
func LegacyKind(kind string) schema.GroupKind {
31-
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
32-
}
33-
3429
// Resource takes an unqualified resource and returns back a Group qualified GroupResource
3530
func Resource(resource string) schema.GroupResource {
3631
return SchemeGroupVersion.WithResource(resource).GroupResource()
3732
}
3833

39-
// LegacyResource takes an unqualified resource and returns back a Group qualified GroupResource
34+
// LegacyResource takes an unqualified resource and returns back a Group qualified
35+
// GroupResource using legacy API
4036
func LegacyResource(resource string) schema.GroupResource {
4137
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
4238
}
4339

44-
// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
45-
// up the API group and also the legacy API.
46-
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
47-
return gk == Kind(kind) || gk == LegacyKind(kind)
48-
}
49-
50-
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
51-
// resource by looking up the API group and also the legacy API.
52-
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
53-
return gr == Resource(resource) || gr == LegacyResource(resource)
54-
}
55-
5640
// Adds the list of known types to api.Scheme.
5741
func addLegacyKnownTypes(scheme *runtime.Scheme) error {
5842
types := []runtime.Object{

pkg/authorization/apis/authorization/register.go

-12
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ func LegacyResource(resource string) schema.GroupResource {
4242
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
4343
}
4444

45-
// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
46-
// up the API group and also the legacy API.
47-
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
48-
return gk == Kind(kind) || gk == LegacyKind(kind)
49-
}
50-
51-
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
52-
// resource by looking up the API group and also the legacy API.
53-
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
54-
return gr == Resource(resource) || gr == LegacyResource(resource)
55-
}
56-
5745
// Adds the list of known types to api.Scheme.
5846
func addKnownTypes(scheme *runtime.Scheme) error {
5947
scheme.AddKnownTypes(SchemeGroupVersion,

pkg/build/admission/jenkinsbootstrapper/admission.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ func (a *jenkinsBootstrapper) Admit(attributes admission.Attributes) error {
6363
return nil
6464
}
6565
gr := attributes.GetResource().GroupResource()
66-
if !buildapi.IsResourceOrLegacy("buildconfigs", gr) && !buildapi.IsResourceOrLegacy("builds", gr) {
66+
switch gr {
67+
case buildapi.Resource("buildconfigs"),
68+
buildapi.Resource("builds"),
69+
buildapi.LegacyResource("buildconfigs"),
70+
buildapi.LegacyResource("builds"):
71+
default:
6772
return nil
6873
}
6974
if !needsJenkinsTemplate(attributes.GetObject()) {

pkg/build/admission/strategyrestrictions/admission.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ func NewBuildByStrategy() admission.Interface {
4949

5050
func (a *buildByStrategy) Admit(attr admission.Attributes) error {
5151
gr := attr.GetResource().GroupResource()
52-
if !buildapi.IsResourceOrLegacy("buildconfigs", gr) && !buildapi.IsResourceOrLegacy("builds", gr) {
53-
return nil
54-
}
55-
// Explicitly exclude the builds/details subresource because it's only
56-
// updating commit info and cannot change build type.
57-
if buildapi.IsResourceOrLegacy("builds", gr) && attr.GetSubresource() == "details" {
52+
switch gr {
53+
case buildapi.Resource("buildconfigs"),
54+
buildapi.LegacyResource("buildconfigs"):
55+
case buildapi.Resource("builds"),
56+
buildapi.LegacyResource("builds"):
57+
// Explicitly exclude the builds/details subresource because it's only
58+
// updating commit info and cannot change build type.
59+
if attr.GetSubresource() == "details" {
60+
return nil
61+
}
62+
default:
5863
return nil
5964
}
6065

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

178183
func (a *buildByStrategy) checkBuildRequestAuthorization(req *buildapi.BuildRequest, attr admission.Attributes) error {
179184
gr := attr.GetResource().GroupResource()
180-
switch {
181-
case buildapi.IsResourceOrLegacy("builds", gr):
185+
switch gr {
186+
case buildapi.Resource("builds"),
187+
buildapi.LegacyResource("builds"):
182188
build, err := a.buildClient.Build().Builds(attr.GetNamespace()).Get(req.Name, metav1.GetOptions{})
183189
if err != nil {
184190
return admission.NewForbidden(attr, err)
@@ -188,7 +194,9 @@ func (a *buildByStrategy) checkBuildRequestAuthorization(req *buildapi.BuildRequ
188194
return admission.NewForbidden(attr, err)
189195
}
190196
return a.checkBuildAuthorization(internalBuild, attr)
191-
case buildapi.IsResourceOrLegacy("buildconfigs", gr):
197+
198+
case buildapi.Resource("buildconfigs"),
199+
buildapi.LegacyResource("buildconfigs"):
192200
buildConfig, err := a.buildClient.Build().BuildConfigs(attr.GetNamespace()).Get(req.Name, metav1.GetOptions{})
193201
if err != nil {
194202
return admission.NewForbidden(attr, err)

pkg/build/apis/build/register.go

-18
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,15 @@ func Kind(kind string) schema.GroupKind {
2727
return SchemeGroupVersion.WithKind(kind).GroupKind()
2828
}
2929

30-
// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
31-
func LegacyKind(kind string) schema.GroupKind {
32-
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
33-
}
34-
3530
// Resource takes an unqualified resource and returns back a Group qualified GroupResource
3631
func Resource(resource string) schema.GroupResource {
3732
return SchemeGroupVersion.WithResource(resource).GroupResource()
3833
}
3934

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

45-
// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
46-
// up the API group and also the legacy API.
47-
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
48-
return gk == Kind(kind) || gk == LegacyKind(kind)
49-
}
50-
51-
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
52-
// resource by looking up the API group and also the legacy API.
53-
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
54-
return gr == Resource(resource) || gr == LegacyResource(resource)
55-
}
56-
5739
// addKnownTypes adds types to API group
5840
func addKnownTypes(scheme *runtime.Scheme) error {
5941
scheme.AddKnownTypes(SchemeGroupVersion,

pkg/build/registry/buildconfig/webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (w *WebHookHandler) ProcessWebHook(writer http.ResponseWriter, req *http.Re
110110

111111
plugin, ok := w.plugins[hookType]
112112
if !ok {
113-
return errors.NewNotFound(buildapi.LegacyResource("buildconfighook"), hookType)
113+
return errors.NewNotFound(buildapi.Resource("buildconfighook"), hookType)
114114
}
115115

116116
config, err := w.buildConfigClient.BuildConfigs(apirequest.NamespaceValue(ctx)).Get(name, metav1.GetOptions{})

pkg/build/registry/buildconfig/webhook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func TestInvokeWebhookMissingPlugin(t *testing.T) {
404404
t.Errorf("Unexpected error: %v", err)
405405
}
406406
if !responder.called ||
407-
!strings.Contains(responder.err.Error(), `buildconfighook "missingplugin" not found`) {
407+
!strings.Contains(responder.err.Error(), `buildconfighook.build.openshift.io "missingplugin" not found`) {
408408
t.Errorf("Expected BadRequest, got %s, expected error %s!", responder.err.Error(), `buildconfighook.build.openshift.io "missingplugin" not found`)
409409
}
410410
}

pkg/image/admission/admission.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (a *imageLimitRangerPlugin) SupportsAttributes(attr admission.Attributes) b
106106
return false
107107
}
108108
gk := attr.GetKind().GroupKind()
109-
return imageapi.IsKindOrLegacy("ImageStreamMapping", gk)
109+
return imageapi.Kind("ImageStreamMapping") == gk || imageapi.LegacyKind("ImageStreamMapping") == gk
110110
}
111111

112112
// SupportsLimit provides a check to see if the limitRange is applicable to image objects.

pkg/image/apis/image/register.go

-12
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ func LegacyResource(resource string) schema.GroupResource {
4040
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
4141
}
4242

43-
// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
44-
// up the API group and also the legacy API.
45-
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
46-
return gk == Kind(kind) || gk == LegacyKind(kind)
47-
}
48-
49-
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
50-
// resource by looking up the API group and also the legacy API.
51-
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
52-
return gr == Resource(resource) || gr == LegacyResource(resource)
53-
}
54-
5543
// Adds the list of known types to api.Scheme.
5644
func addLegacyKnownTypes(scheme *runtime.Scheme) error {
5745
types := []runtime.Object{

pkg/network/apis/network/register.go

-22
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,11 @@ func Kind(kind string) schema.GroupKind {
2727
return SchemeGroupVersion.WithKind(kind).GroupKind()
2828
}
2929

30-
// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
31-
func LegacyKind(kind string) schema.GroupKind {
32-
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
33-
}
34-
3530
// Resource takes an unqualified resource and returns back a Group qualified GroupResource
3631
func Resource(resource string) schema.GroupResource {
3732
return SchemeGroupVersion.WithResource(resource).GroupResource()
3833
}
3934

40-
// LegacyResource takes an unqualified resource and returns back a Group qualified GroupResource
41-
func LegacyResource(resource string) schema.GroupResource {
42-
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
43-
}
44-
45-
// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
46-
// up the API group and also the legacy API.
47-
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
48-
return gk == Kind(kind) || gk == LegacyKind(kind)
49-
}
50-
51-
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
52-
// resource by looking up the API group and also the legacy API.
53-
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
54-
return gr == Resource(resource) || gr == LegacyResource(resource)
55-
}
56-
5735
// Adds the list of known types to api.Scheme.
5836
func addKnownTypes(scheme *runtime.Scheme) error {
5937
scheme.AddKnownTypes(SchemeGroupVersion,

pkg/oauth/apis/oauth/register.go

-22
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,11 @@ func Kind(kind string) schema.GroupKind {
2727
return SchemeGroupVersion.WithKind(kind).GroupKind()
2828
}
2929

30-
// LegacyKind takes an unqualified kind and returns back a Group qualified GroupKind
31-
func LegacyKind(kind string) schema.GroupKind {
32-
return LegacySchemeGroupVersion.WithKind(kind).GroupKind()
33-
}
34-
3530
// Resource takes an unqualified resource and returns back a Group qualified GroupResource
3631
func Resource(resource string) schema.GroupResource {
3732
return SchemeGroupVersion.WithResource(resource).GroupResource()
3833
}
3934

40-
// Resource takes an unqualified resource and returns back a Group qualified GroupResource
41-
func LegacyResource(resource string) schema.GroupResource {
42-
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
43-
}
44-
45-
// IsKindOrLegacy checks if the provided GroupKind matches with the given kind by looking
46-
// up the API group and also the legacy API.
47-
func IsKindOrLegacy(kind string, gk schema.GroupKind) bool {
48-
return gk == Kind(kind) || gk == LegacyKind(kind)
49-
}
50-
51-
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
52-
// resource by looking up the API group and also the legacy API.
53-
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
54-
return gr == Resource(resource) || gr == LegacyResource(resource)
55-
}
56-
5735
// Adds the list of known types to api.Scheme.
5836
func addKnownTypes(scheme *runtime.Scheme) error {
5937
scheme.AddKnownTypes(SchemeGroupVersion,

pkg/oc/admin/policy/reconcile_clusterroles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (o *ReconcileClusterRolesOptions) Complete(cmd *cobra.Command, f *clientcmd
133133
if err != nil {
134134
return err
135135
}
136-
if !authorizationapi.IsResourceOrLegacy("clusterroles", resource) {
136+
if authorizationapi.Resource("clusterroles") != resource {
137137
return fmt.Errorf("%v is not a valid resource type for this command", resource)
138138
}
139139
if len(name) == 0 {

pkg/oc/cli/cmd/cancelbuild.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,16 @@ func (o *CancelBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
154154
return err
155155
}
156156

157-
switch {
158-
case buildapi.IsResourceOrLegacy("buildconfigs", resource):
157+
switch resource {
158+
case buildapi.Resource("buildconfigs"):
159159
list, err := buildutil.BuildConfigBuilds(o.BuildLister, o.Namespace, name, nil)
160160
if err != nil {
161161
return err
162162
}
163163
for _, b := range list {
164164
o.BuildNames = append(o.BuildNames, b.Name)
165165
}
166-
case buildapi.IsResourceOrLegacy("builds", resource):
166+
case buildapi.Resource("builds"):
167167
o.BuildNames = append(o.BuildNames, strings.TrimSpace(name))
168168
default:
169169
return fmt.Errorf("invalid resource provided: %v", resource)

pkg/oc/cli/cmd/logs.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ func (o *OpenShiftLogsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command
156156
gr := resource.GroupResource()
157157
// TODO: podLogOptions should be included in our own logOptions objects.
158158
switch {
159-
case buildapi.IsResourceOrLegacy("build", gr), buildapi.IsResourceOrLegacy("buildconfig", gr):
159+
case buildapi.Resource("build") == gr,
160+
buildapi.Resource("builds") == gr,
161+
buildapi.Resource("buildconfig") == gr,
162+
buildapi.Resource("buildconfigs") == gr:
160163
bopts := &buildapi.BuildLogOptions{
161164
Follow: podLogOptions.Follow,
162165
Previous: podLogOptions.Previous,
@@ -171,7 +174,8 @@ func (o *OpenShiftLogsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command
171174
}
172175
o.Options = bopts
173176

174-
case appsapi.IsResourceOrLegacy("deploymentconfig", gr):
177+
case appsapi.Resource("deploymentconfig") == gr,
178+
appsapi.Resource("deploymentconfigs") == gr:
175179
dopts := &appsapi.DeploymentLogOptions{
176180
Container: podLogOptions.Container,
177181
Follow: podLogOptions.Follow,

pkg/oc/cli/cmd/newapp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ func retryBuildConfig(info *resource.Info, err error) runtime.Object {
715715
buildapi.GitLabWebHookBuildTriggerType: {},
716716
buildapi.BitbucketWebHookBuildTriggerType: {},
717717
}
718-
if buildapi.IsKindOrLegacy("BuildConfig", info.Mapping.GroupVersionKind.GroupKind()) && isInvalidTriggerError(err) {
718+
if buildapi.Kind("BuildConfig") == info.Mapping.GroupVersionKind.GroupKind() && isInvalidTriggerError(err) {
719719
bc, ok := info.Object.(*buildapi.BuildConfig)
720720
if !ok {
721721
return nil

pkg/oc/cli/cmd/startbuild.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ func (o *StartBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, c
256256
if err != nil {
257257
return err
258258
}
259-
switch {
260-
case buildapi.IsResourceOrLegacy("buildconfigs", resource):
259+
switch resource {
260+
case buildapi.Resource("buildconfigs"):
261261
// no special handling required
262-
case buildapi.IsResourceOrLegacy("builds", resource):
262+
case buildapi.Resource("builds"):
263263
if len(o.ListWebhooks) == 0 {
264264
return fmt.Errorf("use --from-build to rerun your builds")
265265
}
@@ -269,7 +269,7 @@ func (o *StartBuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, c
269269
}
270270

271271
// when listing webhooks, allow --from-build to lookup a build config
272-
if buildapi.IsResourceOrLegacy("builds", resource) && len(o.ListWebhooks) > 0 {
272+
if buildapi.Resource("builds") == resource && len(o.ListWebhooks) > 0 {
273273
build, err := o.BuildClient.Builds(namespace).Get(name, metav1.GetOptions{})
274274
if err != nil {
275275
return err

pkg/oc/cli/describe/deployments.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,13 @@ func printDeploymentConfigSpec(kc kclientset.Interface, dc appsapi.DeploymentCon
293293

294294
// Autoscaling info
295295
// FIXME: The CrossVersionObjectReference should specify the Group
296-
printAutoscalingInfo([]schema.GroupResource{appsapi.Resource("DeploymentConfig"), appsapi.LegacyResource("DeploymentConfig")}, dc.Namespace, dc.Name, kc, w)
296+
printAutoscalingInfo(
297+
[]schema.GroupResource{
298+
appsapi.Resource("DeploymentConfig"),
299+
// this needs to remain as long as HPA supports putting in the "wrong" DC scheme
300+
appsapi.LegacyResource("DeploymentConfig"),
301+
},
302+
dc.Namespace, dc.Name, kc, w)
297303

298304
// Triggers
299305
printTriggers(spec.Triggers, w)

pkg/oc/cli/util/clientcmd/factory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func (f *Factory) PodForResource(resource string, timeout time.Duration) (string
341341
return "", err
342342
}
343343
return pod.Name, nil
344-
case appsapi.Resource("deploymentconfigs"), appsapi.LegacyResource("deploymentconfigs"):
344+
case appsapi.Resource("deploymentconfigs"):
345345
appsClient, err := appsclientinternal.NewForConfig(clientConfig)
346346
if err != nil {
347347
return "", err

0 commit comments

Comments
 (0)