diff --git a/Dockerfile b/Dockerfile index 30d6b977..8cd67b17 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,11 @@ RUN apk add --no-cache bash git gcc musl-dev WORKDIR /src COPY . . +RUN GOPROXY=direct go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.19.0 +RUN GOPROXY=direct go install github.com/elastic/crd-ref-docs@v0.2.0 + +RUN go generate ./... + RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \ --mount=type=cache,id=gobuild,target=/root/.cache/go-build \ ./scripts/build @@ -16,9 +21,7 @@ COPY --from=builder /src/bin/helm-controller /bin/ FROM golang:1.24-alpine3.22 AS dev ARG ARCH ENV ARCH=$ARCH -RUN apk add --no-cache bash git gcc musl-dev curl -RUN GOPROXY=direct go install golang.org/x/tools/cmd/goimports@gopls/v0.18.1 -RUN GOPROXY=direct go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.17.3 +RUN apk add --no-cache bash git curl RUN if [ "${ARCH}" != "arm" ]; then \ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.64.7; \ fi @@ -37,6 +40,10 @@ RUN ./scripts/package FROM scratch AS artifacts COPY --from=package /src/dist/artifacts /dist/artifacts +FROM scratch AS crds +COPY --from=builder /src/pkg/crds/yaml/generated/ / +COPY --from=builder /src/doc/helmchart.md /tmp_doc/ + FROM alpine:3.22 AS production COPY bin/helm-controller /usr/bin/ CMD ["helm-controller"] diff --git a/Makefile b/Makefile index e37291a0..b8f3b56f 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,20 @@ IMAGE_NAME ?= helm-controller ARCH ?= amd64 .DEFAULT_GOAL := ci -.PHONY: build test validate package clean +.PHONY: build test validate package clean generate-crd -build: +build: generate-crd DOCKER_BUILDKIT=1 docker build \ --target binary \ --output type=local,dest=. . +generate-crd: + DOCKER_BUILDKIT=1 docker build \ + --target crds \ + --output type=local,dest=./pkg/crds/yaml/generated . + mv ./pkg/crds/yaml/generated/tmp_doc/helmchart.md ./doc/ + rm -r ./pkg/crds/yaml/generated/tmp_doc/ + validate: docker build --target dev --build-arg ARCH=$(ARCH) -t $(IMAGE_NAME)-dev . docker run --rm $(IMAGE_NAME)-dev ./scripts/validate diff --git a/README.md b/README.md index cd59f1db..568ff233 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ helm-controller A simple way to manage helm charts with Custom Resource Definitions in k8s. ## Manifests and Deploying -The `./manifests` folder contains useful YAML manifests to use for deploying and developing the Helm Controller. This simple YAML deployment creates a HelmChart CRD + a Deployment using the `rancher/helm-controller` container. The YAML might need some modifications for your environment so read below for Namespaced vs Cluster deployments and how to use them properly. +The `./manifests` folder contains useful YAML manifests to use for deploying and developing the Helm Controller. However, we recommend using the `deploy-cluster-scoped.yaml` or `deploy-namespaced.yaml` manifests included in the releases. The YAML might need some modifications for your environment so read below for Namespaced vs Cluster deployments and how to use them properly. #### Namespaced Deploys Use the `deploy-namespaced.yaml` to create a namespace and add the Helm Controller and CRD to that namespace locking down the Helm Controller to only see changes to CRDs within that namespace. This is defaulted to `helm-controller` so update the YAML to your needs before running `kubectl create` @@ -20,9 +20,12 @@ Autogenerated API docs for `helm.cattle.io/v1 HelmChart` and `HelmChartConfig` a To remove the Helm Controller run `kubectl delete` and pass the deployment YAML used using to create the Deployment `-f` parameter. ## Developing and Building -The Helm Controller is easy to get running locally, follow the instructions for your needs and requires a running k8s server + CRDs etc. When you have a working k8s cluster, you can use `./manifests/crd.yaml` to create the CRD and `./manifests/example-helmchart.yaml` which runs the `stable/traefik` helm chart. +The Helm Controller is easy to get running locally, follow the instructions for your needs and requires a running k8s server + CRDs etc. When you have a working k8s cluster, you can apply the manifests inside `./pkg/crds/yaml/generated/` to create the CRD and `./manifests/example-helmchart.yaml` which runs the `stable/traefik` helm chart. -#### Locally +### Build +Run `make build` to build the binary and, optionally, generate new CRDs if the API changed. We recommend just running `make` as the default target will also perform validation and testing. + +#### Running it locally Building and running natively will start a daemon which will watch a local k8s API. See Manifests section above about how to create the CRD and Objects using the provided manifests. ``` @@ -30,14 +33,15 @@ go build -o ./bin/helm-controller ./bin/helm-controller --kubeconfig $HOME/.kube/config ``` -#### docker/k8s -An easy way to get started with docker/k8s is to install docker for windows/mac and use the included k8s cluster. Once functioning you can easily build locally and get a docker container to pull the Helm Controller container and run it in k8s. Use `make` to launch a Linux container and build to create a container. Use the `./manifests/deploy-*.yaml` definitions to get it into your cluster and update `containers.image` to point to your locally image e.g. `image: rancher/helm-controller:dev` +#### Running it on k8s +Use the `deploy-cluster-scoped.yaml` or `deploy-namespaced.yaml` manifests in the assets of each release to run the helm-controller in Kubernetes. #### Options and Usage Use `./bin/helm-controller help` to get full usage details. The outside of a k8s Pod the most important options are `--kubeconfig` or `--masterurl` or it will not run. All options have corresponding ENV variables you could use. -## Testing -`go test ./...` +## Testing/Validating +`make test` +`make validate` ## License Copyright (c) 2019 [Rancher Labs, Inc.](http://rancher.com) diff --git a/pkg/crds/yaml/generated/helm.cattle.io_helmchartconfigs.yaml b/pkg/crds/yaml/generated/helm.cattle.io_helmchartconfigs.yaml index b2deb102..86dd7f4a 100644 --- a/pkg/crds/yaml/generated/helm.cattle.io_helmchartconfigs.yaml +++ b/pkg/crds/yaml/generated/helm.cattle.io_helmchartconfigs.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.17.3 + controller-gen.kubebuilder.io/version: v0.19.0 name: helmchartconfigs.helm.cattle.io spec: group: helm.cattle.io diff --git a/pkg/crds/yaml/generated/helm.cattle.io_helmcharts.yaml b/pkg/crds/yaml/generated/helm.cattle.io_helmcharts.yaml index 4c8c4bdc..6f327144 100644 --- a/pkg/crds/yaml/generated/helm.cattle.io_helmcharts.yaml +++ b/pkg/crds/yaml/generated/helm.cattle.io_helmcharts.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.17.3 + controller-gen.kubebuilder.io/version: v0.19.0 name: helmcharts.helm.cattle.io spec: group: helm.cattle.io diff --git a/scripts/validate b/scripts/validate index 4bbe2a7c..5f37533f 100755 --- a/scripts/validate +++ b/scripts/validate @@ -11,7 +11,7 @@ fi echo Running validation echo Running: golangci-lint -golangci-lint run +golangci-lint run --timeout 3m echo Running: go fmt test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)"