Skip to content

Commit

Permalink
refactor code-generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mjudeikis committed Nov 25, 2024
1 parent 38f9887 commit e275ddb
Show file tree
Hide file tree
Showing 48 changed files with 3,655 additions and 558 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: 1.23
- name: Build kgctl Binaries to Be Released
run: make release
- name: Publish Release
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
bin/
tmp/
e2e/kind.yaml*
hack/tools/controller-gen
hack/tools/controller-gen-v0.16.1
hack/tools/openshift-goimports
hack/tools/openshift-goimports-c72f1dc2e3aacfa00aece3391d938c9bc734e791
hack/tools/yaml-patch
hack/tools/yaml-patch-v0.0.11
129 changes: 61 additions & 68 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export GO111MODULE=on
.PHONY: push container clean container-name container-latest push-latest fmt lint test unit gomodtidy header generate crd client deepcopy informer lister manifest manfest-latest manifest-annotate manifest manfest-latest manifest-annotate release gen-docs e2e
.PHONY: push container clean container-name container-latest push-latest fmt lint test unit gomodtidy header generate crds codegen manifest manfest-latest manifest-annotate manifest manfest-latest manifest-annotate release e2e

OS ?= $(shell go env GOOS)
ARCH ?= $(shell go env GOARCH)
Expand Down Expand Up @@ -48,6 +48,65 @@ BASH_UNIT_FLAGS :=
BUILD_IMAGE ?= golang:1.23.0
BASE_IMAGE ?= alpine:3.20

GO_INSTALL = ./hack/go-install.sh
TOOLS_DIR=hack/tools
ROOT_DIR=$(abspath .)
TOOLS_GOBIN_DIR := $(abspath $(TOOLS_DIR))
GOBIN_DIR=$(abspath ./bin)
PATH := $(GOBIN_DIR):$(TOOLS_GOBIN_DIR):$(PATH)

# Detect the path used for the install target
ifeq (,$(shell go env GOBIN))
INSTALL_GOBIN=$(shell go env GOPATH)/bin
else
INSTALL_GOBIN=$(shell go env GOBIN)
endif

CONTROLLER_GEN_VER := v0.16.1
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(TOOLS_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
export CONTROLLER_GEN # so hack scripts can use it

YAML_PATCH_VER ?= v0.0.11
YAML_PATCH_BIN := yaml-patch
YAML_PATCH := $(TOOLS_DIR)/$(YAML_PATCH_BIN)-$(YAML_PATCH_VER)
export YAML_PATCH # so hack scripts can use it

OPENSHIFT_GOIMPORTS_VER := c72f1dc2e3aacfa00aece3391d938c9bc734e791
OPENSHIFT_GOIMPORTS_BIN := openshift-goimports
OPENSHIFT_GOIMPORTS := $(TOOLS_DIR)/$(OPENSHIFT_GOIMPORTS_BIN)-$(OPENSHIFT_GOIMPORTS_VER)
export OPENSHIFT_GOIMPORTS # so hack scripts can use i

$(CONTROLLER_GEN):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)

$(YAML_PATCH):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/pivotal-cf/yaml-patch/cmd/yaml-patch $(YAML_PATCH_BIN) $(YAML_PATCH_VER)

$(OPENSHIFT_GOIMPORTS):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/openshift-eng/openshift-goimports $(OPENSHIFT_GOIMPORTS_BIN) $(OPENSHIFT_GOIMPORTS_VER)

crds: $(CONTROLLER_GEN) $(YAML_PATCH) $(OPENSHIFT_GOIMPORTS) ## Generate crds
./hack/update-codegen-crds.sh
.PHONY: crds

tools: $(CONTROLLER_GEN) $(YAML_PATCH) ## Install tools
.PHONY: tool

codegen: crds ## Generate all
go mod download
./hack/update-codegen-clients.sh
$(MAKE) imports
.PHONY: codegen

.PHONY: imports
imports: $(OPENSHIFT_GOIMPORTS)
$(OPENSHIFT_GOIMPORTS) -m github.com/squat/kilo


tools: $(CONTROLLER_GEN) $(YAML_PATCH) $(OPENSHIFT_GOIMPORTS) ## Install tools
.PHONY: tools

build: $(BINS)

build-%:
Expand Down Expand Up @@ -75,73 +134,7 @@ all-container-latest: $(addprefix container-latest-, $(ALL_ARCH))

all-push-latest: $(addprefix push-latest-, $(ALL_ARCH))

generate: client deepcopy informer lister crd

crd: manifests/crds.yaml
manifests/crds.yaml: pkg/k8s/apis/kilo/v1alpha1/types.go $(CONTROLLER_GEN_BINARY)
$(CONTROLLER_GEN_BINARY) crd \
paths=./pkg/k8s/apis/kilo/... \
output:crd:stdout > $@

client: pkg/k8s/clientset/versioned/typed/kilo/v1alpha1/peer.go
pkg/k8s/clientset/versioned/typed/kilo/v1alpha1/peer.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(CLIENT_GEN_BINARY)
$(CLIENT_GEN_BINARY) \
--clientset-name versioned \
--input-base "" \
--input $(PKG)/pkg/k8s/apis/kilo/v1alpha1 \
--output-base $(CURDIR) \
--output-package $(PKG)/pkg/k8s/clientset \
--go-header-file=.header \
--logtostderr
rm -r pkg/k8s/clientset || true
mv $(PKG)/pkg/k8s/clientset pkg/k8s
rm -r github.com || true
go fmt ./pkg/k8s/clientset/...

