/
apply.go
57 lines (51 loc) · 1.34 KB
/
apply.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 yaml
import (
"github.com/hashicorp/go-multierror"
"github.com/illuminatingKong/kongming-kit/kubego/updater"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (d *Deploy) Apply(objs []runtime.Object, cl client.Client) error {
var errs *multierror.Error
for _, obj := range objs {
switch res := obj.(type) {
case *appsv1.Deployment:
err := updater.CreateOrPatchDeployment(res, cl)
if err != nil {
errs = multierror.Append(errs, err)
}
case *corev1.ConfigMap:
err := updater.CreateOrPatchConfigMap(res, cl)
if err != nil {
errs = multierror.Append(errs, err)
}
case *corev1.Namespace:
err := updater.CreateOrPatchNamespace(res, cl)
if err != nil {
errs = multierror.Append(errs, err)
}
case *corev1.Service:
err := updater.CreateOrPatchService(res, cl)
if err != nil {
errs = multierror.Append(errs, err)
}
}
}
return errs.ErrorOrNil()
}
func (d *Deploy) CreateOrPatch(yamlText string, cl client.Client, opts Option) error {
var errs *multierror.Error
verifyErr := d.Verify(yamlText)
if verifyErr != nil {
errs = multierror.Append(errs, verifyErr...)
return errs
}
rs := cl.Scheme()
obj, err := d.ReNew(yamlText, rs, opts)
if err != nil {
return err
}
return d.Apply(obj, cl)
}