Skip to content

Commit

Permalink
refactor(main): add values \ Chart.yaml \ helpers.tpl
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu committed Jul 12, 2023
1 parent 0a53f4b commit 71620ee
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 16 deletions.
6 changes: 3 additions & 3 deletions plugins/helm/v3/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package scaffolds

import (
"fmt"
rbac2 "github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/rbac"
"github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/templates/rbac"

"github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/samples"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
Expand Down Expand Up @@ -70,8 +70,8 @@ func (s *apiScaffolder) Scaffold() error {
if s.resource.HasAPI() {
if err := scaffold.Execute(
&samples.CRDSample{Force: s.force},
&rbac2.CRDEditorRole{},
&rbac2.CRDViewerRole{},
&rbac.CRDEditorRole{},
&rbac.CRDViewerRole{},
); err != nil {
return fmt.Errorf("error scaffolding kustomize API manifests: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion plugins/helm/v3/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package scaffolds
import (
"fmt"
"github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts"
templates2 "github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/templates"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {

// Scaffold implements cmdutil.Scaffolder
func (s *initScaffolder) Scaffold() error {
fmt.Println("Writing kustomize manifests for you to edit...")
fmt.Println("Writing helm manifests for you to edit...")

// Initialize the machinery.Scaffold that will write the files to disk
scaffold := machinery.NewScaffold(s.fs,
Expand All @@ -75,7 +76,10 @@ func (s *initScaffolder) Scaffold() error {
//&kdefault2.ManagerConfigPatch{},
//&prometheus2.Kustomization{},
//&prometheus2.Monitor{},
&charts.Chart{},
&charts.HelmIgnore{},
&charts.Values{},
&templates2.Helpers{},
}

return scaffold.Execute(templates...)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package charts

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

var _ machinery.Template = &Chart{}

// Chart scaffolds a file that defines the kustomization scheme for the webhook folder
type Chart struct {
machinery.TemplateMixin
machinery.ProjectNameMixin
machinery.RepositoryMixin
Force bool
}

// SetTemplateDefaults implements file.Template
func (f *Chart) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "charts", f.ProjectName, "Chart.yaml")
}

f.TemplateBody = chartTemplate

if f.Force {
f.IfExistsAction = machinery.OverwriteFile
} else {
// If file exists (ex. because a webhook was already created), skip creation.
f.IfExistsAction = machinery.SkipFile
}

return nil
}

const chartTemplate = `apiVersion: v2
name: {{ .ProjectName }}
description: A Helm chart for Kubernetes auto generated by kubebuilder-helm
kubeVersion: "^1.22.0-0"
sources:
- https://{{ .Repo }}
home: https://{{ .Repo }}
keywords:
- {{ .ProjectName }}
type: application
version: 0.0.0
appVersion: "0.0.0"
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023 cuisongliu@qq.com.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package templates

import (
"path/filepath"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

var _ machinery.Template = &Helpers{}

// Helpers scaffolds a file that defines the helm scheme for the helpers.
type Helpers struct {
machinery.TemplateMixin
machinery.ProjectNameMixin
machinery.RepositoryMixin
Force bool
}

// SetTemplateDefaults implements file.Template
func (f *Helpers) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "charts", f.ProjectName, "templates", "_helpers.tpl")
}

f.TemplateBody = helpersTemplate

if f.Force {
f.IfExistsAction = machinery.OverwriteFile
} else {
// If file exists (ex. because a webhook was already created), skip creation.
f.IfExistsAction = machinery.SkipFile
}

return nil
}

const helpersTemplate = `{{"{{"}}/*
Expand the name of the chart.
*/}}
{{"{{"}}- define "{{ .ProjectName }}.name" -}}
{{"{{"}}- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{"{{"}}- end }}
{{"{{"}}/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{"{{"}}- define "{{ .ProjectName }}.fullname" -}}
{{"{{"}}- if .Values.fullnameOverride }}
{{"{{"}}- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{"{{"}}- else }}
{{"{{"}}- $name := default .Chart.Name .Values.nameOverride }}
{{"{{"}}- if contains $name .Release.Name }}
{{"{{"}}- .Release.Name | trunc 63 | trimSuffix "-" }}
{{"{{"}}- else }}
{{"{{"}}- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{"{{"}}- end }}
{{"{{"}}- end }}
{{"{{"}}- end }}
{{"{{"}}/*
Create chart name and version as used by the chart label.
*/}}
{{"{{"}}- define "{{ .ProjectName }}.chart" -}}
{{"{{"}}- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{"{{"}}- end }}
{{"{{"}}/*
Common labels
*/}}
{{"{{"}}- define "{{ .ProjectName }}.labels" -}}
helm.sh/chart: {{"{{"}} include "{{ .ProjectName }}.chart" . }}
{{"{{"}} include "{{ .ProjectName }}.selectorLabels" . }}
{{"{{"}}- if .Chart.AppVersion }}
app.kubernetes.io/version: {{"{{"}} .Chart.AppVersion | quote }}
{{"{{"}}- end }}
app.kubernetes.io/managed-by: {{"{{"}} .Release.Service }}
{{"{{"}}- end }}
{{"{{"}}/*
Selector labels
*/}}
{{"{{"}}- define "{{ .ProjectName }}.selectorLabels" -}}
app.kubernetes.io/name: {{"{{"}} include "{{ .ProjectName }}.name" . }}
app.kubernetes.io/instance: {{"{{"}} .Release.Name }}
{{"{{"}}- end }}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package charts

