Skip to content

Commit

Permalink
Revert "undo import renaming"
Browse files Browse the repository at this point in the history
This reverts commit ddf013e.
  • Loading branch information
faebr authored and Hoanganh.Mai committed Oct 9, 2024
1 parent 6b5b79c commit a206a4e
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 95 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
ipamv1 "github.com/netbox-community/netbox-operator/api/v1"
"github.com/netbox-community/netbox-operator/internal/controller"
//+kubebuilder:scaffold:imports
)
Expand All @@ -53,7 +53,7 @@ var (
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(netboxv1.AddToScheme(scheme))
utilruntime.Must(ipamv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down
18 changes: 9 additions & 9 deletions internal/controller/ipaddress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"
"time"

netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
ipamv1 "github.com/netbox-community/netbox-operator/api/v1"
"github.com/netbox-community/netbox-operator/pkg/config"
"github.com/netbox-community/netbox-operator/pkg/netbox/api"
"github.com/netbox-community/netbox-operator/pkg/netbox/models"
Expand Down Expand Up @@ -69,7 +69,7 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

logger.Info("reconcile loop started")

o := &netboxv1.IpAddress{}
o := &ipamv1.IpAddress{}
err := r.Client.Get(ctx, req.NamespacedName, o)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand All @@ -81,7 +81,7 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if !o.Spec.PreserveInNetbox {
err := r.NetboxClient.DeleteIpAddress(o.Status.IpAddressId)
if err != nil {
setConditionErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyFalseDeletionFailed, corev1.EventTypeWarning, err.Error())
setConditionErr := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpaddressReadyFalseDeletionFailed, corev1.EventTypeWarning, err.Error())
if setConditionErr != nil {
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when deleting IPAddress failed: %w", setConditionErr, err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
Name: or[0].Name,
Namespace: req.Namespace,
}
ipAddressClaim := &netboxv1.IpAddressClaim{}
ipAddressClaim := &ipamv1.IpAddressClaim{}
err = r.Client.Get(ctx, orLookupKey, ipAddressClaim)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -171,7 +171,7 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

netboxIpAddressModel, err := r.NetboxClient.ReserveOrUpdateIpAddress(ipAddressModel)
if err != nil {
updateStatusErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyFalse,
updateStatusErr := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpaddressReadyFalse,
corev1.EventTypeWarning, o.Spec.IpAddress)
return ctrl.Result{}, fmt.Errorf("failed to update ip address status: %w, "+
"after reservation of ip in netbox failed: %w", updateStatusErr, err)
Expand Down Expand Up @@ -232,7 +232,7 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
// 4. update status conditions
o.Status.IpAddressId = netboxIpAddressModel.ID
o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10)
err = r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyTrue, corev1.EventTypeNormal, "")
err = r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpaddressReadyTrue, corev1.EventTypeNormal, "")
if err != nil {
return ctrl.Result{}, err
}
Expand All @@ -245,11 +245,11 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
// SetupWithManager sets up the controller with the Manager.
func (r *IpAddressReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&netboxv1.IpAddress{}).
For(&ipamv1.IpAddress{}).
Complete(r)
}

