-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (119 loc) · 3.19 KB
/
main.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
package main
import (
"flag"
"fmt"
"path/filepath"
"strings"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var (
kubeconfig *string
namespace string
repo string
project string
registry string
seq []string
)
func main() {
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("c", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("c", "", "absolute path to the kubeconfig file")
}
flag.StringVar(&repo, "repo", "", "provide a repo of your project")
flag.StringVar(&namespace, "n", "default", "namespace")
flag.StringVar(&project, "p", "", "your project name")
flag.StringVar(®istry, "r", "", "your registry name")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
canSplit := func(s rune) bool { return s == '/' }
seq := strings.FieldsFunc(registry, canSplit)
pod := &apiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: project,
Namespace: namespace,
Labels: map[string]string{
"app": "kaniko",
},
},
Spec: apiv1.PodSpec{
InitContainers: []apiv1.Container{
{
Name: "init-repo",
Image: "alpine/git",
ImagePullPolicy: apiv1.PullPolicy(apiv1.PullIfNotPresent),
Command: []string{"git", "clone", repo},
VolumeMounts: []apiv1.VolumeMount{
{
Name: "workdir",
MountPath: "/git",
},
},
},
},
Containers: []apiv1.Container{
{
Name: "kaniko",
Image: "gcr.io/kaniko-project/executor:latest",
ImagePullPolicy: apiv1.PullPolicy(apiv1.PullIfNotPresent),
Args: []string{"--dockerfile=/workspace/" + project + "/Dockerfile", "--context=dir://workspace/" + project, "--destination=" + registry, "--cache=true", "--cache-repo=" + seq[0] + "/cache", "--cleanup"},
VolumeMounts: []apiv1.VolumeMount{
{
Name: "workdir",
MountPath: "/workspace",
},
{
Name: "kaniko-secret",
MountPath: "/kaniko/.docker/",
},
},
},
},
NodeSelector: map[string]string{
"kaniko": "enabled",
},
Tolerations: []apiv1.Toleration{
{
Key: "kaniko",
Operator: apiv1.TolerationOperator(apiv1.TolerationOpEqual),
Value: "enabled",
Effect: apiv1.TaintEffect(apiv1.TaintEffectNoSchedule),
},
},
RestartPolicy: apiv1.RestartPolicy(apiv1.RestartPolicyNever),
Volumes: []apiv1.Volume{
apiv1.Volume{
Name: "kaniko-secret",
VolumeSource: apiv1.VolumeSource{
Secret: &apiv1.SecretVolumeSource{
SecretName: "kaniko-secret",
},
},
},
apiv1.Volume{
Name: "workdir",
},
},
},
}
// Deletepods
err = clientset.CoreV1().Pods(namespace).Delete(project, &metav1.DeleteOptions{})
if err != nil {
fmt.Printf("delete pod err :%v", err)
}
_, err = clientset.CoreV1().Pods(namespace).Create(pod)
if err != nil {
panic(err)
}
}