Skip to content

Commit

Permalink
✨ use new scaffolding and more kustomize features
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed May 6, 2019
1 parent 9672e57 commit a3e6e25
Show file tree
Hide file tree
Showing 43 changed files with 1,388 additions and 122 deletions.
1 change: 1 addition & 0 deletions generated_golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ scaffold_test_project() {
$kb alpha webhook --group core --version v1 --kind Namespace --type=mutating --operations=update --make=false
$kb create api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false --make=false
elif [ $version == "2" ]; then
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update --make=false
$kb alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=delete --make=false
Expand Down
17 changes: 17 additions & 0 deletions pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/controller"
resourcev1 "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource"
resourcev2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
crdv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/crd"
)

// API contains configuration for generating scaffolding for Go type
Expand Down Expand Up @@ -159,10 +160,26 @@ func (api *API) scaffoldV2() error {
Resource: r},
&resourcev2.Group{Resource: r},
&resourcev1.CRDSample{Resource: r},
&crdv2.EnableWebhookPatch{Resource: r},
)
if err != nil {
return fmt.Errorf("error scaffolding APIs: %v", err)
}

crdKustomization := &crdv2.Kustomization{Resource: r}
err = (&Scaffold{}).Execute(
input.Options{},
crdKustomization,
&crdv2.KustomizeConfig{},
)
if err != nil && !isAlreadyExistsError(err) {
return fmt.Errorf("error scaffolding kustomization: %v", err)
}

err = crdKustomization.Update()
if err != nil {
return fmt.Errorf("error updating kustomization.yaml: %v", err)
}
} else {
// disable generation of example reconcile body if not scaffolding resource
// because this could result in a fork-bomb of k8s resources where watching a
Expand Down
22 changes: 16 additions & 6 deletions pkg/scaffold/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/v1/manager"

scaffoldv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
)

// Project contains configuration for generating project scaffolding.
Expand Down Expand Up @@ -60,17 +63,11 @@ func (p *Project) Scaffold() error {
return err
}

// default controller manager image name
imgName := "controller:latest"

