Skip to content

Commit 1573742

Browse files
authored
Merge pull request #257 from fluxcd/gotk-standard
Conform with GitOps Toolkit standards
2 parents 1506f3c + d2cc335 commit 1573742

21 files changed

+307
-276
lines changed

api/v1beta1/artifactgenerator_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424

25-
"github.com/fluxcd/pkg/apis/meta"
25+
gotkmeta "github.com/fluxcd/pkg/apis/meta"
2626
)
2727

2828
const (
@@ -157,7 +157,7 @@ type CopyOperation struct {
157157

158158
// ArtifactGeneratorStatus defines the observed state of ArtifactGenerator.
159159
type ArtifactGeneratorStatus struct {
160-
meta.ReconcileRequestStatus `json:",inline"`
160+
gotkmeta.ReconcileRequestStatus `json:",inline"`
161161

162162
// Conditions holds the conditions for the ArtifactGenerator.
163163
// +optional

cmd/main.go

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,24 @@ import (
2929
_ "k8s.io/client-go/plugin/pkg/client/auth"
3030
"k8s.io/utils/ptr"
3131
ctrlruntime "sigs.k8s.io/controller-runtime"
32+
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
3233
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3334
ctrlcfg "sigs.k8s.io/controller-runtime/pkg/config"
3435
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3536

36-
"github.com/fluxcd/pkg/artifact/config"
37-
"github.com/fluxcd/pkg/artifact/digest"
38-
"github.com/fluxcd/pkg/artifact/server"
39-
"github.com/fluxcd/pkg/artifact/storage"
40-
"github.com/fluxcd/pkg/runtime/acl"
41-
"github.com/fluxcd/pkg/runtime/client"
42-
ctrl "github.com/fluxcd/pkg/runtime/controller"
43-
"github.com/fluxcd/pkg/runtime/features"
44-
"github.com/fluxcd/pkg/runtime/jitter"
45-
"github.com/fluxcd/pkg/runtime/leaderelection"
46-
"github.com/fluxcd/pkg/runtime/logger"
47-
"github.com/fluxcd/pkg/runtime/pprof"
48-
"github.com/fluxcd/pkg/runtime/probes"
37+
gotkconfig "github.com/fluxcd/pkg/artifact/config"
38+
gotkdigest "github.com/fluxcd/pkg/artifact/digest"
39+
gotkserver "github.com/fluxcd/pkg/artifact/server"
40+
gotkstorage "github.com/fluxcd/pkg/artifact/storage"
41+
gotkacl "github.com/fluxcd/pkg/runtime/acl"
42+
gotkclient "github.com/fluxcd/pkg/runtime/client"
43+
gotkctrl "github.com/fluxcd/pkg/runtime/controller"
44+
gotkfeatures "github.com/fluxcd/pkg/runtime/features"
45+
gotkjitter "github.com/fluxcd/pkg/runtime/jitter"
46+
gotkelection "github.com/fluxcd/pkg/runtime/leaderelection"
47+
gotklogger "github.com/fluxcd/pkg/runtime/logger"
48+
gotkpprof "github.com/fluxcd/pkg/runtime/pprof"
49+
gotkprobes "github.com/fluxcd/pkg/runtime/probes"
4950
sourcev1 "github.com/fluxcd/source-controller/api/v1"
5051

5152
swapi "github.com/fluxcd/source-watcher/api/v1beta1"
@@ -71,51 +72,59 @@ func main() {
7172
var (
7273
metricsAddr string
7374
healthAddr string
75+
eventsAddr string
7476
concurrent int
7577
httpRetry int
7678
reconciliationTimeout time.Duration
7779
requeueDependency time.Duration
78-
artifactOptions config.Options
79-
aclOptions acl.Options
80-
clientOptions client.Options
81-
logOptions logger.Options
82-
leaderElectionOptions leaderelection.Options
83-
rateLimiterOptions ctrl.RateLimiterOptions
84-
intervalJitterOptions jitter.IntervalOptions
85-
featureGates features.FeatureGates
80+
81+
// GitOps Toolkit (gotk) runtime options.
82+
// https://pkg.go.dev/github.com/fluxcd/pkg/runtime
83+
84+
aclOptions gotkacl.Options
85+
artifactOptions gotkconfig.Options
86+
clientOptions gotkclient.Options
87+
featureGates gotkfeatures.FeatureGates
88+
intervalJitterOptions gotkjitter.IntervalOptions
89+
leaderElectionOptions gotkelection.Options
90+
logOptions gotklogger.Options
91+
rateLimiterOptions gotkctrl.RateLimiterOptions
92+
watchOptions gotkctrl.WatchOptions
8693
)
8794

8895
flag.IntVar(&concurrent, "concurrent", 10, "The number of concurrent resource reconciles.")
8996
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
9097
flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint binds to.")
98+
flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.")
9199
flag.IntVar(&httpRetry, "http-retry", 9,
92100
"The maximum number of retries when failing to fetch artifacts over HTTP.")
93101
flag.DurationVar(&reconciliationTimeout, "reconciliation-timeout", 10*time.Minute,
94102
"The maximum duration of a reconciliation.")
95103
flag.DurationVar(&requeueDependency, "requeue-dependency", 5*time.Second,
96104
"The interval at which failing dependencies are reevaluated.")
97105

98-
artifactOptions.BindFlags(flag.CommandLine)
99106
aclOptions.BindFlags(flag.CommandLine)
107+
artifactOptions.BindFlags(flag.CommandLine)
100108
clientOptions.BindFlags(flag.CommandLine)
101-
logOptions.BindFlags(flag.CommandLine)
109+
featureGates.BindFlags(flag.CommandLine)
110+
intervalJitterOptions.BindFlags(flag.CommandLine)
102111
leaderElectionOptions.BindFlags(flag.CommandLine)
112+
logOptions.BindFlags(flag.CommandLine)
103113
rateLimiterOptions.BindFlags(flag.CommandLine)
104-
intervalJitterOptions.BindFlags(flag.CommandLine)
105-
featureGates.BindFlags(flag.CommandLine)
114+
watchOptions.BindFlags(flag.CommandLine)
106115

107116
flag.Parse()
108117

109-
ctrlruntime.SetLogger(logger.NewLogger(logOptions))
118+
ctrlruntime.SetLogger(gotklogger.NewLogger(logOptions))
110119

111-
digestAlgo, err := digest.AlgorithmForName(artifactOptions.ArtifactDigestAlgo)
120+
digestAlgo, err := gotkdigest.AlgorithmForName(artifactOptions.ArtifactDigestAlgo)
112121
if err != nil {
113122
setupLog.Error(err, "unable to configure canonical digest algorithm")
114123
os.Exit(1)
115124
}
116-
digest.Canonical = digestAlgo
125+
gotkdigest.Canonical = digestAlgo
117126

118-
artifactStorage, err := storage.New(&artifactOptions)
127+
artifactStorage, err := gotkstorage.New(&artifactOptions)
119128
if err != nil {
120129
setupLog.Error(err, "unable to configure artifact storage")
121130
os.Exit(1)
@@ -127,13 +136,14 @@ func main() {
127136
os.Exit(1)
128137
}
129138

130-
ctx := ctrlruntime.SetupSignalHandler()
131139
leaderElectionID := fmt.Sprintf("%s-%s", controllerName, "leader-election")
132-
mgr, err := ctrlruntime.NewManager(ctrlruntime.GetConfigOrDie(), ctrlruntime.Options{
140+
141+
// Configure the manager with the GitOps Toolkit runtime options.
142+
mgrConfig := ctrlruntime.Options{
133143
Scheme: scheme,
134144
Metrics: metricsserver.Options{
135145
BindAddress: metricsAddr,
136-
ExtraHandlers: pprof.GetHandlers(),
146+
ExtraHandlers: gotkpprof.GetHandlers(),
137147
},
138148
HealthProbeBindAddress: healthAddr,
139149
LeaderElection: leaderElectionOptions.Enable,
@@ -149,19 +159,32 @@ func main() {
149159
},
150160
Client: ctrlclient.Options{
151161
Cache: &ctrlclient.CacheOptions{
162+
// We don't cache Secrets and ConfigMaps
163+
// as it would lead to unnecessary memory consumption.
152164
DisableFor: []ctrlclient.Object{&corev1.Secret{}, &corev1.ConfigMap{}},
153165
},
154166
},
155-
})
167+
}
168+
169+
// Limit the watch scope to the runtime namespace if specified.
170+
if !watchOptions.AllNamespaces {
171+
mgrConfig.Cache.DefaultNamespaces = map[string]ctrlcache.Config{
172+
os.Getenv(gotkctrl.EnvRuntimeNamespace): ctrlcache.Config{},
173+
}
174+
}
175+
176+
ctx := ctrlruntime.SetupSignalHandler()
177+
mgr, err := ctrlruntime.NewManager(ctrlruntime.GetConfigOrDie(), mgrConfig)
156178
if err != nil {
157-
setupLog.Error(err, "unable to start manager")
179+
setupLog.Error(err, "unable to create manager")
158180
os.Exit(1)
159181
}
160182

161183
// Note that the liveness check will pass beyond this point, but the readiness
162184
// check will continue to fail until this controller instance is elected leader.
163-
probes.SetupChecks(mgr, setupLog)
185+
gotkprobes.SetupChecks(mgr, setupLog)
164186

187+
// Register the ArtifactGenerator controller with the manager.
165188
if err = (&controller.ArtifactGeneratorReconciler{
166189
ControllerName: controllerName,
167190
Client: mgr.GetClient(),
@@ -173,7 +196,7 @@ func main() {
173196
DependencyRequeueInterval: requeueDependency,
174197
NoCrossNamespaceRefs: aclOptions.NoCrossNamespaceRefs,
175198
}).SetupWithManager(ctx, mgr, controller.ArtifactGeneratorReconcilerOptions{
176-
RateLimiter: ctrl.GetRateLimiter(rateLimiterOptions),
199+
RateLimiter: gotkctrl.GetRateLimiter(rateLimiterOptions),
177200
}); err != nil {
178201
setupLog.Error(err, "unable to create controller", "controller", swapi.ArtifactGeneratorKind)
179202
os.Exit(1)
@@ -190,7 +213,7 @@ func main() {
190213
// This will make the readiness check pass and Kubernetes will start
191214
// routing traffic from kustomize-controller and helm-controller to this instance.
192215
setupLog.Info("starting storage server on " + artifactOptions.StorageAddress)
193-
if err := server.Start(ctx, &artifactOptions); err != nil {
216+
if err := gotkserver.Start(ctx, &artifactOptions); err != nil {
194217
setupLog.Error(err, "artifact server error")
195218
os.Exit(1)
196219
}

config/manager/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ spec:
4848
fieldRef:
4949
fieldPath: metadata.namespace
5050
args:
51+
- --watch-all-namespaces
5152
- --log-level=info
5253
- --log-encoding=json
5354
- --enable-leader-election

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with advanced source composition and decomposition patterns.
2121
| `--artifact-retention-ttl` | duration | The duration of time that artifacts from previous reconciliations will be kept in storage before being garbage collected. (default 1m0s) |
2222
| `--concurrent` | int | The number of concurrent reconciles per controller. (default 10) |
2323
| `--enable-leader-election` | boolean | Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager. |
24+
| `--events-addr` | string | The address of the events receiver. |
2425
| `--health-addr` | string | The address the health endpoint binds to. (default ":9440") |
2526
| `--http-retry` | int | The maximum number of retries when failing to fetch artifacts over HTTP. (default 9) |
2627
| `--interval-jitter-percentage` | uint8 | Percentage of jitter to apply to interval durations. A value of 10 will apply a jitter of +/-10% to the interval duration. It cannot be negative, and must be less than 100. (default 5) |
@@ -39,6 +40,7 @@ with advanced source composition and decomposition patterns.
3940
| `--storage-addr` | string | The address the static file server binds to. (default ":9090") |
4041
| `--storage-adv-addr` | string | The advertised address of the static file server. |
4142
| `--storage-path` | string | The local storage path. (default "/data") |
43+
| `--watch-all-namespaces` | boolean | Watch for resources in all namespaces, if set to false it will only watch the runtime namespace. (default true) |
4244
| `--feature-gates` | mapStringBool | A comma separated list of key=value pairs defining the state of experimental features. |
4345

4446
### Feature Gates

internal/builder/builder.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"golang.org/x/mod/sumdb/dirhash"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030

31-
"github.com/fluxcd/pkg/apis/meta"
32-
"github.com/fluxcd/pkg/artifact/storage"
31+
gotkmeta "github.com/fluxcd/pkg/apis/meta"
32+
gotkstorage "github.com/fluxcd/pkg/artifact/storage"
3333
sourcev1 "github.com/fluxcd/source-controller/api/v1"
3434

3535
swapi "github.com/fluxcd/source-watcher/api/v1beta1"
@@ -38,11 +38,11 @@ import (
3838
// ArtifactBuilder is responsible for building and storing artifacts
3939
// based on a given specification and source files.
4040
type ArtifactBuilder struct {
41-
Storage *storage.Storage
41+
Storage *gotkstorage.Storage
4242
}
4343

4444
// New creates a new ArtifactBuilder with the given storage backend.
45-
func New(storage *storage.Storage) *ArtifactBuilder {
45+
func New(storage *gotkstorage.Storage) *ArtifactBuilder {
4646
return &ArtifactBuilder{
4747
Storage: storage,
4848
}
@@ -58,7 +58,7 @@ func (r *ArtifactBuilder) Build(ctx context.Context,
5858
spec *swapi.OutputArtifact,
5959
sources map[string]string,
6060
namespace string,
61-
workspace string) (*meta.Artifact, error) {
61+
workspace string) (*gotkmeta.Artifact, error) {
6262
// Create a dir to stage the artifact files.
6363
stagingDir := filepath.Join(workspace, spec.Name)
6464
if err := os.MkdirAll(stagingDir, 0o755); err != nil {
@@ -100,7 +100,7 @@ func (r *ArtifactBuilder) Build(ctx context.Context,
100100
defer unlock()
101101

102102
// Create the artifact tarball from the staging dir.
103-
if err := r.Storage.Archive(&artifact, stagingDir, storage.SourceIgnoreFilter(nil, nil)); err != nil {
103+
if err := r.Storage.Archive(&artifact, stagingDir, gotkstorage.SourceIgnoreFilter(nil, nil)); err != nil {
104104
return nil, fmt.Errorf("failed to create artifact: %w", err)
105105
}
106106

0 commit comments

Comments
 (0)