Skip to content

Commit 2d948ac

Browse files
author
aalexand
committed
Initial support for public IP scanning
1 parent a2a44c5 commit 2d948ac

File tree

9 files changed

+165
-1
lines changed

9 files changed

+165
-1
lines changed

charts/cluster-registry-client/crds/registry.ethos.adobe.com_clusters.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ spec:
239239
- Running
240240
- Upgrading
241241
type: string
242+
publicIPs:
243+
description: PublicIPs cluster public IPs
244+
items:
245+
type: string
246+
type: array
242247
region:
243248
description: Cluster internal region name
244249
type: string

cmd/client/client.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ import (
1818
"fmt"
1919
registryv1alpha1 "github.com/adobe/cluster-registry/pkg/api/registry/v1alpha1"
2020
"github.com/adobe/cluster-registry/pkg/client/controllers"
21+
"github.com/adobe/cluster-registry/pkg/client/publicip"
2122
"github.com/adobe/cluster-registry/pkg/config"
2223
monitoring "github.com/adobe/cluster-registry/pkg/monitoring/client"
2324
"github.com/adobe/cluster-registry/pkg/sqs"
25+
"github.com/go-co-op/gocron/v2"
2426
"github.com/prometheus/client_golang/prometheus/promhttp"
2527
"k8s.io/apimachinery/pkg/runtime/schema"
2628
"k8s.io/client-go/discovery"
2729
"k8s.io/client-go/tools/leaderelection/resourcelock"
2830
"net/http"
2931
"os"
3032
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
33+
"time"
3134

3235
configv1 "github.com/adobe/cluster-registry/pkg/api/config/v1"
3336
registryv1 "github.com/adobe/cluster-registry/pkg/api/registry/v1"
@@ -199,6 +202,46 @@ func main() {
199202
os.Exit(1)
200203
}
201204
}()
205+
go func() {
206+
scanInterval := 60 * time.Second
207+
208+
setupLog.Info("starting public IP scanner",
209+
"interval", fmt.Sprintf("%s", scanInterval))
210+
scheduler, err := gocron.NewScheduler(gocron.WithLocation(time.UTC))
211+
defer func() { _ = scheduler.Shutdown() }()
212+
213+
if err != nil {
214+
setupLog.Error(err, "failed to create scheduler")
215+
os.Exit(1)
216+
}
217+
218+
scanner, err := publicip.NewScanner(func(o *publicip.Options) {
219+
o.Client = mgr.GetClient()
220+
o.Logger = ctrl.Log.WithName("publicip-scanner")
221+
o.Namespace = clientConfig.Namespace
222+
})
223+
224+
if err != nil {
225+
setupLog.Error(err, "failed to create public IP scanner")
226+
os.Exit(1)
227+
}
228+
229+
job, err := scheduler.NewJob(
230+
gocron.DurationJob(scanInterval),
231+
gocron.NewTask(scanner.Run, ctx),
232+
)
233+
234+
if err != nil {
235+
setupLog.Error(err, "failed to schedule public IP scanner job")
236+
os.Exit(1)
237+
}
238+
239+
scheduler.Start()
240+
setupLog.Info("started public IP scanner job",
241+
"id", job.ID(),
242+
"interval", fmt.Sprintf("%s", scanInterval))
243+
select {}
244+
}()
202245