s = &Scaffold{}
err = s.Execute(
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},
&manager.Config{Image: imgName},
&project.GitIgnore{},
&project.Kustomize{},
&project.KustomizeRBAC{},
&project.KustomizeManager{},
&project.KustomizeImagePatch{},
&project.KustomizePrometheusMetricsPatch{},
&project.KustomizeAuthProxyPatch{},
Expand Down Expand Up @@ -105,9 +102,12 @@ func (p *Project) scaffoldV1() error {
imgName := "controller:latest"
return (&Scaffold{}).Execute(
input.Options{ProjectPath: p.Info.Path, BoilerplatePath: p.Boilerplate.Path},
&manager.Config{Image: imgName},
&project.Makefile{Image: imgName},
&project.GopkgToml{},
&manager.Dockerfile{},
&project.Kustomize{},
&project.KustomizeManager{},
&manager.APIs{},
&manager.Controller{},
&manager.Webhook{},
Expand All @@ -120,10 +120,20 @@ func (p *Project) scaffoldV2() error {
imgName := "controller:latest"
return (&Scaffold{}).Execute(
input.Options{ProjectPath: p.Info.Path, BoilerplatePath: p.Boilerplate.Path},
&managerv2.Config{Image: imgName},
&scaffoldv2.Main{},
&scaffoldv2.GopkgToml{},
&scaffoldv2.Doc{},
&scaffoldv2.Makefile{Image: imgName},
&scaffoldv2.Dockerfile{},
&scaffoldv2.Kustomize{},
&scaffoldv2.ManagerWebhookPatch{},
&managerv2.Kustomization{},
&webhook.Kustomization{},
&webhook.KustomizeConfigWebhook{},
&webhook.InjectCAPatch{},
&certmanager.CertManager{},
&certmanager.Kustomization{},
&certmanager.KustomizeConfig{},
)
}
15 changes: 14 additions & 1 deletion pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ func (s *Scaffold) Execute(options input.Options, files ...input.File) error {
return nil
}

type errorAlreadyExists struct {
path string
}

func (e *errorAlreadyExists) Error() string {
return fmt.Sprintf("%s already exists", e.path)
}

func isAlreadyExistsError(e error) bool {
_, ok := e.(*errorAlreadyExists)
return ok
}

// doFile scaffolds a single file
func (s *Scaffold) doFile(e input.File) error {
// Set common fields
Expand All @@ -175,7 +188,7 @@ func (s *Scaffold) doFile(e input.File) error {
case input.Skip:
return nil
case input.Error:
return fmt.Errorf("%s already exists", i.Path)
return &errorAlreadyExists{path: i.Path}
}
}

Expand Down
82 changes: 0 additions & 82 deletions pkg/scaffold/v1/resource/crd.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/scaffold/v1/resource/crd_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

var _ input.File = &CRD{}
var _ input.File = &CRDSample{}

// CRDSample scaffolds a manifest for CRD sample.
type CRDSample struct {
Expand Down
63 changes: 63 additions & 0 deletions pkg/scaffold/v2/certmanager/certificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2019 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 certmanager

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// CertManager scaffolds an issuer CR and a certificate CR
type CertManager struct {
input.Input
}

// GetInput implements input.File
func (p *CertManager) GetInput() (input.Input, error) {
if p.Path == "" {
p.Path = filepath.Join("config", "certmanager", "certificate.yaml")
}
p.TemplateBody = certManagerTemplate
return p.Input, nil
}

var certManagerTemplate = `# The following manifests contain a self-signed issuer CR and a certificate CR.
# More document can be found at https://docs.cert-manager.io
apiVersion: certmanager.k8s.io/v1alpha1
kind: Issuer
metadata:
name: selfsigned-issuer
namespace: system
spec:
selfSigned: {}
---
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
namespace: system
spec:
# $(SERVICENAME) and $(NAMESPACE) will be substituted by kustomize
commonName: $(SERVICENAME).$(NAMESPACE).svc
dnsNames:
- $(SERVICENAME).$(NAMESPACE).svc.cluster.local
issuerRef:
kind: Issuer
name: selfsigned-issuer
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
`
52 changes: 52 additions & 0 deletions pkg/scaffold/v2/certmanager/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2019 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 certmanager

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// Kustomization scaffolds the kustomizaiton in the certmanager folder
type Kustomization struct {
input.Input
}

// GetInput implements input.File
func (p *Kustomization) GetInput() (input.Input, error) {
if p.Path == "" {
p.Path = filepath.Join("config", "certmanager", "kustomization.yaml")
}
p.TemplateBody = kustomizationTemplate
return p.Input, nil
}

var kustomizationTemplate = `resources:
- certificate.yaml
vars:
- name: CERTIFICATENAME
objref:
kind: Certificate
group: certmanager.k8s.io
version: v1alpha1
name: serving-cert # this name should match the one in certificate.yaml
configurations:
- kustomizeconfig.yaml
`
55 changes: 55 additions & 0 deletions pkg/scaffold/v2/certmanager/kustomizeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2019 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 certmanager

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// KustomizeConfig scaffolds the kustomizeconfig in the certmanager folder
type KustomizeConfig struct {
input.Input
}

// GetInput implements input.File
func (p *KustomizeConfig) GetInput() (input.Input, error) {
if p.Path == "" {
p.Path = filepath.Join("config", "certmanager", "kustomizeconfig.yaml")
}
p.TemplateBody = kustomizeConfigTemplate
return p.Input, nil
}

var kustomizeConfigTemplate = `# This configuration is for teaching kustomize how to update name ref and var substitution
nameReference:
- kind: Issuer
group: certmanager.k8s.io
fieldSpecs:
- kind: Certificate
group: certmanager.k8s.io
path: spec/issuerRef/name
varReference:
- kind: Certificate
group: certmanager.k8s.io
path: spec/commonName
- kind: Certificate
group: certmanager.k8s.io
path: spec/dnsNames
`
Loading

0 comments on commit a3e6e25

Please sign in to comment.