Skip to content

Commit ce930f7

Browse files
MaIT-HgAHoanganh.Mai
authored andcommitted
add tenancy checks for ip address and prefix
2 parents ecbbec2 + 110cbb7 commit ce930f7

File tree

8 files changed

+64
-48
lines changed

8 files changed

+64
-48
lines changed

.github/ISSUE_TEMPLATE/bug-report.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ body:
5050
id: config
5151
attributes:
5252
label: Netbox operator configuration (command line flags or environment variables)
53-
value: |
54-
# paste your configuration here
53+
description: Please copy and paste your configuration here.
54+
render: Shell
5555

5656
- type: textarea
5757
id: logs

.github/workflows/build-image.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ jobs:
3535
with:
3636
images: ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
3737
tags: |
38-
# for commits on the main branch only, we will generate the tag named `latest`
38+
# (for commits on the main branch only) generate a tag named `latest`
3939
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
40-
# the tag named sha-[short sha value] will be generated in all cases
40+
# (for all commits) generate a tag named sha-[short sha value]
4141
type=sha,enable=true
42+
# (for tagged commits only) generate tags identical to the git tag version, with and without the leading v
43+
type=semver,pattern={{raw}},enable=${{startsWith(github.ref, 'refs/tags/v')}}
44+
type=semver,pattern={{version}},enable=${{startsWith(github.ref, 'refs/tags/v')}}
4245
- name: Build and push
4346
uses: docker/build-push-action@v6
4447
with:
4548
platforms: linux/amd64,linux/arm64
46-
push: ${{ github.ref == format('refs/heads/{0}', 'main') }}
49+
# we push only if the pipeline is run against the commits on main branch or a tag
50+
push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }}
4751
tags: ${{ steps.meta.outputs.tags }}
4852
labels: ${{ steps.meta.outputs.labels }}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ require (
7070
github.com/opentracing/opentracing-go v1.2.0 // indirect
7171
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
7272
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
73-
github.com/prometheus/client_golang v1.20.3 // indirect
73+
github.com/prometheus/client_golang v1.20.4 // indirect
7474
github.com/prometheus/client_model v0.6.1 // indirect
7575
github.com/prometheus/common v0.59.1 // indirect
7676
github.com/prometheus/procfs v0.15.1 // indirect
@@ -82,7 +82,7 @@ require (
8282
github.com/spf13/pflag v1.0.5 // indirect
8383
github.com/subosito/gotenv v1.6.0 // indirect
8484
github.com/x448/float16 v0.8.4 // indirect
85-
go.mongodb.org/mongo-driver v1.16.1 // indirect
85+
go.mongodb.org/mongo-driver v1.17.0 // indirect
8686
go.opentelemetry.io/otel v1.30.0 // indirect
8787
go.opentelemetry.io/otel/metric v1.30.0 // indirect
8888
go.opentelemetry.io/otel/trace v1.30.0 // indirect
@@ -96,7 +96,7 @@ require (
9696
golang.org/x/term v0.24.0 // indirect
9797
golang.org/x/text v0.18.0 // indirect
9898
golang.org/x/time v0.6.0 // indirect
99-
golang.org/x/tools v0.24.0 // indirect
99+
golang.org/x/tools v0.25.0 // indirect
100100
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
101101
google.golang.org/protobuf v1.34.2 // indirect
102102
gopkg.in/inf.v0 v0.9.1 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
117117
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
118118
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
119119
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
120-
github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4=
121-
github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
120+
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
121+
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
122122
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
123123
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
124124
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
@@ -156,8 +156,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
156156
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
157157
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
158158
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
159-
go.mongodb.org/mongo-driver v1.16.1 h1:rIVLL3q0IHM39dvE+z2ulZLp9ENZKThVfuvN/IiN4l8=
160-
go.mongodb.org/mongo-driver v1.16.1/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
159+
go.mongodb.org/mongo-driver v1.17.0 h1:Hp4q2MCjvY19ViwimTs00wHi7G4yzxh4/2+nTx8r40k=
160+
go.mongodb.org/mongo-driver v1.17.0/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4=
161161
go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts=
162162
go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc=
163163
go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w=
@@ -212,8 +212,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
212212
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
213213
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
214214
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
215-
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
216-
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
215+
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
216+
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
217217
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
218218
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
219219
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/controller/ipaddress_controller.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,6 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
101101
return ctrl.Result{}, nil
102102
}
103103

104-
// if PreserveIpInNetbox flag is false then register finalizer if not yet registered
105-
if !o.Spec.PreserveInNetbox && !controllerutil.ContainsFinalizer(o, IpAddressFinalizerName) {
106-
debugLogger.Info("adding the finalizer")
107-
controllerutil.AddFinalizer(o, IpAddressFinalizerName)
108-
if err := r.Update(ctx, o); err != nil {
109-
return ctrl.Result{}, err
110-
}
111-
}
112-
113104
// 1. try to lock lease of parent prefix if IpAddress status condition is not true
114105
// and IpAddress is owned by an IpAddressClaim
115106
or := o.ObjectMeta.OwnerReferences
@@ -170,6 +161,19 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
170161
"after reservation of ip in netbox failed: %w", updateStatusErr, err)
171162
}
172163

164+
// 3. unlock lease of parent prefix
165+
if ll != nil {
166+
ll.Unlock()
167+
}
168+
169+
// 4. update status fields
170+
o.Status.IpAddressId = netboxIpAddressModel.ID
171+
o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10)
172+
err = r.Client.Status().Update(ctx, o)
173+
if err != nil {
174+
return ctrl.Result{}, err
175+
}
176+
173177
// update lastIpAddressMetadata annotation
174178
if annotations == nil {
175179
annotations = make(map[string]string)
@@ -202,16 +206,17 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
202206
r.Recorder.Event(o, corev1.EventTypeWarning, "IpDescriptionTruncated", "ip address was created with truncated description")
203207
}
204208

