-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
128 lines (107 loc) · 3.65 KB
/
compile.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"fmt"
"time"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/krusty"
kustypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/api/konfig"
"strings"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/dynamic"
)
func Compile(fs filesys.FileSystem, dirPath string) (resmap.ResMap, error) {
buildOptions := &krusty.Options{
UseKyaml: false,
DoLegacyResourceSort: true,
LoadRestrictions: kustypes.LoadRestrictionsNone,
AddManagedbyLabel: false,
DoPrune: false,
PluginConfig: konfig.DisabledPluginConfig(),
AllowResourceIdChanges: false,
}
k := krusty.MakeKustomizer(fs, buildOptions)
return k.Run(dirPath)
}
func parseApplyOutput(in []byte) map[string]string {
result := make(map[string]string)
input := strings.Split(string(in), "\n")
if len(input) == 0 {
return result
}
var parts []string
for _, str := range input {
if str != "" {
parts = append(parts, str)
}
}
for _, str := range parts {
kv := strings.Split(str, " ")
if len(kv) > 1 {
result[kv[0]] = kv[1]
}
}
return result
}
func CheckDeployByKustomization(client dynamic.Interface, kustomization kustomizev1.Kustomization) ([]string, error){
var lastTimekustomizationDeployOutput []string
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{
fmt.Sprintf("%s/name", kustomizev1.GroupVersion.Group): kustomization.GetName(),
fmt.Sprintf("%s/namespace", kustomizev1.GroupVersion.Group): kustomization.GetNamespace(),
}}
// read status snapshot
if !kustomization.Spec.Prune || kustomization.Status.Snapshot == nil {
return lastTimekustomizationDeployOutput, nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*120))
defer cancel()
for ns, gvks := range kustomization.Status.Snapshot.NamespacedKinds() {
for _, gvk := range gvks {
gvr := schema.GroupVersionResource{
Group: gvk.Group,
Version: gvk.Version,
Resource: fmt.Sprint(strings.ToLower(gvk.Kind),"s"),
}
resourceList, err := client.Resource(gvr).Namespace(ns).List(ctx, metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
Limit: 100,
})
if err == nil {
for _, item := range resourceList.Items {
id := fmt.Sprintf("%s/%s/%s", fmt.Sprint(strings.ToLower(gvk.Kind),"s"), item.GetNamespace(), item.GetName())
lastTimekustomizationDeployOutput = append(lastTimekustomizationDeployOutput, id)
}
} else {
glog.Infof("client query failed for %s: %v", gvk.Kind, err)
}
}
}
for _, gvk := range kustomization.Status.Snapshot.NonNamespacedKinds() {
gvr := schema.GroupVersionResource{
Group: gvk.Group,
Version: gvk.Version,
Resource: fmt.Sprint(strings.ToLower(gvk.Kind),"s"),
}
resourceList, err := client.Resource(gvr).List(ctx, metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
Limit: 100,
})
if err == nil {
for _, item := range resourceList.Items {
//id := fmt.Sprintf("%s/%s", item.GetKind(), item.GetName())
id := fmt.Sprintf("%s/%s", fmt.Sprint(strings.ToLower(gvk.Kind),"s"), item.GetName())
//glog.Infof("resource is %s", id)
lastTimekustomizationDeployOutput = append(lastTimekustomizationDeployOutput, id)
}
} else {
glog.Infof("client query failed for %s: %v", gvk.Kind, err)
}
}
return lastTimekustomizationDeployOutput, nil
}