@@ -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.
3745var log zerolog.Logger
3846
3947const (
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
4555type 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
5465type ServiceSettings struct {
@@ -75,6 +86,7 @@ func GetRunCommand() *cobra.Command {
7586
7687 return true
7788 }(),
89+ PersistentMetadata : map [string ]string {},
7890 }
7991
8092 var (
@@ -222,7 +234,7 @@ func GetRunCommand() *cobra.Command {
222234 log .Debug ().
223235 Str ("namespace" , opts .Namespace ).
224236 Str ("name" , cloudMetadataCredsSecret ).
225- Msg ("getting service account from secret..." )
237+ Msg ("getting cloud map credentials from secret..." )
226238
227239 ctx , canc := context .WithTimeout (context .Background (), 10 * time .Second )
228240 defer canc ()
@@ -244,21 +256,26 @@ func GetRunCommand() *cobra.Command {
244256 // -- Get data automatically?
245257 netwCfg , err := func () (* cluster.NetworkConfiguration , error ) {
246258 if len (credentialsBytes ) == 0 {
247- return nil , nil
259+ if (opts .CloudMetadata .Network != nil && * opts .CloudMetadata .Network == "auto" ) ||
260+ (opts .CloudMetadata .SubNetwork != nil && * opts .CloudMetadata .SubNetwork == "auto" ) {
261+ return nil , fmt .Errorf ("cannot infer network and/or subnetwork without credentials file. Please provide it via flags or option." )
262+ }
248263 }
249264
250265 ctx , canc := context .WithTimeout (context .Background (), 15 * time .Second )
251266 defer canc ()
252267
253268 switch cluster .WhereAmIRunning () {
254269 case cluster .GKECluster :
270+ opts .PersistentMetadata ["cnwan.io/platform" ] = string (cluster .GKECluster )
255271 nw , err := cluster .GetNetworkFromGKE (ctx , option .WithCredentialsJSON (credentialsBytes ))
256272 if err != nil {
257273 return nil , fmt .Errorf ("cannot get network configuration from GKE: %w" , err )
258274 }
259275
260276 return nw , nil
261277 case cluster .EKSCluster :
278+ opts .PersistentMetadata ["cnwan.io/platform" ] = string (cluster .EKSCluster )
262279 nw , err := cluster .GetNetworkFromEKS (ctx )
263280 if err != nil {
264281 return nil , fmt .Errorf ("cannot get network configuration from EKS: %w" , err )
@@ -273,12 +290,19 @@ func GetRunCommand() *cobra.Command {
273290 return err
274291 }
275292
276- if opts .CloudMetadata .Network != nil && * opts .CloudMetadata .Network == "auto" {
277- opts .CloudMetadata .Network = & netwCfg .NetworkName
293+ if opts .CloudMetadata .Network != nil {
294+ if * opts .CloudMetadata .Network == "auto" {
295+ opts .CloudMetadata .Network = & netwCfg .NetworkName
296+ }
297+
298+ opts .PersistentMetadata ["cnwan.io/network" ] = * opts .CloudMetadata .Network
278299 }
279300
280- if opts .CloudMetadata .SubNetwork != nil && * opts .CloudMetadata .SubNetwork == "auto" {
281- opts .CloudMetadata .SubNetwork = & netwCfg .SubNetworkName
301+ if opts .CloudMetadata .SubNetwork != nil {
302+ if * opts .CloudMetadata .SubNetwork == "auto" {
303+ opts .CloudMetadata .SubNetwork = & netwCfg .SubNetworkName
304+ }
305+ opts .PersistentMetadata ["cnwan.io/sub-network" ] = * opts .CloudMetadata .SubNetwork
282306 }
283307 }
284308
@@ -328,3 +352,64 @@ func GetRunCommand() *cobra.Command {
328352
329353 return cmd
330354}
355+
356+ func run (sr servregistry.ServiceRegistry , opts * Options ) error {
357+ persistentMeta := []servregistry.MetadataPair {}
358+ for key , val := range opts .PersistentMetadata {
359+ persistentMeta = append (persistentMeta , servregistry.MetadataPair {
360+ Key : key ,
361+ Value : val ,
362+ })
363+ }
364+
365+ srBroker , err := servregistry .NewBroker (sr , servregistry.MetadataPair {Key : opKey , Value : opVal }, persistentMeta ... )
366+ if err != nil {
367+ return fmt .Errorf ("cannot start service registry broker: %w" , err )
368+ }
369+
370+ scheme := k8sruntime .NewScheme ()
371+ _ = clientgoscheme .AddToScheme (scheme )
372+ _ = corev1 .AddToScheme (scheme )
373+ // +kubebuilder:scaffold:scheme
374+
375+ // Controller manager
376+ mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
377+ Scheme : scheme ,
378+ LeaderElection : false ,
379+ MetricsBindAddress : "0" ,
380+ })
381+ if err != nil {
382+ return fmt .Errorf ("cannot start controller manager: %w" , err )
383+ }
384+
385+ // Service controller
386+ if err = (& controllers.ServiceReconciler {
387+ Client : mgr .GetClient (),
388+ Log : ctrl .Log .WithName ("controllers" ).WithName ("Service" ),
389+ Scheme : mgr .GetScheme (),
390+ ServRegBroker : srBroker ,
391+ WatchNamespacesByDefault : opts .WatchNamespacesByDefault ,
392+ AllowedAnnotations : opts .ServiceSettings .Annotations ,
393+ }).SetupWithManager (mgr ); err != nil {
394+ return fmt .Errorf ("cannot create service controller: %w" , err )
395+ }
396+
397+ // Namespace controller
398+ if err = (& controllers.NamespaceReconciler {
399+ Client : mgr .GetClient (),
400+ Log : ctrl .Log .WithName ("controllers" ).WithName ("Namespace" ),
401+ Scheme : mgr .GetScheme (),
402+ ServRegBroker : srBroker ,
403+ WatchNamespacesByDefault : opts .WatchNamespacesByDefault ,
404+ AllowedAnnotations : opts .ServiceSettings .Annotations ,
405+ }).SetupWithManager (mgr ); err != nil {
406+ return fmt .Errorf ("cannot create namespace controller: %w" , err )
407+ }
408+ // +kubebuilder:scaffold:builder
409+
410+ if err := mgr .Start (ctrl .SetupSignalHandler ()); err != nil {
411+ return fmt .Errorf ("error while starting controller manager: %w" , err )
412+ }
413+
414+ return nil
415+ }
0 commit comments