205-
debugLogger.Info(fmt.Sprintf("reserved ip address in netbox, ip: %s", o.Spec.IpAddress))
206-
207-
// 3. unlock lease of parent prefix
208-
if ll != nil {
209-
ll.Unlock()
209+
// if PreserveIpInNetbox flag is false then register finalizer if not yet registered
210+
if !o.Spec.PreserveInNetbox && !controllerutil.ContainsFinalizer(o, IpAddressFinalizerName) {
211+
debugLogger.Info("adding the finalizer")
212+
controllerutil.AddFinalizer(o, IpAddressFinalizerName)
213+
if err := r.Update(ctx, o); err != nil {
214+
return ctrl.Result{}, err
215+
}
210216
}
211217

212-
// 4. update status conditions
213-
o.Status.IpAddressId = netboxIpAddressModel.ID
214-
o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10)
218+
debugLogger.Info(fmt.Sprintf("reserved ip address in netbox, ip: %s", o.Spec.IpAddress))
219+
215220
err = r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyTrue, corev1.EventTypeNormal, "")
216221
if err != nil {
217222
return ctrl.Result{}, err

internal/controller/prefix_controller.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,6 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
9999
return ctrl.Result{}, nil
100100
}
101101

102-
// register finalizer if not yet registered
103-
if !prefix.Spec.PreserveInNetbox && !controllerutil.ContainsFinalizer(prefix, PrefixFinalizerName) {
104-
debugLogger.Info("adding the finalizer")
105-
controllerutil.AddFinalizer(prefix, PrefixFinalizerName)
106-
if err := r.Update(ctx, prefix); err != nil {
107-
return ctrl.Result{}, err
108-
}
109-
}
110-
111102
/*
112103
1. try to lock the lease of the parent prefix if all of the following conditions are met
113104
- the prefix is owned by at least 1 prefixClaim
@@ -168,6 +159,19 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
168159
return ctrl.Result{}, fmt.Errorf("failed at update prefix status: %w, "+"after reservation of prefix in netbox failed: %w", updateStatusErr, err)
169160
}
170161

162+
/* 3. unlock lease of parent prefix */
163+
if ll != nil {
164+
ll.Unlock()
165+
}
166+
167+
/* 4. update status fields */
168+
prefix.Status.PrefixId = netboxPrefixModel.ID
169+
prefix.Status.PrefixUrl = config.GetBaseUrl() + "/ipam/prefixes/" + strconv.FormatInt(netboxPrefixModel.ID, 10)
170+
err = r.Client.Status().Update(ctx, prefix)
171+
if err != nil {
172+
return ctrl.Result{}, err
173+
}
174+
171175
// update lastPrefixMetadata annotation
172176
if annotations == nil {
173177
annotations = make(map[string]string)
@@ -197,16 +201,17 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
197201
r.Recorder.Event(prefix, corev1.EventTypeWarning, "PrefixDescriptionTruncated", "prefix was created with truncated description")
198202
}
199203

200-
debugLogger.Info(fmt.Sprintf("reserved prefix in netbox, prefix: %s", prefix.Spec.Prefix))
201-
202-
/* 3. unlock lease of parent prefix */
203-
if ll != nil {
204-
ll.Unlock()
204+
// register finalizer if not yet registered
205+
if !prefix.Spec.PreserveInNetbox && !controllerutil.ContainsFinalizer(prefix, PrefixFinalizerName) {
206+
debugLogger.Info("adding the finalizer")
207+
controllerutil.AddFinalizer(prefix, PrefixFinalizerName)
208+
if err := r.Update(ctx, prefix); err != nil {
209+
return ctrl.Result{}, err
210+
}
205211
}
206212

207-
/* 4. update status conditions */
208-
prefix.Status.PrefixId = netboxPrefixModel.ID
209-
prefix.Status.PrefixUrl = config.GetBaseUrl() + "/ipam/prefixes/" + strconv.FormatInt(netboxPrefixModel.ID, 10)
213+
debugLogger.Info(fmt.Sprintf("reserved prefix in netbox, prefix: %s", prefix.Spec.Prefix))
214+
210215
if err = r.SetConditionAndCreateEvent(ctx, prefix, netboxv1.ConditionPrefixReadyTrue, corev1.EventTypeNormal, ""); err != nil {
211216
return ctrl.Result{}, err
212217
}

pkg/netbox/api/ip_address_claim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (r *NetboxClient) RestoreExistingIpByHash(customFieldName string, hash stri
5858

5959
// GetAvailableIpAddressByClaim searches an available IpAddress in Netbox matching IpAddressClaim requirements
6060
func (r *NetboxClient) GetAvailableIpAddressByClaim(ipAddressClaim *models.IPAddressClaim) (*models.IPAddress, error) {
61+
// Note: the check passes for empty tenant and not for non-existing tenants
6162
_, err := r.GetTenantDetails(ipAddressClaim.Metadata.Tenant)
6263
if err != nil {
6364
return nil, err

pkg/netbox/api/prefix_claim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func isRequestingTheEntireParentPrefix(prefixClaim *models.PrefixClaim) (bool, e
6767

6868
// GetAvailablePrefixByClaim searches an available Prefix in Netbox matching PrefixClaim requirements
6969
func (r *NetboxClient) GetAvailablePrefixByClaim(prefixClaim *models.PrefixClaim) (*models.Prefix, error) {
70+
// Note: the check passes for empty tenant and not for non-existing tenants
7071
_, err := r.GetTenantDetails(prefixClaim.Metadata.Tenant)
7172
if err != nil {
7273
return nil, err

0 commit comments

Comments
 (0)