This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kyverno.go
185 lines (152 loc) · 4.63 KB
/
kyverno.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package deprecation
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"strings"
"text/template"
"github.com/ckotzbauer/chekr/pkg/util"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
var kyvernoMessage = "{{ request.object.apiVersion }}/{{ request.object.kind }} is deprecated and will be removed in %s."
type Version struct {
Version string
Message string
Name string
Kinds []string
}
type Container struct {
Versions []Version
Category string
Subject string
ValidationFailureAction string
Background bool
}
func (d Deprecation) ExecuteKyvernoCreate() string {
apis := fetchDeprecatedApis()
rest.SetDefaultWarningHandler(rest.NoWarnings{})
return d.createKyvernoPolicies(apis)
}
func (d Deprecation) createKyvernoPolicies(apis []GroupVersion) string {
m := make(map[string][]string)
ignoredDeprecatedKinds = append(ignoredDeprecatedKinds, d.IgnoredKinds...)
for _, a := range apis {
for _, r := range a.Resources {
if util.Contains(ignoredDeprecatedKinds, r.Name) {
continue
}
if x, found := m[r.Removed]; found {
x = append(x, fmt.Sprintf("%s/%s/%s", a.Group, a.Version, r.Name))
m[r.Removed] = x
} else {
m[r.Removed] = []string{fmt.Sprintf("%s/%s/%s", a.Group, a.Version, r.Name)}
}
}
}
container := Container{
Versions: []Version{},
Category: d.Category,
Subject: d.Subject,
ValidationFailureAction: d.ValidationFailureAction,
Background: d.Background,
}
for s, v := range m {
if s == "" {
s = "the future"
}
if s != "the future" {
if d.isVersionIgnored(s) {
continue
}
} else if d.K8sVersion != "" {
continue
}
container.Versions = append(container.Versions, Version{
Version: s,
Message: fmt.Sprintf(kyvernoMessage, s),
Name: fmt.Sprintf("validate-%s-removals", strings.Replace(s, ".", "-", -1)),
Kinds: v,
})
}
buf := new(bytes.Buffer)
tpl := template.New("page")
tpl, err := tpl.Parse(KyvernoTemplate)
if err != nil {
logrus.WithError(err).Fatalf("Could not parse policy-template!")
}
err = tpl.Execute(buf, container)
if err != nil {
logrus.WithError(err).Fatalf("Could not render policy-template!")
}
return buf.String()
}
func (d Deprecation) HandleKyvernoResult(stringOutput, output, outputFile string, dryRun bool) {
if dryRun {
if output == "json" {
data, _ := parseYaml(stringOutput)
printResult(string(data), outputFile)
return
}
printResult(stringOutput, outputFile)
} else {
d.applyPolicy(stringOutput)
}
}
func printResult(str, outputFile string) {
if outputFile != "" {
data := []byte(str)
err := os.WriteFile(outputFile, data, 0640)
if err != nil {
logrus.WithError(err).Fatalf("Could write output to file!")
}
} else {
fmt.Fprint(os.Stdout, str)
}
}
func parseYaml(stringOutput string) ([]byte, *unstructured.Unstructured) {
decUnstructured := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
obj := &unstructured.Unstructured{}
_, _, err := decUnstructured.Decode([]byte(stringOutput), nil, obj)
if err != nil {
logrus.WithError(err).Fatalf("Could not decode policy-string.")
}
data, err := json.Marshal(obj)
if err != nil {
logrus.WithError(err).Fatalf("Could not marshal object to json.")
}
return data, obj
}
func (d Deprecation) applyPolicy(stringOutput string) {
data, obj := parseYaml(stringOutput)
dyn, _ := dynamic.NewForConfig(d.KubeClient.Config)
dr := dyn.Resource(schema.GroupVersionResource{Group: "kyverno.io", Version: "v1", Resource: "clusterpolicies"})
_, err := dr.Patch(context.Background(), obj.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{
FieldManager: "chekr",
})
if err != nil {
logrus.WithError(err).Fatalf("Could not apply policy.")
} else {
fmt.Printf("clusterpolicy/%s created/updated\n", obj.GetName())
}
}
func (d Deprecation) DeletePolicy() {
dyn, _ := dynamic.NewForConfig(d.KubeClient.Config)
dr := dyn.Resource(schema.GroupVersionResource{Group: "kyverno.io", Version: "v1", Resource: "clusterpolicies"})
name := "chekr-check-deprecated-apis"
err := dr.Delete(context.Background(), name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
logrus.WithError(err).Fatalf("Could not delete policy.")
} else {
fmt.Printf("clusterpolicy/%s deleted\n", name)
}
}