From 0dd29ae0574a895b40e136f77e66566c3d63cfab Mon Sep 17 00:00:00 2001 From: Christoph Barbian Date: Thu, 5 Sep 2024 18:28:45 +0200 Subject: [PATCH 1/2] refactor code generation, Makefile version handling --- Dockerfile | 2 +- Makefile | 70 ++++----- crds/core.cs.sap.com_components.yaml | 2 +- go.mod | 10 ++ go.sum | 30 ++++ hack/genclient.sh | 43 +++--- hack/tools.go | 15 ++ .../versioned/fake/clientset_generated.go | 6 +- .../core.cs.sap.com/v1alpha1/component.go | 146 ++---------------- .../v1alpha1/fake/fake_component.go | 36 +++-- .../informers/externalversions/factory.go | 1 + .../core.cs.sap.com/v1alpha1/component.go | 39 +---- 12 files changed, 149 insertions(+), 251 deletions(-) create mode 100644 hack/tools.go diff --git a/Dockerfile b/Dockerfile index 070ac3d..ac7bfd6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM --platform=$BUILDPLATFORM golang:1.23 as builder +FROM --platform=$BUILDPLATFORM golang:1.23 AS builder ARG TARGETOS ARG TARGETARCH diff --git a/Makefile b/Makefile index b388e3e..7c449be 100644 --- a/Makefile +++ b/Makefile @@ -13,62 +13,65 @@ all: build ##@ General .PHONY: help -help: ## Display this help. - @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) +help: ## Display this help + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) ##@ Development .PHONY: manifests -manifests: controller-gen ## Generate CustomResourceDefinition objects. +manifests: controller-gen ## Generate CustomResourceDefinition objects $(CONTROLLER_GEN) crd paths="./api/..." output:crd:artifacts:config=crds ;\ test ! -d chart || test -e chart/crds || ln -s ../crds chart/crds .PHONY: generate -generate: controller-gen generate-client ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. +generate: generate-deepcopy generate-client ## Generate everything + +.PHONY: generate-deepcopy +generate-deepcopy: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..." .PHONY: generate-client -generate-client: client-gen informer-gen lister-gen ## Generate typed client. +generate-client: ## Generate typed client ./hack/genclient.sh .PHONY: fmt -fmt: ## Run go fmt against code. +fmt: ## Run go fmt against code go fmt ./... .PHONY: vet -vet: ## Run go vet against code. +vet: ## Run go vet against code go vet ./... ##@ Testing .PHONY: test -test: manifests generate fmt vet envtest ## Run tests. +test: manifests generate-deepcopy fmt vet envtest ## Run tests KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out ##@ Build .PHONY: build -build: generate fmt vet ## Build manager binary. +build: generate-deepcopy fmt vet ## Build manager binary go build -o bin/manager main.go .PHONY: run -run: manifests generate fmt vet ## Run a controller from your host. +run: manifests generate-deepcopy fmt vet ## Run a controller from your host go run ./main.go -# Build docker image in current architecture and tag it as ${IMG}. +# Build docker image in current architecture and tag it as ${IMG} .PHONY: docker-build -docker-build: ## Build docker image with the manager. +docker-build: ## Build docker image with the manager docker build -t ${IMG} . -# Push docker image to the target specified in ${IMG}. +# Push docker image to the target specified in ${IMG} .PHONY: docker-push -docker-push: ## Push docker image with the manager. +docker-push: ## Push docker image with the manager docker push ${IMG} -# Build and push docker image for all given platforms. +# Build and push docker image for all given platforms PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le .PHONY: docker-buildx -docker-buildx: ## Build and push docker image for the manager for cross-platform support. +docker-buildx: ## Build and push docker image for the manager for cross-platform support - docker buildx create --name project-v3-builder docker buildx use project-v3-builder - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} . @@ -83,43 +86,22 @@ $(LOCALBIN): ## Tool Binaries CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen -CLIENT_GEN ?= $(LOCALBIN)/client-gen -INFORMER_GEN ?= $(LOCALBIN)/informer-gen -LISTER_GEN ?= $(LOCALBIN)/lister-gen SETUP_ENVTEST ?= $(LOCALBIN)/setup-envtest -## Tool Versions -CONTROLLER_TOOLS_VERSION ?= v0.16.1 -CODE_GENERATOR_VERSION ?= v0.29.8 -SETUP_ENVTEST_VERSION ?= latest - .PHONY: controller-gen -controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. +controller-gen: $(CONTROLLER_GEN) ## Install controller-gen $(CONTROLLER_GEN): $(LOCALBIN) - test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) - -.PHONY: client-gen -client-gen: $(CLIENT_GEN) ## Download client-gen locally if necessary. -$(CLIENT_GEN): $(LOCALBIN) - test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION) - -.PHONY: informer-gen -informer-gen: $(INFORMER_GEN) ## Download informer-gen locally if necessary. -$(INFORMER_GEN): $(LOCALBIN) - test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION) - -.PHONY: lister-gen -lister-gen: $(LISTER_GEN) ## Download lister-gen locally if necessary. -$(LISTER_GEN): $(LOCALBIN) - test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION) + go mod download sigs.k8s.io/controller-tools + GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-tools)/cmd/controller-gen .PHONY: setup-envtest -setup-envtest: $(SETUP_ENVTEST) ## Download setup-envtest locally if necessary. +setup-envtest: $(SETUP_ENVTEST) ## Install setup-envtest $(SETUP_ENVTEST): $(LOCALBIN) - test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VERSION) + go mod download sigs.k8s.io/controller-runtime/tools/setup-envtest + GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-runtime/tools/setup-envtest) .PHONY: envtest -envtest: setup-envtest +envtest: setup-envtest ## Install envtest binaries ENVTESTDIR=$$($(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\ chmod -R u+w $$ENVTESTDIR ;\ rm -f $(LOCALBIN)/k8s/current ;\ diff --git a/crds/core.cs.sap.com_components.yaml b/crds/core.cs.sap.com_components.yaml index 6e4f0a8..c13772a 100644 --- a/crds/core.cs.sap.com_components.yaml +++ b/crds/core.cs.sap.com_components.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.16.1 + controller-gen.kubebuilder.io/version: v0.16.2 name: components.core.cs.sap.com spec: group: core.cs.sap.com diff --git a/go.mod b/go.mod index cc5af7d..4f4c139 100644 --- a/go.mod +++ b/go.mod @@ -13,8 +13,11 @@ require ( k8s.io/apiextensions-apiserver v0.31.0 k8s.io/apimachinery v0.31.0 k8s.io/client-go v0.31.0 + k8s.io/code-generator v0.31.0 k8s.io/kube-aggregator v0.31.0 sigs.k8s.io/controller-runtime v0.19.0 + sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20240903085516-38546806f2fa + sigs.k8s.io/controller-tools v0.16.2 sigs.k8s.io/yaml v1.4.0 ) @@ -83,6 +86,7 @@ require ( github.com/go-openapi/jsonpointer v0.20.2 // indirect github.com/go-openapi/jsonreference v0.20.4 // indirect github.com/go-openapi/swag v0.22.9 // indirect + github.com/gobuffalo/flect v1.0.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect @@ -109,6 +113,7 @@ require ( github.com/huandu/xstrings v1.5.0 // indirect github.com/iancoleman/strcase v0.3.0 // indirect github.com/imdario/mergo v0.3.16 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -136,7 +141,9 @@ require ( github.com/ryanuber/go-glob v1.0.0 // indirect github.com/shopspring/decimal v1.4.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect + github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.7.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/urfave/cli v1.22.15 // indirect github.com/x448/float16 v0.8.4 // indirect @@ -152,6 +159,7 @@ require ( go.uber.org/zap v1.26.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect + golang.org/x/mod v0.20.0 // indirect golang.org/x/net v0.28.0 // indirect golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sync v0.8.0 // indirect @@ -159,6 +167,7 @@ require ( golang.org/x/term v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect golang.org/x/time v0.6.0 // indirect + golang.org/x/tools v0.24.0 // indirect gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect google.golang.org/api v0.186.0 // indirect google.golang.org/genproto v0.0.0-20240624140628-dc46fd24d27d // indirect @@ -172,6 +181,7 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/api v0.31.0 // indirect + k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect k8s.io/klog/v2 v2.130.1 // indirect k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect diff --git a/go.sum b/go.sum index d53ee8f..a7d7a6f 100644 --- a/go.sum +++ b/go.sum @@ -178,6 +178,8 @@ github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-viper/mapstructure/v2 v2.0.0 h1:dhn8MZ1gZ0mzeodTG3jt5Vj/o87xZKuNAprG2mQfMfc= github.com/go-viper/mapstructure/v2 v2.0.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA= +github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= @@ -258,6 +260,8 @@ github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSAS github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -270,6 +274,7 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -308,6 +313,10 @@ github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/ github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/ginkgo/v2 v2.20.2 h1:7NVCeyIWROIAheY21RLS+3j2bb52W0W82tkberYytp4= github.com/onsi/ginkgo/v2 v2.20.2/go.mod h1:K9gyxPIlb+aIvnZ8bd9Ak+YP18w3APlR+5coaZoE2ag= github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8= @@ -322,8 +331,10 @@ github.com/ory/dockertest/v3 v3.10.0 h1:4K3z2VMe8Woe++invjaTB7VRyQXQy5UY+loujO4a github.com/ory/dockertest/v3 v3.10.0/go.mod h1:nr57ZbRWMqfsdGdFNLHz5jjNdDb7VVFnzAeW1n5N1Lg= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -352,8 +363,12 @@ github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= +github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w= github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -362,6 +377,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -406,6 +422,7 @@ go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN8 go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= @@ -418,6 +435,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= +golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -515,6 +534,9 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= @@ -531,6 +553,10 @@ k8s.io/apimachinery v0.31.0 h1:m9jOiSr3FoSSL5WO9bjm1n6B9KROYYgNZOb4tyZ1lBc= k8s.io/apimachinery v0.31.0/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= k8s.io/client-go v0.31.0 h1:QqEJzNjbN2Yv1H79SsS+SWnXkBgVu4Pj3CJQgbx0gI8= k8s.io/client-go v0.31.0/go.mod h1:Y9wvC76g4fLjmU0BA+rV+h2cncoadjvjjkkIGoTLcGU= +k8s.io/code-generator v0.31.0 h1:w607nrMi1KeDKB3/F/J4lIoOgAwc+gV9ZKew4XRfMp8= +k8s.io/code-generator v0.31.0/go.mod h1:84y4w3es8rOJOUUP1rLsIiGlO1JuEaPFXQPA9e/K6U0= +k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo= +k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70/go.mod h1:VH3AT8AaQOqiGjMF9p0/IM1Dj+82ZwjfxUP1IxaHE+8= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= k8s.io/kube-aggregator v0.31.0 h1:3DqSpmqHF8rey7fY+qYXLJms0tYPhxrgWvjpnKVnS0Y= @@ -543,6 +569,10 @@ sigs.k8s.io/cli-utils v0.37.2 h1:GOfKw5RV2HDQZDJlru5KkfLO1tbxqMoyn1IYUxqBpNg= sigs.k8s.io/cli-utils v0.37.2/go.mod h1:V+IZZr4UoGj7gMJXklWBg6t5xbdThFBcpj4MrZuCYco= sigs.k8s.io/controller-runtime v0.19.0 h1:nWVM7aq+Il2ABxwiCizrVDSlmDcshi9llbaFbC0ji/Q= sigs.k8s.io/controller-runtime v0.19.0/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4= +sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20240903085516-38546806f2fa h1:W4h+w5hhRPo6CPiZ1aUFhe4XQ5Ng9391PMSN6VUnb6I= +sigs.k8s.io/controller-runtime/tools/setup-envtest v0.0.0-20240903085516-38546806f2fa/go.mod h1:IaDsO8xSPRxRG1/rm9CP7+jPmj0nMNAuNi/yiHnLX8k= +sigs.k8s.io/controller-tools v0.16.2 h1:uUFF/AW3phBWPiERvkSNOVct//L427bPS7xGfKi6Tz4= +sigs.k8s.io/controller-tools v0.16.2/go.mod h1:0I0xqjR65YTfoO12iR+mZR6s6UAVcUARgXRlsu0ljB0= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/kustomize/api v0.17.3 h1:6GCuHSsxq7fN5yhF2XrC+AAr8gxQwhexgHflOAD/JJU= diff --git a/hack/genclient.sh b/hack/genclient.sh index bcc2b56..8e22d38 100755 --- a/hack/genclient.sh +++ b/hack/genclient.sh @@ -2,9 +2,15 @@ set -eo pipefail -export GOROOT=$(go env GOROOT) +BASEDIR=$(realpath "$(dirname "$0")"/..) + +export GOBIN=$BASEDIR/bin +mkdir -p "$GOBIN" + +go mod download k8s.io/code-generator +CODEGEN_DIR=$(go list -m -f '{{.Dir}}' k8s.io/code-generator) +go install "$CODEGEN_DIR"/cmd/* -BASEDIR=$(realpath $(dirname "$0")/..) TEMPDIR=$BASEDIR/tmp/gen trap 'rm -rf "$TEMPDIR"' EXIT mkdir -p "$TEMPDIR" @@ -12,39 +18,38 @@ mkdir -p "$TEMPDIR" mkdir -p "$TEMPDIR"/apis/core.cs.sap.com ln -s "$BASEDIR"/api/v1alpha1 "$TEMPDIR"/apis/core.cs.sap.com/v1alpha1 -"$BASEDIR"/bin/client-gen \ +"$GOBIN"/client-gen \ --clientset-name versioned \ - --input-base "" \ - --input github.com/sap/component-operator/tmp/gen/apis/core.cs.sap.com/v1alpha1 \ + --input-base "$TEMPDIR"/apis \ + --input core.cs.sap.com/v1alpha1 \ --go-header-file "$BASEDIR"/hack/boilerplate.go.txt \ - --output-package github.com/sap/component-operator/pkg/client/clientset \ - --output-base "$TEMPDIR"/pkg/client \ + --output-pkg github.com/sap/component-operator/pkg/client/clientset \ + --output-dir "$TEMPDIR"/pkg/client/clientset \ --plural-exceptions Component:components -"$BASEDIR"/bin/lister-gen \ - --input-dirs github.com/sap/component-operator/tmp/gen/apis/core.cs.sap.com/v1alpha1 \ +"$GOBIN"/lister-gen \ --go-header-file "$BASEDIR"/hack/boilerplate.go.txt \ - --output-package github.com/sap/component-operator/pkg/client/listers \ - --output-base "$TEMPDIR"/pkg/client \ - --plural-exceptions Component:components + --output-pkg github.com/sap/component-operator/pkg/client/listers \ + --output-dir "$TEMPDIR"/pkg/client/listers \ + --plural-exceptions Component:components \ + github.com/sap/component-operator/tmp/gen/apis/core.cs.sap.com/v1alpha1 -"$BASEDIR"/bin/informer-gen \ - --input-dirs github.com/sap/component-operator/tmp/gen/apis/core.cs.sap.com/v1alpha1 \ +"$GOBIN"/informer-gen \ --versioned-clientset-package github.com/sap/component-operator/pkg/client/clientset/versioned \ --listers-package github.com/sap/component-operator/pkg/client/listers \ --go-header-file "$BASEDIR"/hack/boilerplate.go.txt \ - --output-package github.com/sap/component-operator/pkg/client/informers \ - --output-base "$TEMPDIR"/pkg/client \ - --plural-exceptions Component:components + --output-pkg github.com/sap/component-operator/pkg/client/informers \ + --output-dir "$TEMPDIR"/pkg/client/informers \ + --plural-exceptions Component:components \ + github.com/sap/component-operator/tmp/gen/apis/core.cs.sap.com/v1alpha1 find "$TEMPDIR"/pkg/client -name "*.go" -exec \ perl -pi -e "s#github\.com/sap/component-operator/tmp/gen/apis/core\.cs\.sap\.com/v1alpha1#github.com/sap/component-operator/api/v1alpha1#g" \ {} + rm -rf "$BASEDIR"/pkg/client -mv "$TEMPDIR"/pkg/client/github.com/sap/component-operator/pkg/client "$BASEDIR"/pkg +mv "$TEMPDIR"/pkg/client "$BASEDIR"/pkg cd "$BASEDIR" -go mod tidy go fmt ./pkg/client/... go vet ./pkg/client/... diff --git a/hack/tools.go b/hack/tools.go new file mode 100644 index 0000000..bd89ecd --- /dev/null +++ b/hack/tools.go @@ -0,0 +1,15 @@ +//go:build tools +// +build tools + +/* +SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and component-operator contributors +SPDX-License-Identifier: Apache-2.0 +*/ + +package tools + +import ( + _ "k8s.io/code-generator" + _ "sigs.k8s.io/controller-runtime/tools/setup-envtest" + _ "sigs.k8s.io/controller-tools/cmd/controller-gen" +) diff --git a/pkg/client/clientset/versioned/fake/clientset_generated.go b/pkg/client/clientset/versioned/fake/clientset_generated.go index c02d0f1..36039e9 100644 --- a/pkg/client/clientset/versioned/fake/clientset_generated.go +++ b/pkg/client/clientset/versioned/fake/clientset_generated.go @@ -20,8 +20,12 @@ import ( // NewSimpleClientset returns a clientset that will respond with the provided objects. // It's backed by a very simple object tracker that processes creates, updates and deletions as-is, -// without applying any validations and/or defaults. It shouldn't be considered a replacement +// without applying any field management, validations and/or defaults. It shouldn't be considered a replacement // for a real clientset and is mostly useful in simple unit tests. +// +// DEPRECATED: NewClientset replaces this with support for field management, which significantly improves +// server side apply testing. NewClientset is only available when apply configurations are generated (e.g. +// via --with-applyconfig). func NewSimpleClientset(objects ...runtime.Object) *Clientset { o := testing.NewObjectTracker(scheme, codecs.UniversalDecoder()) for _, obj := range objects { diff --git a/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/component.go b/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/component.go index e0654a1..cc4a707 100644 --- a/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/component.go +++ b/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/component.go @@ -9,14 +9,13 @@ package v1alpha1 import ( "context" - "time" v1alpha1 "github.com/sap/component-operator/api/v1alpha1" scheme "github.com/sap/component-operator/pkg/client/clientset/versioned/scheme" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" types "k8s.io/apimachinery/pkg/types" watch "k8s.io/apimachinery/pkg/watch" - rest "k8s.io/client-go/rest" + gentype "k8s.io/client-go/gentype" ) // ComponentsGetter has a method to return a ComponentInterface. @@ -29,6 +28,7 @@ type ComponentsGetter interface { type ComponentInterface interface { Create(ctx context.Context, component *v1alpha1.Component, opts v1.CreateOptions) (*v1alpha1.Component, error) Update(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (*v1alpha1.Component, error) + // Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). UpdateStatus(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (*v1alpha1.Component, error) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error @@ -41,144 +41,18 @@ type ComponentInterface interface { // components implements ComponentInterface type components struct { - client rest.Interface - ns string + *gentype.ClientWithList[*v1alpha1.Component, *v1alpha1.ComponentList] } // newComponents returns a Components func newComponents(c *CoreV1alpha1Client, namespace string) *components { return &components{ - client: c.RESTClient(), - ns: namespace, + gentype.NewClientWithList[*v1alpha1.Component, *v1alpha1.ComponentList]( + "components", + c.RESTClient(), + scheme.ParameterCodec, + namespace, + func() *v1alpha1.Component { return &v1alpha1.Component{} }, + func() *v1alpha1.ComponentList { return &v1alpha1.ComponentList{} }), } } - -// Get takes name of the component, and returns the corresponding component object, and an error if there is any. -func (c *components) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1alpha1.Component, err error) { - result = &v1alpha1.Component{} - err = c.client.Get(). - Namespace(c.ns). - Resource("components"). - Name(name). - VersionedParams(&options, scheme.ParameterCodec). - Do(ctx). - Into(result) - return -} - -// List takes label and field selectors, and returns the list of Components that match those selectors. -func (c *components) List(ctx context.Context, opts v1.ListOptions) (result *v1alpha1.ComponentList, err error) { - var timeout time.Duration - if opts.TimeoutSeconds != nil { - timeout = time.Duration(*opts.TimeoutSeconds) * time.Second - } - result = &v1alpha1.ComponentList{} - err = c.client.Get(). - Namespace(c.ns). - Resource("components"). - VersionedParams(&opts, scheme.ParameterCodec). - Timeout(timeout). - Do(ctx). - Into(result) - return -} - -// Watch returns a watch.Interface that watches the requested components. -func (c *components) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { - var timeout time.Duration - if opts.TimeoutSeconds != nil { - timeout = time.Duration(*opts.TimeoutSeconds) * time.Second - } - opts.Watch = true - return c.client.Get(). - Namespace(c.ns). - Resource("components"). - VersionedParams(&opts, scheme.ParameterCodec). - Timeout(timeout). - Watch(ctx) -} - -// Create takes the representation of a component and creates it. Returns the server's representation of the component, and an error, if there is any. -func (c *components) Create(ctx context.Context, component *v1alpha1.Component, opts v1.CreateOptions) (result *v1alpha1.Component, err error) { - result = &v1alpha1.Component{} - err = c.client.Post(). - Namespace(c.ns). - Resource("components"). - VersionedParams(&opts, scheme.ParameterCodec). - Body(component). - Do(ctx). - Into(result) - return -} - -// Update takes the representation of a component and updates it. Returns the server's representation of the component, and an error, if there is any. -func (c *components) Update(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (result *v1alpha1.Component, err error) { - result = &v1alpha1.Component{} - err = c.client.Put(). - Namespace(c.ns). - Resource("components"). - Name(component.Name). - VersionedParams(&opts, scheme.ParameterCodec). - Body(component). - Do(ctx). - Into(result) - return -} - -// UpdateStatus was generated because the type contains a Status member. -// Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). -func (c *components) UpdateStatus(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (result *v1alpha1.Component, err error) { - result = &v1alpha1.Component{} - err = c.client.Put(). - Namespace(c.ns). - Resource("components"). - Name(component.Name). - SubResource("status"). - VersionedParams(&opts, scheme.ParameterCodec). - Body(component). - Do(ctx). - Into(result) - return -} - -// Delete takes name of the component and deletes it. Returns an error if one occurs. -func (c *components) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { - return c.client.Delete(). - Namespace(c.ns). - Resource("components"). - Name(name). - Body(&opts). - Do(ctx). - Error() -} - -// DeleteCollection deletes a collection of objects. -func (c *components) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { - var timeout time.Duration - if listOpts.TimeoutSeconds != nil { - timeout = time.Duration(*listOpts.TimeoutSeconds) * time.Second - } - return c.client.Delete(). - Namespace(c.ns). - Resource("components"). - VersionedParams(&listOpts, scheme.ParameterCodec). - Timeout(timeout). - Body(&opts). - Do(ctx). - Error() -} - -// Patch applies the patch and returns the patched component. -func (c *components) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha1.Component, err error) { - result = &v1alpha1.Component{} - err = c.client.Patch(pt). - Namespace(c.ns). - Resource("components"). - Name(name). - SubResource(subresources...). - VersionedParams(&opts, scheme.ParameterCodec). - Body(data). - Do(ctx). - Into(result) - return -} diff --git a/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/fake/fake_component.go b/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/fake/fake_component.go index 44a915c..1704a8c 100644 --- a/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/fake/fake_component.go +++ b/pkg/client/clientset/versioned/typed/core.cs.sap.com/v1alpha1/fake/fake_component.go @@ -30,22 +30,24 @@ var componentsKind = v1alpha1.SchemeGroupVersion.WithKind("Component") // Get takes name of the component, and returns the corresponding component object, and an error if there is any. func (c *FakeComponents) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1alpha1.Component, err error) { + emptyResult := &v1alpha1.Component{} obj, err := c.Fake. - Invokes(testing.NewGetAction(componentsResource, c.ns, name), &v1alpha1.Component{}) + Invokes(testing.NewGetActionWithOptions(componentsResource, c.ns, name, options), emptyResult) if obj == nil { - return nil, err + return emptyResult, err } return obj.(*v1alpha1.Component), err } // List takes label and field selectors, and returns the list of Components that match those selectors. func (c *FakeComponents) List(ctx context.Context, opts v1.ListOptions) (result *v1alpha1.ComponentList, err error) { + emptyResult := &v1alpha1.ComponentList{} obj, err := c.Fake. - Invokes(testing.NewListAction(componentsResource, componentsKind, c.ns, opts), &v1alpha1.ComponentList{}) + Invokes(testing.NewListActionWithOptions(componentsResource, componentsKind, c.ns, opts), emptyResult) if obj == nil { - return nil, err + return emptyResult, err } label, _, _ := testing.ExtractFromListOptions(opts) @@ -64,40 +66,43 @@ func (c *FakeComponents) List(ctx context.Context, opts v1.ListOptions) (result // Watch returns a watch.Interface that watches the requested components. func (c *FakeComponents) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { return c.Fake. - InvokesWatch(testing.NewWatchAction(componentsResource, c.ns, opts)) + InvokesWatch(testing.NewWatchActionWithOptions(componentsResource, c.ns, opts)) } // Create takes the representation of a component and creates it. Returns the server's representation of the component, and an error, if there is any. func (c *FakeComponents) Create(ctx context.Context, component *v1alpha1.Component, opts v1.CreateOptions) (result *v1alpha1.Component, err error) { + emptyResult := &v1alpha1.Component{} obj, err := c.Fake. - Invokes(testing.NewCreateAction(componentsResource, c.ns, component), &v1alpha1.Component{}) + Invokes(testing.NewCreateActionWithOptions(componentsResource, c.ns, component, opts), emptyResult) if obj == nil { - return nil, err + return emptyResult, err } return obj.(*v1alpha1.Component), err } // Update takes the representation of a component and updates it. Returns the server's representation of the component, and an error, if there is any. func (c *FakeComponents) Update(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (result *v1alpha1.Component, err error) { + emptyResult := &v1alpha1.Component{} obj, err := c.Fake. - Invokes(testing.NewUpdateAction(componentsResource, c.ns, component), &v1alpha1.Component{}) + Invokes(testing.NewUpdateActionWithOptions(componentsResource, c.ns, component, opts), emptyResult) if obj == nil { - return nil, err + return emptyResult, err } return obj.(*v1alpha1.Component), err } // UpdateStatus was generated because the type contains a Status member. // Add a +genclient:noStatus comment above the type to avoid generating UpdateStatus(). -func (c *FakeComponents) UpdateStatus(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (*v1alpha1.Component, error) { +func (c *FakeComponents) UpdateStatus(ctx context.Context, component *v1alpha1.Component, opts v1.UpdateOptions) (result *v1alpha1.Component, err error) { + emptyResult := &v1alpha1.Component{} obj, err := c.Fake. - Invokes(testing.NewUpdateSubresourceAction(componentsResource, "status", c.ns, component), &v1alpha1.Component{}) + Invokes(testing.NewUpdateSubresourceActionWithOptions(componentsResource, "status", c.ns, component, opts), emptyResult) if obj == nil { - return nil, err + return emptyResult, err } return obj.(*v1alpha1.Component), err } @@ -112,7 +117,7 @@ func (c *FakeComponents) Delete(ctx context.Context, name string, opts v1.Delete // DeleteCollection deletes a collection of objects. func (c *FakeComponents) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { - action := testing.NewDeleteCollectionAction(componentsResource, c.ns, listOpts) + action := testing.NewDeleteCollectionActionWithOptions(componentsResource, c.ns, opts, listOpts) _, err := c.Fake.Invokes(action, &v1alpha1.ComponentList{}) return err @@ -120,11 +125,12 @@ func (c *FakeComponents) DeleteCollection(ctx context.Context, opts v1.DeleteOpt // Patch applies the patch and returns the patched component. func (c *FakeComponents) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha1.Component, err error) { + emptyResult := &v1alpha1.Component{} obj, err := c.Fake. - Invokes(testing.NewPatchSubresourceAction(componentsResource, c.ns, name, pt, data, subresources...), &v1alpha1.Component{}) + Invokes(testing.NewPatchSubresourceActionWithOptions(componentsResource, c.ns, name, pt, data, opts, subresources...), emptyResult) if obj == nil { - return nil, err + return emptyResult, err } return obj.(*v1alpha1.Component), err } diff --git a/pkg/client/informers/externalversions/factory.go b/pkg/client/informers/externalversions/factory.go index d1135bb..c623db9 100644 --- a/pkg/client/informers/externalversions/factory.go +++ b/pkg/client/informers/externalversions/factory.go @@ -217,6 +217,7 @@ type SharedInformerFactory interface { // Start initializes all requested informers. They are handled in goroutines // which run until the stop channel gets closed. + // Warning: Start does not block. When run in a go-routine, it will race with a later WaitForCacheSync. Start(stopCh <-chan struct{}) // Shutdown marks a factory as shutting down. At that point no new diff --git a/pkg/client/listers/core.cs.sap.com/v1alpha1/component.go b/pkg/client/listers/core.cs.sap.com/v1alpha1/component.go index bbee35b..5d729c7 100644 --- a/pkg/client/listers/core.cs.sap.com/v1alpha1/component.go +++ b/pkg/client/listers/core.cs.sap.com/v1alpha1/component.go @@ -9,8 +9,8 @@ package v1alpha1 import ( v1alpha1 "github.com/sap/component-operator/api/v1alpha1" - "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/listers" "k8s.io/client-go/tools/cache" ) @@ -27,25 +27,17 @@ type ComponentLister interface { // componentLister implements the ComponentLister interface. type componentLister struct { - indexer cache.Indexer + listers.ResourceIndexer[*v1alpha1.Component] } // NewComponentLister returns a new ComponentLister. func NewComponentLister(indexer cache.Indexer) ComponentLister { - return &componentLister{indexer: indexer} -} - -// List lists all Components in the indexer. -func (s *componentLister) List(selector labels.Selector) (ret []*v1alpha1.Component, err error) { - err = cache.ListAll(s.indexer, selector, func(m interface{}) { - ret = append(ret, m.(*v1alpha1.Component)) - }) - return ret, err + return &componentLister{listers.New[*v1alpha1.Component](indexer, v1alpha1.Resource("component"))} } // Components returns an object that can list and get Components. func (s *componentLister) Components(namespace string) ComponentNamespaceLister { - return componentNamespaceLister{indexer: s.indexer, namespace: namespace} + return componentNamespaceLister{listers.NewNamespaced[*v1alpha1.Component](s.ResourceIndexer, namespace)} } // ComponentNamespaceLister helps list and get Components. @@ -63,26 +55,5 @@ type ComponentNamespaceLister interface { // componentNamespaceLister implements the ComponentNamespaceLister // interface. type componentNamespaceLister struct { - indexer cache.Indexer - namespace string -} - -// List lists all Components in the indexer for a given namespace. -func (s componentNamespaceLister) List(selector labels.Selector) (ret []*v1alpha1.Component, err error) { - err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) { - ret = append(ret, m.(*v1alpha1.Component)) - }) - return ret, err -} - -// Get retrieves the Component from the indexer for a given namespace and name. -func (s componentNamespaceLister) Get(name string) (*v1alpha1.Component, error) { - obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name) - if err != nil { - return nil, err - } - if !exists { - return nil, errors.NewNotFound(v1alpha1.Resource("component"), name) - } - return obj.(*v1alpha1.Component), nil + listers.ResourceIndexer[*v1alpha1.Component] } From d39453f00857a79a16864728f54a434bc1695953 Mon Sep 17 00:00:00 2001 From: Christoph Barbian Date: Thu, 5 Sep 2024 19:47:24 +0200 Subject: [PATCH 2/2] some fixes to code generation --- Makefile | 4 ++-- hack/genclient.sh | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7c449be..257f6cd 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ manifests: controller-gen ## Generate CustomResourceDefinition objects test ! -d chart || test -e chart/crds || ln -s ../crds chart/crds .PHONY: generate -generate: generate-deepcopy generate-client ## Generate everything +generate: generate-deepcopy generate-client ## Generate required code pieces .PHONY: generate-deepcopy generate-deepcopy: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations @@ -97,7 +97,7 @@ $(CONTROLLER_GEN): $(LOCALBIN) .PHONY: setup-envtest setup-envtest: $(SETUP_ENVTEST) ## Install setup-envtest $(SETUP_ENVTEST): $(LOCALBIN) - go mod download sigs.k8s.io/controller-runtime/tools/setup-envtest + go mod download sigs.k8s.io/controller-runtime GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-runtime/tools/setup-envtest) .PHONY: envtest diff --git a/hack/genclient.sh b/hack/genclient.sh index 8e22d38..1696ca6 100755 --- a/hack/genclient.sh +++ b/hack/genclient.sh @@ -51,5 +51,6 @@ rm -rf "$BASEDIR"/pkg/client mv "$TEMPDIR"/pkg/client "$BASEDIR"/pkg cd "$BASEDIR" +go mod tidy go fmt ./pkg/client/... go vet ./pkg/client/...