-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
57 lines (48 loc) · 1.66 KB
/
create.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
package kube
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/resource"
)
// Create creates a resource with the given content
func (c *Client) Create(name string, content []byte) error {
r := c.ResultForContent(name, content, true)
return c.create(r)
}
// CreateFile creates a resource in the given local filename or HTTP URL
func (c *Client) CreateFile(filename string) error {
filenames := []string{filename}
r := c.ResultForFilenameParam(filenames, true)
return c.create(r)
}
func (c *Client) create(r *resource.Result) error {
return r.Visit(func(info *resource.Info, err error) error {
var resKind string
if info.Mapping != nil {
resKind = info.Mapping.GroupVersionKind.Kind + " "
}
if err != nil {
c.ui.Log.Debugf("cannot create object %s%q on namespace %s, received error: %s", resKind, info.Name, info.Namespace, err)
return err
}
c.ui.Log.Debugf("creating object %s%q on namespace %s", resKind, info.Name, info.Namespace)
// if err := kubectl.CreateApplyAnnotation(info.Object, unstructured.UnstructuredJSONScheme); err != nil {
// return fmt.Errorf("creating %s. %s", info.String(), err)
// }
return create(info)
})
}
func create(info *resource.Info) error {
options := metav1.CreateOptions{}
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object, &options)
if err != nil {
return fmt.Errorf("creating %s. %s", info.String(), err)
}
info.Refresh(obj, true)
return nil
}
func (c *Client) reCreate(info *resource.Info) error {
// TODO: this method is to delete and create the resource. Requires the
// implementation of a delete method
return nil
}