Skip to content

Commit

Permalink
Add custom DHCPSubnet CRD and controller to gen them per vpcsubnet
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostman committed Dec 7, 2023
1 parent a4f11df commit 83b7e21
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 3 deletions.
8 changes: 8 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,12 @@ resources:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: githedgehog.com
group: dhcp
kind: DHCPSubnet
path: go.githedgehog.com/fabric/api/dhcp/v1alpha2
version: v1alpha2
version: "3"
78 changes: 78 additions & 0 deletions api/dhcp/v1alpha2/dhcpsubnet_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2023 Hedgehog.
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 (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// DHCPSubnetSpec defines the desired state of DHCPSubnet
type DHCPSubnetSpec struct {
Subnet string `json:"subnet"` // e.g. vpc-0/default (vpc name + vpc subnet name)
CIDRBlock string `json:"cidrBlock"` // e.g. 10.10.10.0/24
Gateway string `json:"gateway"` // e.g. 10.10.10.1
StartIP string `json:"startIP"` // e.g. 10.10.10.10
EndIP string `json:"endIP"` // e.g. 10.10.10.99
VRF string `json:"vrf"` // e.g. VrfVvpc-1 as it's named on switch
CircuitID string `json:"circuitID"` // e.g. Vlan1000 as it's named on switch
}

// DHCPSubnetStatus defines the observed state of DHCPSubnet
type DHCPSubnetStatus struct {
AllocatedIPs map[string]DHCPAllocatedIP `json:"allocatedIPs,omitempty"`
}

type DHCPAllocatedIP struct {
Expiry metav1.Time `json:"expiry"`
MAC string `json:"mac"`
Hostname string `json:"hostname"` // from dhcp request
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:categories=hedgehog;fabric,shortName=dhcp
// +kubebuilder:printcolumn:name="Subnet",type=string,JSONPath=`.spec.subnet`,priority=0
// +kubebuilder:printcolumn:name="CIDRBlock",type=string,JSONPath=`.spec.cidrBlock`,priority=0
// +kubebuilder:printcolumn:name="Gateway",type=string,JSONPath=`.spec.gateway`,priority=0
// +kubebuilder:printcolumn:name="StartIP",type=string,JSONPath=`.spec.startIP`,priority=0
// +kubebuilder:printcolumn:name="EndIP",type=string,JSONPath=`.spec.endIP`,priority=0
// +kubebuilder:printcolumn:name="VRF",type=string,JSONPath=`.spec.vrf`,priority=1
// +kubebuilder:printcolumn:name="CircuitID",type=string,JSONPath=`.spec.circuitID`,priority=1
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`,priority=0
// DHCPSubnet is the Schema for the dhcpsubnets API
type DHCPSubnet struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec DHCPSubnetSpec `json:"spec,omitempty"`
Status DHCPSubnetStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&DHCPSubnet{}, &DHCPSubnetList{})
}
36 changes: 36 additions & 0 deletions api/dhcp/v1alpha2/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023 Hedgehog.
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 dhcp v1alpha2 API group
// +kubebuilder:object:generate=true
// +groupName=dhcp.githedgehog.com
package v1alpha2

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "dhcp.githedgehog.com", Version: "v1alpha2"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
138 changes: 138 additions & 0 deletions api/dhcp/v1alpha2/zz_generated.deepcopy.go

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

2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"

agentv1alpha2 "go.githedgehog.com/fabric/api/agent/v1alpha2"
dhcpv1alpha2 "go.githedgehog.com/fabric/api/dhcp/v1alpha2"
vpcv1alpha2 "go.githedgehog.com/fabric/api/vpc/v1alpha2"
wiringv1alpha2 "go.githedgehog.com/fabric/api/wiring/v1alpha2"
agentcontroller "go.githedgehog.com/fabric/pkg/ctrl/agent"
Expand Down Expand Up @@ -68,6 +69,7 @@ func init() {
utilruntime.Must(agentv1alpha2.AddToScheme(scheme))
utilruntime.Must(wiringv1alpha2.AddToScheme(scheme))
utilruntime.Must(vpcv1alpha2.AddToScheme(scheme))
utilruntime.Must(dhcpv1alpha2.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down
Loading

0 comments on commit 83b7e21

Please sign in to comment.