...

Text file src/github.com/GoogleCloudPlatform/k8s-config-connector/Makefile

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector

     1# Copyright 2022 Google LLC
     2#
     3# Licensed under the Apache License, Version 2.0 (the "License");
     4# you may not use this file except in compliance with the License.
     5# You may obtain a copy of the License at
     6#
     7#      http://www.apache.org/licenses/LICENSE-2.0
     8#
     9# Unless required by applicable law or agreed to in writing, software
    10# distributed under the License is distributed on an "AS IS" BASIS,
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12# See the License for the specific language governing permissions and
    13# limitations under the License.
    14
    15PROJECT_ID := $(shell gcloud config get-value project)
    16SHORT_SHA := $(shell git rev-parse --short=7 HEAD)
    17BUILDER_IMG ?= gcr.io/${PROJECT_ID}/builder:${SHORT_SHA}
    18CONTROLLER_IMG ?= gcr.io/${PROJECT_ID}/controller:${SHORT_SHA}
    19RECORDER_IMG ?= gcr.io/${PROJECT_ID}/recorder:${SHORT_SHA}
    20WEBHOOK_IMG ?= gcr.io/${PROJECT_ID}/webhook:${SHORT_SHA}
    21DELETION_DEFENDER_IMG ?= gcr.io/${PROJECT_ID}/deletiondefender:${SHORT_SHA}
    22UNMANAGED_DETECTOR_IMG ?= gcr.io/${PROJECT_ID}/unmanageddetector:${SHORT_SHA}
    23
    24# Use Docker BuildKit when building images to allow usage of 'setcap' in
    25# multi-stage builds (https://github.com/moby/moby/issues/38132)
    26DOCKER_BUILD := DOCKER_BUILDKIT=1 docker build
    27
    28ifneq ($(origin KUBECONTEXT), undefined)
    29CONTEXT_FLAG := --context ${KUBECONTEXT}
    30endif
    31
    32.PHONY: all
    33all: test manager operator config-connector
    34
    35# Run tests
    36.PHONY: test
    37test: generate fmt vet manifests
    38	make -C operator test
    39	go test -v ./pkg/... ./cmd/... ./config/tests/... ./scripts/generate-go-crd-clients/... -coverprofile cover.out -count=1
    40
    41# Build config-connector binary
    42.PHONY: config-connector
    43config-connector: generate fmt vet
    44	./scripts/config-connector/build.sh
    45
    46# Build the operator's manager binary
    47.PHONY: operator
    48operator:
    49	make -C operator manager
    50
    51# Build manager binary
    52.PHONY: manager
    53manager: generate fmt vet
    54	go build -o bin/manager github.com/GoogleCloudPlatform/k8s-config-connector/cmd/manager
    55
    56# Generate manifests e.g. CRD, RBAC etc.
    57.PHONY: manifests
    58manifests: generate
    59	make -C operator manifests
    60	rm -rf config/crds/resources
    61	rm -rf config/crds/tmp_resources
    62	go build -o bin/generate-crds ./scripts/generate-crds && ./bin/generate-crds -output-dir=config/crds/tmp_resources
    63	go run ./scripts/generate-cnrm-cluster-roles/main.go
    64	# add kustomize patches on all CRDs
    65	mkdir config/crds/resources
    66	cp config/crds/kustomization.yaml kustomization.yaml
    67	kustomize edit add resource config/crds/tmp_resources/*.yaml
    68	kustomize build -o config/crds/resources
    69	rm -rf config/crds/tmp_resources
    70	rm kustomization.yaml
    71
    72# Format code
    73.PHONY: fmt
    74fmt:
    75	make -C operator fmt
    76	go run -mod=readonly golang.org/x/tools/cmd/goimports@latest -w pkg cmd scripts config/tests
    77	# 04bfe4ee9ca5764577b029acc6a1957fd1997153 includes fix to not log "Skipped" for each skipped file
    78	GOFLAGS= go run github.com/google/addlicense@04bfe4ee9ca5764577b029acc6a1957fd1997153 -c "Google LLC" -l apache \
    79	-ignore "vendor/**" -ignore "third_party/**" \
    80	-ignore "config/crds/**" -ignore "config/cloudcodesnippets/**" \
    81	-ignore "**/*.html" -ignore "config/installbundle/components/clusterroles/cnrm_admin.yaml" \
    82	-ignore "config/installbundle/components/clusterroles/cnrm_viewer.yaml" \
    83	-ignore "operator/channels/**" \
    84	-ignore "operator/autopilot-channels/**" \
    85	-ignore "operator/config/crd/bases/**" \
    86	-ignore "operator/config/gke-addon/image_configmap.yaml" \
    87	-ignore "operator/config/rbac/cnrm_viewer_role.yaml" \
    88	-ignore "operator/vendor/**" \
    89	./
    90
    91.PHONY: lint
    92lint:
    93	for f in `find pkg cmd -name "*.go"`; do golint -set_exit_status $$f || exit $?; done
    94
    95# Run go vet against code
    96.PHONY: vet
    97vet:
    98	make -C operator vet
    99	go vet -tags integration ./pkg/... ./cmd/... ./config/tests/...
   100
   101# Generate code
   102.PHONY: generate
   103generate:
   104	# Don't run go generate on `pkg/clients/generated` in the normal development flow due to high latency.
   105	# This path will be covered by `generate-go-client` target specifically.
   106	go mod vendor -o temp-vendor # So we can load DCL resources
   107	go generate $$(go list ./pkg/... ./cmd/... ./scripts/resource-autogen/... | grep -v ./pkg/clients/generated)
   108	rm -rf temp-vendor
   109	make fmt
   110
   111# Build the docker images
   112.PHONY: docker-build
   113docker-build: docker-build-manager docker-build-recorder docker-build-webhook docker-build-deletiondefender docker-build-unmanageddetector
   114
   115# build all the binaries into the builder docker image
   116.PHONY: docker-build-builder
   117docker-build-builder:
   118	$(DOCKER_BUILD) . -f build/builder/Dockerfile -t ${BUILDER_IMG}
   119
   120# Build the manager docker image
   121.PHONY: docker-build-manager
   122docker-build-manager: docker-build-builder
   123	$(DOCKER_BUILD) -t ${CONTROLLER_IMG} --build-arg BUILDER_IMG=${BUILDER_IMG} - < build/manager/Dockerfile
   124	@echo "updating kustomize image patch file for manager resource"
   125	cp config/installbundle/components/manager/base/manager_image_patch_template.yaml config/installbundle/components/manager/base/manager_image_patch.yaml
   126	sed -i'' -e 's@image: .*@image: '"${CONTROLLER_IMG}"'@' ./config/installbundle/components/manager/base/manager_image_patch.yaml
   127
   128# Build the recorder docker image
   129.PHONY: docker-build-recorder
   130docker-build-recorder: docker-build-builder
   131	$(DOCKER_BUILD) -t ${RECORDER_IMG} --build-arg BUILDER_IMG=${BUILDER_IMG} - < build/recorder/Dockerfile
   132	@echo "updating kustomize image patch file for recorder resource"
   133	cp config/installbundle/components/recorder/recorder_image_patch_template.yaml config/installbundle/components/recorder/recorder_image_patch.yaml
   134	sed -i'' -e 's@image: .*@image: '"${RECORDER_IMG}"'@' ./config/installbundle/components/recorder/recorder_image_patch.yaml
   135
   136# Build the webhook docker image
   137.PHONY: docker-build-webhook
   138docker-build-webhook: docker-build-builder
   139	$(DOCKER_BUILD) -t ${WEBHOOK_IMG} --build-arg BUILDER_IMG=${BUILDER_IMG} - < build/webhook/Dockerfile
   140	@echo "updating kustomize image patch file for webhook resource"
   141	cp config/installbundle/components/webhook/webhook_image_patch_template.yaml config/installbundle/components/webhook/webhook_image_patch.yaml
   142	sed -i'' -e 's@image: .*@image: '"${WEBHOOK_IMG}"'@' ./config/installbundle/components/webhook/webhook_image_patch.yaml
   143
   144.PHONY: docker-build-deletiondefender
   145docker-build-deletiondefender: docker-build-builder
   146	$(DOCKER_BUILD) -t ${DELETION_DEFENDER_IMG} --build-arg BUILDER_IMG=${BUILDER_IMG} - < build/deletiondefender/Dockerfile
   147	@echo "updating kustomize image patch file for deletion defender resource"
   148	cp config/installbundle/components/deletiondefender/deletiondefender_image_patch_template.yaml config/installbundle/components/deletiondefender/deletiondefender_image_patch.yaml
   149	sed -i'' -e 's@image: .*@image: '"${DELETION_DEFENDER_IMG}"'@' ./config/installbundle/components/deletiondefender/deletiondefender_image_patch.yaml
   150
   151.PHONY: docker-build-unmanageddetector
   152docker-build-unmanageddetector: docker-build-builder
   153	$(DOCKER_BUILD) -t ${UNMANAGED_DETECTOR_IMG} --build-arg BUILDER_IMG=${BUILDER_IMG} - < build/unmanageddetector/Dockerfile
   154	@echo "updating kustomize image patch file for unmanaged detector resource"
   155	cp config/installbundle/components/unmanageddetector/unmanageddetector_image_patch_template.yaml config/installbundle/components/unmanageddetector/unmanageddetector_image_patch.yaml
   156	sed -i'' -e 's@image: .*@image: '"${UNMANAGED_DETECTOR_IMG}"'@' ./config/installbundle/components/unmanageddetector/unmanageddetector_image_patch.yaml
   157
   158# Push the docker image
   159.PHONY: docker-push
   160docker-push:
   161	docker push ${CONTROLLER_IMG}
   162	docker push ${RECORDER_IMG}
   163	docker push ${WEBHOOK_IMG}
   164	docker push ${DELETION_DEFENDER_IMG}
   165	docker push ${UNMANAGED_DETECTOR_IMG}
   166
   167# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
   168.PHONY: deploy
   169deploy: manifests install
   170	kustomize build config/installbundle/releases/scopes/cluster/withworkloadidentity | sed -e 's/$${PROJECT_ID?}/${PROJECT_ID}/g'| kubectl apply -f - ${CONTEXT_FLAG}
   171
   172# Install CRDs into a cluster
   173.PHONY: install
   174install: manifests
   175	kubectl apply -f config/crds/resources/ ${CONTEXT_FLAG}
   176
   177# Deploy controller only, this will skip CRD install in the configured K8s and usually runs much
   178# faster than "make deploy". It is useful if you only want to quickly apply code change in controller
   179.PHONY: deploy-controller
   180deploy-controller: docker-build docker-push
   181	kustomize build config/installbundle/releases/scopes/cluster/withworkloadidentity | sed -e 's/$${PROJECT_ID?}/${PROJECT_ID}/g'| kubectl apply -f - ${CONTEXT_FLAG}
   182
   183
   184# Generate strong-typed definitions for existing CRDs
   185.PHONY: client-types
   186client-types:
   187	go run ./scripts/generate-go-crd-clients
   188	make fmt
   189
   190# Generate CRD go clients
   191.PHONY: generate-go-client
   192generate-go-client: client-types
   193	go generate ./pkg/clients/generated/...
   194	./scripts/generate-go-crd-clients/generate-clients.sh
   195

View as plain text