Skip to content

Commit

Permalink
Add target and tests to distribute the project
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Sep 19, 2023
1 parent c904bd9 commit e1b428b
Show file tree
Hide file tree
Showing 31 changed files with 5,355 additions and 2 deletions.
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}}}" --no-warnings
@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

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
golangci-lint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ 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
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default >> dist/install.yaml

##@ Deployment

ifndef ignore-not-found
Expand Down
19 changes: 19 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,25 @@ make uninstall
make undeploy
```

## Distributing your project

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

### Build the installer
To build the installer for the image built and published in the registry:

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

NOTE: The above makefile target will generate the install.yaml in the directory dist contains all resources
build with Kustomize to install this project.

### To install via the installer

You must just run kubectl apply -f dist/install.yaml.
Therefore, users can install this project if they have access to the repository by running:

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
9 changes: 9 additions & 0 deletions docs/book/src/cronjob-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ 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
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default >> dist/install.yaml

##@ Deployment

ifndef ignore-not-found
Expand Down
19 changes: 19 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,25 @@ make uninstall
make undeploy
```

## Distributing your project

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

### Build the installer
To build the installer for the image built and published in the registry:

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

NOTE: The above makefile target will generate the install.yaml in the directory dist contains all resources
build with Kustomize to install this project.

### To install via the installer

You must just run kubectl apply -f dist/install.yaml.
Therefore, users can install this project if they have access to the repository by running:

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
11 changes: 11 additions & 0 deletions pkg/plugins/common/kustomize/v2/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest

pluginutil redeclared in this block

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest

imported and not used: "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" as pluginutil

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / golangci-lint

pluginutil redeclared in this block

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / golangci-lint

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" imported as pluginutil and not used

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / golangci-lint

pluginutil redeclared in this block

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / golangci-lint

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" imported as pluginutil and not used

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / Verify testdata directory

pluginutil redeclared in this block

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / Verify testdata directory

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" imported as pluginutil and not used

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest

pluginutil redeclared in this block

Check failure on line 28 in pkg/plugins/common/kustomize/v2/scaffolds/api.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" imported as pluginutil and not used
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/crd/patches"
Expand Down Expand Up @@ -73,6 +74,16 @@ func (s *apiScaffolder) Scaffold() error {

// Keep track of these values before the update
if s.resource.HasAPI() {
kustomizeFilePath := "config/default/kustomization.yaml"
err := pluginutil.UncommentCode(kustomizeFilePath, "#- ../crd", `#`)
if err != nil {
hasCRUncommented, err := pluginutil.HasFragment(kustomizeFilePath, "- ../crd")
if !hasCRUncommented || err != nil {
log.Errorf("Unable to find the target #- ../crd to uncomment in the file "+
"%s.", kustomizeFilePath)
}
}

if err := scaffold.Execute(
&samples.CRDSample{Force: s.force},
&rbac.CRDEditorRole{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ 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
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default >> dist/install.yaml
##@ Deployment
ifndef ignore-not-found
Expand Down
19 changes: 19 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ You can apply the samples (examples) from the config/sample:
%s
## Distributing your project
Following the steps for you build the installer and to distribute this project for users.
### Build the installer
To build the installer for the image built and published in the registry:
make build-installer IMG=<some-registry>/{{ .ProjectName }}:tag
NOTE: The above makefile target will generate the install.yaml in the directory dist contains all resources
build with Kustomize to install this project.
### To install via the installer
You must just run kubectl apply -f dist/install.yaml.
Therefore, users can install this project if they have access to the repository by running:
kubectl apply -f https://raw.githubusercontent.com/<org>/{{ .ProjectName }}/<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
2 changes: 1 addition & 1 deletion test/e2e/v4/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
. "github.com/onsi/gomega"
)

// Run e2e tests using the Ginkgo runner.
// RunWith e2e tests using the Ginkgo runner.
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
fmt.Fprintf(GinkgoWriter, "Starting kubebuilder suite\n")
Expand Down
126 changes: 126 additions & 0 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,135 @@ var _ = Describe("kubebuilder", func() {
GenerateV4(kbc)
Run(kbc)
})
It("should generate a runnable project"+
" with the Installer", func() {
GenerateV4(kbc)
RunWithInstaller(kbc)
})
})
})

