Skip to content

Commit a41fbc8

Browse files
committed
refactor: create run function
The `run` function executes the program as it used to, but is now oblivious of the service registry you choose. Signed-off-by: Elis Lulja <elulja@cisco.com>
1 parent 50e41ab commit a41fbc8

File tree

1 file changed

+88
-6
lines changed

1 file changed

+88
-6
lines changed

pkg/command/run/run.go

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,40 @@ import (
2626
"os"
2727
"time"
2828

29+
"github.com/CloudNativeSDWAN/cnwan-operator/controllers"
2930
"github.com/CloudNativeSDWAN/cnwan-operator/pkg/cluster"
31+
"github.com/CloudNativeSDWAN/cnwan-operator/pkg/servregistry"
3032
"github.com/rs/zerolog"
3133
"github.com/spf13/cobra"
3234
"google.golang.org/api/option"
3335
"gopkg.in/yaml.v3"
36+
corev1 "k8s.io/api/core/v1"
37+
k8sruntime "k8s.io/apimachinery/pkg/runtime"
38+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
39+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3440
"k8s.io/client-go/rest"
41+
ctrl "sigs.k8s.io/controller-runtime"
3542
)
3643

44+
// TODO: when #81 is fixed ctrl.Log will be removed in favor of zerolog.
3745
var log zerolog.Logger
3846

3947
const (
4048
defaultNamespaceName string = "cnwan-operator-system"
4149
namespaceEnvName string = "CNWAN_OPERATOR_NAMESPACE"
4250
defaultSettingsConfigMapName string = "cnwan-operator-settings"
51+
opKey string = "owner"
52+
opVal string = "cnwan-operator"
4353
)
4454

4555
type Options struct {
4656
WatchNamespacesByDefault bool `yaml:"watchNamespacesByDefault"`
4757
ServiceSettings *ServiceSettings `yaml:",inline"`
4858
CloudMetadata *CloudMetadataSettings `yaml:"cloudMetadata"`
4959

50-
RunningInK8s bool
51-
Namespace string
60+
PersistentMetadata map[string]string
61+
RunningInK8s bool
62+
Namespace string
5263
}
5364

5465
type ServiceSettings struct {
@@ -75,6 +86,7 @@ func GetRunCommand() *cobra.Command {
7586

7687
return true
7788
}(),
89+
PersistentMetadata: map[string]string{},
7890
}
7991

8092
var (
@@ -252,13 +264,15 @@ func GetRunCommand() *cobra.Command {
252264

253265
switch cluster.WhereAmIRunning() {
254266
case cluster.GKECluster:
267+
opts.PersistentMetadata["cnwan.io/platform"] = string(cluster.GKECluster)
255268
nw, err := cluster.GetNetworkFromGKE(ctx, option.WithCredentialsJSON(credentialsBytes))
256269
if err != nil {
257270
return nil, fmt.Errorf("cannot get network configuration from GKE: %w", err)
258271
}
259272

260273
return nw, nil
261274
case cluster.EKSCluster:
275+
opts.PersistentMetadata["cnwan.io/platform"] = string(cluster.EKSCluster)
262276
nw, err := cluster.GetNetworkFromEKS(ctx)
263277
if err != nil {
264278
return nil, fmt.Errorf("cannot get network configuration from EKS: %w", err)
@@ -273,12 +287,19 @@ func GetRunCommand() *cobra.Command {
273287
return err
274288
}
275289

276-
if opts.CloudMetadata.Network != nil && *opts.CloudMetadata.Network == "auto" {
277-
opts.CloudMetadata.Network = &netwCfg.NetworkName
290+
if opts.CloudMetadata.Network != nil {
291+
if *opts.CloudMetadata.Network == "auto" {
292+
opts.CloudMetadata.Network = &netwCfg.NetworkName
293+
}
294+
295+
opts.PersistentMetadata["cnwan.io/network"] = *opts.CloudMetadata.Network
278296
}
279297

280-
if opts.CloudMetadata.SubNetwork != nil && *opts.CloudMetadata.SubNetwork == "auto" {
281-
opts.CloudMetadata.SubNetwork = &netwCfg.SubNetworkName
298+
if opts.CloudMetadata.SubNetwork != nil {
299+
if *opts.CloudMetadata.SubNetwork == "auto" {
300+
opts.CloudMetadata.SubNetwork = &netwCfg.SubNetworkName
301+
}
302+
opts.PersistentMetadata["cnwan.io/sub-network"] = *opts.CloudMetadata.Network
282303
}
283304
}
284305

@@ -328,3 +349,64 @@ func GetRunCommand() *cobra.Command {
328349

329350
return cmd
330351
}
352+
353+
func run(sr servregistry.ServiceRegistry, opts *Options) error {
354+
persistentMeta := []servregistry.MetadataPair{}
355+
for key, val := range opts.PersistentMetadata {
356+
persistentMeta = append(persistentMeta, servregistry.MetadataPair{
357+
Key: key,
358+
Value: val,
359+
})
360+
}
361+
362+
srBroker, err := servregistry.NewBroker(sr, servregistry.MetadataPair{Key: opKey, Value: opVal}, persistentMeta...)
363+
if err != nil {
364+
return fmt.Errorf("cannot start service registry broker: %w", err)
365+
}
366+
367+
scheme := k8sruntime.NewScheme()
368+
_ = clientgoscheme.AddToScheme(scheme)
369+
_ = corev1.AddToScheme(scheme)
370+
// +kubebuilder:scaffold:scheme
371+
372+
// Controller manager
373+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
374+
Scheme: scheme,
375+
LeaderElection: false,
376+
MetricsBindAddress: "0",
377+
})
378+
if err != nil {
379+
return fmt.Errorf("cannot start controller manager: %w", err)
380+
}
381+
382+
// Service controller
383+
if err = (&controllers.ServiceReconciler{
384+
Client: mgr.GetClient(),
385+
Log: ctrl.Log.WithName("controllers").WithName("Service"),
386+
Scheme: mgr.GetScheme(),
387+
ServRegBroker: srBroker,
388+
WatchNamespacesByDefault: opts.WatchNamespacesByDefault,
389+
AllowedAnnotations: opts.ServiceSettings.Annotations,
390+
}).SetupWithManager(mgr); err != nil {
391+
return fmt.Errorf("cannot create service controller: %w", err)
392+
}
393+
394+
// Namespace controller
395+
if err = (&controllers.NamespaceReconciler{
396+
Client: mgr.GetClient(),
397+
Log: ctrl.Log.WithName("controllers").WithName("Namespace"),
398+
Scheme: mgr.GetScheme(),
399+
ServRegBroker: srBroker,
400+
WatchNamespacesByDefault: opts.WatchNamespacesByDefault,
401+
AllowedAnnotations: opts.ServiceSettings.Annotations,
402+
}).SetupWithManager(mgr); err != nil {
403+
return fmt.Errorf("cannot create namespace controller: %w", err)
404+
}
405+
// +kubebuilder:scaffold:builder
406+
407+
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
408+
return fmt.Errorf("error while starting controller manager: %w", err)
409+
}
410+
411+
return nil
412+
}

0 commit comments

Comments
 (0)