forked from devstream-io/devstream
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinstaller.go
More file actions
78 lines (68 loc) · 2.08 KB
/
Copy pathinstaller.go
File metadata and controls
78 lines (68 loc) · 2.08 KB
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
package kubectl
import (
"fmt"
"io"
"strings"
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/plugin/installer"
"github.com/devstream-io/devstream/pkg/util/kubectl"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/pkgerror"
"github.com/devstream-io/devstream/pkg/util/template"
)
func ProcessByContent(action, content string) installer.BaseOperation {
return func(options configmanager.RawOptions) error {
reader, err := renderKubectlContent(content, options)
if err != nil {
return err
}
return processByIOReader(action, reader)
}
}
func renderKubectlContent(content string, options configmanager.RawOptions) (io.Reader, error) {
content, err := template.New().FromContent(content).SetDefaultRender("kubectl", options).Render()
if err != nil {
return nil, err
}
if content == "" {
return nil, fmt.Errorf("kubectl content is empty")
}
return strings.NewReader(content), nil
}
func ProcessByURL(action, url string) installer.BaseOperation {
return func(options configmanager.RawOptions) error {
content, err := template.New().FromURL(url).SetDefaultRender("kubectl", options).Render()
if err != nil {
return err
}
if content == "" {
return fmt.Errorf("kubectl content is empty")
}
reader := strings.NewReader(content)
return processByIOReader(action, reader)
}
}
func processByIOReader(action string, reader io.Reader) error {
// generate k8s config file for apply
var err error
// kubectl apply -f
switch action {
case kubectl.Create:
err = kubectl.KubeApplyFromIOReader(reader)
case kubectl.Apply:
err = kubectl.KubeApplyFromIOReader(reader)
case kubectl.Delete:
err = kubectl.KubeDeleteFromIOReader(reader)
// ignore resource not exist error
if err != nil && pkgerror.CheckErrorMatchByMessage(err, kubectl.ArgocdApplicationNotExist) {
log.Warnf("kubectl config is already deleted")
err = nil
}
default:
err = fmt.Errorf("kubectl not support this kind of action: %v", action)
}
if err != nil {
return err
}
return nil
}