-
Notifications
You must be signed in to change notification settings - Fork 33
first code commit #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,77 @@ | ||
| # Temporary Build Files | ||
| build/_output | ||
| build/_test | ||
| # Created by https://www.gitignore.io/api/go,vim,emacs,visualstudiocode | ||
| ### Emacs ### | ||
| # -*- mode: gitignore; -*- | ||
| *~ | ||
| \#*\# | ||
| /.emacs.desktop | ||
| /.emacs.desktop.lock | ||
| *.elc | ||
| auto-save-list | ||
| tramp | ||
| .\#* | ||
| # Org-mode | ||
| .org-id-locations | ||
| *_archive | ||
| # flymake-mode | ||
| *_flymake.* | ||
| # eshell files | ||
| /eshell/history | ||
| /eshell/lastdir | ||
| # elpa packages | ||
| /elpa/ | ||
| # reftex files | ||
| *.rel | ||
| # AUCTeX auto folder | ||
| /auto/ | ||
| # cask packages | ||
| .cask/ | ||
| dist/ | ||
| # Flycheck | ||
| flycheck_*.el | ||
| # server auth directory | ||
| /server/ | ||
| # projectiles files | ||
| .projectile | ||
| projectile-bookmarks.eld | ||
| # directory configuration | ||
| .dir-locals.el | ||
| # saveplace | ||
| places | ||
| # url cache | ||
| url/cache/ | ||
| # cedet | ||
| ede-projects.el | ||
| # smex | ||
| smex-items | ||
| # company-statistics | ||
| company-statistics-cache.el | ||
| # anaconda-mode | ||
| anaconda-mode/ | ||
| ### Go ### | ||
| # Binaries for programs and plugins | ||
| *.exe | ||
| *.exe~ | ||
| *.dll | ||
| *.so | ||
| *.dylib | ||
|
|
||
| # Test binary, built with `go test -c` | ||
| # Test binary, build with 'go test -c' | ||
| *.test | ||
|
|
||
| # Output of the go coverage tool, specifically when used with LiteIDE | ||
| *.out | ||
|
|
||
| # Dependency directories (remove the comment below to include it) | ||
| # vendor/ | ||
| ### Vim ### | ||
| # swap | ||
| .sw[a-p] | ||
| .*.sw[a-p] | ||
| # session | ||
| Session.vim | ||
| # temporary | ||
| .netrwhist | ||
| # auto-generated tag files | ||
| tags | ||
| ### VisualStudioCode ### | ||
| .vscode/* | ||
| .history | ||
| # End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # cincinnati-operator | ||
|
|
||
mhrivnak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Run locally | ||
|
|
||
| To run locally, you must set the operand image as shown below. | ||
|
|
||
| ``` | ||
| export OPERAND_IMAGE="quay.io/app-sre/cincinnati:2873c6b" | ||
| operator-sdk run --local | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| FROM docker.io/openshift/origin-release:golang-1.13 AS builder | ||
| WORKDIR /go/src/github.com/openshift/cincinnati-operator/ | ||
| COPY . . | ||
| RUN go build -mod=vendor -o /cincinnati-operator github.com/openshift/cincinnati-operator/cmd/manager | ||
|
|
||
| FROM registry.access.redhat.com/ubi8/ubi-minimal:latest | ||
|
|
||
| ENV OPERATOR=/usr/local/bin/cincinnati-operator \ | ||
| USER_UID=1001 \ | ||
| USER_NAME=cincinnati-operator | ||
|
|
||
| # install operator binary | ||
| COPY --from=builder /cincinnati-operator ${OPERATOR} | ||
|
|
||
| COPY build/bin /usr/local/bin | ||
| RUN /usr/local/bin/user_setup | ||
|
|
||
| ENTRYPOINT ["/usr/local/bin/entrypoint"] | ||
|
|
||
| USER ${USER_UID} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/bin/sh -e | ||
|
|
||
| exec ${OPERATOR} $@ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #!/bin/sh | ||
| set -x | ||
|
|
||
| # ensure $HOME exists and is accessible by group 0 (we don't know what the runtime UID will be) | ||
| echo "${USER_NAME}:x:${USER_UID}:0:${USER_NAME} user:${HOME}:/sbin/nologin" >> /etc/passwd | ||
| mkdir -p ${HOME} | ||
| chown ${USER_UID}:0 ${HOME} | ||
| chmod ug+rwx ${HOME} | ||
|
|
||
| # no need for this script to remain in the image after running | ||
| rm $0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) | ||
| _ "k8s.io/client-go/plugin/pkg/client/auth" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for my own edification. What is going on here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea. This was part of the scaffolding from operator-sdk. |
||
| "k8s.io/client-go/rest" | ||
|
|
||
| "github.com/operator-framework/operator-sdk/pkg/k8sutil" | ||
| kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" | ||
| "github.com/operator-framework/operator-sdk/pkg/leader" | ||
| "github.com/operator-framework/operator-sdk/pkg/log/zap" | ||
| "github.com/operator-framework/operator-sdk/pkg/metrics" | ||
| sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
| "github.com/spf13/pflag" | ||
| v1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/util/intstr" | ||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/config" | ||
| logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
|
|
||
| "github.com/openshift/cincinnati-operator/pkg/apis" | ||
| "github.com/openshift/cincinnati-operator/pkg/controller" | ||
| cincontroller "github.com/openshift/cincinnati-operator/pkg/controller/cincinnati" | ||
| "github.com/openshift/cincinnati-operator/version" | ||
| ) | ||
|
|
||
| // Change below variables to serve metrics on different host or port. | ||
| var ( | ||
| metricsHost = "0.0.0.0" | ||
| metricsPort int32 = 8383 | ||
| operatorMetricsPort int32 = 8686 | ||
| ) | ||
| var log = logf.Log.WithName("cmd") | ||
|
|
||
| func printVersion() { | ||
| log.Info(fmt.Sprintf("Operator Version: %s", version.Version)) | ||
| log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) | ||
| log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) | ||
| log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version)) | ||
| } | ||
|
|
||
| func main() { | ||
| // Add the zap logger flag set to the CLI. The flag set must | ||
| // be added before calling pflag.Parse(). | ||
| pflag.CommandLine.AddFlagSet(zap.FlagSet()) | ||
|
|
||
| // Add flags registered by imported packages (e.g. glog and | ||
| // controller-runtime) | ||
| pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
|
||
| pflag.Parse() | ||
|
|
||
| // Use a zap logr.Logger implementation. If none of the zap | ||
| // flags are configured (or if the zap flag set is not being | ||
| // used), this defaults to a production zap logger. | ||
| // | ||
| // The logger instantiated here can be changed to any logger | ||
| // implementing the logr.Logger interface. This logger will | ||
| // be propagated through the whole operator, generating | ||
| // uniform and structured logs. | ||
| logf.SetLogger(zap.Logger()) | ||
|
|
||
| printVersion() | ||
|
|
||
| namespace, err := k8sutil.GetWatchNamespace() | ||
| if err != nil { | ||
| log.Error(err, "Failed to get watch namespace") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Get a config to talk to the apiserver | ||
| cfg, err := config.GetConfig() | ||
| if err != nil { | ||
| log.Error(err, "") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| ctx := context.TODO() | ||
| // Become the leader before proceeding | ||
| err = leader.Become(ctx, "cincinnati-operator-lock") | ||
| if err != nil { | ||
| log.Error(err, "") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Set default manager options | ||
| options := manager.Options{ | ||
| Namespace: namespace, | ||
| MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), | ||
| } | ||
|
|
||
| // Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2) | ||
| // Note that this is not intended to be used for excluding namespaces, this is better done via a Predicate | ||
| // Also note that you may face performance issues when using this with a high number of namespaces. | ||
| // More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder | ||
| if strings.Contains(namespace, ",") { | ||
| options.Namespace = "" | ||
| options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ",")) | ||
| } | ||
|
Comment on lines
+102
to
+109
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this. I'm guessing it comes with operator-sdk scaffolding nowadays.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You guessed right. I don't anticipate needing it, but it seems harmless so I'm inclined to leave it for now. |
||
|
|
||
| // Create a new manager to provide shared dependencies and start components | ||
| mgr, err := manager.New(cfg, options) | ||
| if err != nil { | ||
| log.Error(err, "") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| log.Info("Registering Components.") | ||
|
|
||
| // Setup Scheme for all resources | ||
| if err := apis.AddToScheme(mgr.GetScheme()); err != nil { | ||
| log.Error(err, "") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| opts := cincontroller.Options{ | ||
| OperandImage: os.Getenv("OPERAND_IMAGE"), | ||
| } | ||
| if opts.OperandImage == "" { | ||
| log.Error(errors.New("Must set envvar OPERAND_IMAGE"), "") | ||
| os.Exit(1) | ||
| } | ||
| // Usually this is done with an init() function in the package, but since we | ||
| // need to provide input, we're doing it here instead. | ||
| controller.AddToManagerFuncs = append(controller.AddToManagerFuncs, func(mgr manager.Manager) error { | ||
| return cincontroller.Add(mgr, opts) | ||
| }) | ||
| // Setup Cincinnati controllers | ||
| if err := controller.AddToManager(mgr); err != nil { | ||
| log.Error(err, "") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Add the Metrics Service | ||
| addMetrics(ctx, cfg) | ||
|
|
||
| log.Info("Starting the Cmd.") | ||
|
|
||
| // Start the Cmd | ||
| if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
| log.Error(err, "Manager exited non-zero") | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| // addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using | ||
| // the Prometheus operator | ||
| func addMetrics(ctx context.Context, cfg *rest.Config) { | ||
| // Get the namespace the operator is currently deployed in. | ||
| operatorNs, err := k8sutil.GetOperatorNamespace() | ||
| if err != nil { | ||
| if errors.Is(err, k8sutil.ErrRunLocal) { | ||
| log.Info("Skipping CR metrics server creation; not running in a cluster.") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if err := serveCRMetrics(cfg, operatorNs); err != nil { | ||
| log.Info("Could not generate and serve custom resource metrics", "error", err.Error()) | ||
| } | ||
|
|
||
| // Add to the below struct any other metrics ports you want to expose. | ||
| servicePorts := []v1.ServicePort{ | ||
| {Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}}, | ||
| {Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}}, | ||
| } | ||
|
|
||
| // Create Service object to expose the metrics port(s). | ||
| service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts) | ||
| if err != nil { | ||
| log.Info("Could not create metrics Service", "error", err.Error()) | ||
| } | ||
|
|
||
| // CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources | ||
| // necessary to configure Prometheus to scrape metrics from this operator. | ||
| services := []*v1.Service{service} | ||
|
|
||
| // The ServiceMonitor is created in the same namespace where the operator is deployed | ||
| _, err = metrics.CreateServiceMonitors(cfg, operatorNs, services) | ||
| if err != nil { | ||
| log.Info("Could not create ServiceMonitor object", "error", err.Error()) | ||
| // If this operator is deployed to a cluster without the prometheus-operator running, it will return | ||
| // ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation. | ||
| if err == metrics.ErrServiceMonitorNotPresent { | ||
| log.Info("Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types. | ||
| // It serves those metrics on "http://metricsHost:operatorMetricsPort". | ||
| func serveCRMetrics(cfg *rest.Config, operatorNs string) error { | ||
| // The function below returns a list of filtered operator/CR specific GVKs. For more control, override the GVK list below | ||
| // with your own custom logic. Note that if you are adding third party API schemas, probably you will need to | ||
| // customize this implementation to avoid permissions issues. | ||
| filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // The metrics will be generated from the namespaces which are returned here. | ||
| // NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error. | ||
| ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Generate and serve custom resource specific metrics. | ||
| err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
til gitignore.io