You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.6 KiB

package controllers
import (
"bytes"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"text/template"
networkv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/util/yaml"
appsv1 "k8s.io/api/apps/v1"
myAppsv1 "mashibing.com/pkg/mashibing-deployment/api/v1"
)
func parseTemplate(md *myAppsv1.MsbDeployment, templateName string) ([]byte, error) {
tmpl, err := template.ParseFiles(fmt.Sprintf("controllers/templates/%s", templateName))
if err != nil {
return nil, err
}
b := &bytes.Buffer{}
if err := tmpl.Execute(b, md); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func NewDeployment(md *myAppsv1.MsbDeployment) (*appsv1.Deployment, error) {
content, err := parseTemplate(md, "deployment.yaml")
if err != nil {
return nil, err
}
deploy := new(appsv1.Deployment)
if err := yaml.Unmarshal(content, deploy); err != nil {
return nil, err
}
return deploy, nil
}
func NewIngress(md *myAppsv1.MsbDeployment) (*networkv1.Ingress, error) {
var (
content []byte
err error
)
if md.Spec.Expose.Tls {
// 添加 tls 的支持
content, err = parseTemplate(md, "ingress-with-tls.yaml")
} else {
content, err = parseTemplate(md, "ingress.yaml")
}
if err != nil {
return nil, err
}
ig := new(networkv1.Ingress)
if err := yaml.Unmarshal(content, ig); err != nil {
return nil, err
}
return ig, nil
}
func NewService(md *myAppsv1.MsbDeployment) (*corev1.Service, error) {
content, err := parseTemplate(md, "service.yaml")
if err != nil {
return nil, err
}
svc := new(corev1.Service)
if err := yaml.Unmarshal(content, svc); err != nil {
return nil, err
}
return svc, nil
}
func NewServiceNP(md *myAppsv1.MsbDeployment) (*corev1.Service, error) {
content, err := parseTemplate(md, "service-np.yaml")
if err != nil {
return nil, err
}
svc := new(corev1.Service)
if err := yaml.Unmarshal(content, svc); err != nil {
return nil, err
}
return svc, nil
}
// NewIssuer 实现创建issuer资源对象
func NewIssuer(md *myAppsv1.MsbDeployment) (*unstructured.Unstructured, error) {
if md.Spec.Expose.Mode != myAppsv1.ModeIngress ||
!md.Spec.Expose.Tls {
return nil, nil
}
// Sample
//apiVersion: cert-manager.io/v1
//kind: Issuer
//metadata:
// name: selfsigned-issuer
//spec:
// selfSigned: {}
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "cert-manager.io/v1",
"kind": "Issuer",
"metadata": map[string]interface{}{
"name": md.Name,
"namespace": md.Namespace,
},
"spec": map[string]interface{}{
"selfSigned": map[string]interface{}{},
},
},
}, nil
}
// NewCert 实现创建certificate资源
func NewCert(md *myAppsv1.MsbDeployment) (*unstructured.Unstructured, error) {
if md.Spec.Expose.Mode != myAppsv1.ModeIngress ||
!md.Spec.Expose.Tls {
return nil, nil
}
// Sample
//apiVersion: cert-manager.io/v1
//kind: Certificate
//metadata:
// name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
// namespace: system
//spec:
// dnsNames:
// - <spec.expose.ingressDomain>
// issuerRef:
// kind: Issuer
// name: selfsigned-issuer
// secretName: webhook-server-cert
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "cert-manager.io/v1",
"kind": "Certificate",
"metadata": map[string]interface{}{
"name": md.Name,
"namespace": md.Namespace,
},
"spec": map[string]interface{}{
"dnsNames": []interface{}{
md.Spec.Expose.IngressDomain,
},
"issuerRef": map[string]interface{}{
"kind": "Issuer",
"name": md.Name,
},
"secretName": md.Name,
},
},
}, nil
}