forked from kcp-dev/init-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (134 loc) · 5.7 KB
/
main.go
File metadata and controls
171 lines (134 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2026 The kcp Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
golog "log"
"github.com/go-logr/zapr"
"github.com/spf13/pflag"
"go.uber.org/zap"
"github.com/kcp-dev/init-agent/internal/controller/initcontroller"
"github.com/kcp-dev/init-agent/internal/controller/targetcontroller"
"github.com/kcp-dev/init-agent/internal/initialize/source"
"github.com/kcp-dev/init-agent/internal/initialize/source/inittemplate"
"github.com/kcp-dev/init-agent/internal/kcp"
syncagentlog "github.com/kcp-dev/init-agent/internal/log"
"github.com/kcp-dev/init-agent/internal/manifest"
"github.com/kcp-dev/init-agent/internal/version"
initializationv1alpha1 "github.com/kcp-dev/init-agent/sdk/apis/initialization/v1alpha1"
"github.com/kcp-dev/logicalcluster/v3"
kcpcorev1alpha1 "github.com/kcp-dev/sdk/apis/core/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrlruntime "sigs.k8s.io/controller-runtime"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
)
const (
// numInitWorkers is the number of parallel works in the initcontroller.
numInitWorkers = 4
)
func main() {
ctx := context.Background()
opts := NewOptions()
opts.AddFlags(pflag.CommandLine)
// ctrl-runtime will have added its --kubeconfig to Go's flag set
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
if err := opts.Validate(); err != nil {
golog.Fatalf("Invalid command line: %v", err)
}
log := syncagentlog.NewFromOptions(opts.LogOptions)
if err := opts.Complete(); err != nil {
log.With(zap.Error(err)).Fatal("Invalid command line")
}
sugar := log.Sugar()
// set the logger used by sigs.k8s.io/controller-runtime
ctrlruntimelog.SetLogger(zapr.NewLogger(log.WithOptions(zap.AddCallerSkip(1))))
if err := run(ctx, sugar, opts); err != nil {
sugar.Fatalw("Init Agent has encountered an error", zap.Error(err))
}
}
func run(ctx context.Context, log *zap.SugaredLogger, opts *Options) error {
hello := log.With(
"version", version.GitVersion,
"configws", opts.ConfigWorkspace,
"targetselector", opts.InitTargetSelector.String(),
)
hello.Info("Hei, I'm the kcp Init Agent")
cfg := ctrlruntime.GetConfigOrDie()
clusterClient := kcp.NewClusterClient(kcp.StripCluster(cfg))
// prepare the source factory, responsible for resolving and instantiating all
// possible init sources of an InitTarget
sourceFactory, err := setupSourceFactory(clusterClient)
if err != nil {
return fmt.Errorf("failed to setup source factory: %w", err)
}
// manifestApplier controls how the manifests of an init source are applied in
// the target workspace
manifestApplier := manifest.NewApplier()
// create the ctrl-runtime manager
mgr, err := setupManager(ctx, cfg, opts)
if err != nil {
return fmt.Errorf("failed to setup local manager: %w", err)
}
// This controller watches InitTargets and spawns multicluster-managers for each of them,
// which in turn run the actual business logic controllers.
// wrap this controller creation in a closure to prevent giving all the initcontroller
// dependencies to the targetcontroller
newInitController := func(remoteManager mcmanager.Manager, targetProvider initcontroller.InitTargetProvider, initializer kcpcorev1alpha1.LogicalClusterInitializer) error {
return initcontroller.Create(remoteManager, targetProvider, sourceFactory, manifestApplier, initializer, log, numInitWorkers)
}
if err := targetcontroller.Add(ctx, mgr, log, opts.InitTargetSelector, clusterClient, newInitController); err != nil {
return fmt.Errorf("failed to add targetcontroller controller: %w", err)
}
log.Info("Starting kcp Init Agent…")
return mgr.Start(ctx)
}
// setupManager creates a regular controller-runtime manager that is the basis
// of the init-agent. This manager will run a controller to watch for InitTargets and
// then dynamically spawn new multicluster-managers for each InitTarget.
func setupManager(ctx context.Context, cfg *rest.Config, opts *Options) (manager.Manager, error) {
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("failed to register local scheme %s: %w", corev1.SchemeGroupVersion, err)
}
if err := initializationv1alpha1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("failed to register local scheme %s: %w", initializationv1alpha1.SchemeGroupVersion, err)
}
cfg = kcp.RetargetRestConfig(cfg, logicalcluster.Name(opts.ConfigWorkspace))
return manager.New(cfg, manager.Options{
Scheme: scheme,
BaseContext: func() context.Context {
return ctx
},
Metrics: metricsserver.Options{BindAddress: opts.MetricsAddr},
LeaderElection: opts.EnableLeaderElection,
LeaderElectionID: "init-agent.kcp.io",
LeaderElectionNamespace: opts.LeaderElectionNamespace,
HealthProbeBindAddress: opts.HealthAddr,
})
}
func setupSourceFactory(clusterClient kcp.ClusterClient) (*source.Factory, error) {
deps := source.Dependencies{
Template: inittemplate.Dependencies{
ClusterClient: clusterClient,
},
}
return source.NewFactory(deps), nil
}