func (r *IpAddressReconciler) SetConditionAndCreateEvent(ctx context.Context, o *netboxv1.IpAddress, condition metav1.Condition, eventType string, conditionMessageAppend string) error {
func (r *IpAddressReconciler) SetConditionAndCreateEvent(ctx context.Context, o *ipamv1.IpAddress, condition metav1.Condition, eventType string, conditionMessageAppend string) error {
if len(conditionMessageAppend) > 0 {
condition.Message = condition.Message + ". " + conditionMessageAppend
}
Expand All @@ -264,7 +264,7 @@ func (r *IpAddressReconciler) SetConditionAndCreateEvent(ctx context.Context, o
return nil
}

func generateNetboxIpAddressModelFromIpAddressSpec(spec *netboxv1.IpAddressSpec, req ctrl.Request, lastIpAddressMetadata string) (*models.IPAddress, error) {
func generateNetboxIpAddressModelFromIpAddressSpec(spec *ipamv1.IpAddressSpec, req ctrl.Request, lastIpAddressMetadata string) (*models.IPAddress, error) {
// unmarshal lastIpAddressMetadata json string to map[string]string
lastAppliedCustomFields := make(map[string]string)
if lastIpAddressMetadata != "" {
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/ipaddress_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
ipamv1 "github.com/netbox-community/netbox-operator/api/v1"
)

var _ = Describe("IpAddress Controller", Ordered, func() {
Expand All @@ -48,11 +48,11 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
})

DescribeTable("Reconciler (ip address CR without owner reference)", func(
cr *netboxv1.IpAddress, // our CR as typed object
cr *ipamv1.IpAddress, // our CR as typed object
IpamMocksIpAddress []func(*mock_interfaces.MockIpamInterface, chan error),
TenancyMocks []func(*mock_interfaces.MockTenancyInterface, chan error),
expectedConditionReady bool, // Expected state of the ConditionReady condition
expectedCRStatus netboxv1.IpAddressStatus, // Expected status of the CR
expectedCRStatus ipamv1.IpAddressStatus, // Expected status of the CR
) {
By("Setting up mocks")
for _, mock := range IpamMocksIpAddress {
Expand Down Expand Up @@ -82,7 +82,7 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
Eventually(k8sClient.Create(ctx, cr), timeout, interval).Should(Succeed())

// check that reconcile loop did run a least once by checking that conditions are set
createdCR := &netboxv1.IpAddress{}
createdCR := &ipamv1.IpAddress{}
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: cr.GetNamespace()}, createdCR)
return err == nil && len(createdCR.Status.Conditions) > 0
Expand All @@ -92,7 +92,7 @@ var _ = Describe("IpAddress Controller", Ordered, func() {
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: cr.GetNamespace()}, createdCR)
return err == nil &&
apismeta.IsStatusConditionTrue(createdCR.Status.Conditions, netboxv1.ConditionIpaddressReadyTrue.Type) == expectedConditionReady
apismeta.IsStatusConditionTrue(createdCR.Status.Conditions, ipamv1.ConditionIpaddressReadyTrue.Type) == expectedConditionReady
}, timeout, interval).Should(BeTrue())

// Check that the expected ip address is present in the status
Expand Down
24 changes: 12 additions & 12 deletions internal/controller/ipaddressclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
"time"

netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
ipamv1 "github.com/netbox-community/netbox-operator/api/v1"
"github.com/netbox-community/netbox-operator/pkg/config"
"github.com/netbox-community/netbox-operator/pkg/netbox/api"
"github.com/netbox-community/netbox-operator/pkg/netbox/models"
Expand Down Expand Up @@ -65,7 +65,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
logger.Info("reconcile loop started")

/* 0. check if the matching IpAddressClaim object exists */
o := &netboxv1.IpAddressClaim{}
o := &ipamv1.IpAddressClaim{}
err := r.Client.Get(ctx, req.NamespacedName, o)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand All @@ -78,7 +78,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}

// 1. check if matching IpAddress object already exists
ipAddress := &netboxv1.IpAddress{}
ipAddress := &ipamv1.IpAddress{}
ipAddressName := o.ObjectMeta.Name
ipAddressLookupKey := types.NamespacedName{
Name: ipAddressName,
Expand Down Expand Up @@ -124,7 +124,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
h := generateIpAddressRestorationHash(o)
ipAddressModel, err := r.NetboxClient.RestoreExistingIpByHash(config.GetOperatorConfig().NetboxRestorationHashFieldName, h)
if err != nil {
if setConditionErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpAssignedFalse, corev1.EventTypeWarning, err.Error()); setConditionErr != nil {
if setConditionErr := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpAssignedFalse, corev1.EventTypeWarning, err.Error()); setConditionErr != nil {
return ctrl.Result{}, fmt.Errorf("error updating status: %w, looking up ip by hash failed: %w", setConditionErr, err)
}
return ctrl.Result{Requeue: true}, nil
Expand All @@ -141,7 +141,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
},
})
if err != nil {
if setConditionErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpAssignedFalse, corev1.EventTypeWarning, err.Error()); setConditionErr != nil {
if setConditionErr := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpAssignedFalse, corev1.EventTypeWarning, err.Error()); setConditionErr != nil {
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when assignment of ip address failed: %w", setConditionErr, err)
}
return ctrl.Result{Requeue: true}, nil
Expand All @@ -162,14 +162,14 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque

