-
Notifications
You must be signed in to change notification settings - Fork 67
/
kubernetes_deploy.go
137 lines (108 loc) · 3.6 KB
/
kubernetes_deploy.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
package exec
import (
"context"
"fmt"
"os"
"path"
"runtime"
"github.com/pkg/errors"
"github.com/portainer/agent"
)
// KubernetesDeployer represents a service to deploy resources inside a Kubernetes environment.
type KubernetesDeployer struct {
command string
}
// NewKubernetesDeployer initializes a new KubernetesDeployer service.
func NewKubernetesDeployer(binaryPath string) *KubernetesDeployer {
command := path.Join(binaryPath, "kubectl")
if runtime.GOOS == "windows" {
command = path.Join(binaryPath, "kubectl.exe")
}
return &KubernetesDeployer{
command: command,
}
}
func (deployer *KubernetesDeployer) operation(ctx context.Context, name string, filePaths []string, operation, namespace string) error {
if len(filePaths) == 0 {
return errors.New("missing file paths")
}
stackFilePath := filePaths[0]
args, err := buildArgs(&argOptions{
Namespace: namespace,
})
if err != nil {
return err
}
args = append(args, operation, "-f", stackFilePath)
_, err = runCommandAndCaptureStdErr(deployer.command, args, nil)
return err
}
// Deploy will deploy a Kubernetes manifest inside the default namespace
// it will use kubectl to deploy the manifest.
// kubectl uses in-cluster config.
func (deployer *KubernetesDeployer) Deploy(ctx context.Context, name string, filePaths []string, options agent.DeployOptions) error {
return deployer.operation(ctx, name, filePaths, "apply", options.Namespace)
}
func (deployer *KubernetesDeployer) Remove(ctx context.Context, name string, filePaths []string, options agent.RemoveOptions) error {
return deployer.operation(ctx, name, filePaths, "delete", options.Namespace)
}
// Pull is a dummy method for Kube
func (deployer *KubernetesDeployer) Pull(ctx context.Context, name string, filePaths []string, options agent.PullOptions) error {
return nil
}
// Validate is a dummy method for Kubernetes manifest validation
// https://portainer.atlassian.net/browse/EE-6292?focusedCommentId=29674
func (deployer *KubernetesDeployer) Validate(ctx context.Context, name string, filePaths []string, options agent.ValidateOptions) error {
return nil
}
// DeployRawConfig will deploy a Kubernetes manifest inside a specific namespace
// it will use kubectl to deploy the manifest and receives a raw config.
// kubectl uses in-cluster config.
func (deployer *KubernetesDeployer) DeployRawConfig(token, config string, namespace string) ([]byte, error) {
args, err := buildArgs(&argOptions{
Namespace: namespace,
Token: token,
})
if err != nil {
return nil, err
}
args = append(args, "apply", "-f", "-")
return runCommandAndCaptureStdErr(deployer.command, args, &cmdOpts{Input: config})
}
type argOptions struct {
Namespace string
Token string
}
func buildArgs(opts *argOptions) ([]string, error) {
args := []string{}
if opts == nil {
return args, nil
}
if opts.Namespace != "" {
args = append(args, "--namespace", opts.Namespace)
}
if opts.Token != "" {
tokenArgs, err := buildTokenArgs(opts.Token)
if err != nil {
return nil, errors.Wrap(err, "failed building token args")
}
args = append(args, tokenArgs...)
}
return args, nil
}
func buildTokenArgs(token string) ([]string, error) {
host := os.Getenv(agent.KubernetesServiceHost)
if host == "" {
return nil, fmt.Errorf("%s env var is not defined", agent.KubernetesServiceHost)
}
port := os.Getenv(agent.KubernetesServicePortHttps)
if port == "" {
return nil, fmt.Errorf("%s env var is not defined", agent.KubernetesServicePortHttps)
}
server := fmt.Sprintf("https://%s:%s", host, port)
return []string{
"--token", token,
"--server", server,
"--insecure-skip-tls-verify",
}, nil
}