// TODO: We need to clean up and refactor these tests.
// Remove duplicates and ensure cohesion and encapsulation
// For Example: Run should not have make generate and manifests it should be part of the build of the project
// Also we can encapsulate the checks such as: checkManager, checkPrometheus, checkCertManager so that we can reuse them
// when or not is required and create many scenarios.

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

By("creating manager namespace")
err = kbc.CreateManagerNamespace()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("updating the go.mod")
err = kbc.Tidy()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("run make manifests")
err = kbc.Make("manifests")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("run make generate")
err = kbc.Make("generate")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("building the controller image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("loading the controller docker image into the kind cluster")
err = kbc.LoadImageToKindCluster()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("building the installer")
err = kbc.Make("build-installer", "IMG="+kbc.ImageName)
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())

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get pod name
podOutput, err := kbc.Kubectl.Get(
true,
"pods", "-l", "control-plane=controller-manager",
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+
"{{ \"\\n\" }}{{ end }}{{ end }}")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
podNames := util.GetNonEmptyLines(podOutput)
if len(podNames) != 1 {
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
}
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))

// Validate pod status
status, err := kbc.Kubectl.Get(
true,
"pods", controllerPodName, "-o", "jsonpath={.status.phase}")
ExpectWithOffset(2, err).NotTo(HaveOccurred())
if status != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
}
defer func() {
out, err := kbc.Kubectl.CommandInNamespace("describe", "all")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
fmt.Fprintln(GinkgoWriter, out)
}()
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())

By("granting permissions to access the metrics")
_, err = kbc.Kubectl.Command(
"create", "clusterrolebinding", fmt.Sprintf("metrics-%s", kbc.TestSuffix),
fmt.Sprintf("--clusterrole=e2e-%s-metrics-reader", kbc.TestSuffix),
fmt.Sprintf("--serviceaccount=%s:%s", kbc.Kubectl.Namespace, kbc.Kubectl.ServiceAccount))
ExpectWithOffset(1, err).NotTo(HaveOccurred())

_ = curlMetrics(kbc)

By("validating that cert-manager has provisioned the certificate Secret")
EventuallyWithOffset(1, func() error {
_, err := kbc.Kubectl.Get(
true,
"secrets", "webhook-server-cert")
return err
}, time.Minute, time.Second).Should(Succeed())

By("validating that the Prometheus manager has provisioned the Service")
EventuallyWithOffset(1, func() error {
_, err := kbc.Kubectl.Get(
false,
"Service", "prometheus-operator")
return err
}, time.Minute, time.Second).Should(Succeed())

By("validating that the ServiceMonitor for Prometheus is applied in the namespace")
_, err = kbc.Kubectl.Get(
true,
"ServiceMonitor")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("validating that the created resource object gets reconciled in the controller")
metricsOutput := curlMetrics(kbc)
ExpectWithOffset(1, metricsOutput).To(ContainSubstring(fmt.Sprintf(
`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
strings.ToLower(kbc.Kind),
)))
}

// Run runs a set of e2e tests for a scaffolded project defined by a TestContext.
func Run(kbc *utils.TestContext) {
var controllerPodName string
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ function scaffold_test_project {
fi

make generate manifests
if [[ $project =~ v4 ]]; then
make build-installer
fi
rm -f go.sum
go mod tidy
popd
Expand Down
9 changes: 9 additions & 0 deletions testdata/project-v4-multigroup-with-deploy-image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ 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
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default >> dist/install.yaml

##@ Deployment

ifndef ignore-not-found
Expand Down
19 changes: 19 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,25 @@ make uninstall
make undeploy
```

## Distributing your project

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

### Build the installer
To build the installer for the image built and published in the registry:

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

NOTE: The above makefile target will generate the install.yaml in the directory dist contains all resources
build with Kustomize to install this project.

### To install via the installer

You must just run kubectl apply -f dist/install.yaml.
Therefore, users can install this project if they have access to the repository by running:

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
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
Loading

0 comments on commit e1b428b

Please sign in to comment.