Skip to content

Commit 7a91a35

Browse files
committed
feat: add nautobot service reference
1 parent 0819969 commit 7a91a35

File tree

6 files changed

+89
-7
lines changed

6 files changed

+89
-7
lines changed

go/nautobotop/api/v1alpha1/nautobot_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type NautobotSpec struct {
2828
// +kubebuilder:default=10
2929
SyncIntervalSeconds int `json:"syncIntervalSeconds,omitempty"`
3030
NautobotSecretRef SecretKeySelector `json:"nautobotSecretRef,omitempty"`
31+
NautobotServiceRef ServiceSelector `json:"nautobotServiceRef,omitempty"`
3132
DeviceTypesRef []ConfigMapRef `json:"deviceTypeRef,omitempty"`
3233
}
3334

go/nautobotop/api/v1alpha1/secret_type.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ type SecretKeySelector struct {
2323
// +kubebuilder:validation:Pattern:=^[-._a-zA-Z0-9]+$
2424
Key string `json:"key,omitempty"`
2525
}
26+
27+
type ServiceSelector struct {
28+
// The name of the Service resource being referred to.
29+
// +kubebuilder:validation:MinLength:=1
30+
// +kubebuilder:validation:MaxLength:=253
31+
// +kubebuilder:validation:Pattern:=^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
32+
Name string `json:"name,omitempty"`
33+
34+
// The namespace of the Service resource being referred to.
35+
// Ignored if referent is not cluster-scoped, otherwise defaults to the namespace of the referent.
36+
// +optional
37+
// +kubebuilder:validation:MinLength:=1
38+
// +kubebuilder:validation:MaxLength:=63
39+
// +kubebuilder:validation:Pattern:=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
40+
Namespace *string `json:"namespace,omitempty"`
41+
}

go/nautobotop/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/nautobotop/config/crd/bases/sync.rax.io_nautobots.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ spec:
9393
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
9494
type: string
9595
type: object
96+
nautobotServiceRef:
97+
properties:
98+
name:
99+
description: The name of the Service resource being referred to.
100+
maxLength: 253
101+
minLength: 1
102+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
103+
type: string
104+
namespace:
105+
description: |-
106+
The namespace of the Service resource being referred to.
107+
Ignored if referent is not cluster-scoped, otherwise defaults to the namespace of the referent.
108+
maxLength: 63
109+
minLength: 1
110+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
111+
type: string
112+
type: object
96113
syncIntervalSeconds:
97114
default: 10
98115
type: integer

go/nautobotop/helm/crds/clients.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ spec:
9393
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
9494
type: string
9595
type: object
96+
nautobotServiceRef:
97+
properties:
98+
name:
99+
description: The name of the Service resource being referred to.
100+
maxLength: 253
101+
minLength: 1
102+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
103+
type: string
104+
namespace:
105+
description: |-
106+
The namespace of the Service resource being referred to.
107+
Ignored if referent is not cluster-scoped, otherwise defaults to the namespace of the referent.
108+
maxLength: 63
109+
minLength: 1
110+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
111+
type: string
112+
type: object
96113
syncIntervalSeconds:
97114
default: 10
98115
type: integer

go/nautobotop/internal/controller/nautobot_controller.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ func (r *NautobotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8888
}
8989
}
9090

91-
// Fetch the Nautobot nautobotService to get its ClusterIP
92-
var nautobotService corev1.Service
93-
if err := r.Get(ctx, types.NamespacedName{Namespace: "default", Name: "nautobot-default"}, &nautobotService); err != nil {
94-
log.Error(err, "failed to fetch Service nautobot-default")
95-
return ctrl.Result{}, err
96-
}
97-
9891
// Retrieve the Nautobot authentication token from a secret or external source
9992
nautobotAuthToken, err := r.getAuthTokenFromSecretRef(ctx, nautobotCR)
10093
if err != nil {
10194
log.Error(err, "failed parse find nautoBotAuthToken")
10295
return ctrl.Result{}, err
10396
}
10497

98+
// Fetch the Nautobot service to get its ClusterIP
99+
nautobotService, err := r.getServiceFromServiceRef(ctx, nautobotCR)
100+
if err != nil {
101+
log.Error(err, "failed to fetch Nautobot service")
102+
return ctrl.Result{}, err
103+
}
104+
105105
// Aggregate device type data from all referenced ConfigMaps
106106
deviceTypeMap, err := r.aggregateDeviceTypeDataFromConfigMap(ctx, nautobotCR.Spec.DeviceTypesRef)
107107
if err != nil {
@@ -202,6 +202,16 @@ func (r *NautobotReconciler) getAuthTokenFromSecretRef(ctx context.Context, naut
202202
return "", fmt.Errorf("secret key %s not found in secret", nautobotCR.Spec.NautobotSecretRef.Key)
203203
}
204204

205+
// getServiceFromServiceRef: this will fetch Nautobot service from the given reference.
206+
func (r *NautobotReconciler) getServiceFromServiceRef(ctx context.Context, nautobotCR syncv1alpha1.Nautobot) (*corev1.Service, error) {
207+
service := &corev1.Service{}
208+
err := r.Get(ctx, types.NamespacedName{Name: nautobotCR.Spec.NautobotServiceRef.Name, Namespace: *nautobotCR.Spec.NautobotServiceRef.Namespace}, service)
209+
if err != nil {
210+
return nil, err
211+
}
212+
return service, nil
213+
}
214+
205215
// SetupWithManager sets up the controller with the Manager.
206216
func (r *NautobotReconciler) SetupWithManager(mgr ctrl.Manager) error {
207217
return ctrl.NewControllerManagedBy(mgr).

0 commit comments

Comments
 (0)