Skip to content

Commit 1e495ea

Browse files
authored
Merge pull request #3626 from camilamacedo86/add-distro-mode
✨ (go/v4): Add method to allow users distribute the project
2 parents 93d8fb8 + f39e53e commit 1e495ea

File tree

25 files changed

+2430
-15
lines changed

25 files changed

+2430
-15
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
9191

9292
.PHONY: yamllint
9393
yamllint:
94-
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/yamllint:latest testdata -d '{extends: relaxed, rules: {line-length: {max: 120}}, ignore: "/testdata/project-v2/\n/testdata/project-v3/"}'
94+
@files=$$(find testdata -name '*.yaml' ! -path 'testdata/*/dist/install.yaml'); \
95+
docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/yamllint:latest $$files -d "{extends: relaxed, rules: {line-length: {max: 120}}}" --no-warnings
9596

9697
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
9798
golangci-lint:

docs/book/src/component-config-tutorial/testdata/project/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
115115
- $(CONTAINER_TOOL) buildx rm project-v3-builder
116116
rm Dockerfile.cross
117117

118+
.PHONY: build-installer
119+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
120+
mkdir -p dist
121+
@if [ -d "config/crd" ]; then \
122+
$(KUSTOMIZE) build config/crd > dist/install.yaml; \
123+
fi
124+
echo "---" >> dist/install.yaml # Add a document separator before appending
125+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
126+
$(KUSTOMIZE) build config/default >> dist/install.yaml
127+
118128
##@ Deployment
119129

120130
ifndef ignore-not-found

docs/book/src/component-config-tutorial/testdata/project/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ make uninstall
6666
make undeploy
6767
```
6868

69+
## Project Distribution
70+
71+
Following are the steps to build the installer and distribute this project to users.
72+
73+
1. Build the installer for the image built and published in the registry:
74+
75+
```sh
76+
make build-installer IMG=<some-registry>/project:tag
77+
```
78+
79+
NOTE: The makefile target mentioned above generates an 'install.yaml'
80+
file in the dist directory. This file contains all the resources built
81+
with Kustomize, which are necessary to install this project without
82+
its dependencies.
83+
84+
2. Using the installer
85+
86+
Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
87+
88+
```sh
89+
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
90+
```
91+
6992
## Contributing
7093
// TODO(user): Add detailed information on how you would like others to contribute to this project
7194

docs/book/src/cronjob-tutorial/testdata/project/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
115115
- $(CONTAINER_TOOL) buildx rm project-v3-builder
116116
rm Dockerfile.cross
117117

118+
.PHONY: build-installer
119+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
120+
mkdir -p dist
121+
@if [ -d "config/crd" ]; then \
122+
$(KUSTOMIZE) build config/crd > dist/install.yaml; \
123+
fi
124+
echo "---" >> dist/install.yaml # Add a document separator before appending
125+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
126+
$(KUSTOMIZE) build config/default >> dist/install.yaml
127+
118128
##@ Deployment
119129

120130
ifndef ignore-not-found

docs/book/src/cronjob-tutorial/testdata/project/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ make uninstall
6666
make undeploy
6767
```
6868

69+
## Project Distribution
70+
71+
Following are the steps to build the installer and distribute this project to users.
72+
73+
1. Build the installer for the image built and published in the registry:
74+
75+
```sh
76+
make build-installer IMG=<some-registry>/project:tag
77+
```
78+
79+
NOTE: The makefile target mentioned above generates an 'install.yaml'
80+
file in the dist directory. This file contains all the resources built
81+
with Kustomize, which are necessary to install this project without
82+
its dependencies.
83+
84+
2. Using the installer
85+
86+
Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
87+
88+
```sh
89+
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
90+
```
91+
6992
## Contributing
7093
// TODO(user): Add detailed information on how you would like others to contribute to this project
7194

pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
204204
- $(CONTAINER_TOOL) buildx rm project-v3-builder
205205
rm Dockerfile.cross
206206
207+
.PHONY: build-installer
208+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
209+
mkdir -p dist
210+
@if [ -d "config/crd" ]; then \
211+
$(KUSTOMIZE) build config/crd > dist/install.yaml; \
212+
fi
213+
echo "---" >> dist/install.yaml # Add a document separator before appending
214+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
215+
$(KUSTOMIZE) build config/default >> dist/install.yaml
216+
207217
##@ Deployment
208218
209219
ifndef ignore-not-found

pkg/plugins/golang/v4/scaffolds/internal/templates/readme.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func (f *Readme) SetTemplateDefaults() error {
5151
codeFence("kubectl apply -k config/samples/"),
5252
codeFence("kubectl delete -k config/samples/"),
5353
codeFence("make uninstall"),
54-
codeFence("make undeploy"))
54+
codeFence("make undeploy"),
55+
codeFence("make build-installer IMG=<some-registry>/{{ .ProjectName }}:tag"),
56+
codeFence("kubectl apply -f https://raw.githubusercontent.com/<org>/{{ .ProjectName }}/"+
57+
"<tag or branch>/dist/install.yaml"),
58+
)
5559

