Skip to content

Commit

Permalink
Add hpa and env to helm create
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem Ullah <naseemkullah@gmail.com>
  • Loading branch information
naseemkullah committed Dec 29, 2018
1 parent c82c0b6 commit dfa227b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/helm/create_test.go
Expand Up @@ -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)
}
Expand Down
121 changes: 88 additions & 33 deletions pkg/chartutil/create.go
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 "<CHARTNAME>.name" . }}
Expand All @@ -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
Expand All @@ -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 "<CHARTNAME>.name" . }}
helm.sh/chart: {{ include "<CHARTNAME>.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
name: {{ template "<CHARTNAME>.fullname" . }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ template "<CHARTNAME>.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
Expand Down Expand Up @@ -413,6 +463,11 @@ func Create(chartfile *chart.Metadata, dir string) (string, error) {
path: filepath.Join(cdir, TemplatesDir, DeploymentName),
content: Transform(defaultDeployment, "<CHARTNAME>", chartfile.Name),
},
{
// hpa.yaml
path: filepath.Join(cdir, TemplatesDir, HorizontalPodAutoscalerName),
content: Transform(defaultHorizontalPodAutoscaler, "<CHARTNAME>", chartfile.Name),
},
{
// service.yaml
path: filepath.Join(cdir, TemplatesDir, ServiceName),
Expand Down

0 comments on commit dfa227b

Please sign in to comment.