1 package k8s
2
3 import (
4 "strings"
5
6 serverv1beta2 "github.com/linkerd/linkerd2/controller/gen/apis/server/v1beta2"
7 sazv1beta1 "github.com/linkerd/linkerd2/controller/gen/apis/serverauthorization/v1beta1"
8 spv1alpha2 "github.com/linkerd/linkerd2/controller/gen/apis/serviceprofile/v1alpha2"
9 "github.com/linkerd/linkerd2/pkg/k8s"
10 "google.golang.org/grpc/codes"
11 "google.golang.org/grpc/status"
12 admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
13 appsv1 "k8s.io/api/apps/v1"
14 batchv1 "k8s.io/api/batch/v1"
15 v1 "k8s.io/api/core/v1"
16 discoveryv1 "k8s.io/api/discovery/v1"
17 "k8s.io/apimachinery/pkg/runtime/schema"
18 )
19
20
21
22 type APIResource int
23
24
25
26 const (
27 CJ APIResource = iota
28 CM
29 Deploy
30 DS
31 Endpoint
32 ES
33 ExtWorkload
34 Job
35 MWC
36 NS
37 Pod
38 RC
39 RS
40 SP
41 SS
42 Svc
43 Node
44 Secret
45 Srv
46 Saz
47 )
48
49
50 func (res APIResource) GVK() (schema.GroupVersionKind, error) {
51 switch res {
52 case CJ:
53 return batchv1.SchemeGroupVersion.WithKind("CronJob"), nil
54 case CM:
55 return v1.SchemeGroupVersion.WithKind("ConfigMap"), nil
56 case Deploy:
57 return appsv1.SchemeGroupVersion.WithKind("Deployment"), nil
58 case DS:
59 return appsv1.SchemeGroupVersion.WithKind("DaemonSet"), nil
60 case Endpoint:
61 return v1.SchemeGroupVersion.WithKind("Endpoint"), nil
62 case ES:
63 return discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice"), nil
64 case Job:
65 return batchv1.SchemeGroupVersion.WithKind("Job"), nil
66 case MWC:
67 return admissionregistrationv1.SchemeGroupVersion.WithKind("MutatingWebhookConfiguration"), nil
68 case Node:
69 return v1.SchemeGroupVersion.WithKind("Node"), nil
70 case NS:
71 return v1.SchemeGroupVersion.WithKind("Namespace"), nil
72 case Pod:
73 return v1.SchemeGroupVersion.WithKind("Pod"), nil
74 case RC:
75 return v1.SchemeGroupVersion.WithKind("ReplicationController"), nil
76 case RS:
77 return appsv1.SchemeGroupVersion.WithKind("ReplicaSet"), nil
78 case Saz:
79 return sazv1beta1.SchemeGroupVersion.WithKind("ServerAuthorization"), nil
80 case Secret:
81 return v1.SchemeGroupVersion.WithKind("Secret"), nil
82 case SP:
83 return spv1alpha2.SchemeGroupVersion.WithKind("ServiceProfile"), nil
84 case SS:
85 return appsv1.SchemeGroupVersion.WithKind("StatefulSet"), nil
86 case Srv:
87 return serverv1beta2.SchemeGroupVersion.WithKind("Server"), nil
88 case Svc:
89 return v1.SchemeGroupVersion.WithKind("Service"), nil
90 default:
91 return schema.GroupVersionKind{}, status.Errorf(codes.Unimplemented, "unimplemented resource type: %d", res)
92 }
93 }
94
95
96 func GetAPIResource(kind string) (APIResource, error) {
97 switch strings.ToLower(kind) {
98 case k8s.CronJob:
99 return CJ, nil
100 case k8s.ConfigMap:
101 return CM, nil
102 case k8s.Deployment:
103 return Deploy, nil
104 case k8s.DaemonSet:
105 return DS, nil
106 case k8s.Endpoints:
107 return Endpoint, nil
108 case k8s.EndpointSlices:
109 return ES, nil
110 case k8s.Job:
111 return Job, nil
112 case k8s.MutatingWebhookConfig:
113 return MWC, nil
114 case k8s.Namespace:
115 return NS, nil
116 case k8s.Node:
117 return Node, nil
118 case k8s.Pod:
119 return Pod, nil
120 case k8s.ReplicationController:
121 return RC, nil
122 case k8s.ReplicaSet:
123 return RS, nil
124 case k8s.ServerAuthorization:
125 return Saz, nil
126 case k8s.Secret:
127 return Secret, nil
128 case k8s.ServiceProfile:
129 return SP, nil
130 case k8s.Service:
131 return Svc, nil
132 case k8s.StatefulSet:
133 return SS, nil
134 case k8s.Server:
135 return Srv, nil
136 default:
137 return 0, status.Errorf(codes.Unimplemented, "unimplemented resource type: %s", kind)
138 }
139 }
140
View as plain text