Skip to content

Commit

Permalink
Add v1alpha2 Pool CRDs
Browse files Browse the repository at this point in the history
- removes subnet, first, and last from the pool spec
- add new converstion webhook for v1alpha1 to v1alpha2 pools

Co-authored-by: Christian Ang <[email protected]>
Co-authored-by: Aidan Obley <[email protected]>
Co-authored-by: Tyler Schultz <[email protected]>
  • Loading branch information
4 people committed May 15, 2023
1 parent fac8e38 commit c8d841c
Show file tree
Hide file tree
Showing 39 changed files with 1,778 additions and 559 deletions.
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ endif

HACK_BIN=$(shell pwd)/hack/bin

# Set --output-base for conversion-gen if we are not within GOPATH
ifneq ($(abspath $(ROOT_DIR)),$(shell go env GOPATH)/src/github.com/telekom/cluster-api-ipam-provider-in-cluster)
OUTPUT_BASE := --output-base=$(ROOT_DIR)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
Expand Down Expand Up @@ -46,8 +51,11 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: controller-gen conversion-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
$(CONVERSION_GEN) --input-dirs=./api/v1alpha1 \
--output-file-base=zz_generated.conversion $(OUTPUT_BASE) \
--go-header-file=./hack/boilerplate.go.txt

.PHONY: fmt
fmt: ## Run go fmt against code.
Expand Down Expand Up @@ -176,3 +184,8 @@ GO_LICENSES = $(HACK_BIN)/go-licenses
.PHONY: go-licenses
go-licenses:
env GOBIN=$(HACK_BIN) go install github.com/google/go-licenses@latest

CONVERSION_GEN = $(HACK_BIN)/conversion-gen
.PHONY: conversion-gen
conversion-gen: ## Download conversion-gen locally if necessary.
env GOBIN=$(HACK_BIN) go install k8s.io/code-generator/cmd/conversion-gen@latest
37 changes: 37 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: cluster.x-k8s.io
layout:
- go.kubebuilder.io/v3
Expand All @@ -12,4 +16,37 @@ resources:
kind: InClusterIPPool
path: github.com/telekom/cluster-api-ipam-provider-in-cluster/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: ipam
kind: InClusterIPPool
path: github.com/telekom/cluster-api-ipam-provider-in-cluster/api/v1alpha2
version: v1alpha2
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: ipam
kind: GlobalInClusterIPPool
path: github.com/telekom/cluster-api-ipam-provider-in-cluster/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: ipam
kind: GlobalInClusterIPPool
path: github.com/telekom/cluster-api-ipam-provider-in-cluster/api/v1alpha2
version: v1alpha2
webhooks:
conversion: true
webhookVersion: v1
version: "3"
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,10 @@ This is a IPAM provider for Cluster API that manages pools of IP addresses using

## Usage

This provider comes with a `InClusterIPPool` resource to specify the pools from which addresses should be assigned. You can provide a subnet in CIDR notation, or specify an address range using start and end addresses, as well as a prefix length, or a set of addresses with the prefix and gateway.
This provider comes with a `InClusterIPPool` resource to specify the pools from which addresses should be assigned. You can provide an address range using start and end addresses, as well as a prefix length, or a set of addresses with the prefix and gateway.

```yaml
apiVersion: ipam.cluster.x-k8s.io/v1alpha1
kind: InClusterIPPool
metadata:
name: inclusterippool-sample
spec:
subnet: 10.0.0.0/24
gateway: 10.0.0.1
```
```yaml
apiVersion: ipam.cluster.x-k8s.io/v1alpha1
kind: InClusterIPPool
metadata:
name: inclusterippool-sample
spec:
first: 10.0.0.10
last: 10.10.0.42
prefix: 24
gateway: 10.0.0.1
```
```yaml
apiVersion: ipam.cluster.x-k8s.io/v1alpha1
apiVersion: ipam.cluster.x-k8s.io/v1alpha2
kind: InClusterIPPool
metadata:
name: inclusterippool-sample
Expand Down
59 changes: 59 additions & 0 deletions api/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2023 Deutsche Telekom AG.
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.
*/

package v1alpha1

import (
"fmt"
"net/netip"

"go4.org/netipx"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/util/validation/field"

v1alpha2 "github.com/telekom/cluster-api-ipam-provider-in-cluster/api/v1alpha2"
)

func Convert_v1alpha1_InClusterIPPoolSpec_To_v1alpha2_InClusterIPPoolSpec(in *InClusterIPPoolSpec, out *v1alpha2.InClusterIPPoolSpec, scope conversion.Scope) error {
out.Gateway = in.Gateway
out.Prefix = in.Prefix
out.Addresses = in.Addresses

if in.Subnet != "" {
prefix, err := netip.ParsePrefix(in.Subnet)
if err != nil {
return field.Invalid(field.NewPath("spec", "subnet"), in.Subnet, err.Error())
}

prefixRange := netipx.RangeOfPrefix(prefix)
if in.First == "" {
in.First = prefixRange.From().Next().String() // omits the first address, the assumed gateway
}
if in.Last == "" {
in.Last = prefixRange.To().Prev().String() // omits the last address, the assumed broadcast
}
if in.Prefix == 0 {
in.Prefix = prefix.Bits()
out.Prefix = prefix.Bits()
}
}

if in.First != "" && in.Last != "" {
out.Addresses = append(out.Addresses, fmt.Sprintf("%s-%s", in.First, in.Last))
}

return nil
}
Loading

0 comments on commit c8d841c

Please sign in to comment.