Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Make ip-address-manager an IPAM provider for CAPI #692

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ linters-settings:
- pkg: sigs.k8s.io/cluster-api/api/v1beta1
alias: clusterv1
# IPAM
- pkg: github.com/metal3-io/ip-address-manager/api/v1alpha1
alias: ipamv1
- pkg: github.com/metal3-io/ip-address-manager/api/v1alpha2
alias: ipamv2
nolintlint:
allow-unused: false
allow-leading-space: false
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ARG BUILD_IMAGE=docker.io/golang:1.22.6@sha256:d5e49f92b9566b0ddfc59a0d9d85cd8a8
ARG BASE_IMAGE=gcr.io/distroless/static:nonroot@sha256:9ecc53c269509f63c69a266168e4a687c7eb8c0cfd753bd8bfcaa4f58a90876f

# Build the manager binary on golang image
FROM $BUILD_IMAGE as builder
FROM $BUILD_IMAGE AS builder
WORKDIR /workspace

# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Deploys IPAM CRDs and deploys IPAM controllers
Runs IPAM controller locally

```sh
kubectl scale -n capm3-system \
kubectl scale -n ipam-system \
deployment.v1.apps/metal3-ipam-controller-manager --replicas 0
make run
```
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ limitations under the License.
// +k8s:defaulter-gen=TypeMeta
// +kubebuilder:object:generate=true
// +groupName=ipam.metal3.io
// +k8s:conversion-gen=github.com/metal3-io/ip-address-manager/api/v1alpha2
package v1alpha1
1 change: 0 additions & 1 deletion api/v1alpha1/ipaddress_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type IPAddressSpec struct {
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:path=ipaddresses,scope=Namespaced,categories=metal3,shortName=ipa;ipaddress;m3ipa;m3ipaddress;m3ipaddresses;metal3ipa;metal3ipaddress;metal3ipaddresses
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Metal3IPAddress"
// +kubebuilder:storageversion
// +kubebuilder:object:root=true
// IPAddress is the Schema for the ipaddresses API.
type IPAddress struct {
Expand Down
1 change: 0 additions & 1 deletion api/v1alpha1/ipclaim_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ type IPClaimStatus struct {

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:path=ipclaims,scope=Namespaced,categories=cluster-api,shortName=ipc;ipclaim;m3ipc;m3ipclaim;m3ipclaims;metal3ipc;metal3ipclaim;metal3ipclaims
// +kubebuilder:storageversion
// +kubebuilder:subresource:status
// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Metal3IPClaim"
Expand Down
1 change: 0 additions & 1 deletion api/v1alpha1/ippool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ type IPPoolStatus struct {

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:path=ippools,scope=Namespaced,categories=cluster-api,shortName=ipp;ippool;m3ipp;m3ippool;m3ippools;metal3ipp;metal3ippool;metal3ippools
// +kubebuilder:storageversion
// +kubebuilder:subresource:status
// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.labels.cluster\\.x-k8s\\.io/cluster-name",description="Cluster to which this template belongs"
Expand Down
19 changes: 19 additions & 0 deletions api/v1alpha2/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2019 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 v1alpha2

func (*IPPool) Hub() {}
23 changes: 23 additions & 0 deletions api/v1alpha2/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2019 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 v1alpha2 contains API Schema definitions for the metal3 v1alpha2 API group
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:defaulter-gen=TypeMeta
// +kubebuilder:object:generate=true
// +groupName=ipam.metal3.io
package v1alpha2
53 changes: 53 additions & 0 deletions api/v1alpha2/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2019 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 v1alpha2 contains API Schema definitions for the infrastructure v1alpha2 API group
// +kubebuilder:object:generate=true
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:defaulter-gen=TypeMeta
// +groupName=ipam.metal3.io
package v1alpha2

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var (
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "ipam.metal3.io", Version: "v1alpha2"}

/// schemeBuilder is used to add go types to the GroupVersionKind scheme.
schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = schemeBuilder.AddToScheme

objectTypes = []runtime.Object{}
)

func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(GroupVersion, objectTypes...)
metav1.AddToGroupVersion(scheme, GroupVersion)
return nil
}

// Resource is required by pkg/client/listers/...
// func Resource(resource string) schema.GroupResource {
// return SchemeGroupVersion.WithResource(resource).GroupResource()
// }
77 changes: 77 additions & 0 deletions api/v1alpha2/ipaddress_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2019 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 v1alpha2

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// DataFinalizer allows IPAddressReconciler to clean up resources
// associated with IPAddress before removing it from the apiserver.
IPAddressFinalizer = "ipaddress.ipam.metal3.io"
)

// IPAddressSpec defines the desired state of IPAddress.
type IPAddressSpec struct {

// Claim points to the object the IPClaim was created for.
Claim corev1.ObjectReference `json:"claim"`

// Pool is the IPPool this was generated from.
Pool corev1.ObjectReference `json:"pool"`

// +kubebuilder:validation:Maximum=128
// Prefix is the mask of the network as integer (max 128)
Prefix int `json:"prefix,omitempty"`

// Gateway is the gateway ip address
Gateway *string `json:"gateway,omitempty"`

// Address contains the IP address
Address string `json:"address"`

// DNSServers is the list of dns servers
DNSServers []string `json:"dnsServers,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:path=ipaddresses,scope=Namespaced,categories=metal3,shortName=ipa;ipaddress;m3ipa;m3ipaddress;m3ipaddresses;metal3ipa;metal3ipaddress;metal3ipaddresses
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Metal3IPAddress"
// +kubebuilder:storageversion
// +kubebuilder:object:root=true
// IPAddress is the Schema for the ipaddresses API.
type IPAddress struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec IPAddressSpec `json:"spec,omitempty"`
}

// +kubebuilder:object:root=true

// IPAddressList contains a list of IPAddress.
type IPAddressList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []IPAddress `json:"items"`
}

func init() {
objectTypes = append(objectTypes, &IPAddress{}, &IPAddressList{})
}
159 changes: 159 additions & 0 deletions api/v1alpha2/ipaddress_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
Copyright 2020 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 v1alpha2

import (
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (c *IPAddress) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(c).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-ipam-metal3-io-v1alpha2-ipaddress,mutating=false,failurePolicy=fail,groups=ipam.metal3.io,resources=ipaddresses,versions=v1alpha2,name=validation.ipaddress.ipam.metal3.io,matchPolicy=Equivalent,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-ipam-metal3-io-v1alpha2-ipaddress,mutating=true,failurePolicy=fail,groups=ipam.metal3.io,resources=ipaddresses,versions=v1alpha2,name=default.ipaddress.ipam.metal3.io,matchPolicy=Equivalent,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Defaulter = &IPAddress{}
var _ webhook.Validator = &IPAddress{}

func (c *IPAddress) Default() {
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (c *IPAddress) ValidateCreate() (admission.Warnings, error) {
allErrs := field.ErrorList{}
if c.Spec.Pool.Name == "" {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "pool", "name"),
c.Spec.Pool.Name,
"cannot be empty",
),
)
}

if c.Spec.Claim.Name == "" {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "claim", "name"),
c.Spec.Claim.Name,
"cannot be empty",
),
)
}

if c.Spec.Address == "" {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "address"),
c.Spec.Address,
"cannot be empty",
),
)
}

if len(allErrs) == 0 {
return nil, nil
}
return nil, apierrors.NewInvalid(GroupVersion.WithKind("IPAddress").GroupKind(), c.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (c *IPAddress) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
allErrs := field.ErrorList{}
oldIPAddress, ok := old.(*IPAddress)
if !ok || oldIPAddress == nil {
return nil, apierrors.NewInternalError(errors.New("unable to convert existing object"))
}

if c.Spec.Address != oldIPAddress.Spec.Address {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "address"),
c.Spec.Address,
"cannot be modified",
),
)
}

if c.Spec.Pool.Name != oldIPAddress.Spec.Pool.Name {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "pool"),
c.Spec.Pool,
"cannot be modified",
),
)
} else if c.Spec.Pool.Namespace != oldIPAddress.Spec.Pool.Namespace {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "pool"),
c.Spec.Pool,
"cannot be modified",
),
)
} else if c.Spec.Pool.Kind != oldIPAddress.Spec.Pool.Kind {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "pool"),
c.Spec.Pool,
"cannot be modified",
),
)
}

if c.Spec.Claim.Name != oldIPAddress.Spec.Claim.Name {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "claim"),
c.Spec.Claim,
"cannot be modified",
),
)
} else if c.Spec.Claim.Namespace != oldIPAddress.Spec.Claim.Namespace {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "claim"),
c.Spec.Claim,
"cannot be modified",
),
)
} else if c.Spec.Claim.Kind != oldIPAddress.Spec.Claim.Kind {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("spec", "claim"),
c.Spec.Claim,
"cannot be modified",
),
)
}

if len(allErrs) == 0 {
return nil, nil
}
return nil, apierrors.NewInvalid(GroupVersion.WithKind("IPAddress").GroupKind(), c.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *IPAddress) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
Loading
Loading