Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (go/v4): Add method to allow users distribute the project #3626

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes

.PHONY: yamllint
yamllint:
@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/"}'
@files=$$(find testdata -name '*.yaml' ! -path 'testdata/*/dist/install.yaml'); \
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

camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
golangci-lint:
Expand Down
10 changes: 10 additions & 0 deletions docs/book/src/component-config-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

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

##@ Deployment

ifndef ignore-not-found
Expand Down
23 changes: 23 additions & 0 deletions docs/book/src/component-config-tutorial/testdata/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ make uninstall
make undeploy
```

## Project Distribution

Following are the steps to build the installer and distribute this project to users.

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
```

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
10 changes: 10 additions & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

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

##@ Deployment

ifndef ignore-not-found
Expand Down
23 changes: 23 additions & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ make uninstall
make undeploy
```

## Project Distribution

Following are the steps to build the installer and distribute this project to users.

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project/<tag or branch>/dist/install.yaml
```

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
10 changes: 10 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

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

camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
##@ Deployment

ifndef ignore-not-found
Expand Down
25 changes: 24 additions & 1 deletion pkg/plugins/golang/v4/scaffolds/internal/templates/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func (f *Readme) SetTemplateDefaults() error {
codeFence("kubectl apply -k config/samples/"),
codeFence("kubectl delete -k config/samples/"),
codeFence("make uninstall"),
codeFence("make undeploy"))
codeFence("make undeploy"),
codeFence("make build-installer IMG=<some-registry>/{{ .ProjectName }}:tag"),
codeFence("kubectl apply -f https://raw.githubusercontent.com/<org>/{{ .ProjectName }}/"+
"<tag or branch>/dist/install.yaml"),
)

return nil
}
Expand Down Expand Up @@ -111,6 +115,25 @@ You can apply the samples (examples) from the config/sample:

%s

## Project Distribution

Following are the steps to build the installer and distribute this project to users.

1. Build the installer for the image built and published in the registry:

%s

NOTE: The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:

camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
%s

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
48 changes: 35 additions & 13 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,24 @@ var _ = Describe("kubebuilder", func() {
" with restricted pods", func() {
kbc.IsRestricted = true
GenerateV4(kbc)
Run(kbc, true)
Run(kbc, true, false)
})
It("should generate a runnable project without webhooks"+
" with restricted pods", func() {
kbc.IsRestricted = true
GenerateV4WithoutWebhooks(kbc)
Run(kbc, false)
Run(kbc, false, false)
})
It("should generate a runnable project"+
" with the Installer", func() {
GenerateV4(kbc)
Run(kbc, false, true)
})
})
})

// Run runs a set of e2e tests for a scaffolded project defined by a TestContext.
func Run(kbc *utils.TestContext, hasWebhook bool) {
func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller bool) {
var controllerPodName string
var err error

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

// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager")
var output []byte
if !isToUseInstaller {
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager")

cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
output, err = kbc.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
} else {
By("building the installer")
err = kbc.Make("build-installer", "IMG="+kbc.ImageName)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
output, err := kbc.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding \
// --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying the controller-manager with the installer")

_, err = kbc.Kubectl.Apply(true, "-f", "dist/install.yaml")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

if kbc.IsRestricted {
if kbc.IsRestricted && !isToUseInstaller {
By("validating that manager Pod/container(s) are restricted")
ExpectWithOffset(1, output).NotTo(ContainSubstring("Warning: would violate PodSecurity"))
}
Expand Down
9 changes: 9 additions & 0 deletions test/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ function scaffold_test_project {
fi

make generate manifests
# TODO fix the error with multigroup layout and allow it be generated
# with this one.
# Error: trouble configuring builtin PatchTransformer with config: `
# path: patches/webhook_in_sea-creatures_krakens.yaml
# `: 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
if [[ $project =~ v4 && ! $project =~ multigroup ]]; then
make build-installer
fi

rm -f go.sum
go mod tidy
popd
Expand Down
10 changes: 10 additions & 0 deletions testdata/project-v4-multigroup-with-deploy-image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

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

##@ Deployment

ifndef ignore-not-found
Expand Down
23 changes: 23 additions & 0 deletions testdata/project-v4-multigroup-with-deploy-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ make uninstall
make undeploy
```

## Project Distribution

Following are the steps to build the installer and distribute this project to users.

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project-v4-multigroup-with-deploy-image:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project-v4-multigroup-with-deploy-image/<tag or branch>/dist/install.yaml
```

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
10 changes: 10 additions & 0 deletions testdata/project-v4-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

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

##@ Deployment

ifndef ignore-not-found
Expand Down
23 changes: 23 additions & 0 deletions testdata/project-v4-multigroup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ make uninstall
make undeploy
```

## Project Distribution

Following are the steps to build the installer and distribute this project to users.

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project-v4-multigroup:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project-v4-multigroup/<tag or branch>/dist/install.yaml
```

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
10 changes: 10 additions & 0 deletions testdata/project-v4-with-deploy-image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross

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

##@ Deployment

ifndef ignore-not-found
Expand Down
23 changes: 23 additions & 0 deletions testdata/project-v4-with-deploy-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ make uninstall
make undeploy
```

## Project Distribution

Following are the steps to build the installer and distribute this project to users.

1. Build the installer for the image built and published in the registry:

```sh
make build-installer IMG=<some-registry>/project-v4-with-deploy-image:tag
```

NOTE: The makefile target mentioned above generates an 'install.yaml'
file in the dist directory. This file contains all the resources built
with Kustomize, which are necessary to install this project without
its dependencies.

2. Using the installer

Users can just run kubectl apply -f <URL for YAML BUNDLE> to install the project, i.e.:

```sh
kubectl apply -f https://raw.githubusercontent.com/<org>/project-v4-with-deploy-image/<tag or branch>/dist/install.yaml
```

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
resources:
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: controller
newTag: latest