Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
186 commits
Select commit Hold shift + click to select a range
c902d05
first draft to create targets
mcdillson Apr 7, 2026
95be5ab
added deletion of targets
mcdillson Apr 7, 2026
4e3104e
renamed client functions
mcdillson Apr 7, 2026
32eb936
added update functionality
mcdillson Apr 7, 2026
3c4141f
refactored crud options into separate function with struct
mcdillson Apr 7, 2026
590a494
refactored delete to use map lookup
mcdillson Apr 7, 2026
2666986
added comments
mcdillson Apr 7, 2026
b9cd85e
fixed bug with deleted object name being empty
mcdillson Apr 7, 2026
109a85e
added info logs
mcdillson Apr 7, 2026
372fe71
merged initial folder structure & moved existing files
mcdillson Apr 8, 2026
3290233
Merge remote-tracking branch 'origin/feature/target-loading-architect…
mcdillson Apr 13, 2026
402c70a
changed NewLoader function call
mcdillson Apr 13, 2026
7f83285
added discovery message to types
mcdillson Apr 13, 2026
7d03411
changed target source channel type
mcdillson Apr 13, 2026
4fb0373
fixed http_pull implementation based on new types
mcdillson Apr 13, 2026
2294f1e
implemented first draft of target creation using loaders
mcdillson Apr 13, 2026
912e05a
removed manual targetsource spec
mcdillson Apr 14, 2026
bf28aad
cleaned up reconciliation loop and refactored into target manager
mcdillson Apr 14, 2026
aa17c82
Merge branch 'feature/targetsource-crd-fields' into feat/target-creation
mcdillson Apr 14, 2026
022dbae
restructured project to introduce new architecture
mcdillson Apr 15, 2026
4dc2eb3
renamed targetsource package to discovery
mcdillson Apr 15, 2026
ad172c9
removed unnecessary files and updated gitignore
mcdillson Apr 15, 2026
181ea95
extend TargetSource CRD by http token
denyost Apr 16, 2026
2fddddf
add pull logic as poc
denyost Apr 16, 2026
64a83cd
fix request header typo
denyost Apr 16, 2026
98823e8
refactor pull implementation
denyost Apr 16, 2026
cfe0273
Merge branch 'feature/target-loading-architecture' of https://github.…
denyost Apr 16, 2026
e76c6f3
restructure discovery structs
denyost Apr 17, 2026
3c18fb5
offload sending logic from loader implementation
denyost Apr 17, 2026
86ab0f3
implement type assertion based on received message
denyost Apr 17, 2026
10a9978
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 17, 2026
8b36d7d
add http_push skeleton
denyost Apr 17, 2026
efbf727
add http_push skeleton
denyost Apr 17, 2026
60a5eb3
refactor targetsource_controller.go
denyost Apr 17, 2026
1bc5d2b
remove targetsource ressource to not impact main
denyost Apr 17, 2026
14e7765
add batching to DiscoveryEvent's
denyost Apr 17, 2026
b4337ea
refactored sender.go
denyost Apr 17, 2026
30f3ecb
load buffer and chunk size from env variable
denyost Apr 17, 2026
6579a7d
Merge branch 'feature/targetsource-crd-fields' into feat/target-creation
mcdillson Apr 17, 2026
4ef6268
Merge branch 'feature/limit-target-loading' into feat/target-creation
mcdillson Apr 17, 2026
6684aa9
ran go mod tidy
mcdillson Apr 17, 2026
c952cbf
changed events to delete/apply and implemented draft with snapshots
mcdillson Apr 18, 2026
d4a9053
first implementation for full snapshot processing
mcdillson Apr 20, 2026
5af4f5e
fixed mapper function to work for empty existing targets
mcdillson Apr 20, 2026
d5ea4da
introduce observedGeneration for pipeline restart
mcdillson Apr 20, 2026
fe086e2
tests with targetsource status field to restart pipeline
mcdillson Apr 20, 2026
bd60568
added status update to targetmanager
mcdillson Apr 20, 2026
1e81e11
separated status updates & cleaned up functions
mcdillson Apr 20, 2026
d3e708c
restructured target manager logic to handle events
mcdillson Apr 21, 2026
9a78cf4
fixed small issues
mcdillson Apr 21, 2026
201ccb3
Merge branch 'feature/targetsource-crd-fields' into feature/limit-tar…
mcdillson Apr 21, 2026
c8dfdf1
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 22, 2026
6cc0eb1
Merge branch 'feature/target-loading-architecture' of https://github.…
denyost Apr 22, 2026
e7ff5f8
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 22, 2026
3cb9160
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 22, 2026
586001e
rename file to helpers
denyost Apr 22, 2026
7430815
rebuild and reformat
denyost Apr 22, 2026
4fb6755
removed unnecessary map
mcdillson Apr 22, 2026
aaf9f2b
added prefix to targets
mcdillson Apr 22, 2026
255a1f3
consolidate pull and push to http
denyost Apr 23, 2026
bd2b45f
rename target manager to target applier
denyost Apr 23, 2026
5a561a7
implement a generic registry
denyost Apr 23, 2026
f5481b8
add a discoveryTegistry to share targetchannel between apiserver and …
denyost Apr 23, 2026
22683f4
remove unused event action from DiscoverySnapshot
denyost Apr 23, 2026
922bbc6
rename target manager to target applier
denyost Apr 24, 2026
733927f
implement key for registry as a comparable
denyost Apr 24, 2026
9d30560
fix error message and add a word of caution for key comparables
denyost Apr 24, 2026
dafa82b
consistently use namespaced name as refference to the targetsource
denyost Apr 24, 2026
2973c03
improve context cancling and error handling
denyost Apr 24, 2026
c95bdaf
add supervised goroutines
denyost Apr 24, 2026
0aa883d
refactor target applier
denyost Apr 24, 2026
27b2b1f
add supervisor for the discovery pipelines
denyost Apr 24, 2026
22fe2d8
improve readability
denyost Apr 24, 2026
58538c7
remove side-effects from getter getTargetSource
denyost Apr 24, 2026
4f0457e
redesign supervisor
denyost Apr 24, 2026
2b728c4
added const file for common labels
mcdillson Apr 24, 2026
5abbd63
simplified name and namespace calls for objects
mcdillson Apr 24, 2026
3d7ff38
changed label handling and target object creation
mcdillson Apr 24, 2026
4d0a937
fixed label filtering for existing targets
mcdillson Apr 24, 2026
60491be
add dependency handling of discovery pipeline components
denyost Apr 25, 2026
b8a6d27
refactor code
denyost Apr 25, 2026
eedfedf
improve context handling of and target applier semantics
denyost Apr 25, 2026
1715ae1
Merge branch 'feature/limit-target-loading' into feat/target-creation
mcdillson Apr 27, 2026
a66accb
moved finalizer label into const file
mcdillson Apr 27, 2026
3b2d925
fixed typo
mcdillson Apr 27, 2026
3ba86cb
restructured loaders package
mcdillson Apr 27, 2026
d0ac86b
restructured target handler
mcdillson Apr 27, 2026
240a2bc
ran go mod tidy
mcdillson Apr 27, 2026
5a7ba1a
Merge branch 'feature/limit-target-loading' into feat/target-creation
mcdillson Apr 27, 2026
7ef1281
renamed target applier to message processor & created client.go for g…
mcdillson Apr 27, 2026
7bcbcc0
added const file for common labels
mcdillson Apr 24, 2026
4fa58b6
Merge branch 'feature/limit-target-loading' into feature/restructuring
mcdillson Apr 27, 2026
d10fc9a
removed all package
mcdillson Apr 27, 2026
108bd2d
changed error lookup to apierrors
mcdillson Apr 27, 2026
b7dd036
remove unused fiels
denyost Apr 29, 2026
d3a9b5c
rename files and restructure packages
denyost Apr 29, 2026
0c80394
rename target handler to target reconciler
denyost Apr 29, 2026
04208bf
rename handler to reconciler
denyost Apr 29, 2026
c3818ce
clarify interface files
denyost Apr 29, 2026
e4df0d4
define EventAction to be go idomatic
denyost Apr 29, 2026
86c0af0
add webhook activation info to metadata of DiscoveryRegistry
denyost Apr 29, 2026
284b1f2
moved reconciler files to discovery
mcdillson Apr 29, 2026
b59897c
renamed messageProcessor to targetReconciler
mcdillson Apr 29, 2026
c268808
moved registry.go to discovery
mcdillson Apr 29, 2026
0295896
moved supervisor to discovery
mcdillson Apr 29, 2026
4d32c40
moved factory.go to discovery/loaders.go
mcdillson Apr 29, 2026
7671c1a
moved send.go to loaders package
mcdillson Apr 29, 2026
5f1e9cb
eliminated message.go
mcdillson Apr 29, 2026
6d67537
moved const.go to discovery.go
mcdillson Apr 29, 2026
3914630
renamed core package within targetsource controller
mcdillson Apr 29, 2026
931a4cf
Merge branch 'feature/limit-target-loading' into feat/target-creation
mcdillson Apr 29, 2026
46a201f
changed events to delete / apply
mcdillson Apr 29, 2026
7b17f7e
moved send.go into separate utils for loaders
mcdillson Apr 29, 2026
f37e172
Merge branch 'feature/restructuring' into feat/target-creation
mcdillson Apr 29, 2026
4540163
replaced legacy registry package
mcdillson Apr 29, 2026
2c3260e
Merge pull request #57 from gnmic/feature/restructuring
mcdillson Apr 29, 2026
090fa26
Merge branch 'feature/limit-target-loading' into feat/target-creation
mcdillson Apr 29, 2026
183abe2
moved client/mapper functions out of target_reconciler.go
mcdillson Apr 29, 2026
0811afd
renamed functions
mcdillson Apr 29, 2026
f269a85
Merge branch 'main' of https://github.com/gnmic/operator into feature…
denyost Apr 30, 2026
c728fa2
add supervisor restart policy to targetsource spec configuration
denyost Apr 30, 2026
589bc9f
add targetsource example for lab
denyost Apr 30, 2026
a5dde06
remove targetsource example to not add unnecassary logging to main
denyost Apr 30, 2026
4be9c27
update gitignore to not push targetsources in order to prevent loggin…
denyost Apr 30, 2026
65eb7f6
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 30, 2026
7337541
add component info to logging
denyost Apr 30, 2026
4ec0bb6
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 30, 2026
41d5498
make snapshot id a bit smaller
denyost Apr 30, 2026
3ec3203
if context is canceled return with ctx.Err() not a clean exit
denyost Apr 30, 2026
0eaffdc
applied kubebuilder best-practise logging
denyost Apr 30, 2026
5140605
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 30, 2026
e447b3b
improved logging
denyost Apr 30, 2026
fca37e0
improved logging
denyost Apr 30, 2026
fd4abe7
improved logging
denyost Apr 30, 2026
a6bc114
simplified pipeline context handling
denyost Apr 30, 2026
34cd51e
Merge branch 'feature/limit-target-loading' of https://github.com/gnm…
denyost Apr 30, 2026
535ee49
rename target reconciler to message processor
denyost May 1, 2026
c09c68f
rename pipeline to runtime
denyost May 1, 2026
e4c01ba
removed supervisor
denyost May 1, 2026
77dbd7e
tidy loader configuration abstraction
denyost May 1, 2026
fe900e3
regenearte manifests without restartPolicy
denyost May 1, 2026
c1d7a91
tidy up comments
denyost May 1, 2026
05c7538
move webhook spec into provider and rename it to acceptPush
denyost May 1, 2026
061d4b8
regenerate manifests
denyost May 1, 2026
41655a0
remove spec from laoder config
denyost May 1, 2026
97849ae
update LoaderConfig in registry
denyost May 1, 2026
e977868
Merge branch 'feature/limit-target-loading' into feat/target-creation
mcdillson May 3, 2026
1bdc294
moved updateStatus function to client.go
mcdillson May 4, 2026
9255240
changed updateStauts handling
mcdillson May 4, 2026
29c4974
removed owned targets from targetsource reconciliation
mcdillson May 4, 2026
24cc376
added predicate for targetsource reconciliation
mcdillson May 4, 2026
8da8052
changed updateStatus calling for event
mcdillson May 4, 2026
f4d6bac
added comments to mapper.go
mcdillson May 4, 2026
41c1fec
added more comments
mcdillson May 4, 2026
4a46908
added initial targetCount fetch to deal with process restarts
mcdillson May 4, 2026
a0b55b3
added comment
mcdillson May 4, 2026
426e27a
fix: use defined variable
denyost May 4, 2026
d14b10c
fixed cluster variable for netbox deployment
mcdillson May 4, 2026
f04c130
added netbox integration test setup
mcdillson May 4, 2026
765c6ed
renamed sync-test-netbox-data
mcdillson May 4, 2026
935c49f
moved netbox clab topology into netbox folder
mcdillson May 4, 2026
5fb7700
added targetsource integration resource
mcdillson May 4, 2026
1c349ae
added comments + fixed netbox test topology path
mcdillson May 5, 2026
6c22ef1
Merge branch 'feature/limit-target-loading' into feature/discovery-e2…
mcdillson May 5, 2026
a0d48ca
Merge branch 'feat/target-creation' into feature/discovery-e2e-implem…
mcdillson May 5, 2026
60736be
Merge branch 'testing/netbox-integration-setup' into feature/discover…
mcdillson May 5, 2026
012a6a5
fixed missing separator
mcdillson May 5, 2026
09aaaa4
fixed targetProfile key
mcdillson May 5, 2026
631eeaf
Merge branch 'testing/netbox-integration-setup' into feature/discover…
mcdillson May 5, 2026
4bcd01e
fixed name normalization
mcdillson May 5, 2026
ecbef25
Merge branch 'feat/target-creation' into feature/discovery-e2e-implem…
mcdillson May 5, 2026
47e4851
Merge remote-tracking branch 'refs/remotes/origin/feature/discovery-e…
mcdillson May 5, 2026
b9ab471
eliminated recursive make calls
mcdillson May 5, 2026
1a6239a
added recursive clab folder to gitignore
mcdillson May 5, 2026
4f879aa
added resources for http static server test
mcdillson May 5, 2026
a534406
added make targets for http testing pod
mcdillson May 5, 2026
f5e822d
Merge branch 'testing/netbox-integration-setup' into feature/discover…
mcdillson May 8, 2026
9ff3ba1
fixed make target name
mcdillson May 8, 2026
0031b5a
added http targetsource integration test to Makefile
mcdillson May 8, 2026
c3dc34c
changed resource names + mapped target inventory to clab
mcdillson May 8, 2026
efead83
fixed http target url and profile
mcdillson May 8, 2026
4d461a3
removed netbox targetsource for integration test
mcdillson May 8, 2026
f7c627a
mapped operator resources to new http test
mcdillson May 8, 2026
f37cc4a
removed netbox clab and changed address
mcdillson May 8, 2026
4f4abf7
Merge branch 'testing/netbox-integration-setup' into feature/discover…
mcdillson May 8, 2026
61ef95d
generated manifests
mcdillson May 8, 2026
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ notes/
docs/public
docs/resources/_gen/
docs/.hugo_build.lock
test/integration/clab-*
test/integration/**/clab-*

