|
| 1 | +package kubeclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 11 | + "k8s.io/client-go/rest" |
| 12 | +) |
| 13 | + |
| 14 | +type DeploymentClient interface { |
| 15 | + Get(ctx context.Context, name string, opts metav1.GetOptions) (*appsv1.Deployment, error) |
| 16 | + Create(ctx context.Context, deployment *appsv1.Deployment, opts metav1.CreateOptions) (*appsv1.Deployment, error) |
| 17 | + Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error |
| 18 | +} |
| 19 | + |
| 20 | +type ConfigMapClient interface { |
| 21 | + Create(ctx context.Context, configMap *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) |
| 22 | + Update(ctx context.Context, configMap *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) |
| 23 | + Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error |
| 24 | +} |
| 25 | + |
| 26 | +type PodClient interface { |
| 27 | + List(ctx context.Context, opts metav1.ListOptions) (*corev1.PodList, error) |
| 28 | + RESTClient() rest.Interface |
| 29 | +} |
| 30 | + |
| 31 | +type Clients struct { |
| 32 | + Deployments DeploymentClient |
| 33 | + ConfigMaps ConfigMapClient |
| 34 | + Pods PodClient |
| 35 | +} |
| 36 | + |
| 37 | +func New(config *rest.Config, namespace string) (*Clients, error) { |
| 38 | + httpClient, err := rest.HTTPClientFor(config) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + appsClient, err := newRESTClient(config, httpClient, appsv1.SchemeGroupVersion, "/apis") |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + coreClient, err := newRESTClient(config, httpClient, corev1.SchemeGroupVersion, "/api") |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return &Clients{ |
| 54 | + Deployments: &deploymentClient{client: appsClient, namespace: namespace}, |
| 55 | + ConfigMaps: &configMapClient{client: coreClient, namespace: namespace}, |
| 56 | + Pods: &podClient{client: coreClient, namespace: namespace}, |
| 57 | + }, nil |
| 58 | +} |
| 59 | + |
| 60 | +func newRESTClient(config *rest.Config, httpClient *http.Client, gv schema.GroupVersion, apiPath string) (rest.Interface, error) { |
| 61 | + cfg := *config |
| 62 | + cfg.GroupVersion = &gv |
| 63 | + cfg.APIPath = apiPath |
| 64 | + cfg.NegotiatedSerializer = rest.CodecFactoryForGeneratedClient(Scheme, Codecs).WithoutConversion() |
| 65 | + if cfg.UserAgent == "" { |
| 66 | + cfg.UserAgent = rest.DefaultKubernetesUserAgent() |
| 67 | + } |
| 68 | + return rest.RESTClientForConfigAndClient(&cfg, httpClient) |
| 69 | +} |
| 70 | + |
| 71 | +type deploymentClient struct { |
| 72 | + client rest.Interface |
| 73 | + namespace string |
| 74 | +} |
| 75 | + |
| 76 | +func (c *deploymentClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*appsv1.Deployment, error) { |
| 77 | + result := &appsv1.Deployment{} |
| 78 | + err := c.client.Get(). |
| 79 | + UseProtobufAsDefault(). |
| 80 | + Namespace(c.namespace). |
| 81 | + Resource("deployments"). |
| 82 | + Name(name). |
| 83 | + VersionedParams(&opts, ParameterCodec). |
| 84 | + Do(ctx). |
| 85 | + Into(result) |
| 86 | + return result, err |
| 87 | +} |
| 88 | + |
| 89 | +func (c *deploymentClient) Create(ctx context.Context, deployment *appsv1.Deployment, opts metav1.CreateOptions) (*appsv1.Deployment, error) { |
| 90 | + result := &appsv1.Deployment{} |
| 91 | + err := c.client.Post(). |
| 92 | + UseProtobufAsDefault(). |
| 93 | + Namespace(c.namespace). |
| 94 | + Resource("deployments"). |
| 95 | + VersionedParams(&opts, ParameterCodec). |
| 96 | + Body(deployment). |
| 97 | + Do(ctx). |
| 98 | + Into(result) |
| 99 | + return result, err |
| 100 | +} |
| 101 | + |
| 102 | +func (c *deploymentClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { |
| 103 | + return c.client.Delete(). |
| 104 | + UseProtobufAsDefault(). |
| 105 | + Namespace(c.namespace). |
| 106 | + Resource("deployments"). |
| 107 | + Name(name). |
| 108 | + Body(&opts). |
| 109 | + Do(ctx). |
| 110 | + Error() |
| 111 | +} |
| 112 | + |
| 113 | +type configMapClient struct { |
| 114 | + client rest.Interface |
| 115 | + namespace string |
| 116 | +} |
| 117 | + |
| 118 | +func (c *configMapClient) Create(ctx context.Context, configMap *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) { |
| 119 | + result := &corev1.ConfigMap{} |
| 120 | + err := c.client.Post(). |
| 121 | + UseProtobufAsDefault(). |
| 122 | + Namespace(c.namespace). |
| 123 | + Resource("configmaps"). |
| 124 | + VersionedParams(&opts, ParameterCodec). |
| 125 | + Body(configMap). |
| 126 | + Do(ctx). |
| 127 | + Into(result) |
| 128 | + return result, err |
| 129 | +} |
| 130 | + |
| 131 | +func (c *configMapClient) Update(ctx context.Context, configMap *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) { |
| 132 | + result := &corev1.ConfigMap{} |
| 133 | + err := c.client.Put(). |
| 134 | + UseProtobufAsDefault(). |
| 135 | + Namespace(c.namespace). |
| 136 | + Resource("configmaps"). |
| 137 | + Name(configMap.Name). |
| 138 | + VersionedParams(&opts, ParameterCodec). |
| 139 | + Body(configMap). |
| 140 | + Do(ctx). |
| 141 | + Into(result) |
| 142 | + return result, err |
| 143 | +} |
| 144 | + |
| 145 | +func (c *configMapClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { |
| 146 | + return c.client.Delete(). |
| 147 | + UseProtobufAsDefault(). |
| 148 | + Namespace(c.namespace). |
| 149 | + Resource("configmaps"). |
| 150 | + Name(name). |
| 151 | + Body(&opts). |
| 152 | + Do(ctx). |
| 153 | + Error() |
| 154 | +} |
| 155 | + |
| 156 | +type podClient struct { |
| 157 | + client rest.Interface |
| 158 | + namespace string |
| 159 | +} |
| 160 | + |
| 161 | +func (c *podClient) List(ctx context.Context, opts metav1.ListOptions) (*corev1.PodList, error) { |
| 162 | + result := &corev1.PodList{} |
| 163 | + err := c.client.Get(). |
| 164 | + UseProtobufAsDefault(). |
| 165 | + Namespace(c.namespace). |
| 166 | + Resource("pods"). |
| 167 | + VersionedParams(&opts, ParameterCodec). |
| 168 | + Do(ctx). |
| 169 | + Into(result) |
| 170 | + return result, err |
| 171 | +} |
| 172 | + |
| 173 | +func (c *podClient) RESTClient() rest.Interface { |
| 174 | + return c.client |
| 175 | +} |
0 commit comments