-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move controller manager command to internal
Currently the code of the command that starts the operator is in a separate `main.go` file inside the `cmd` directory. This is different to all the other commands that start things, as they are sub-commands of the `oran-oims start` command and their code lives in the `internal/cmd` package. In addition that operator command also uses a different logging framework than the rest of the project. In order to improve consistency this patch changes that command to use the same infrastructure than the rest of the project, including logging. The command will now be `oran-o2ims start controller-manager`. Signed-off-by: Juan Hernandez <[email protected]>
- Loading branch information
Showing
8 changed files
with
199 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
Copyright 2024 Red Hat Inc. | ||
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 operator | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) | ||
// to ensure that exec-entrypoint and run can make use of them. | ||
_ "k8s.io/client-go/plugin/pkg/client/auth" | ||
"k8s.io/klog/v2" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/healthz" | ||
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
|
||
"github.com/go-logr/logr" | ||
oranv1alpha1 "github.com/openshift-kni/oran-o2ims/api/v1alpha1" | ||
"github.com/openshift-kni/oran-o2ims/internal" | ||
"github.com/openshift-kni/oran-o2ims/internal/controllers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ControllerManager creates and returns the `start controller-manager` command. | ||
func ControllerManager() *cobra.Command { | ||
c := NewControllerManager() | ||
result := &cobra.Command{ | ||
Use: "controller-manager", | ||
Short: "Starts the controller manager", | ||
Args: cobra.NoArgs, | ||
RunE: c.run, | ||
} | ||
flags := result.Flags() | ||
flags.StringVar( | ||
&c.metricsAddr, | ||
"metrics-bind-address", | ||
":8080", | ||
"The address the metric endpoint binds to.", | ||
) | ||
flags.StringVar( | ||
&c.probeAddr, | ||
"health-probe-bind-address", | ||
":8081", | ||
"The address the probe endpoint binds to.", | ||
) | ||
flags.BoolVar( | ||
&c.enableLeaderElection, | ||
"leader-elect", | ||
false, | ||
"Enable leader election for controller manager. "+ | ||
"Enabling this will ensure there is only one active controller manager.", | ||
) | ||
return result | ||
} | ||
|
||
// ControllerManagerCommand contains the data and logic needed to run the `start controller-manager` | ||
// command. | ||
type ControllerManagerCommand struct { | ||
metricsAddr string | ||
enableLeaderElection bool | ||
probeAddr string | ||
} | ||
|
||
// NewControllerManager creates a new runner that knows how to execute the `start | ||
// controller-manager` command. | ||
func NewControllerManager() *ControllerManagerCommand { | ||
return &ControllerManagerCommand{} | ||
} | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func init() { | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
utilruntime.Must(oranv1alpha1.AddToScheme(scheme)) | ||
} | ||
|
||
// run executes the `start controller-manager` command. | ||
func (c *ControllerManagerCommand) run(cmd *cobra.Command, argv []string) error { | ||
// Get the context: | ||
ctx := cmd.Context() | ||
|
||
// Get the dependencies from the context: | ||
logger := internal.LoggerFromContext(ctx) | ||
|
||
// Configure the controller runtime library to use our logger: | ||
adapter := logr.FromSlogHandler(logger.Handler()) | ||
ctrl.SetLogger(adapter) | ||
klog.SetLogger(adapter) | ||
|
||
// Restrict to the following namespaces - subject to change. | ||
namespaces := [...]string{"default", "oran", "o2ims", "oran-o2ims"} // List of Namespaces | ||
defaultNamespaces := make(map[string]cache.Config) | ||
|
||
for _, ns := range namespaces { | ||
defaultNamespaces[ns] = cache.Config{} | ||
} | ||
|
||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
Scheme: scheme, | ||
Metrics: metricsserver.Options{BindAddress: c.metricsAddr}, | ||
HealthProbeBindAddress: c.probeAddr, | ||
LeaderElection: c.enableLeaderElection, | ||
LeaderElectionID: "a73bc4d2.openshift.io", | ||
Cache: cache.Options{ | ||
DefaultNamespaces: defaultNamespaces, | ||
}, | ||
|
||
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily | ||
// when the Manager ends. This requires the binary to immediately end when the | ||
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly | ||
// speeds up voluntary leader transitions as the new leader don't have to wait | ||
// LeaseDuration time first. | ||
// | ||
// In the default scaffold provided, the program ends immediately after | ||
// the manager stops, so would be fine to enable this option. However, | ||
// if you are doing or is intended to do any operation such as perform cleanups | ||
// after the manager stops then its usage might be unsafe. | ||
// LeaderElectionReleaseOnCancel: true, | ||
}) | ||
if err != nil { | ||
logger.Error( | ||
"Unable to start manager", | ||
slog.String("error", err.Error()), | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
if err = (&controllers.ORANO2IMSReconciler{ | ||
Client: mgr.GetClient(), | ||
Log: ctrl.Log.WithName("controller").WithName("ORAN-O2IMS"), | ||
Scheme: mgr.GetScheme(), | ||
}).SetupWithManager(mgr); err != nil { | ||
logger.Error( | ||
"Unable to create controller", | ||
slog.String("controller", "ORANO2IMS"), | ||
slog.String("error", err.Error()), | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { | ||
logger.Error( | ||
"Unable to set up health check", | ||
slog.String("error", err.Error()), | ||
) | ||
os.Exit(1) | ||
} | ||
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { | ||
logger.Error( | ||
"Unable to set up ready check", | ||
slog.String("error", err.Error()), | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
logger.Info("Starting manager") | ||
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||
logger.Error( | ||
"Problem running manager", | ||
slog.String("error", err.Error()), | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.