5660
return nil
5761
}
@@ -111,6 +115,25 @@ You can apply the samples (examples) from the config/sample:
111115
112116
%s
113117
118+
## Project Distribution
119+
120+
Following are the steps to build the installer and distribute this project to users.
121+
122+
1. Build the installer for the image built and published in the registry:
123+
124+
%s
125+
126+
NOTE: The makefile target mentioned above generates an 'install.yaml'
127+
file in the dist directory. This file contains all the resources built
128+
with Kustomize, which are necessary to install this project without
129+
its dependencies.
130+
131+
2. Using the installer
132+
133+
Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:
134+
135+
%s
136+
114137
## Contributing
115138
// TODO(user): Add detailed information on how you would like others to contribute to this project
116139

test/e2e/v4/plugin_cluster_test.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,24 @@ var _ = Describe("kubebuilder", func() {
8585
" with restricted pods", func() {
8686
kbc.IsRestricted = true
8787
GenerateV4(kbc)
88-
Run(kbc, true)
88+
Run(kbc, true, false)
8989
})
9090
It("should generate a runnable project without webhooks"+
9191
" with restricted pods", func() {
9292
kbc.IsRestricted = true
9393
GenerateV4WithoutWebhooks(kbc)
94-
Run(kbc, false)
94+
Run(kbc, false, false)
95+
})
96+
It("should generate a runnable project"+
97+
" with the Installer", func() {
98+
GenerateV4(kbc)
99+
Run(kbc, false, true)
95100
})
96101
})
97102
})
98103

99104
// Run runs a set of e2e tests for a scaffolded project defined by a TestContext.
100-
func Run(kbc *utils.TestContext, hasWebhook bool) {
105+
func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller bool) {
101106
var controllerPodName string
102107
var err error
103108

@@ -129,18 +134,35 @@ func Run(kbc *utils.TestContext, hasWebhook bool) {
129134
err = kbc.LoadImageToKindCluster()
130135
ExpectWithOffset(1, err).NotTo(HaveOccurred())
131136

132-
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
133-
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
134-
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
135-
// --clusterrole=cluster-admin [email protected]
136-
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
137-
By("deploying the controller-manager")
137+
var output []byte
138+
if !isToUseInstaller {
139+
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
140+
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
141+
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
142+
// --clusterrole=cluster-admin [email protected]
143+
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
144+
By("deploying the controller-manager")
145+
146+
cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
147+
output, err = kbc.Run(cmd)
148+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
149+
} else {
150+
By("building the installer")
151+
err = kbc.Make("build-installer", "IMG="+kbc.ImageName)
152+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
138153

139-
cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
140-
output, err := kbc.Run(cmd)
141-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
154+
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
155+
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
156+
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
157+
// --clusterrole=cluster-admin [email protected]
158+
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
159+
By("deploying the controller-manager with the installer")
160+
161+
_, err = kbc.Kubectl.Apply(true, "-f", "dist/install.yaml")
162+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
163+
}
142164

143-
if kbc.IsRestricted {
165+
if kbc.IsRestricted && !isToUseInstaller {
144166
By("validating that manager Pod/container(s) are restricted")
145167
ExpectWithOffset(1, output).NotTo(ContainSubstring("Warning: would violate PodSecurity"))
146168
}

test/testdata/generate.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ function scaffold_test_project {
116116
fi
117117

118118
make generate manifests
119+
# TODO fix the error with multigroup layout and allow it be generated
120+
# with this one.
121+
# Error: trouble configuring builtin PatchTransformer with config: `
122+
# path: patches/webhook_in_sea-creatures_krakens.yaml
123+
# `: failed to get the patch file from path(patches/webhook_in_sea-creatures_krakens.yaml): evalsymlink failure on '/Users/camiladeomacedo/go/src/sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/config/crd/patches/webhook_in_sea-creatures_krakens.yaml' : lstat go/src/sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/config/crd/patches/webhook_in_sea-creatures_krakens.yaml: no such file or directory
124+
if [[ $project =~ v4 && ! $project =~ multigroup ]]; then
125+
make build-installer
126+
fi
127+
119128
rm -f go.sum
120129
go mod tidy
121130
popd

testdata/project-v4-multigroup-with-deploy-image/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
115115
- $(CONTAINER_TOOL) buildx rm project-v3-builder
116116
rm Dockerfile.cross
117117

118+
.PHONY: build-installer
119+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
120+
mkdir -p dist
121+
@if [ -d "config/crd" ]; then \
122+
$(KUSTOMIZE) build config/crd > dist/install.yaml; \
123+
fi
124+
echo "---" >> dist/install.yaml # Add a document separator before appending
125+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
126+
$(KUSTOMIZE) build config/default >> dist/install.yaml
127+
118128
##@ Deployment
119129

120130
ifndef ignore-not-found

0 commit comments

Comments
 (0)