-
Notifications
You must be signed in to change notification settings - Fork 665
/
kind.go
111 lines (107 loc) · 3.44 KB
/
kind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8s
import (
contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
v1 "k8s.io/api/core/v1"
networking_v1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
gatewayapi_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayapi_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)
// KindOf returns the kind string for the given Kubernetes object.
//
// The API machinery doesn't populate the metav1.TypeMeta field for
// objects, so we have to use a type assertion to detect kinds that
// we care about.
func KindOf(obj any) string {
object, ok := obj.(runtime.Object)
if !ok {
return ""
}
gvk, _, err := scheme.Scheme.ObjectKinds(object)
if err != nil {
switch obj := obj.(type) {
case *v1.Secret:
return "Secret"
case *v1.Service:
return "Service"
case *v1.Endpoints:
return "Endpoints"
case *networking_v1.Ingress:
return "Ingress"
case *contour_api_v1.HTTPProxy:
return "HTTPProxy"
case *gatewayapi_v1beta1.HTTPRoute:
return "HTTPRoute"
case *gatewayapi_v1alpha2.GRPCRoute:
return "GRPCRoute"
case *gatewayapi_v1alpha2.TLSRoute:
return "TLSRoute"
case *gatewayapi_v1alpha2.TCPRoute:
return "TCPRoute"
case *gatewayapi_v1beta1.Gateway:
return "Gateway"
case *gatewayapi_v1beta1.GatewayClass:
return "GatewayClass"
case *gatewayapi_v1beta1.ReferenceGrant:
return "ReferenceGrant"
case *contour_api_v1.TLSCertificateDelegation:
return "TLSCertificateDelegation"
case *v1alpha1.ExtensionService:
return "ExtensionService"
case *v1alpha1.ContourConfiguration:
return "ContourConfiguration"
case *v1alpha1.ContourDeployment:
return "ContourDeployment"
case *v1.Namespace:
return "Namespace"
case *unstructured.Unstructured:
return obj.GetKind()
default:
return ""
}
}
for _, gv := range gvk {
return gv.GroupKind().Kind
}
return ""
}
// VersionOf returns the GroupVersion string for the given Kubernetes object.
func VersionOf(obj any) string {
// If err is not nil we have the GVK and we can use it. Otherwise we're going to use switch case method as failover
gvk, _, err := scheme.Scheme.ObjectKinds(obj.(runtime.Object))
if err != nil {
switch obj := obj.(type) {
case *v1.Secret, *v1.Service, *v1.Endpoints:
return v1.SchemeGroupVersion.String()
case *networking_v1.Ingress:
return networking_v1.SchemeGroupVersion.String()
case *contour_api_v1.HTTPProxy, *contour_api_v1.TLSCertificateDelegation:
return contour_api_v1.GroupVersion.String()
case *v1alpha1.ExtensionService:
return v1alpha1.GroupVersion.String()
case *unstructured.Unstructured:
return obj.GetAPIVersion()
default:
return ""
}
}
for _, gv := range gvk {
return gv.GroupVersion().String()
}
return ""
}