err = r.Client.Create(ctx, ipAddressResource)
if err != nil {
setConditionErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpAssignedFalse, corev1.EventTypeWarning, "")
setConditionErr := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpAssignedFalse, corev1.EventTypeWarning, "")
if setConditionErr != nil {
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when creation of ip address object failed: %w", setConditionErr, err)
}
return ctrl.Result{}, err
}

err = r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpAssignedTrue, corev1.EventTypeNormal, "")
err = r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpAssignedTrue, corev1.EventTypeNormal, "")
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -204,13 +204,13 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
o.Status.IpAddress = ipAddress.Spec.IpAddress
o.Status.IpAddressDotDecimal = strings.Split(ipAddress.Spec.IpAddress, "/")[0]
o.Status.IpAddressName = ipAddress.Name
err := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpClaimReadyTrue, corev1.EventTypeNormal, "")
err := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpClaimReadyTrue, corev1.EventTypeNormal, "")
if err != nil {
return ctrl.Result{}, err
}
} else {
debugLogger.Info("ipaddress status ready false")
err := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpClaimReadyFalse, corev1.EventTypeWarning, "")
err := r.SetConditionAndCreateEvent(ctx, o, ipamv1.ConditionIpClaimReadyFalse, corev1.EventTypeWarning, "")
if err != nil {
return ctrl.Result{}, err
}
Expand All @@ -224,13 +224,13 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// SetupWithManager sets up the controller with the Manager.
func (r *IpAddressClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&netboxv1.IpAddressClaim{}).
Owns(&netboxv1.IpAddress{}).
For(&ipamv1.IpAddressClaim{}).
Owns(&ipamv1.IpAddress{}).
Complete(r)
}

// SetConditionAndCreateEvent updates the condition and creates a log entry and event for this condition change
func (r *IpAddressClaimReconciler) SetConditionAndCreateEvent(ctx context.Context, o *netboxv1.IpAddressClaim, condition metav1.Condition, eventType string, conditionMessageAppend string) error {
func (r *IpAddressClaimReconciler) SetConditionAndCreateEvent(ctx context.Context, o *ipamv1.IpAddressClaim, condition metav1.Condition, eventType string, conditionMessageAppend string) error {
if len(conditionMessageAppend) > 0 {
condition.Message = condition.Message + ". " + conditionMessageAppend
}
Expand Down
20 changes: 10 additions & 10 deletions internal/controller/ipaddressclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
apismeta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/types"

netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
ipamv1 "github.com/netbox-community/netbox-operator/api/v1"
)

var _ = Describe("IpAddressClaim Controller", Ordered, func() {
Expand All @@ -54,15 +54,15 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
})

DescribeTable("Reconciler (ip address claim CR)", func(
cr *netboxv1.IpAddressClaim, // our CR as typed object
ipcr *netboxv1.IpAddress, // ip address CR expected to be created by ip address claim controller
ipcrMockStatus netboxv1.IpAddressStatus, // the that will be added to mock the ip address controller
cr *ipamv1.IpAddressClaim, // our CR as typed object
ipcr *ipamv1.IpAddress, // ip address CR expected to be created by ip address claim controller
ipcrMockStatus ipamv1.IpAddressStatus, // the that will be added to mock the ip address controller
IpamMocksIpAddressClaim []func(*mock_interfaces.MockIpamInterface, chan error),
IpamMocksIpAddress []func(*mock_interfaces.MockIpamInterface, chan error),
TenancyMocks []func(*mock_interfaces.MockTenancyInterface, chan error),
expectedConditionReady bool, // Expected state of the ConditionReady condition
expectedConditionIpAssigned bool, // Expected state of the ConditionReady condition
expectedCRStatus netboxv1.IpAddressClaimStatus, // Expected status of the CR
expectedCRStatus ipamv1.IpAddressClaimStatus, // Expected status of the CR
prefixLockedByOtherOwner bool, // If prefix is locked by other owner when ipaddress claim CR is created
) {
By("Setting up mocks")
Expand Down Expand Up @@ -110,7 +110,7 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
Eventually(k8sClient.Create(ctx, cr), timeout, interval).Should(Succeed())

// check that ip address claim CR was created
createdCR := &netboxv1.IpAddressClaim{}
createdCR := &ipamv1.IpAddressClaim{}
Eventually(func() bool {
// the created ip address CR has the same namespacedname as the ip address claim CR
err := k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: cr.GetNamespace()}, createdCR)
Expand All @@ -121,10 +121,10 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: cr.GetNamespace()}, createdCR)
return err == nil &&
apismeta.IsStatusConditionTrue(createdCR.Status.Conditions, netboxv1.ConditionIpAssignedFalse.Type) == expectedConditionIpAssigned
apismeta.IsStatusConditionTrue(createdCR.Status.Conditions, ipamv1.ConditionIpAssignedFalse.Type) == expectedConditionIpAssigned
}, timeout, interval).Should(BeTrue())