import (
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

var _ machinery.Template = &Values{}

// Values scaffolds a file that defines the kustomization scheme for the webhook folder
type Values struct {
machinery.TemplateMixin
machinery.ProjectNameMixin
machinery.RepositoryMixin
Force bool
GithubDockerRepo string
}

// SetTemplateDefaults implements file.Template
func (f *Values) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "charts", f.ProjectName, "values.yaml")
}

f.GithubDockerRepo = strings.Join(strings.Split(f.Repo, "/")[:2], "/")

f.TemplateBody = valuesTemplate

if f.Force {
f.IfExistsAction = machinery.OverwriteFile
} else {
// If file exists (ex. because a webhook was already created), skip creation.
f.IfExistsAction = machinery.SkipFile
}

return nil
}

const valuesTemplate = `# Default values for {{ .ProjectName }}.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
nameOverride: ""
fullnameOverride: ""
image:
repository: {{ .GithubDockerRepo }}
pullPolicy: IfNotPresent
image: {{ .ProjectName }}
# Overrides the image tag whose default is the chart appVersion.
tag: "latest"
`
23 changes: 11 additions & 12 deletions plugins/helm/v3/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package scaffolds

import (
"fmt"
certmanager2 "github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/certmanager"
kdefault2 "github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/kdefault"
webhook2 "github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/webhook"

"github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/templates/certmanager"
"github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/templates/kdefault"
"github.com/labring/kubebuilder-helm/plugins/helm/v3/scaffolds/internal/templates/config/charts/templates/webhook"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
Expand Down Expand Up @@ -68,14 +67,14 @@ func (s *webhookScaffolder) Scaffold() error {
}

if err := scaffold.Execute(
&kdefault2.WebhookCAInjectionPatch{},
&kdefault2.ManagerWebhookPatch{},
&webhook2.Kustomization{Force: s.force},
&webhook2.KustomizeConfig{},
&webhook2.Service{},
&certmanager2.Certificate{},
&certmanager2.Kustomization{},
&certmanager2.KustomizeConfig{},
&kdefault.WebhookCAInjectionPatch{},
&kdefault.ManagerWebhookPatch{},
&webhook.Kustomization{Force: s.force},
&webhook.KustomizeConfig{},
&webhook.Service{},
&certmanager.Certificate{},
&certmanager.Kustomization{},
&certmanager.KustomizeConfig{},
); err != nil {
return fmt.Errorf("error scaffolding kustomize webhook manifests: %v", err)
}
Expand Down

0 comments on commit 71620ee

Please sign in to comment.