From 03bb315cc21f970d7ba77e221005b76ffc9dbae1 Mon Sep 17 00:00:00 2001 From: tangx Date: Tue, 12 Mar 2024 14:13:08 +0800 Subject: [PATCH] feat: add apiversion --- pkg/kustz/apiversion.go | 5 +++++ pkg/kustz/k_pod.go | 4 ++-- pkg/kustz/kustz.yml | 11 ++++++----- pkg/kustz/kustz_test.go | 9 +++++++++ pkg/kustz/types.go | 18 ++++++++++++++---- 5 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 pkg/kustz/apiversion.go diff --git a/pkg/kustz/apiversion.go b/pkg/kustz/apiversion.go new file mode 100644 index 0000000..9c9d5e7 --- /dev/null +++ b/pkg/kustz/apiversion.go @@ -0,0 +1,5 @@ +package kustz + +type Metadata struct { + APIVersion string `json:"apiVersion" yaml:"apiVersion"` +} diff --git a/pkg/kustz/k_pod.go b/pkg/kustz/k_pod.go index 92333b6..d51e3db 100644 --- a/pkg/kustz/k_pod.go +++ b/pkg/kustz/k_pod.go @@ -13,8 +13,8 @@ func (kz *Config) KubePod() corev1.PodTemplateSpec { Spec: corev1.PodSpec{ Containers: kz.KubeContainer(), ImagePullSecrets: toImagePullSecrets(kz.Service.ImagePullSecrets), - DNSConfig: toPodDNSConfig(&kz.DNS), - DNSPolicy: toDNSPolicy(&kz.DNS), + DNSConfig: toPodDNSConfig(kz.DNS), + DNSPolicy: toDNSPolicy(kz.DNS), }, } } diff --git a/pkg/kustz/kustz.yml b/pkg/kustz/kustz.yml index 9bd0957..76a1c47 100644 --- a/pkg/kustz/kustz.yml +++ b/pkg/kustz/kustz.yml @@ -1,3 +1,5 @@ +apiVersion: kustz/v1 + namespace: demo-demo name: srv-webapp-demo @@ -41,7 +43,7 @@ service: # dns: # config: # nameservers: -# - 10.133.10.55 # align internat dns +# - 10.133.10.55 # searches: # - ns1.svc.cluster.local # - my.dns.search.suffix @@ -58,7 +60,6 @@ ingress: - http://api.example.com/ping?tls=star-example-com&svc=srv-webapp-demo:8080 - http://api.balala.com/* - # https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/configmapgenerator/ configmaps: envs: @@ -76,10 +77,10 @@ secrets: files: - name: srv-webapp-demo-files files: - - tls.crt=catsecret/tls.crt - - tls.key=secret/tls.key + - tls.crt=catsecret/tls.crt + - tls.key=secret/tls.key type: "kubernetes.io/tls" - name: aliyun-repo files: - .dockerconfigjson=docker-config.json - type: kubernetes.io/dockerconfigjson \ No newline at end of file + type: kubernetes.io/dockerconfigjson diff --git a/pkg/kustz/kustz_test.go b/pkg/kustz/kustz_test.go index e14f36e..910393a 100644 --- a/pkg/kustz/kustz_test.go +++ b/pkg/kustz/kustz_test.go @@ -11,6 +11,15 @@ var ( kz = NewKustzFromConfig("./kustz.yml") ) +func Test_YamlMarshal(t *testing.T) { + b, err := kubeutils.YAMLMarshal(kz) + if err != nil { + panic(err) + } + + fmt.Printf("%s\n", b) +} + func Test_KustzDeployment(t *testing.T) { dep := kz.KubeDeployment() output(dep) diff --git a/pkg/kustz/types.go b/pkg/kustz/types.go index 6c7cfb0..d0bd4ed 100644 --- a/pkg/kustz/types.go +++ b/pkg/kustz/types.go @@ -6,14 +6,20 @@ import ( "github.com/tangx/kustz/pkg/kubeutils" ) +const ( + APIVersion = "kustz/v1" +) + type Config struct { + Metadata `json:",inline"` + Name string `json:"name"` Namespace string `json:"namespace"` Service Service `json:"service"` - Ingress Ingress `json:"ingress"` - ConfigMaps Generator `json:"configmaps"` - Secrets Generator `json:"secrets"` - DNS DNS `json:"dns"` + Ingress Ingress `json:"ingress,omitempty"` + ConfigMaps Generator `json:"configmaps,omitempty"` + Secrets Generator `json:"secrets,omitempty"` + DNS *DNS `json:"dns,omitempty"` } func NewKustzFromConfig(cfg string) *Config { @@ -28,6 +34,10 @@ func NewKustzFromConfig(cfg string) *Config { panic(err) } + if kz.Metadata.APIVersion == "" { + kz.Metadata.APIVersion = APIVersion + } + return kz }