-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
86 lines (69 loc) · 2.15 KB
/
config.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
package main
import (
"encoding/base64"
"io/ioutil"
"os"
"strconv"
"github.com/pkg/errors"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
kubeconfigPath = "run-k8s-job-kubeconfig"
)
var (
errNoAuth = errors.New("you must provide either 'kubeconfig-file' or both 'cluster-url' and 'cluster-token'")
)
type ActionInput struct {
kubeconfigFile string
image string
jobName string
namespace string
clusterURL string
clusterToken string
caFile string
allowInsecure string
}
func BuildK8sConfig(input ActionInput) (*rest.Config, error) {
if len(input.image) == 0 {
return nil, errors.New("'image' is a required input but was empty")
}
if len(input.kubeconfigFile) == 0 {
return buildConfigWithSecondaryAuth(input)
}
data, err := base64.StdEncoding.DecodeString(input.kubeconfigFile)
if err != nil {
return nil, errors.Wrap(err, "could not decode kubeconfig file")
}
err = ioutil.WriteFile(kubeconfigPath, data, 0644)
if err != nil {
return nil, errors.Wrap(err, "could not decode kubeconfig file")
}
defer os.Remove(kubeconfigPath)
return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
}
func buildConfigWithSecondaryAuth(input ActionInput) (*rest.Config, error) {
if len(input.clusterURL) == 0 || len(input.clusterToken) == 0 {
return nil, errors.Wrap(errNoAuth, "missing input for cluster authentication")
}
allowInsecure, err := strconv.ParseBool(input.allowInsecure)
if err != nil {
return nil, errors.Errorf("'allow-insecure input must be either 'true' or 'false', was %s", input.allowInsecure)
}
if !allowInsecure {
if len(input.caFile) == 0 {
return nil, errors.New("you must either include 'caPath' or set 'allow-insecure' to true")
}
if _, err := os.Stat(input.caFile); os.IsNotExist(err) {
return nil, errors.Errorf("could not locate file %s; please make sure the file is available in the runner's context", input.caFile)
}
}
config, err := clientcmd.BuildConfigFromFlags(input.clusterURL, "")
if err != nil {
return nil, err
}
config.Insecure = allowInsecure
config.BearerToken = input.clusterToken
config.CAFile = input.caFile
return config, nil
}