@@ -20,105 +20,91 @@ import (
20
20
"github.com/coreos/operator-sdk/pkg/k8sclient"
21
21
sdkTypes "github.com/coreos/operator-sdk/pkg/sdk/types"
22
22
"github.com/coreos/operator-sdk/pkg/util/k8sutil"
23
-
24
- apierrors "k8s.io/apimachinery/pkg/api/errors"
25
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
23
)
27
24
28
- const (
29
- // Supported function types
30
- KubeApplyFunc sdkTypes.FuncType = "kube-apply"
31
- KubeDeleteFunc sdkTypes.FuncType = "kube-delete"
32
- )
25
+ // Create creates the provided object on the server and updates the arg
26
+ // "object" with the result from the server(UID, resourceVersion, etc).
27
+ // Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name/GenerateName, Namespace) is missing or incorrect.
28
+ // Can also return an api error from the server
29
+ // e.g AlreadyExists https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L423
30
+ func Create (object sdkTypes.Object ) (err error ) {
31
+ _ , namespace , err := k8sutil .GetNameAndNamespace (object )
32
+ if err != nil {
33
+ return err
34
+ }
35
+ gvk := object .GetObjectKind ().GroupVersionKind ()
36
+ apiVersion , kind := gvk .ToAPIVersionAndKind ()
33
37
34
- var (
35
- // kubeFuncs is the mapping of the supported functions
36
- kubeFuncs = map [sdkTypes.FuncType ]sdkTypes.KubeFunc {
37
- KubeApplyFunc : KubeApply ,
38
- KubeDeleteFunc : KubeDelete ,
38
+ resourceClient , _ , err := k8sclient .GetResourceClient (apiVersion , kind , namespace )
39
+ if err != nil {
40
+ return fmt .Errorf ("failed to get resource client: %v" , err )
39
41
}
40
- )
41
42
42
- // ProcessAction invokes the function specified by action.Func
43
- func ProcessAction (action sdkTypes.Action ) error {
44
- kubeFunc , ok := kubeFuncs [action .Func ]
45
- if ! ok {
46
- return fmt .Errorf ("failed to process action: unsupported function (%v)" , action .Func )
43
+ unstructObj := k8sutil .UnstructuredFromRuntimeObject (object )
44
+ unstructObj , err = resourceClient .Create (unstructObj )
45
+ if err != nil {
46
+ return err
47
47
}
48
- err := kubeFunc (action .Object )
48
+
49
+ // Update the arg object with the result
50
+ err = k8sutil .UnstructuredIntoRuntimeObject (unstructObj , object )
49
51
if err != nil {
50
- return fmt .Errorf ("failed to process action : %v" , err )
52
+ return fmt .Errorf ("failed to unmarshal the retrieved data : %v" , err )
51
53
}
52
54
return nil
53
55
}
54
56
55
- // KubeApply tries to create the specified object or update it if it already exists
56
- func KubeApply (object sdkTypes.Object ) (err error ) {
57
- defer func () {
58
- if err != nil {
59
- err = fmt .Errorf ("kube-apply failed: %v" , err )
60
- }
61
- }()
62
-
63
- name , namespace , err := k8sutil .GetNameAndNamespace (object )
57
+ // Update updates the provided object on the server and updates the arg
58
+ // "object" with the result from the server(UID, resourceVersion, etc).
59
+ // Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name, Namespace) is missing or incorrect.
60
+ // Can also return an api error from the server
61
+ // e.g Conflict https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L428
62
+ func Update (object sdkTypes.Object ) (err error ) {
63
+ _ , namespace , err := k8sutil .GetNameAndNamespace (object )
64
64
if err != nil {
65
65
return err
66
66
}
67
67
gvk := object .GetObjectKind ().GroupVersionKind ()
68
68
apiVersion , kind := gvk .ToAPIVersionAndKind ()
69
- objectInfo := k8sutil .ObjectInfo (kind , name , namespace )
70
69
71
70
resourceClient , _ , err := k8sclient .GetResourceClient (apiVersion , kind , namespace )
72
71
if err != nil {
73
- return fmt .Errorf ("failed to get resource client for object : %v" , err )
72
+ return fmt .Errorf ("failed to get resource client: %v" , err )
74
73
}
75
74
76
75
unstructObj := k8sutil .UnstructuredFromRuntimeObject (object )
77
-
78
- // Create the resource if it doesn't exist
79
- _ , err = resourceClient .Create (unstructObj )
80
- if err == nil {
81
- return nil
82
- }
83
- if err != nil && ! apierrors .IsAlreadyExists (err ) {
84
- return fmt .Errorf ("failed to create object (%s): %v " , objectInfo , err )
76
+ unstructObj , err = resourceClient .Update (unstructObj )
77
+ if err != nil {
78
+ return err
85
79
}
86
80
87
- // Update it if it already exists
88
- // NOTE: The update could fail if there is a resourceVersion conflict.
89
- // That means the object is stale, and the user needs to retry the Action with
90
- // an updated object that has the latest resourceVersion
91
- _ , err = resourceClient .Update (unstructObj )
81
+ // Update the arg object with the result
82
+ err = k8sutil .UnstructuredIntoRuntimeObject (unstructObj , object )
92
83
if err != nil {
93
- return fmt .Errorf ("failed to update object (%s) : %v " , objectInfo , err )
84
+ return fmt .Errorf ("failed to unmarshal the retrieved data : %v" , err )
94
85
}
95
86
return nil
96
87
}
97
88
98
- // KubeDelete deletes an object if it still exists
99
- func KubeDelete (object sdkTypes.Object ) (err error ) {
100
- defer func () {
101
- if err != nil {
102
- err = fmt .Errorf ("kube-delete failed: %v" , err )
103
- }
104
- }()
105
-
89
+ // Delete deletes the specified object
90
+ // Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name, Namespace) is missing or incorrect.
91
+ // e.g NotFound https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L418
92
+ // “opts” configures the DeleteOptions
93
+ // When passed WithDeleteOptions(o), the specified metav1.DeleteOptions are set.
94
+ func Delete (object sdkTypes.Object , opts ... DeleteOption ) (err error ) {
106
95
name , namespace , err := k8sutil .GetNameAndNamespace (object )
107
96
if err != nil {
108
97
return err
109
98
}
110
99
gvk := object .GetObjectKind ().GroupVersionKind ()
111
100
apiVersion , kind := gvk .ToAPIVersionAndKind ()
112
- objectInfo := k8sutil .ObjectInfo (kind , name , namespace )
113
101
114
102
resourceClient , _ , err := k8sclient .GetResourceClient (apiVersion , kind , namespace )
115
103
if err != nil {
116
- return fmt .Errorf ("failed to get resource client for object : %v" , err )
104
+ return fmt .Errorf ("failed to get resource client: %v" , err )
117
105
}
118
106
119
- err = resourceClient .Delete (name , & metav1.DeleteOptions {})
120
- if err != nil && ! apierrors .IsNotFound (err ) {
121
- return fmt .Errorf ("failed to delete object (%s): %v" , objectInfo , err )
122
- }
123
- return nil
107
+ o := NewDeleteOp ()
108
+ o .applyOpts (opts )
109
+ return resourceClient .Delete (name , o .metaDeleteOptions )
124
110
}
0 commit comments