代码拉取完成,页面将自动刷新
package helmx
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"github.com/go-courier/helmx/spec"
"github.com/onsi/gomega"
)
func init() {
os.Setenv(spec.EnvKeyImagePullSecret, "qcloud-registry://username:password@docker.io/pf-")
}
func Test(t *testing.T) {
hx := NewHelmX()
hx.AddTemplate("pullSecret", pullSecret)
hx.AddTemplate("serviceAccount", serviceAccount)
hx.AddTemplate("ingress", ingress)
hx.AddTemplate("service", service)
hx.AddTemplate("deployment", deployment)
hx.AddTemplate("job", job)
hx.AddTemplate("cronJob", cronJob)
if err := hx.FromYAML([]byte(
`
project:
name: helmx
feature: test
group: helmx
version: 0.0.0
description: helmx
service:
hostNetwork: true
imagePullSecret: qcloud-registry://username:password@docker.io/pf-
hostAliases:
- "127.0.0.1:test1.com,test2.com"
- "127.0.0.2:test2.com,test3.com"
mounts:
- "data:/usr/share/nginx:ro"
ports:
- "80:80"
- "!20000:80"
livenessProbe:
action: "http://:80"
lifecycle:
preStop: "nginx -s quit"
ingresses:
- "http://helmx:80/helmx"
tls:
- "secretName"
- "secretNameWithHost:helmx"
serviceAccountName: test
serviceAccountRoleRules:
- secrets#get,update
securityContext:
runAsUser: 1024
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
privileged: true
topologySpreadConstraints:
- maxSkew: 1
topologyKey: zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
foo: bar
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: NotIn
values:
- zoneC
initials:
- image: dockercloud/hello-world
mounts:
- "data:/usr/share/nginx"
command:
- mv
args:
- /www
- /usr/share/nginx/html
jobs:
doonce:
image: busybox
backoffLimit: 4
dosomecron:
image: busybox
cron:
schedule: "*/1 * * * *"
envs:
env: "test"
valueWithDot: "value.with.dot"
secretFalse: "####secretName.secretKey.false####"
secretTrue: "####secretName.secretKey.true####"
configMap: "####configMapName.configMapKey####"
resources:
cpu: 10/20m
memory: 0/20Mi
nvidia.com/gpu: 0/20
tolerations:
- env=test
- project
volumes:
data:
emptyDir:
medium: Memory
sizeLimit: "1Gi"
upstreams:
- redis
- mysql
labels:
testKey1: testValue1
testKey2: testValue2
`)); err != nil {
t.Fatal(err)
}
if err := hx.ExecuteAll(os.Stdout, &hx.Spec); err != nil {
panic(err)
}
}
func TestTemplates(t *testing.T) {
baseProject := `
project:
name: helmx
feature: test
group: helmx
version: 0.0.0
description: helmx
`
t.Run("service", func(t *testing.T) {
check(t, baseProject+`
service:
ports:
- "80:80"
`,
service,
`
---
apiVersion: v1
kind: Service
metadata:
name: helmx--test
annotations:
helmx/project: >-
{"name":"helmx","feature":"test","version":"0.0.0","group":"helmx","description":"helmx"}
helmx/upstreams: ""
spec:
selector:
srv: helmx--test
type: ClusterIP
ports:
- name: http-80
port: 80
targetPort: 80
protocol: TCP
`,
)
})
t.Run("headlessService", func(t *testing.T) {
check(t, baseProject+`
service:
headless: true
ports:
- "80:80"
`,
service,
`
---
apiVersion: v1
kind: Service
metadata:
name: helmx--test
annotations:
helmx/project: >-
{"name":"helmx","feature":"test","version":"0.0.0","group":"helmx","description":"helmx"}
helmx/upstreams: ""
spec:
selector:
srv: helmx--test
clusterIP: None
type: ClusterIP
ports:
- name: http-80
port: 80
targetPort: 80
protocol: TCP
`,
)
})
t.Run("service with nodePort", func(t *testing.T) {
check(t, baseProject+`
service:
ports:
- "!20000:80"
- "!80"
- "!25000:25000"
- "!40000:80"
- "80:8080"
- "80"
`,
service,
`
---
apiVersion: v1
kind: Service
metadata:
name: helmx--test
annotations:
helmx/project: >-
{"name":"helmx","feature":"test","version":"0.0.0","group":"helmx","description":"helmx"}
helmx/upstreams: ""
spec:
selector:
srv: helmx--test
type: NodePort
ports:
- name: np-http-20000
nodePort: 20000
port: 80
targetPort: 80
protocol: TCP
- name: np-http-80
port: 80
targetPort: 80
protocol: TCP
- name: np-http-25000
nodePort: 25000
port: 25000
targetPort: 25000
protocol: TCP
- name: np-http-40000
nodePort: 40000
port: 80
targetPort: 80
protocol: TCP
- name: http-80
port: 80
targetPort: 8080
protocol: TCP
- name: http-80
port: 80
targetPort: 80
protocol: TCP
`,
)
})
t.Run("ingress with tls", func(t *testing.T) {
check(t, baseProject+`
service:
ingresses:
- "http://helmx:80/helmx"
tls:
- "secretName:helmx"
`,
ingress,
`
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: helmx--test
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: helmx
http:
paths:
- path: /helmx
backend:
serviceName: helmx--test
servicePort: 80
tls:
- host:
- helmx
secretName: secretName
`,
)
})
t.Run("deployment", func(t *testing.T) {
check(t, baseProject+`
service:
hostNetwork: true
hosts:
- "127.0.0.1:test1.com,test2.com"
- "127.0.0.2:test3.com,test4.com"
ports:
- "80:80"
securityContext:
runAsUser: 1024
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
privileged: true
envs:
env: "test"
valueWithDot: "value.with.dot"
secretFalse: "####secretName.secretKey.false####"
secretTrue: "####secretName.secretKey.true####"
configMap: "####configMapName.configMapKey####"
`,
deployment,
`
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helmx--test
labels:
app: helmx--test
version: 0.0.0
annotations:
helmx: "{\"project\":{\"name\":\"helmx\",\"feature\":\"test\",\"version\":\"0.0.0\",\"group\":\"helmx\",\"description\":\"helmx\"},\"service\":{\"securityContext\":{\"runAsUser\":1024,\"runAsGroup\":1000,\"runAsNonRoot\":true,\"readOnlyRootFilesystem\":true,\"privileged\":true},\"hostNetwork\":true,\"hosts\":[\"127.0.0.1:test1.com,test2.com\",\"127.0.0.2:test3.com,test4.com\"],\"ports\":[\"80\"]},\"envs\":{\"configMap\":\"####configMapName.configMapKey####\",\"env\":\"test\",\"secretFalse\":\"####secretName.secretKey.false####\",\"secretTrue\":\"####secretName.secretKey.true####\",\"valueWithDot\":\"value.with.dot\"}}"
spec:
selector:
matchLabels:
srv: helmx--test
template:
metadata:
labels:
srv: helmx--test
spec:
containers:
- name: helmx--test
securityContext:
runAsUser: 1024
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
privileged: true
image: docker.io/pf-helmx/helmx:0.0.0
ports:
- containerPort: 80
protocol: TCP
env:
- name: configMap
valueFrom:
configMapKeyRef:
key: configMapKey
name: configMapName
- name: env
value: test
- name: secretFalse
valueFrom:
secretKeyRef:
key: secretKey
name: secretName
optional: false
- name: secretTrue
valueFrom:
secretKeyRef:
key: secretKey
name: secretName
optional: true
- name: valueWithDot
value: value.with.dot
imagePullSecrets:
- name: qcloud-registry
hostNetwork: true
hostAliases:
- ip: 127.0.0.1
hostnames:
- test1.com
- test2.com
- ip: 127.0.0.2
hostnames:
- test3.com
- test4.com
`,
)
})
t.Run("job", func(t *testing.T) {
check(t, baseProject+`
jobs:
doonce:
image: busybox
restartPolicy: Never
backoffLimit: 4
docron:
image: busybox
restartPolicy: Never
cron:
schedule: "*/1 * * * *"
`,
job,
`
---
apiVersion: batch/v1
kind: Job
metadata:
name: helmx--test--doonce
spec:
backoffLimit: 4
template:
spec:
containers:
- name: helmx--test
image: busybox
imagePullSecrets:
- name: qcloud-registry
restartPolicy: Never
`,
)
})
t.Run("cronJob", func(t *testing.T) {
check(t, baseProject+`
jobs:
doonce:
image: busybox
restartPolicy: Never
backoffLimit: 4
docron:
image: busybox
restartPolicy: Never
cron:
schedule: "*/1 * * * *"
`,
cronJob,
`
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: helmx--test--docron
spec:
schedule: '*/1 * * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: helmx--test
image: busybox
imagePullSecrets:
- name: qcloud-registry
restartPolicy: Never
`,
)
})
}
var (
service = `
{{ if ( and ( exists .Service ) ( gt ( len .Service.Ports ) 0 ) ) }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ ( .Project.FullName ) }}
annotations:
helmx/project: >-
{{ toJson .Project }}
helmx/upstreams: {{ join .Upstreams "," | quote }}
spec:
selector:
srv: {{ ( .Project.FullName ) }}
{{ spaces 2 | toYamlIndent ( toKubeServiceSpec . ) }}
{{ end }}
`
deployment = `
{{ if ( exists .Service ) }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ ( .Project.FullName ) }}
labels:
app: {{ ( .Project.FullName ) }}
version: {{ ( .Project.Version ) }}
annotations:
helmx: {{ toJson . | quote }}
spec:
selector:
matchLabels:
srv: {{ ( .Project.FullName ) }}
{{ spaces 2 | toYamlIndent ( toKubeDeploymentSpec . ) }}
{{ end }}
`
job = `
{{ $spec := .}}
{{ range $name, $job := .Jobs }}
{{ if (not (exists $job.Cron)) }}
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ ( $spec.Project.FullName ) }}--{{ $name }}
spec:
{{ spaces 2 | toYamlIndent ( toKubeJobSpec $spec $job ) }}
{{ end }}
{{ end }}
`
cronJob = `
{{ $spec := .}}
{{ range $name, $job := .Jobs }}
{{ if (exists $job.Cron) }}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: {{ ( $spec.Project.FullName ) }}--{{ $name }}
spec:
{{ spaces 2 | toYamlIndent ( toKubeCronJobSpec $spec $job ) }}
{{ end }}
{{ end }}
`
ingress = `
{{ if ( len .Service.Ingresses ) }}
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ ( .Project.FullName ) }}
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
{{ spaces 2 | toYamlIndent ( toKubeIngressSpec . )}}
{{ end }}
`
serviceAccount = `
{{ if ( len .Service.ServiceAccountRoleRules ) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ ( .Service.ServiceAccountName ) }}
rules:
{{ spaces 2 | toYamlIndent ( toKubeRoleRules . )}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ ( .Service.ServiceAccountName ) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ ( .Service.ServiceAccountName ) }}
subjects:
- kind: ServiceAccount
name: {{ ( .Service.ServiceAccountName ) }}
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: {{ ( .Service.ServiceAccountName ) }}
apiGroup: rbac.authorization.k8s.io
{{ end }}
`
pullSecret = `
{{ if ( exists .Service.ImagePullSecret ) }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ ( .Service.ImagePullSecret.Name ) }}
data:
.dockerconfigjson: {{ ( .Service.ImagePullSecret.Base64EncodedDockerConfigJSON ) }}
{{ end }}
`
)
func check(t *testing.T, helmx string, tmpl string, expect string) {
fmt.Println(helmx)
hx := NewHelmX()
err := hx.FromYAML([]byte(helmx))
gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
hx.AddTemplate("tmpl", tmpl)
buf := &bytes.Buffer{}
err = hx.ExecuteAll(buf, &hx.Spec)
gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
fmt.Println("----------helmx----------------")
fmt.Println(buf.String())
fmt.Println("-------------------------------")
gomega.NewWithT(t).Expect(strings.TrimSpace(buf.String())).To(gomega.Equal(strings.TrimSpace(expect)))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。