-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_tpr.go
113 lines (95 loc) · 4.04 KB
/
controller_tpr.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
112
113
/*
Copyright 2017 The Rook Authors. All rights reserved.
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.
Some of the code below came from https://github.com/coreos/etcd-operator
which also has the apache 2.0 license.
*/
package crd
import (
"encoding/json"
"fmt"
"github.com/coreos/pkg/capnslog"
"github.com/rook/rook/pkg/operator/k8sutil"
"k8s.io/client-go/kubernetes"
)
const (
tprKind = "Volumeattachment"
)
var tprlogger = capnslog.NewPackageLogger("github.com/rook/rook", "rook-agent-tpr")
// VolumeAttachmentTPRController is a controller to manage VolumeAttachment TPR objects
// CRD is available on v1.7.0. TPR became deprecated on v1.7.0
// Remove this code when TPR is not longer supported
type VolumeAttachmentTPRController struct {
clientset kubernetes.Interface
}
// NewTPR creates a new VolumeAttachment controller for TPR resources. Only valid for k8s 1.6 and older
func NewTPR(clientset kubernetes.Interface) *VolumeAttachmentTPRController {
return &VolumeAttachmentTPRController{
clientset: clientset,
}
}
// Get queries the VolumeAttachment TPR from Kubernetes
func (c *VolumeAttachmentTPRController) Get(namespace, name string) (VolumeAttachment, error) {
var result VolumeAttachment
uri := fmt.Sprintf("apis/%s/%s/namespaces/%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, namespace, CustomResourceNamePlural)
return result, c.clientset.Core().RESTClient().Get().
RequestURI(uri).
Name(name).
Do().
Into(&result)
}
// List lists all the volume attachment TPR resources in the given namespace
func (c *VolumeAttachmentTPRController) List(namespace string) (VolumeAttachmentList, error) {
var result VolumeAttachmentList
uri := fmt.Sprintf("apis/%s/%s/namespaces/%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, namespace, CustomResourceNamePlural)
return result, c.clientset.Core().RESTClient().Get().
RequestURI(uri).
Do().
Into(&result)
}
// Create creates the volume attach TPR resource in Kubernetes
func (c *VolumeAttachmentTPRController) Create(volumeAttachment VolumeAttachment) error {
volumeAttachment.APIVersion = fmt.Sprintf("%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1)
volumeAttachment.Kind = tprKind
body, _ := json.Marshal(volumeAttachment)
uri := fmt.Sprintf("apis/%s/%s/namespaces/%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, volumeAttachment.Namespace, CustomResourceNamePlural)
return c.clientset.Core().RESTClient().Post().
RequestURI(uri).
Body(body).
Do().Error()
}
// Update updates VolumeAttachment TPR resource
func (c *VolumeAttachmentTPRController) Update(volumeAttachment VolumeAttachment) error {
volumeAttachment.APIVersion = fmt.Sprintf("%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1)
volumeAttachment.Kind = tprKind
body, _ := json.Marshal(volumeAttachment)
uri := fmt.Sprintf("apis/%s/%s/namespaces/%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, volumeAttachment.Namespace, CustomResourceNamePlural)
err := c.clientset.Core().RESTClient().Put().
RequestURI(uri).
Name(volumeAttachment.Name).
Body(body).
Do().Error()
if err != nil {
tprlogger.Errorf("failed to update VolumeAttachment CRD. %+v", err)
return err
}
tprlogger.Infof("updated Volumeattach TPR %s", volumeAttachment.ObjectMeta.Name)
return nil
}
// Delete deletes the volume attach TPR resource in Kubernetes
func (c *VolumeAttachmentTPRController) Delete(namespace, name string) error {
uri := fmt.Sprintf("apis/%s/%s/namespaces/%s/%s", k8sutil.CustomResourceGroup, k8sutil.V1Alpha1, namespace, CustomResourceNamePlural)
return c.clientset.Core().RESTClient().Delete().
RequestURI(uri).
Name(name).
Do().
Error()
}