github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/kubernetes/client/templates.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/util/kubernetes/client/templates.go
    14  
    15  package client
    16  
    17  var templates = map[string]string{
    18  	"deployment":      deploymentTmpl,
    19  	"service":         serviceTmpl,
    20  	"namespace":       namespaceTmpl,
    21  	"secret":          secretTmpl,
    22  	"serviceaccount":  serviceAccountTmpl,
    23  	"networkpolicies": networkPolicyTmpl,
    24  	"networkpolicy":   networkPolicyTmpl,
    25  	"resourcequota":   resourceQuotaTmpl,
    26  }
    27  
    28  var deploymentTmpl = `
    29  apiVersion: apps/v1
    30  kind: Deployment
    31  metadata:
    32    name: "{{ .Metadata.Name }}"
    33    namespace: "{{ .Metadata.Namespace }}"
    34    labels:
    35      {{- with .Metadata.Labels }}
    36      {{- range $key, $value := . }}
    37      {{ $key }}: "{{ $value }}"
    38      {{- end }}
    39      {{- end }}
    40    annotations:
    41      {{- with .Metadata.Annotations }}
    42      {{- range $key, $value := . }}
    43      {{ $key }}: "{{ $value }}"
    44      {{- end }}
    45      {{- end }}
    46  spec:
    47    replicas: {{ .Spec.Replicas }}
    48    selector:
    49      matchLabels:
    50        {{- with .Spec.Selector.MatchLabels }}
    51        {{- range $key, $value := . }}
    52        {{ $key }}: "{{ $value }}"
    53        {{- end }}
    54        {{- end }}
    55    template:
    56      metadata:
    57        labels:
    58          {{- with .Spec.Template.Metadata.Labels }}
    59          {{- range $key, $value := . }}
    60          {{ $key }}: "{{ $value }}"
    61          {{- end }}
    62          {{- end }}
    63        annotations:
    64          {{- with .Spec.Template.Metadata.Annotations }}
    65          {{- range $key, $value := . }}
    66          {{ $key }}: "{{ $value }}"
    67          {{- end }}
    68          {{- end }}
    69      spec: 
    70        runtimeClassName: {{ .Spec.Template.PodSpec.RuntimeClassName }}
    71        serviceAccountName: {{ .Spec.Template.PodSpec.ServiceAccountName }}
    72        containers:
    73        {{- with .Spec.Template.PodSpec.Containers }}
    74        {{- range . }}
    75          - name: {{ .Name }}
    76            env:
    77            {{- with .Env }}
    78            {{- range . }}
    79            - name: "{{ .Name }}"
    80              value: "{{ .Value }}"
    81            {{- if .ValueFrom }}
    82            {{- with .ValueFrom }}
    83              valueFrom: 
    84                {{- if .SecretKeyRef }}
    85                {{- with .SecretKeyRef }}
    86                secretKeyRef:
    87                  key: {{ .Key }}
    88                  name: {{ .Name }}
    89                  optional: {{ .Optional }}
    90                {{- end }}
    91                {{- end }}
    92            {{- end }}
    93            {{- end }}
    94            {{- end }}
    95            {{- end }}
    96            args:
    97            {{- range .Args }}
    98            - {{.}}
    99            {{- end }}
   100            command:
   101            {{- range .Command }}
   102            - {{.}}
   103            {{- end }}
   104            image: {{ .Image }}
   105            imagePullPolicy: IfNotPresent
   106            ports:
   107            {{- with .Ports }}
   108            {{- range . }}
   109            - containerPort: {{ .ContainerPort }}
   110              name: {{ .Name }}
   111            {{- end }}
   112            {{- end }}
   113            {{- if .ReadinessProbe }}
   114            {{- with .ReadinessProbe }}
   115            readinessProbe:
   116              {{- with .TCPSocket }}
   117              tcpSocket:
   118                {{- if .Host }}
   119                host: {{ .Host }}
   120                {{- end }}
   121                port: {{ .Port }}
   122              {{- end }}
   123              initialDelaySeconds: {{ .InitialDelaySeconds }}
   124              periodSeconds: {{ .PeriodSeconds }}
   125            {{- end }}
   126            {{- end }}
   127            {{- if .Resources }}
   128            {{- with .Resources }}
   129            resources:
   130              {{- if .Limits }}
   131              {{- with .Limits }}
   132              limits:
   133                {{- if .Memory }}
   134                memory: {{ .Memory }}
   135                {{- end }}
   136                {{- if .CPU }}
   137                cpu: {{ .CPU }}
   138                {{- end }}
   139                {{- if .EphemeralStorage }}
   140                ephemeral-storage: {{ .EphemeralStorage }}
   141                {{- end }}
   142              {{- end }}
   143              {{- end }}
   144              {{- if .Requests }}
   145              {{- with .Requests }}
   146              requests:
   147                {{- if .Memory }}
   148                memory: {{ .Memory }}
   149                {{- end }}
   150                {{- if .CPU }}
   151                cpu: {{ .CPU }}
   152                {{- end }}
   153                {{- if .EphemeralStorage }}
   154                ephemeral-storage: {{ .EphemeralStorage }}
   155                {{- end }}
   156              {{- end }}
   157              {{- end }}
   158            {{- end }}
   159            {{- end }}
   160            volumeMounts:
   161            {{- with .VolumeMounts }}
   162            {{- range . }}
   163              - name: {{ .Name }}
   164                mountPath: {{ .MountPath }}
   165            {{- end }}
   166            {{- end }}
   167        {{- end }}
   168        {{- end }} 
   169        volumes:
   170        {{- with .Spec.Template.PodSpec.Volumes }}
   171        {{- range . }}
   172          - name: {{ .Name }}
   173            persistentVolumeClaim:
   174              claimName: {{ .PersistentVolumeClaim.ClaimName }}
   175        {{- end }}
   176        {{- end }}
   177  `
   178  
   179  var serviceTmpl = `
   180  apiVersion: v1
   181  kind: Service
   182  metadata:
   183    name: "{{ .Metadata.Name }}"
   184    namespace: "{{ .Metadata.Namespace }}"
   185    labels:
   186      {{- with .Metadata.Labels }}
   187      {{- range $key, $value := . }}
   188      {{ $key }}: "{{ $value }}"
   189      {{- end }}
   190      {{- end }}
   191  spec:
   192    selector:
   193      {{- with .Spec.Selector }}
   194      {{- range $key, $value := . }}
   195      {{ $key }}: "{{ $value }}"
   196      {{- end }}
   197      {{- end }}
   198    ports:
   199    {{- with .Spec.Ports }}
   200    {{- range . }}
   201    - name: "{{ .Name }}"
   202      port: {{ .Port }}
   203      protocol: {{ .Protocol }}
   204    {{- end }}
   205    {{- end }}
   206  `
   207  
   208  var namespaceTmpl = `
   209  apiVersion: v1
   210  kind: Namespace
   211  metadata:
   212    name: "{{ .Metadata.Name }}"
   213    labels:
   214      {{- with .Metadata.Labels }}
   215      {{- range $key, $value := . }}
   216      {{ $key }}: "{{ $value }}"
   217      {{- end }}
   218      {{- end }}
   219  `
   220  
   221  var secretTmpl = `
   222  apiVersion: v1
   223  kind: Secret
   224  type: "{{ .Type }}"
   225  metadata:
   226    name: "{{ .Metadata.Name }}"
   227    namespace: "{{ .Metadata.Namespace }}"
   228    labels:
   229      {{- with .Metadata.Labels }}
   230      {{- range $key, $value := . }}
   231      {{ $key }}: "{{ $value }}"
   232      {{- end }}
   233      {{- end }}
   234  data:
   235    {{- with .Data }}
   236    {{- range $key, $value := . }}
   237    {{ $key }}: "{{ $value }}"
   238    {{- end }}
   239    {{- end }}
   240  `
   241  
   242  var serviceAccountTmpl = `
   243  apiVersion: v1
   244  kind: ServiceAccount
   245  metadata:
   246    name: "{{ .Metadata.Name }}"
   247    labels:
   248      {{- with .Metadata.Labels }}
   249      {{- range $key, $value := . }}
   250      {{ $key }}: "{{ $value }}"
   251      {{- end }}
   252      {{- end }}
   253  imagePullSecrets:
   254  {{- with .ImagePullSecrets }}
   255  {{- range . }}
   256  - name: "{{ .Name }}"
   257  {{- end }}
   258  {{- end }}
   259  `
   260  
   261  var networkPolicyTmpl = `
   262  apiVersion: networking.k8s.io/v1
   263  kind: NetworkPolicy
   264  metadata:
   265    name: "{{ .Metadata.Name }}"
   266    namespace: "{{ .Metadata.Namespace }}"
   267    labels:
   268      {{- with .Metadata.Labels }}
   269      {{- range $key, $value := . }}
   270      {{ $key }}: "{{ $value }}"
   271      {{- end }}
   272      {{- end }}
   273  spec:
   274    podSelector:
   275      matchLabels:
   276    ingress:
   277    - from: # Allow pods in this namespace to talk to each other
   278      - podSelector: {}
   279    - from: # Allow pods in the namespaces bearing the specified labels to talk to pods in this namespace:
   280      - namespaceSelector:
   281          matchLabels:
   282            {{- with (index (index .Spec.Ingress 1).From 0).NamespaceSelector.MatchLabels }}
   283            {{- range $key, $value := . }}
   284            {{ $key }}: "{{ $value }}"
   285            {{- end }}
   286            {{- end }}
   287  `
   288  
   289  var resourceQuotaTmpl = `
   290  apiVersion: v1
   291  kind: ResourceQuota
   292  metadata:
   293    name: "{{ .Metadata.Name }}"
   294    namespace: "{{ .Metadata.Namespace }}"
   295    labels:
   296      {{- with .Metadata.Labels }}
   297      {{- range $key, $value := . }}
   298      {{ $key }}: "{{ $value }}"
   299      {{- end }}
   300      {{- end }}
   301  spec:
   302    hard:
   303    {{- if .Spec.Hard }}
   304    {{- with .Spec.Hard }}
   305      {{- if .LimitsMemory }}
   306      limits.memory: {{ .LimitsMemory }}
   307      {{- end }}
   308      {{- if .LimitsCPU }}
   309      limits.cpu: {{ .LimitsCPU }}
   310      {{- end }}
   311      {{- if .LimitsEphemeralStorage }}
   312      limits.ephemeral-storage: {{ .LimitsEphemeralStorage }}
   313      {{- end }}
   314      {{- if .RequestsMemory }}
   315      requests.memory: {{ .RequestsMemory }}
   316      {{- end }}
   317      {{- if .RequestsCPU }}
   318      requests.cpu: {{ .RequestsCPU }}
   319      {{- end }}
   320      {{- if .RequestsEphemeralStorage }}
   321      requests.ephemeral-storage: {{ .RequestsEphemeralStorage }}
   322      {{- end }}
   323    {{- end }}
   324    {{- end }}
   325  `