203246
setupLog.Info("starting cluster-registry-client")
204247
if err := mgr.Start(ctx); err != nil {

config/crd/bases/registry.ethos.adobe.com_clusters.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ spec:
239239
- Running
240240
- Upgrading
241241
type: string
242+
publicIPs:
243+
description: PublicIPs cluster public IPs
244+
items:
245+
type: string
246+
type: array
242247
region:
243248
description: Cluster internal region name
244249
type: string

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require (
3737
k8s.io/client-go v0.31.2
3838
k8s.io/component-base v0.31.2
3939
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6
40-
sigs.k8s.io/controller-runtime v0.19.1
40+
sigs.k8s.io/controller-runtime v0.19.2
4141
sigs.k8s.io/yaml v1.4.0
4242
)
4343

@@ -72,6 +72,7 @@ require (
7272
github.com/fsnotify/fsnotify v1.7.0 // indirect
7373
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
7474
github.com/ghodss/yaml v1.0.0 // indirect
75+
github.com/go-co-op/gocron/v2 v2.12.4 // indirect
7576
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
7677
github.com/go-logr/stdr v1.2.2 // indirect
7778
github.com/go-logr/zapr v1.3.0 // indirect
@@ -93,6 +94,7 @@ require (
9394
github.com/google/gofuzz v1.2.0 // indirect
9495
github.com/imdario/mergo v0.3.6 // indirect
9596
github.com/jmespath/go-jmespath v0.4.0 // indirect
97+
github.com/jonboulle/clockwork v0.4.0 // indirect
9698
github.com/josharian/intern v1.0.0 // indirect
9799
github.com/klauspost/compress v1.17.9 // indirect
98100
github.com/kylelemons/godebug v1.1.0 // indirect
@@ -123,6 +125,7 @@ require (
123125
github.com/prometheus/client_model v0.6.1 // indirect
124126
github.com/prometheus/common v0.55.0 // indirect
125127
github.com/prometheus/procfs v0.15.1 // indirect
128+
github.com/robfig/cron/v3 v3.0.1 // indirect
126129
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
127130
github.com/shoenig/go-m1cpu v0.1.6 // indirect
128131
github.com/sirupsen/logrus v1.9.3 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv
9898
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
9999
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
100100
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
101+
github.com/go-co-op/gocron/v2 v2.12.4 h1:h1HWApo3T+61UrZqEY2qG1LUpDnB7tkYITxf6YIK354=
102+
github.com/go-co-op/gocron/v2 v2.12.4/go.mod h1:xY7bJxGazKam1cz04EebrlP4S9q4iWdiAylMGP3jY9w=
101103
github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k=
102104
github.com/go-jose/go-jose/v3 v3.0.3/go.mod h1:5b+7YgP7ZICgJDBdfjZaIt+H/9L9T/YQrVfLAMboGkQ=
103105
github.com/go-jose/go-jose/v4 v4.0.4 h1:VsjPI33J0SB9vQM6PLmNjoHqMQNGPiZ0rHL7Ni7Q6/E=
@@ -190,6 +192,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
190192
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
191193
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
192194
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
195+
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
196+
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
193197
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
194198
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
195199
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -293,6 +297,8 @@ github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0
293297
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
294298
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
295299
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
300+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
301+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
296302
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
297303
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
298304
github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4=
@@ -579,6 +585,8 @@ sigs.k8s.io/controller-runtime v0.19.0 h1:nWVM7aq+Il2ABxwiCizrVDSlmDcshi9llbaFbC
579585
sigs.k8s.io/controller-runtime v0.19.0/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
580586
sigs.k8s.io/controller-runtime v0.19.1 h1:Son+Q40+Be3QWb+niBXAg2vFiYWolDjjRfO8hn/cxOk=
581587
sigs.k8s.io/controller-runtime v0.19.1/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
588+
sigs.k8s.io/controller-runtime v0.19.2 h1:3sPrF58XQEPzbE8T81TN6selQIMGbtYwuaJ6eDssDF8=
589+
sigs.k8s.io/controller-runtime v0.19.2/go.mod h1:iRmWllt8IlaLjvTTDLhRBXIEtkCK6hwVBJJsYS9Ajf4=
582590
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
583591
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
584592
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=

pkg/api/registry/v1/cluster_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ type ClusterSpec struct {
132132

133133
// AvailabilityZones cluster availability zones
134134
AvailabilityZones []AvailabilityZone `json:"availabilityZones,omitempty"`
135+
136+
// PublicIPs cluster public IPs
137+
PublicIPs []string `json:"publicIPs,omitempty"`
135138
}
136139

137140
// Offering the cluster is meant for

pkg/api/registry/v1/zz_generated.deepcopy.go

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

pkg/client/publicip/internal.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package publicip
2+
3+
import (
4+
"context"
5+
registryv1 "github.com/adobe/cluster-registry/pkg/api/registry/v1"
6+
"github.com/go-logr/logr"
7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
)
9+
10+
type scanner struct {
11+
client client.Client
12+
logger logr.Logger
13+
namespace string
14+
}
15+
16+
func (s *scanner) GetClient() client.Client {
17+
return s.client
18+
}
19+
20+
func (s *scanner) Run(ctx context.Context) error {
21+
clusterList := &registryv1.ClusterList{}
22+
err := s.client.List(context.TODO(), clusterList, &client.ListOptions{Namespace: s.namespace})
23+
if err != nil {
24+
return err
25+
}
26+
27+
for _, cluster := range clusterList.Items {
28+
switch cluster.Spec.CloudType {
29+
case "aws", "eks":
30+
s.logger.Info("Querying AWS cloud provider API", "cluster", cluster.Name)
31+
32+
case "azure", "aks":
33+
s.logger.Info("Querying Azure cloud provider API", "cluster", cluster.Name)
34+
35+
case "datacenter":
36+
// not yet implemented
37+
s.logger.Info("Skipping datacenter cluster", "cluster", cluster.Name)
38+
39+
default:
40+
s.logger.Info("Unknown cloud provider", "cluster", cluster.Name)
41+
}
42+
}
43+
44+
return nil
45+
}

pkg/client/publicip/scanner.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package publicip
2+
3+
import (
4+
"context"
5+
"github.com/go-logr/logr"
6+
"sigs.k8s.io/controller-runtime/pkg/client"
7+
"sigs.k8s.io/controller-runtime/pkg/log"
8+
)
9+
10+
type Scanner interface {
11+
GetClient() client.Client
12+
Run(ctx context.Context) error
13+
}
14+
15+
func NewScanner(opts ...Option) (Scanner, error) {
16+
options := Options{}
17+
for _, o := range opts {
18+
o(&options)
19+
}
20+
options, err := setDefaultOptions(options)
21+
if err != nil {
22+
options.Logger.Error(err, "failed to set defaults")
23+
return nil, err
24+
}
25+
26+
return &scanner{
27+
client: options.Client,
28+
logger: options.Logger,
29+
namespace: options.Namespace,
30+
}, nil
31+
}
32+
33+
type Options struct {
34+
Logger logr.Logger
35+
Client client.Client
36+
Namespace string
37+
}
38+
39+
type Option func(*Options)
40+
41+
func setDefaultOptions(options Options) (Options, error) {
42+
if options.Logger.GetSink() == nil {
43+
options.Logger = log.Log.WithName("publicip-scanner")
44+
}
45+
46+
return options, nil
47+
}

0 commit comments

Comments
 (0)