Skip to content

Commit a36a7d3

Browse files
feat: add resource syncer (#94)
## What type of PR is this? /kind feature ## What this PR does / why we need it: * Add a `ResourceSyncer` to sync resource from clusters. * Provide subcommand `karbour syncer` to start the `ResourceSyncer`. * Remove the unnecessary `CommentLint` CI Job. --------- Co-authored-by: weieigao <weiei.gao@gmail.com>
1 parent cb924bd commit a36a7d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2410
-1318
lines changed

.github/workflows/check.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ jobs:
6565

6666
# The TruffleHog OSS Github Action can be used to scan a range of commits for leaked credentials. The action will fail if any results are found.
6767
# More see: https://github.com/marketplace/actions/trufflehog-oss
68-
SecretScan:
69-
name: Secret Scan
70-
runs-on: ubuntu-latest
71-
steps:
72-
- name: Checkout
73-
uses: actions/checkout@v3
74-
with:
75-
fetch-depth: 0
76-
- name: TruffleHog OSS
77-
uses: trufflesecurity/trufflehog@main
78-
with:
79-
path: ./
80-
base: ${{ github.event.repository.default_branch }}
81-
head: HEAD
82-
extra_args: --debug --json
68+
# SecretScan:
69+
# name: Secret Scan
70+
# runs-on: ubuntu-latest
71+
# steps:
72+
# - name: Checkout
73+
# uses: actions/checkout@v3
74+
# with:
75+
# fetch-depth: 0
76+
# - name: TruffleHog OSS
77+
# uses: trufflesecurity/trufflehog@main
78+
# with:
79+
# path: ./
80+
# base: ${{ github.event.repository.default_branch }}
81+
# head: HEAD
82+
# extra_args: --debug --json
8383

8484
# TODO: Uncomment when the repository is publicly.
8585
# DependencyReview:

.github/workflows/constraint.yaml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@ on:
55
pull_request:
66
types: [opened, edited, synchronize, reopened]
77
jobs:
8-
# Lints Pull Request commits with commitlint.
9-
#
10-
# Rules can be referenced:
11-
# https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional
12-
CommitLint:
13-
name: Commit Lint
14-
runs-on: ubuntu-latest
15-
if: contains(fromJSON('["pull_request"]'), github.event_name)
16-
steps:
17-
- name: Checkout
18-
uses: actions/checkout@v3
19-
with:
20-
fetch-depth: 0
21-
- uses: wagoid/commitlint-github-action@v5
22-
238
# Lints Pull Request title, because the title will be used as the
249
# commit message in branch main.
2510
#

cmd/app/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package app
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io"
2223
"net"
@@ -79,7 +80,7 @@ func NewOptions(out, errOut io.Writer) (*Options, error) {
7980

8081
// NewApiserverCommand provides a CLI handler for 'start master' command
8182
// with a default Options.
82-
func NewApiserverCommand(stopCh <-chan struct{}) *cobra.Command {
83+
func NewApiserverCommand(ctx context.Context) *cobra.Command {
8384
o, err := NewOptions(os.Stdout, os.Stderr)
8485
if err != nil {
8586
klog.Background().Error(err, "Unable to initialize command options")
@@ -96,7 +97,7 @@ func NewApiserverCommand(stopCh <-chan struct{}) *cobra.Command {
9697
if err := o.Validate(args); err != nil {
9798
return err
9899
}
99-
if err := o.RunServer(stopCh); err != nil {
100+
if err := o.RunServer(ctx.Done()); err != nil {
100101
return err
101102
}
102103
return nil

cmd/app/syncer.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright The Karbour Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package app
16+
17+
import (
18+
"context"
19+
20+
"github.com/KusionStack/karbour/pkg/scheme"
21+
"github.com/KusionStack/karbour/pkg/search/storage/elasticsearch"
22+
"github.com/KusionStack/karbour/pkg/syncer"
23+
esclient "github.com/elastic/go-elasticsearch/v8"
24+
"github.com/spf13/cobra"
25+
"github.com/spf13/pflag"
26+
"k8s.io/klog/v2"
27+
ctrl "sigs.k8s.io/controller-runtime"
28+
"sigs.k8s.io/controller-runtime/pkg/healthz"
29+
)
30+
31+
type syncerOptions struct {
32+
MetricsAddr string
33+
ProbeAddr string
34+
ESAddress string
35+
}
36+
37+
func NewSyncerOptions() *syncerOptions {
38+
return &syncerOptions{}
39+
}
40+
41+
func (o *syncerOptions) AddFlags(fs *pflag.FlagSet) {
42+
fs.StringVar(&o.MetricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
43+
fs.StringVar(&o.ProbeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
44+
fs.StringVar(&o.ESAddress, "es-address", "", "The address of Elasticsearch.")
45+
}
46+
47+
func NewSyncerCommand(ctx context.Context) *cobra.Command {
48+
options := NewSyncerOptions()
49+
cmd := &cobra.Command{
50+
Use: "syncer",
51+
Short: "start a resource syncer to sync resource from clusters",
52+
RunE: func(cmd *cobra.Command, args []string) error {
53+
return run(ctx, options)
54+
},
55+
}
56+
options.AddFlags(cmd.Flags())
57+
return cmd
58+
}
59+
60+
func run(ctx context.Context, options *syncerOptions) error {
61+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
62+
Scheme: scheme.Scheme,
63+
MetricsBindAddress: options.MetricsAddr,
64+
HealthProbeBindAddress: options.ProbeAddr,
65+
})
66+
if err != nil {
67+
klog.ErrorS(err, "unable to start manager")
68+
return err
69+
}
70+
71+
// TODO: add startup parameters to change the type of storage
72+
es, err := elasticsearch.NewESClient(esclient.Config{
73+
Addresses: []string{options.ESAddress},
74+
})
75+
if err != nil {
76+
klog.ErrorS(err, "unable to init elasticsearch client")
77+
return err
78+
}
79+
80+
if err = syncer.NewSyncReconciler(es).SetupWithManager(mgr); err != nil {
81+
klog.ErrorS(err, "unable to create syncer")
82+
return err
83+
}
84+
85+
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
86+
klog.ErrorS(err, "unable to set up health check")
87+
return err
88+
}
89+
90+
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
91+
klog.ErrorS(err, "unable to set up ready check")
92+
return err
93+
}
94+
95+
klog.Infof("starting manager")
96+
if err := mgr.Start(ctx); err != nil {
97+
klog.ErrorS(err, "problem running manager")
98+
return err
99+
}
100+
101+
return nil
102+
}

cmd/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525
)
2626

2727
func main() {
28-
stopCh := genericapiserver.SetupSignalHandler()
29-
cmd := app.NewApiserverCommand(stopCh)
28+
ctx := genericapiserver.SetupSignalContext()
29+
cmd := app.NewApiserverCommand(ctx)
30+
syncCmd := app.NewSyncerCommand(ctx)
31+
cmd.AddCommand(syncCmd)
3032
code := cli.Run(cmd)
3133
os.Exit(code)
3234
}

go.mod

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ require (
2222
github.com/stretchr/testify v1.8.4
2323
github.com/swaggo/http-swagger v1.3.4
2424
github.com/swaggo/swag v1.16.2
25+
go.uber.org/multierr v1.6.0
2526
gopkg.in/yaml.v3 v3.0.1
26-
k8s.io/api v0.26.0
27-
k8s.io/apimachinery v0.26.0
28-
k8s.io/apiserver v0.26.0
29-
k8s.io/client-go v0.26.0
30-
k8s.io/code-generator v0.26.0
31-
k8s.io/component-base v0.26.0
27+
k8s.io/api v0.26.1
28+
k8s.io/apimachinery v0.26.1
29+
k8s.io/apiserver v0.26.1
30+
k8s.io/client-go v0.26.1
31+
k8s.io/code-generator v0.26.1
32+
k8s.io/component-base v0.26.1
3233
k8s.io/klog/v2 v2.80.1
3334
k8s.io/kube-openapi v0.0.0-20230106171958-10e5f0effbd2
3435
k8s.io/metrics v0.26.0
35-
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
36+
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
37+
sigs.k8s.io/controller-runtime v0.14.6
3638
sigs.k8s.io/structured-merge-diff/v4 v4.2.3
3739
sigs.k8s.io/yaml v1.3.0
3840
)
@@ -54,6 +56,7 @@ require (
5456
github.com/elastic/elastic-transport-go/v8 v8.2.0 // indirect
5557
github.com/elastic/go-elasticsearch/v7 v7.6.0 // indirect
5658
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
59+
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
5760
github.com/fatih/structs v1.1.0 // indirect
5861
github.com/felixge/httpsnoop v1.0.3 // indirect
5962
github.com/fsnotify/fsnotify v1.6.0 // indirect
@@ -90,7 +93,6 @@ require (
9093
github.com/prometheus/client_model v0.3.0 // indirect
9194
github.com/prometheus/common v0.37.0 // indirect
9295
github.com/prometheus/procfs v0.8.0 // indirect
93-
github.com/rogpeppe/go-internal v1.8.0 // indirect
9496
github.com/shopspring/decimal v1.2.0 // indirect
9597
github.com/sirupsen/logrus v1.9.0 // indirect
9698
github.com/spf13/cast v1.3.1 // indirect
@@ -111,8 +113,7 @@ require (
111113
go.opentelemetry.io/otel/trace v1.10.0 // indirect
112114
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
113115
go.uber.org/atomic v1.7.0 // indirect
114-
go.uber.org/multierr v1.6.0 // indirect
115-
go.uber.org/zap v1.19.0 // indirect
116+
go.uber.org/zap v1.24.0 // indirect
116117
golang.org/x/crypto v0.16.0 // indirect
117118
golang.org/x/mod v0.14.0 // indirect
118119
golang.org/x/net v0.19.0 // indirect
@@ -121,18 +122,19 @@ require (
121122
golang.org/x/sys v0.15.0 // indirect
122123
golang.org/x/term v0.15.0 // indirect
123124
golang.org/x/text v0.14.0 // indirect
124-
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
125-
golang.org/x/tools v0.16.0 // indirect
125+
golang.org/x/time v0.3.0 // indirect
126+
golang.org/x/tools v0.16.0 // indirect; indirectt
127+
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
126128
google.golang.org/appengine v1.6.7 // indirect
127129
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
128130
google.golang.org/grpc v1.51.0 // indirect
129131
google.golang.org/protobuf v1.28.1 // indirect
130132
gopkg.in/inf.v0 v0.9.1 // indirect
131133
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
132134
gopkg.in/yaml.v2 v2.4.0 // indirect
133-
k8s.io/apiextensions-apiserver v0.23.5 // indirect
135+
k8s.io/apiextensions-apiserver v0.26.1 // indirect
134136
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
135-
k8s.io/kms v0.26.0 // indirect
137+
k8s.io/kms v0.26.1 // indirect
136138
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.35 // indirect
137139
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
138140
)

0 commit comments

Comments
 (0)