-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (97 loc) · 3.11 KB
/
main.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
package k8s // import "github.com/caldito/soup/pkg/k8s"
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
yamlk8s "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"os"
)
func DoSSA(ctx context.Context, cfg *rest.Config, namespace string, manifest string) error {
var decUnstructured = yamlk8s.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
// 1. Prepare a RESTMapper to find GVR
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return err
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))
// 2. Prepare the dynamic client
dyn, err := dynamic.NewForConfig(cfg)
if err != nil {
return err
}
// 3. Decode YAML manifest into unstructured.Unstructured
yamlFile, err := ioutil.ReadFile(manifest)
if err != nil {
fmt.Println("Error reading manifest " + manifest)
// The program should not crash if the manifest path does not exist
return nil
}
obj := &unstructured.Unstructured{}
_, gvk, err := decUnstructured.Decode(yamlFile, nil, obj)
if err != nil {
return err
}
// 4. Find GVR
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return err
}
// 5. Obtain REST interface for the GVR and set namespace
var dr dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
// namespaced resources should specify the namespace
obj.SetNamespace(namespace)
dr = dyn.Resource(mapping.Resource).Namespace(obj.GetNamespace())
} else {
// for cluster-wide resources
dr = dyn.Resource(mapping.Resource)
}
// 6. Marshal object into JSON
data, err := json.Marshal(obj)
if err != nil {
return err
}
// 7. Create or Update the object with SSA
// types.ApplyPatchType indicates SSA.
// FieldManager specifies the field owner ID.
_, err = dr.Patch(ctx, obj.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{
FieldManager: "sample-controller",
})
return err
}
func DeclareNamespaceSSA(ctx context.Context, config *rest.Config, namespace string) error {
// create namespace manifest
file, err := os.Create("/tmp/soup/ns-creation.yml")
if err != nil {
fmt.Println("Error creating file for ensuring namespace " + namespace + " exists")
panic(err)
}
linesToWrite := []string{"kind: Namespace", "apiVersion: v1", "metadata:", " name: " + namespace, " labels:", " name: " + namespace}
for _, line := range linesToWrite {
file.WriteString(line + "\n")
}
file.Close()
// SSA for namespace
err = DoSSA(ctx, config, namespace, "/tmp/soup/ns-creation.yml")
if err != nil {
fmt.Println("Error creating namespace " + namespace)
panic(err)
}
// delete namespace manifest
err = os.Remove("/tmp/soup/ns-creation.yml")
if err != nil {
fmt.Println("Error deleting file for ensuring namespace " + namespace + " exists")
panic(err)
}
return nil
}