createdIpCR := &netboxv1.IpAddress{}
createdIpCR := &ipamv1.IpAddress{}
if expectedConditionIpAssigned {
// check that ip address CR was created
Eventually(func() bool {
Expand All @@ -141,7 +141,7 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: cr.GetName(), Namespace: cr.GetNamespace()}, createdCR)
return err == nil &&
apismeta.IsStatusConditionTrue(createdCR.Status.Conditions, netboxv1.ConditionIpClaimReadyTrue.Type) == expectedConditionReady
apismeta.IsStatusConditionTrue(createdCR.Status.Conditions, ipamv1.ConditionIpClaimReadyTrue.Type) == expectedConditionReady
}, timeout, interval).Should(BeTrue())

// Check that the expected ip address is present in the status
Expand Down Expand Up @@ -202,6 +202,6 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
nil,
nil,
nil,
false, false, netboxv1.IpAddressClaimStatus{}, true),
false, false, ipamv1.IpAddressClaimStatus{}, true),
)
})
12 changes: 6 additions & 6 deletions internal/controller/ipaddressclaim_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"fmt"

"github.com/go-logr/logr"
netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
ipamv1 "github.com/netbox-community/netbox-operator/api/v1"
"github.com/netbox-community/netbox-operator/pkg/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func generateIpAddressFromIpAddressClaim(claim *netboxv1.IpAddressClaim, ip string, logger logr.Logger) *netboxv1.IpAddress {
ipAddressResource := &netboxv1.IpAddress{
func generateIpAddressFromIpAddressClaim(claim *ipamv1.IpAddressClaim, ip string, logger logr.Logger) *ipamv1.IpAddress {
ipAddressResource := &ipamv1.IpAddress{
ObjectMeta: metav1.ObjectMeta{
Name: claim.Name,
Namespace: claim.ObjectMeta.Namespace,
Expand All @@ -37,7 +37,7 @@ func generateIpAddressFromIpAddressClaim(claim *netboxv1.IpAddressClaim, ip stri
return ipAddressResource
}

func generateIpAddressSpec(claim *netboxv1.IpAddressClaim, ip string, logger logr.Logger) netboxv1.IpAddressSpec {
func generateIpAddressSpec(claim *ipamv1.IpAddressClaim, ip string, logger logr.Logger) ipamv1.IpAddressSpec {
// log a warning if the netboxOperatorRestorationHash name is a key in the customFields map of the IpAddressClaim
_, ok := claim.Spec.CustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName]
if ok {
Expand All @@ -52,7 +52,7 @@ func generateIpAddressSpec(claim *netboxv1.IpAddressClaim, ip string, logger log

customFields[config.GetOperatorConfig().NetboxRestorationHashFieldName] = generateIpAddressRestorationHash(claim)

return netboxv1.IpAddressSpec{
return ipamv1.IpAddressSpec{
IpAddress: ip,
Tenant: claim.Spec.Tenant,
CustomFields: customFields,
Expand All @@ -62,7 +62,7 @@ func generateIpAddressSpec(claim *netboxv1.IpAddressClaim, ip string, logger log
}
}

func generateIpAddressRestorationHash(claim *netboxv1.IpAddressClaim) string {
func generateIpAddressRestorationHash(claim *ipamv1.IpAddressClaim) string {
rd := IpAddressClaimRestorationData{
Namespace: claim.Namespace,
Name: claim.Name,
Expand Down
Loading

0 comments on commit a206a4e

Please sign in to comment.