diff --git a/cmd/helm/create_test.go b/cmd/helm/create_test.go index c9459b477..20bace864 100644 --- a/cmd/helm/create_test.go +++ b/cmd/helm/create_test.go @@ -143,7 +143,7 @@ func TestCreateStarterCmd(t *testing.T) { t.Errorf("Wrong API version: %q", c.Metadata.ApiVersion) } - expectedTemplateCount := 7 + expectedTemplateCount := 8 if l := len(c.Templates); l != expectedTemplateCount { t.Errorf("Expected %d templates, got %d", expectedTemplateCount, l) } diff --git a/pkg/chartutil/create.go b/pkg/chartutil/create.go index 8f713fcd1..743c7ca79 100644 --- a/pkg/chartutil/create.go +++ b/pkg/chartutil/create.go @@ -40,6 +40,8 @@ const ( IngressFileName = "ingress.yaml" // DeploymentName is the name of the example deployment file. DeploymentName = "deployment.yaml" + // HorizontalPodAutoscalerName is the name of the example hpa file. + HorizontalPodAutoscalerName = "hpa.yaml" // ServiceName is the name of the example service file. ServiceName = "service.yaml" // NotesName is the name of the example NOTES.txt file. @@ -56,16 +58,48 @@ const defaultValues = `# Default values for %s. # This is a YAML-formatted file. # Declare variables to be passed into your templates. -replicaCount: 1 - -image: - repository: nginx - tag: stable - pullPolicy: IfNotPresent - nameOverride: "" fullnameOverride: "" +deployment: + image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent + + replicaCount: 1 + + hpa: + enabled: false + minReplicas: 1 + maxReplicas: 10 + targetCPUUtilizationPercentage: 80 + targetMemoryUtilizationPercentage: 80 + + resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + + env: {} + # - name: FOO + # value: bar + # - name: BAZ + # value: qux + + nodeSelector: {} + + tolerations: [] + + affinity: {} + service: type: ClusterIP port: 80 @@ -82,24 +116,6 @@ ingress: # - secretName: chart-example-tls # hosts: # - chart-example.local - -resources: {} - # We usually recommend not to specify default resources and to leave this as a conscious - # choice for the user. This also increases chances charts run on environments with little - # resources, such as Minikube. If you do want to specify resources, uncomment the following - # lines, adjust them as necessary, and remove the curly braces after 'resources:'. - # limits: - # cpu: 100m - # memory: 128Mi - # requests: - # cpu: 100m - # memory: 128Mi - -nodeSelector: {} - -tolerations: [] - -affinity: {} ` const defaultIgnore = `# Patterns to ignore when building packages. @@ -178,7 +194,7 @@ metadata: app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ .Release.Service }} spec: - replicas: {{ .Values.replicaCount }} + replicas: {{ .Values.deployment.replicaCount }} selector: matchLabels: app.kubernetes.io/name: {{ include ".name" . }} @@ -191,8 +207,8 @@ spec: spec: containers: - name: {{ .Chart.Name }} - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" - imagePullPolicy: {{ .Values.image.pullPolicy }} + image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}" + imagePullPolicy: {{ .Values.deployment.image.pullPolicy }} ports: - name: http containerPort: 80 @@ -206,20 +222,54 @@ spec: path: / port: http resources: - {{- toYaml .Values.resources | nindent 12 }} - {{- with .Values.nodeSelector }} + {{- toYaml .Values.deployment.resources | nindent 12 }} + env: + {{- toYaml .Values.deployment.env | nindent 12 }} + {{- with .Values.deployment.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.affinity }} + {{- end }} + {{- with .Values.deployment.affinity }} affinity: {{- toYaml . | nindent 8 }} {{- end }} - {{- with .Values.tolerations }} + {{- with .Values.deployment.tolerations }} tolerations: {{- toYaml . | nindent 8 }} {{- end }} ` +const defaultHorizontalPodAutoscaler = `{{- if .Values.deployment.hpa.enabled }} +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + labels: + app.kubernetes.io/name: {{ include ".name" . }} + helm.sh/chart: {{ include ".chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + name: {{ template ".fullname" . }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ template ".fullname" . }} + minReplicas: {{ .Values.hpa.minReplicas }} + maxReplicas: {{ .Values.hpa.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.hpa.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} +` const defaultService = `apiVersion: v1 kind: Service @@ -413,6 +463,11 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) { path: filepath.Join(cdir, TemplatesDir, DeploymentName), content: Transform(defaultDeployment, "", chartfile.Name), }, + { + // hpa.yaml + path: filepath.Join(cdir, TemplatesDir, HorizontalPodAutoscalerName), + content: Transform(defaultHorizontalPodAutoscaler, "", chartfile.Name), + }, { // service.yaml path: filepath.Join(cdir, TemplatesDir, ServiceName),