-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (96 loc) · 4.18 KB
/
main.go
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
// Copyright (C) 2022 Red Hat
// SPDX-License-Identifier: Apache-2.0
package main
import (
"os"
"github.com/spf13/cobra"
// 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"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/softwarefactory-project/sf-operator/cli/cmd"
dev "github.com/softwarefactory-project/sf-operator/cli/cmd/dev"
cliutils "github.com/softwarefactory-project/sf-operator/cli/cmd/utils"
zuul "github.com/softwarefactory-project/sf-operator/cli/cmd/zuul"
"github.com/softwarefactory-project/sf-operator/controllers"
"github.com/softwarefactory-project/sf-operator/controllers/libs/utils"
//+kubebuilder:scaffold:imports
)
// getWatchNamespace returns the Namespace the operator should be watching for changes
func getWatchNamespace() (string, error) {
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which specifies the Namespace to watch.
// An empty value means the operator is running with cluster scope.
return utils.GetEnvVarValue("WATCH_NAMESPACE")
}
func operatorCmd(kmd *cobra.Command, args []string) {
cliCtx, err := cliutils.GetCLIContext(kmd)
if err != nil {
ctrl.Log.Error(err, "Error initializing:")
os.Exit(1)
}
ns := cliCtx.Namespace
metricsAddr, _ := kmd.Flags().GetString("metrics-bind-address")
probeAddr, _ := kmd.Flags().GetString("health-probe-bind-address")
enableLeaderElection, _ := kmd.Flags().GetBool("leader-elect")
if ns == "" {
var err error
ns, err = getWatchNamespace()
if err != nil {
controllers.SetupLog.Info("Unable to get WATCH_NAMESPACE env, " +
"the manager will watch and manage resources in all namespaces")
} else {
controllers.SetupLog.Info("Got WATCH_NAMESPACE env, " +
"the manager will watch and manage resources in " + ns + " namespace")
}
}
controllers.Main(ns, metricsAddr, probeAddr, enableLeaderElection, false)
}
func main() {
var (
metricsAddr string
enableLeaderElection bool
probeAddr string
ns string
kubeContext string
fqdn string
cliContext string
configFile string
rootCmd = &cobra.Command{
Short: "SF Operator CLI",
Long: `Multi-purpose command line utility related to sf-operator, SF instances management, and development tools.`,
}
operatorCmd = &cobra.Command{
Use: "operator",
Short: "Start the SF Operator",
Long: `This command starts the sf-operator service locally, for the cluster defined in the current kube context. The SF CRDs must be installed on the cluster.`,
Run: operatorCmd,
}
)
// Global flags
rootCmd.PersistentFlags().StringVarP(&ns, "namespace", "n", "", "The namespace on which to perform actions.")
rootCmd.PersistentFlags().StringVarP(&kubeContext, "kube-context", "k", "", "The cluster context to use to perform calls to the K8s API.")
rootCmd.PersistentFlags().StringVarP(&fqdn, "fqdn", "d", "", "The FQDN of the deployment (if no manifest is provided).")
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "C", "", "Path to the CLI configuration file.")
rootCmd.PersistentFlags().StringVarP(&cliContext, "context", "c", "", "Context to use in the configuration file. Defaults to the \"default-context\" value in the config file if set, or the first available context in the config file.")
rootCmd.PersistentFlags().Bool("debug", false, "Enable DEBUG logs")
// Flags for the operator command
operatorCmd.Flags().StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
operatorCmd.Flags().StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
operatorCmd.Flags().BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
// Add sub commands
subcommands := []*cobra.Command{
cmd.MkInitCmd(),
cmd.MkSFCmd(),
cmd.MkNodepoolCmd(),
operatorCmd,
dev.MkDevCmd(),
zuul.MkZuulCmd(),
}
for _, c := range subcommands {
rootCmd.AddCommand(c)
}
rootCmd.Execute()
}