# Only for development and testing purposes
# To be removed after development of targetsource
# ignored in order to not add unnecassary logging messages
lab/dev/resources/targetsources
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ delete-targetsources-dev-lab: ## Delete the target sources for the development l
##@ Testing Lab

.PHONY: run-integration-tests
run-integration-tests: docker-build undeploy-test-cluster deploy-test-cluster install-test-cluster-dependencies load-test-image deploy install-kubectl install-gnmic install-containerlab deploy-test-topology apply-test-resources
run-integration-tests: docker-build undeploy-test-cluster deploy-test-cluster install-test-cluster-dependencies load-test-image deploy deploy-test-http-server install-kubectl install-gnmic install-containerlab deploy-test-topology apply-test-resources
kubectl wait --for=condition=Ready cluster --all --timeout=180s
kubectl wait --for=condition=Ready pipeline --all --timeout=180s
kubectl wait --for=jsonpath='{.status.targetsCount}'=3 targetsource --all --timeout=180s
kubectl wait --for=jsonpath='{.status.connectionState}'=READY target --all --timeout=180s
kubectl get subscriptions -o yaml
kubectl get outputs -o yaml
Expand Down
15 changes: 10 additions & 5 deletions api/v1alpha1/targetsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// +kubebuilder:validation:Required
type TargetSourceSpec struct {
Provider *ProviderSpec `json:"provider"`
//

// +kubebuilder:validation:Optional
TargetLabels map[string]string `json:"targetLabels,omitempty"`

// +kubebuilder:validation:MinLength=1
Expand All @@ -39,7 +40,10 @@ type ProviderSpec struct {

type HTTPConfig struct {
// +kubebuilder:validation:MinLength=1
URL string `json:"url"`
URL string `json:"url"`
Token string `json:"token,omitempty"`
// +kubebuilder:validation:Optional
AcceptPush bool `json:"acceptPush,omitempty"`
}

type ConsulConfig struct {
Expand All @@ -49,9 +53,10 @@ type ConsulConfig struct {

// TargetSourceStatus defines the observed state of TargetSource
type TargetSourceStatus struct {
Status string `json:"status"`
TargetsCount int32 `json:"targetsCount"`
LastSync metav1.Time `json:"lastSync"`
Status string `json:"status,omitempty"`
ObservedGeneration int64 `json:"observedGeneration"`
TargetsCount int32 `json:"targetsCount,omitempty"`
LastSync metav1.Time `json:"lastSync,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
17 changes: 15 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -40,6 +41,8 @@ import (
operatorv1alpha1 "github.com/gnmic/operator/api/v1alpha1"
"github.com/gnmic/operator/internal/apiserver"
"github.com/gnmic/operator/internal/controller"
"github.com/gnmic/operator/internal/controller/discovery"
"github.com/gnmic/operator/internal/controller/discovery/core"
webhookv1alpha1 "github.com/gnmic/operator/internal/webhook/v1alpha1"
//+kubebuilder:scaffold:imports
)
Expand All @@ -64,13 +67,17 @@ func main() {
var probeAddr string
var devMode bool
var apiAddr string
var discoveryChunkSize int
var discoveryBufferSize int
flag.StringVar(&apiAddr, "api-bind-address", "", "The address the operator API endpoint binds to. Disabled if empty.")
flag.BoolVar(&devMode, "dev-mode", false, "Enable development mode.")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&discoveryChunkSize, "discovery-chunk-size", 100, "Maximum number of targets/events sent in a single discovery message.")
flag.IntVar(&discoveryBufferSize, "discovery-buffer-size", 10, "Amount of discovery messages that can be queued in the channel buffer.")
opts := zap.Options{
Development: devMode,
}
Expand All @@ -79,6 +86,8 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

discoveryRegistry := discovery.NewRegistry[types.NamespacedName, core.DiscoveryRegistryValue]()

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
Expand Down Expand Up @@ -117,8 +126,11 @@ func main() {
os.Exit(1)
}
if err := (&controller.TargetSourceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
BufferSize: discoveryBufferSize,
ChunkSize: discoveryChunkSize,
DiscoveryRegistry: discoveryRegistry,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TargetSource")
os.Exit(1)
Expand Down Expand Up @@ -220,6 +232,7 @@ func main() {

if apiAddr != "" {
apiServer := apiserver.New(apiAddr, clusterReconciler)
apiServer.DiscoveryRegistry = discoveryRegistry
err = mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
errCh := make(chan error)
go func() {
Expand Down
11 changes: 8 additions & 3 deletions config/crd/bases/operator.gnmic.dev_targetsources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ spec:
type: object
http:
properties:
acceptPush:
type: boolean
token:
type: string
url:
minLength: 1
type: string
Expand Down Expand Up @@ -77,15 +81,16 @@ spec:
lastSync:
format: date-time
type: string
observedGeneration:
format: int64
type: integer
status:
type: string
targetsCount:
format: int32
type: integer
required:
- lastSync
- status
- targetsCount
- observedGeneration
type: object
type: object
served: true
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.25.5
require (
github.com/cert-manager/cert-manager v1.19.3
github.com/go-logr/logr v1.4.3
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.27.3
github.com/onsi/gomega v1.38.3
github.com/openconfig/gnmic/pkg/api v0.1.10
Expand Down Expand Up @@ -47,7 +48,6 @@ require (
github.com/google/gnostic-models v0.7.1 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/pprof v0.0.0-20251213031049-b05bdaca462f // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
Expand Down
5 changes: 5 additions & 0 deletions internal/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (
"net/http"

"github.com/gnmic/operator/internal/controller"
"github.com/gnmic/operator/internal/controller/discovery"
"github.com/gnmic/operator/internal/controller/discovery/core"
"k8s.io/apimachinery/pkg/types"
)

type APIServer struct {
Server *http.Server
clusterReconciler *controller.ClusterReconciler

DiscoveryRegistry *discovery.Registry[types.NamespacedName, core.DiscoveryRegistryValue]
}

func New(addr string, clusterReconciler *controller.ClusterReconciler) *APIServer {
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
LabelCertType = "operator.gnmic.dev/cert-type"
LabelValueCertTypeClient = "client"
LabelValueCertTypeTunnel = "tunnel"

LabelTargetSourceFinalizer = "operator.gnmic.dev/targetsource-finalizer"
)

const (
Expand Down
72 changes: 67 additions & 5 deletions internal/controller/discovery/client.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package discovery

// File may become obsolete, depends on how the logic to compare desired vs. existing state will get implemented

import (
"context"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

gnmicv1alpha1 "github.com/gnmic/operator/api/v1alpha1"
)

func FetchExistingTargets(ctx context.Context, c client.Client, ts gnmicv1alpha1.TargetSource) ([]gnmicv1alpha1.Target, error) {
func fetchExistingTargets(ctx context.Context, c client.Client, ts *gnmicv1alpha1.TargetSource) ([]gnmicv1alpha1.Target, error) {
var targetList gnmicv1alpha1.TargetList

err := c.List(ctx, &targetList,
err := c.List(
ctx,
&targetList,
client.InNamespace(ts.Namespace),
client.MatchingLabels{
"gnmic.io/source": ts.Name,
LabelTargetSourceName: ts.Name,
},
)
if err != nil {
Expand All @@ -25,3 +31,59 @@ func FetchExistingTargets(ctx context.Context, c client.Client, ts gnmicv1alpha1

return targetList.Items, nil
}

func applyTarget(ctx context.Context, c client.Client, s *runtime.Scheme, desired *gnmicv1alpha1.Target, ts *gnmicv1alpha1.TargetSource) error {
existing := &gnmicv1alpha1.Target{
ObjectMeta: metav1.ObjectMeta{
Name: desired.Name,
Namespace: desired.Namespace,
},
}

_, err := controllerutil.CreateOrUpdate(ctx, c, existing, func() error {
existing.Spec = desired.Spec
existing.Labels = desired.Labels

return controllerutil.SetControllerReference(ts, existing, s)
})

return err
}

func deleteTarget(ctx context.Context, c client.Client, name string, namespace string) error {
existing := &gnmicv1alpha1.Target{}

err := c.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, existing)
if apierrors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}

err = c.Delete(ctx, existing)
if apierrors.IsNotFound(err) {
return nil
}

return err
}

// updateTargetSourceStatus updates the status of the TargetSource Object ts. The only fields updated are targetCount and LastSync, which takes the current timestamp.
func updateTargetSourceStatus(ctx context.Context, c client.Client, ts *gnmicv1alpha1.TargetSource, targetCount int32) error {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
latest := &gnmicv1alpha1.TargetSource{}
if err := c.Get(ctx, client.ObjectKeyFromObject(ts), latest); err != nil {
return err
}

latest.Status.TargetsCount = targetCount
latest.Status.LastSync = metav1.Now()

return c.Status().Update(ctx, latest)
})

return err
}
13 changes: 13 additions & 0 deletions internal/controller/discovery/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package discovery

const (
// Kubernetes Side Labels
LabelTargetSourceName = "operator.gnmic.dev/targetsource"
)

const (
// Prefix and Labels for external systems
ExternalLabelPrefix = "gnmic_operator_"

ExternalLabelTargetProfile = ExternalLabelPrefix + "target_profile"
)
11 changes: 3 additions & 8 deletions internal/controller/discovery/core/loader_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ type Loader interface {
// Name returns the unique loader identifier e.g. "pull"
Name() string

// Start begins discovery and pushes target snapshots into the out channel
// The loader must stop cleanly when ctx is cancelled
Start(
ctx context.Context,
targetsourceName string,
spec gnmicv1alpha1.TargetSourceSpec,
out chan<- []DiscoveryMessage,
) error
// Run begins discovery and pushes target snapshots or events into the out channel
// The loader must stop cleanly when ctx is canceled
Run(ctx context.Context, out chan<- []DiscoveryMessage, spec gnmicv1alpha1.TargetSourceSpec) error
}
8 changes: 8 additions & 0 deletions internal/controller/discovery/core/message_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core

type DiscoveryMessage interface {
isDiscoveryMessage()
}

func (DiscoveryEvent) isDiscoveryMessage() {}
func (DiscoverySnapshot) isDiscoveryMessage() {}
Loading
Loading