Skip to content

Commit

Permalink
Merge pull request #123 from adobley/remove-old-pool-fields
Browse files Browse the repository at this point in the history
Remove subnet, first and last from the pool spec and create v1alpha2 Pool
  • Loading branch information
k8s-ci-robot authored Jun 26, 2023
2 parents 3bd67fa + e14ee8b commit 157ecc2
Show file tree
Hide file tree
Showing 39 changed files with 1,803 additions and 581 deletions.
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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/sigs.k8s.io/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 @@ -59,8 +64,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 @@ -192,4 +200,9 @@ go-licenses:

.PHONY: verify-boilerplate
verify-boilerplate: ## Verifies all sources have appropriate boilerplate
./hack/verify-boilerplate.sh
./hack/verify-boilerplate.sh

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: sigs.k8s.io/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: sigs.k8s.io/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: sigs.k8s.io/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: sigs.k8s.io/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 The Kubernetes 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.
*/

package v1alpha1

import (
"fmt"
"net/netip"

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

v1alpha2 "sigs.k8s.io/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 network address
}
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 157ecc2

Please sign in to comment.