deepcopy: pkg/k8s/apis/kilo/v1alpha1/zz_generated.deepcopy.go
pkg/k8s/apis/kilo/v1alpha1/zz_generated.deepcopy.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(DEEPCOPY_GEN_BINARY)
$(DEEPCOPY_GEN_BINARY) \
--input-dirs ./$(@D) \
--go-header-file=.header \
--logtostderr \
--output-base $(CURDIR) \
--output-file-base zz_generated.deepcopy
mv $(PKG)/$@ $@ || true
rm -r github.com || true
go fmt $@

informer: pkg/k8s/informers/kilo/v1alpha1/peer.go
pkg/k8s/informers/kilo/v1alpha1/peer.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(INFORMER_GEN_BINARY)
$(INFORMER_GEN_BINARY) \
--input-dirs $(PKG)/pkg/k8s/apis/kilo/v1alpha1 \
--go-header-file=.header \
--logtostderr \
--versioned-clientset-package $(PKG)/pkg/k8s/clientset/versioned \
--listers-package $(PKG)/pkg/k8s/listers \
--output-base $(CURDIR) \
--output-package $(PKG)/pkg/k8s/informers \
--single-directory
rm -r pkg/k8s/informers || true
mv $(PKG)/pkg/k8s/informers pkg/k8s
rm -r github.com || true
go fmt ./pkg/k8s/informers/...

lister: pkg/k8s/listers/kilo/v1alpha1/peer.go
pkg/k8s/listers/kilo/v1alpha1/peer.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(LISTER_GEN_BINARY)
$(LISTER_GEN_BINARY) \
--input-dirs $(PKG)/pkg/k8s/apis/kilo/v1alpha1 \
--go-header-file=.header \
--logtostderr \
--output-base $(CURDIR) \
--output-package $(PKG)/pkg/k8s/listers
rm -r pkg/k8s/listers || true
mv $(PKG)/pkg/k8s/listers pkg/k8s
rm -r github.com || true
go fmt ./pkg/k8s/listers/...

gen-docs: generate docs/api.md docs/kg.md
docs/api.md: pkg/k8s/apis/kilo/v1alpha1/types.go $(DOCS_GEN_BINARY)
$(DOCS_GEN_BINARY) $< > $@
generate: codegen crds

$(BINS): $(SRC) go.mod
@mkdir -p bin/$(word 2,$(subst /, ,$@))/$(word 3,$(subst /, ,$@))
Expand Down
1 change: 1 addition & 0 deletions cmd/kg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/spf13/cobra"

apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand Down
1 change: 1 addition & 0 deletions cmd/kg/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"

v1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
1 change: 1 addition & 0 deletions cmd/kgctl/connect_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/spf13/cobra"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down
1 change: 1 addition & 0 deletions cmd/kgctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/go-kit/kit/log"
"github.com/spf13/cobra"

apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand Down
1 change: 1 addition & 0 deletions cmd/kgctl/showconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/spf13/cobra"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down
94 changes: 94 additions & 0 deletions config/crds/kilo.squat.ai_peers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
name: peers.kilo.squat.ai
spec:
group: kilo.squat.ai
names:
kind: Peer
listKind: PeerList
plural: peers
singular: peer
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Peer is a WireGuard peer that should have access to the VPN.
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: |-
Specification of the desired behavior of the Kilo Peer. More info:
https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#spec-and-status
properties:
allowedIPs:
description: |-
AllowedIPs is the list of IP addresses that are allowed
for the given peer's tunnel.
items:
type: string
type: array
endpoint:
description: Endpoint is the initial endpoint for connections to the
peer.
properties:
dnsOrIP:
description: DNSOrIP is a DNS name or an IP address.
properties:
dns:
description: DNS must be a valid RFC 1123 subdomain.
type: string
ip:
description: IP must be a valid IP address.
type: string
type: object
port:
description: Port must be a valid port number.
format: int32
type: integer
required:
- dnsOrIP
- port
type: object
persistentKeepalive:
description: |-
PersistentKeepalive is the interval in seconds of the emission
of keepalive packets by the peer. This defaults to 0, which
disables the feature.
type: integer
presharedKey:
description: PresharedKey is the optional symmetric encryption key
for the peer.
type: string
publicKey:
description: PublicKey is the WireGuard public key for the peer.
type: string
required:
- allowedIPs
- publicKey
type: object
required:
- spec
type: object
served: true
storage: true
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
k8s.io/apimachinery v0.31.2
k8s.io/client-go v0.31.2
k8s.io/code-generator v0.31.2
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
sigs.k8s.io/controller-tools v0.16.5
)

Expand Down Expand Up @@ -87,7 +88,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // 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
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
16 changes: 16 additions & 0 deletions hack/boilerplate/boilerplate.Dockerfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# syntax=docker/dockerfile:1.4

# Copyright YEAR Kilo & Cast AI Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

14 changes: 14 additions & 0 deletions hack/boilerplate/boilerplate.Makefile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright YEAR Kilo & Cast AI Authors. Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16 changes: 16 additions & 0 deletions hack/boilerplate/boilerplate.generatego.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright YEAR Kilo & Cast AI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

16 changes: 16 additions & 0 deletions hack/boilerplate/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright YEAR Kilo & Cast AI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

14 changes: 14 additions & 0 deletions hack/boilerplate/boilerplate.py.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright YEAR Kilo & Cast AI Authors. Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Loading

0 comments on commit e275ddb

Please sign in to comment.