Skip to content

Commit

Permalink
add typed client at pkg/client
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap committed Jul 10, 2023
1 parent ed48f08 commit c672b64
Show file tree
Hide file tree
Showing 30 changed files with 1,375 additions and 19 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY webhooks/ webhooks/
COPY pkg/ pkg/
COPY internal/ internal/
COPY crds/ crds/
COPY Makefile Makefile
Expand Down
40 changes: 37 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="main.go" paths="./api/..." paths="./controllers/..." paths="./webhooks/..." paths="./internal/..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="main.go" paths="./api/..." paths="./controllers/..." paths="./webhooks/..." paths="./pkg/..." paths="./internal/..." output:crd:artifacts:config=config/crd/bases
rm -rf crds && cp -r config/crd/bases crds

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: controller-gen client-gen informer-gen lister-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..."
./hack/gen-typed-client

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand Down Expand Up @@ -74,7 +75,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build --secret id=github-token,env=GITHUB_TOKEN -t ${IMG} .
docker build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down Expand Up @@ -131,10 +132,14 @@ $(LOCALBIN):
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
CLIENT_GEN ?= $(shell pwd)/bin/client-gen
INFORMER_GEN ?= $(shell pwd)/bin/informer-gen
LISTER_GEN ?= $(shell pwd)/bin/lister-gen

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.9.2
CODE_GENERATOR_VERSION ?= v0.23.4

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand All @@ -154,3 +159,32 @@ $(ENVTEST): $(LOCALBIN)
ENVTESTDIR=$$($(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\
rm -f $(LOCALBIN)/k8s/current ;\
ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current

.PHONY: client-gen
client-gen: $(CLIENT_GEN) ## Download client-gen
$(CLIENT_GEN): $(LOCALBIN)
$(call go-install-tool,$(CLIENT_GEN),k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION))

.PHONY: informer-gen
informer-gen: $(INFORMER_GEN) ## Download informer-gen
$(INFORMER_GEN): $(LOCALBIN)
$(call go-install-tool,$(INFORMER_GEN),k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION))

.PHONY: lister-gen
lister-gen: $(LISTER_GEN) ## Download lister-gen
$(LISTER_GEN): $(LOCALBIN)
$(call go-install-tool,$(LISTER_GEN),k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION))

# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
10 changes: 10 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ var (
// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

var (
// Needed by kubernetes/code-generator
SchemeGroupVersion = GroupVersion
)

// Needed by kubernetes/code-generator
func Resource(resource string) schema.GroupResource {
return GroupVersion.WithResource(resource).GroupResource()
}
17 changes: 9 additions & 8 deletions api/v1alpha1/project_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ type Condition struct {
// Condition type (such as 'Ready', ...)
Type ConditionType `json:"type"`
// Condition status (one of True, False, Unknown)
//+kubebuilder:validation:Enum=True;False;Unknown
// +kubebuilder:validation:Enum=True;False;Unknown
Status ConditionStatus `json:"status"`
// Timestamp of the last change of ConditionStatus
//+optional
// +optional
LastTransitionTime *metav1.Time `json:"lastTransitionTime,omitempty"`
// Reason is a programmatically usable identifier explaining the current ConditionStatus.
//+optional
// +optional
Reason string `json:"reason,omitempty"`
// Message is a human readable explanation of the current ConditionStatus.
//+optional
// +optional
Message string `json:"message,omitempty"`
}

Expand Down Expand Up @@ -93,11 +93,12 @@ const (
StateDeleting State = "Deleting"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
// +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +genclient

// Project is the Schema for the projects API.
type Project struct {
Expand All @@ -109,7 +110,7 @@ type Project struct {
Status ProjectStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// ProjectList contains a list of Project.
type ProjectList struct {
Expand Down
6 changes: 3 additions & 3 deletions controllers/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func NewProjectReconciler(client client.Client, scheme *runtime.Scheme, options
}
}

//+kubebuilder:rbac:groups=core.cs.sap.com,resources=projects,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core.cs.sap.com,resources=projects/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=core.cs.sap.com,resources=projects/finalizers,verbs=update
// +kubebuilder:rbac:groups=core.cs.sap.com,resources=projects,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core.cs.sap.com,resources=projects/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=core.cs.sap.com,resources=projects/finalizers,verbs=update

func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
log := log.FromContext(ctx)
Expand Down
44 changes: 44 additions & 0 deletions hack/gen-typed-client
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -eo pipefail

BASEDIR=$(realpath $(dirname "$0")/..)

rm -rf "$BASEDIR"/tmp
mkdir -p "$BASEDIR"/tmp/apis/core.cs.sap.com

ln -s "$BASEDIR"/api/v1alpha1 "$BASEDIR"/tmp/apis/core.cs.sap.com/v1alpha1

"$BASEDIR"/bin/client-gen \
--clientset-name versioned \
--input-base "" \
--input github.com/sap/project-operator/tmp/apis/core.cs.sap.com/v1alpha1 \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package github.com/sap/project-operator/pkg/client/clientset \
--output-base "$BASEDIR"/tmp/pkg/client

"$BASEDIR"/bin/lister-gen \
--input-dirs github.com/sap/project-operator/tmp/apis/core.cs.sap.com/v1alpha1 \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package github.com/sap/project-operator/pkg/client/listers \
--output-base "$BASEDIR"/tmp/pkg/client

"$BASEDIR"/bin/informer-gen \
--input-dirs github.com/sap/project-operator/tmp/apis/core.cs.sap.com/v1alpha1 \
--versioned-clientset-package github.com/sap/project-operator/pkg/client/clientset/versioned \
--listers-package github.com/sap/project-operator/pkg/client/listers \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package github.com/sap/project-operator/pkg/client/informers \
--output-base "$BASEDIR"/tmp/pkg/client

find "$BASEDIR"/tmp/pkg/client -name "*.go" -exec \
perl -pi -e "s#github\.com/sap/project-operator/tmp/apis/core\.cs\.sap\.com/v1alpha1#github\.com/sap/project-operator/api/v1alpha1#g" \
{} +

rm -rf "$BASEDIR"/pkg/client
mv "$BASEDIR"/tmp/pkg/client/github.com/sap/project-operator/pkg/client "$BASEDIR"/pkg

rm -rf "$BASEDIR"/tmp

go fmt "$BASEDIR"/pkg/client/...
go vet "$BASEDIR"/pkg/client/...
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
corev1alpha1 "github.com/sap/project-operator/api/v1alpha1"
"github.com/sap/project-operator/controllers"
"github.com/sap/project-operator/webhooks"
//+kubebuilder:scaffold:imports
// +kubebuilder:scaffold:imports
)

const (
Expand All @@ -42,7 +42,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(corev1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
// +kubebuilder:scaffold:scheme
}

func main() {
Expand All @@ -58,7 +58,7 @@ func main() {
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&webhookAddr, "webhook-bind-address", ":9443", "The address the webhook endpoint binds to.")
flag.StringVar(&webhookCertDir, "webhook-tls-directory", "", "The directory containing tls server key and certificate, as tls.key and tls.crt; defaults to $TMPDIR/k8s-webhook-server/serving-certs")
flag.StringVar(&webhookCertDir, "webhook-tls-directory", "", "The directory containing tls server key and certificate, as tls.key and tls.crt; defaults to $TMPDIR/k8s-webhook-server/serving-certs.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&namespacePrefix, "namespace-prefix", "project-", "Prefix of generated namespaces.")
flag.StringVar(&adminClusterRole, "admin-cluster-role", "cluster-admin", "Cluster role that admin users/groups will be granted on project namespace level.")
Expand Down Expand Up @@ -132,7 +132,7 @@ func main() {
setupLog.Error(err, "unable to create webhook", "webhook", "Project")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
Expand Down
106 changes: 106 additions & 0 deletions pkg/client/clientset/versioned/clientset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/client/clientset/versioned/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c672b64

Please sign in to comment.