|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 5 | + "k8s.io/apimachinery/pkg/runtime" |
| 6 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 7 | + |
| 8 | + appsv1 "github.com/openshift/api/apps/v1" |
| 9 | + authorizationv1 "github.com/openshift/api/authorization/v1" |
| 10 | + buildv1 "github.com/openshift/api/build/v1" |
| 11 | + imagev1 "github.com/openshift/api/image/v1" |
| 12 | + networkv1 "github.com/openshift/api/network/v1" |
| 13 | + oauthv1 "github.com/openshift/api/oauth/v1" |
| 14 | + projectv1 "github.com/openshift/api/project/v1" |
| 15 | + quotav1 "github.com/openshift/api/quota/v1" |
| 16 | + routev1 "github.com/openshift/api/route/v1" |
| 17 | + securityv1 "github.com/openshift/api/security/v1" |
| 18 | + templatev1 "github.com/openshift/api/template/v1" |
| 19 | + userv1 "github.com/openshift/api/user/v1" |
| 20 | + "github.com/openshift/origin/pkg/apps/apis/apps" |
| 21 | + "github.com/openshift/origin/pkg/authorization/apis/authorization" |
| 22 | + "github.com/openshift/origin/pkg/build/apis/build" |
| 23 | + "github.com/openshift/origin/pkg/image/apis/image" |
| 24 | + "github.com/openshift/origin/pkg/network/apis/network" |
| 25 | + "github.com/openshift/origin/pkg/oauth/apis/oauth" |
| 26 | + "github.com/openshift/origin/pkg/project/apis/project" |
| 27 | + "github.com/openshift/origin/pkg/quota/apis/quota" |
| 28 | + "github.com/openshift/origin/pkg/route/apis/route" |
| 29 | + "github.com/openshift/origin/pkg/security/apis/security" |
| 30 | + "github.com/openshift/origin/pkg/template/apis/template" |
| 31 | + "github.com/openshift/origin/pkg/user/apis/user" |
| 32 | +) |
| 33 | + |
| 34 | +func OAPIToGroupifiedGVK(gvk *schema.GroupVersionKind) { |
| 35 | + if len(gvk.Group) > 0 { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + newGroup, ok := oapiKindsToGroup[gvk.Kind] |
| 40 | + if !ok { |
| 41 | + return |
| 42 | + } |
| 43 | + gvk.Group = newGroup |
| 44 | +} |
| 45 | + |
| 46 | +func OAPIToGroupified(uncast runtime.Object, gvk *schema.GroupVersionKind) { |
| 47 | + if len(gvk.Group) > 0 { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + switch obj := uncast.(type) { |
| 52 | + case *unstructured.Unstructured: |
| 53 | + newGroup := fixOAPIGroupKindInTopLevelUnstructured(obj.Object) |
| 54 | + if len(newGroup) > 0 { |
| 55 | + gvk.Group = newGroup |
| 56 | + } |
| 57 | + case *unstructured.UnstructuredList: |
| 58 | + newGroup := fixOAPIGroupKindInTopLevelUnstructured(obj.Object) |
| 59 | + if len(newGroup) > 0 { |
| 60 | + gvk.Group = newGroup |
| 61 | + } |
| 62 | + |
| 63 | + case *apps.DeploymentConfig, *appsv1.DeploymentConfig, *apps.DeploymentConfigList, *appsv1.DeploymentConfigList, |
| 64 | + *apps.DeploymentConfigRollback, *appsv1.DeploymentConfigRollback: |
| 65 | + gvk.Group = apps.GroupName |
| 66 | + |
| 67 | + case *authorization.ClusterRoleBinding, *authorizationv1.ClusterRoleBinding, *authorization.ClusterRoleBindingList, *authorizationv1.ClusterRoleBindingList, |
| 68 | + *authorization.ClusterRole, *authorizationv1.ClusterRole, *authorization.ClusterRoleList, *authorizationv1.ClusterRoleList, |
| 69 | + *authorization.Role, *authorizationv1.Role, *authorization.RoleList, *authorizationv1.RoleList, |
| 70 | + *authorization.RoleBinding, *authorizationv1.RoleBinding, *authorization.RoleBindingList, *authorizationv1.RoleBindingList, |
| 71 | + *authorization.RoleBindingRestriction, *authorizationv1.RoleBindingRestriction, *authorization.RoleBindingRestrictionList, *authorizationv1.RoleBindingRestrictionList: |
| 72 | + gvk.Group = authorization.GroupName |
| 73 | + |
| 74 | + case *build.BuildConfig, *buildv1.BuildConfig, *build.BuildConfigList, *buildv1.BuildConfigList, |
| 75 | + *build.Build, *buildv1.Build, *build.BuildList, *buildv1.BuildList: |
| 76 | + gvk.Group = build.GroupName |
| 77 | + |
| 78 | + case *image.Image, *imagev1.Image, *image.ImageList, *imagev1.ImageList, |
| 79 | + *image.ImageSignature, *imagev1.ImageSignature, |
| 80 | + *image.ImageStreamImage, *imagev1.ImageStreamImage, |
| 81 | + *image.ImageStreamImport, *imagev1.ImageStreamImport, |
| 82 | + *image.ImageStreamMapping, *imagev1.ImageStreamMapping, |
| 83 | + *image.ImageStream, *imagev1.ImageStream, *image.ImageStreamList, *imagev1.ImageStreamList, |
| 84 | + *image.ImageStreamTag, *imagev1.ImageStreamTag: |
| 85 | + gvk.Group = image.GroupName |
| 86 | + |
| 87 | + case *network.ClusterNetwork, *networkv1.ClusterNetwork, *network.ClusterNetworkList, *networkv1.ClusterNetworkList, |
| 88 | + *network.NetNamespace, *networkv1.NetNamespace, *network.NetNamespaceList, *networkv1.NetNamespaceList, |
| 89 | + *network.HostSubnet, *networkv1.HostSubnet, *network.HostSubnetList, *networkv1.HostSubnetList, |
| 90 | + *network.EgressNetworkPolicy, *networkv1.EgressNetworkPolicy, *network.EgressNetworkPolicyList, *networkv1.EgressNetworkPolicyList: |
| 91 | + gvk.Group = network.GroupName |
| 92 | + |
| 93 | + case *project.Project, *projectv1.Project, *project.ProjectList, *projectv1.ProjectList, |
| 94 | + *project.ProjectRequest, *projectv1.ProjectRequest: |
| 95 | + gvk.Group = project.GroupName |
| 96 | + |
| 97 | + case *quota.ClusterResourceQuota, *quotav1.ClusterResourceQuota, *quota.ClusterResourceQuotaList, *quotav1.ClusterResourceQuotaList: |
| 98 | + gvk.Group = quota.GroupName |
| 99 | + |
| 100 | + case *oauth.OAuthAuthorizeToken, *oauthv1.OAuthAuthorizeToken, *oauth.OAuthAuthorizeTokenList, *oauthv1.OAuthAuthorizeTokenList, |
| 101 | + *oauth.OAuthClientAuthorization, *oauthv1.OAuthClientAuthorization, *oauth.OAuthClientAuthorizationList, *oauthv1.OAuthClientAuthorizationList, |
| 102 | + *oauth.OAuthClient, *oauthv1.OAuthClient, *oauth.OAuthClientList, *oauthv1.OAuthClientList, |
| 103 | + *oauth.OAuthAccessToken, *oauthv1.OAuthAccessToken, *oauth.OAuthAccessTokenList, *oauthv1.OAuthAccessTokenList: |
| 104 | + gvk.Group = oauth.GroupName |
| 105 | + |
| 106 | + case *route.Route, *routev1.Route, *route.RouteList, *routev1.RouteList: |
| 107 | + gvk.Group = route.GroupName |
| 108 | + |
| 109 | + case *security.SecurityContextConstraints, *securityv1.SecurityContextConstraints, *security.SecurityContextConstraintsList, *securityv1.SecurityContextConstraintsList: |
| 110 | + gvk.Group = security.GroupName |
| 111 | + |
| 112 | + case *template.Template, *templatev1.Template, *template.TemplateList, *templatev1.TemplateList: |
| 113 | + gvk.Group = template.GroupName |
| 114 | + |
| 115 | + case *user.Group, *userv1.Group, *user.GroupList, *userv1.GroupList, |
| 116 | + *user.Identity, *userv1.Identity, *user.IdentityList, *userv1.IdentityList, |
| 117 | + *user.UserIdentityMapping, *userv1.UserIdentityMapping, |
| 118 | + *user.User, *userv1.User, *user.UserList, *userv1.UserList: |
| 119 | + gvk.Group = user.GroupName |
| 120 | + |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +var oapiKindsToGroup = map[string]string{ |
| 125 | + "DeploymentConfigRollback": "apps.openshift.io", |
| 126 | + "DeploymentConfig": "apps.openshift.io", "DeploymentConfigList": "apps.openshift.io", |
| 127 | + "ClusterRoleBinding": "authorization.openshift.io", "ClusterRoleBindingList": "authorization.openshift.io", |
| 128 | + "ClusterRole": "authorization.openshift.io", "ClusterRoleList": "authorization.openshift.io", |
| 129 | + "RoleBindingRestriction": "authorization.openshift.io", "RoleBindingRestrictionList": "authorization.openshift.io", |
| 130 | + "RoleBinding": "authorization.openshift.io", "RoleBindingList": "authorization.openshift.io", |
| 131 | + "Role": "authorization.openshift.io", "RoleList": "authorization.openshift.io", |
| 132 | + "BuildConfig": "build.openshift.io", "BuildConfigList": "build.openshift.io", |
| 133 | + "Build": "build.openshift.io", "BuildList": "build.openshift.io", |
| 134 | + "Image": "image.openshift.io", "ImageList": "image.openshift.io", |
| 135 | + "ImageSignature": "image.openshift.io", |
| 136 | + "ImageStreamImage": "image.openshift.io", |
| 137 | + "ImageStreamImport": "image.openshift.io", |
| 138 | + "ImageStreamMapping": "image.openshift.io", |
| 139 | + "ImageStream": "image.openshift.io", "ImageStreamList": "image.openshift.io", |
| 140 | + "ImageStreamTag": "image.openshift.io", "ImageStreamTagList": "image.openshift.io", |
| 141 | + "ClusterNetwork": "network.openshift.io", "ClusterNetworkList": "network.openshift.io", |
| 142 | + "EgressNetworkPolicy": "network.openshift.io", "EgressNetworkPolicyList": "network.openshift.io", |
| 143 | + "HostSubnet": "network.openshift.io", "HostSubnetList": "network.openshift.io", |
| 144 | + "NetNamespace": "network.openshift.io", "NetNamespaceList": "network.openshift.io", |
| 145 | + "OAuthAccessToken": "oauth.openshift.io", "OAuthAccessTokenList": "oauth.openshift.io", |
| 146 | + "OAuthAuthorizeToken": "oauth.openshift.io", "OAuthAuthorizeTokenList": "oauth.openshift.io", |
| 147 | + "OAuthClientAuthorization": "oauth.openshift.io", "OAuthClientAuthorizationList": "oauth.openshift.io", |
| 148 | + "OAuthClient": "oauth.openshift.io", "OAuthClientList": "oauth.openshift.io", |
| 149 | + "Project": "project.openshift.io", "ProjectList": "project.openshift.io", |
| 150 | + "ProjectRequest": "project.openshift.io", |
| 151 | + "ClusterResourceQuota": "quota.openshift.io", "ClusterResourceQuotaList": "quota.openshift.io", |
| 152 | + "Route": "route.openshift.io", "RouteList": "route.openshift.io", |
| 153 | + "SecurityContextConstraint": "security.openshift.io", "SecurityContextConstraintList": "security.openshift.io", |
| 154 | + "Template": "template.openshift.io", "TemplateList": "template.openshift.io", |
| 155 | + "Group": "user.openshift.io", "GroupList": "user.openshift.io", |
| 156 | + "Identity": "user.openshift.io", "IdentityList": "user.openshift.io", |
| 157 | + "UserIdentityMapping": "user.openshift.io", |
| 158 | + "User": "user.openshift.io", "UserList": "user.openshift.io", |
| 159 | +} |
| 160 | + |
| 161 | +func fixOAPIGroupKindInTopLevelUnstructured(obj map[string]interface{}) string { |
| 162 | + kind, ok := obj["kind"] |
| 163 | + if !ok { |
| 164 | + return "" |
| 165 | + } |
| 166 | + kindStr, ok := kind.(string) |
| 167 | + if !ok { |
| 168 | + return "" |
| 169 | + } |
| 170 | + newGroup, ok := oapiKindsToGroup[kindStr] |
| 171 | + if !ok { |
| 172 | + return "" |
| 173 | + } |
| 174 | + |
| 175 | + apiVersion, ok := obj["apiVersion"] |
| 176 | + if !ok { |
| 177 | + return newGroup |
| 178 | + } |
| 179 | + apiVersionStr, ok := apiVersion.(string) |
| 180 | + if !ok { |
| 181 | + return newGroup |
| 182 | + } |
| 183 | + |
| 184 | + if apiVersionStr != "v1" { |
| 185 | + return newGroup |
| 186 | + } |
| 187 | + obj["apiVersion"] = newGroup + "/v1" |
| 188 | + |
| 189 | + return newGroup |
| 190 | +} |
0 commit comments