Skip to content

Commit

Permalink
CLI main.go: Add support for cli config file
Browse files Browse the repository at this point in the history
A YAML config file listing contexts can be passed to the CLI. The config
file contains parameters that can be used by the CLI. Several contexts
can be defined.

CLI arguments can override a context.

Rename the "standalone" subcommand as "apply" as per ADR12.
Rename the default command to "operator" to run the operator.

The "apply" subcommand supports the config file parameters.

Change-Id: Ie4bff45ec748985c6d13ae4c3d5dadfc374b912c
  • Loading branch information
mhuin committed Dec 19, 2023
1 parent ee0d50e commit e5889f6
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532

ENTRYPOINT ["/manager"]
ENTRYPOINT ["/manager", "operator"]
1 change: 1 addition & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ spec:
containers:
- command:
- /manager
- operator
args:
- --leader-elect
restartPolicy: Never
Expand Down
15 changes: 13 additions & 2 deletions controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
config "sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"

certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
Expand Down Expand Up @@ -66,6 +67,16 @@ func getPodRESTClient(config *rest.Config) rest.Interface {
return restClient
}

func GetConfigContextOrDie(contextName string) *rest.Config {
var conf *rest.Config
var err error
if conf, err = config.GetConfigWithContext(contextName); err != nil {
ctrl.Log.Error(err, "couldn't find context "+contextName)
os.Exit(1)
}
return conf
}

func Main(ns string, metricsAddr string, probeAddr string, enableLeaderElection bool, oneShot bool) {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Namespace: ns,
Expand Down Expand Up @@ -135,9 +146,9 @@ func Main(ns string, metricsAddr string, probeAddr string, enableLeaderElection
}
}

func Standalone(sf sfv1.SoftwareFactory, ns string) {
func Standalone(sf sfv1.SoftwareFactory, ns string, kubeContext string) {

config := ctrl.GetConfigOrDie()
config := GetConfigContextOrDie(kubeContext)
cl, err := client.New(config, client.Options{
Scheme: controllerScheme,
})
Expand Down
4 changes: 2 additions & 2 deletions doc/developer/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ To iterate on the development of the `sf-operator` you can either start the oper
Run the operator with the following command:

```sh
go run ./main.go --namespace sf
go run ./main.go --namespace sf operator
```

> The command does not return and wait for events to run the reconcile.
Expand All @@ -105,7 +105,7 @@ Any change on the applied resource re-trigger the reconcile.
Run the operator with the following command:

```sh
go run ./main.go --namespace sf standalone --cr playbooks/files/sf.yaml
go run ./main.go --namespace sf apply --cr playbooks/files/sf.yaml
```

> The command returns when the expected state is applied.
Expand Down
2 changes: 1 addition & 1 deletion doc/developer/howtos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Here is an usage example of this resource:
# Create a dedicated namespace
kubectl create ns logserver
# Start the operator for the dedicated namespace
go run ./main.go --namespace logserver
go run ./main.go --namespace logserver operator
```

```shell
Expand Down
5 changes: 5 additions & 0 deletions doc/reference/cli/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# sfconfig

> [!NOTE]
> As per [ADR12](../../adr/0012-CLI-overhaul.md) this CLI is getting phased out.
> Its features are being reworked into the `main` binary of the operator.
> Temporary documentation on this Work-In-Progress can be found [here](./main.md).
SF-Operator comes with a Command Line Interface (CLI) called `sfconfig` that can be used to perform actions in relation with the lifecycle
of the operator, of a deployment, or also to help with the development of the operator.

Expand Down
121 changes: 121 additions & 0 deletions doc/reference/cli/main.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# CLI

The operator's `main.go` can be used to perform various actions related to the management of Software Factory
deployments, beyond what can be defined in a custom resource manifest.

## Table of Contents

1. [Running the CLI](#running-the-cli)
1. [Global Flags](#global-flags)
1. [Configuration File](#configuration-file)
1. [Running the Operator](#running-the-operator)
1. [Subcommands](#subcommands)
1. [Apply](#apply)
1. [Operator](#apply)

## Running the CLI

To run the CLI, assuming you are at the root directory of the sf-operator repository:

```sh
go run ./main.go [GLOBAL FLAGS] [SUBCOMMAND] [SUBCOMMAND FLAGS] ...
```

## Global Flags

These flags apply

| Argument | Type | Description | Optional | Default |
|----------|------|-------|----|----|
|-n, --namespace |string | The namespace on which to perform actions | Dependent |-|
|-d, --fqdn | string | The FQDN of the deployment (if no manifest is provided) | Yes | sfop.me |
|-C, --config | string | Path to the CLI configuration file | Yes | - |
|-c, --context | string | Context to use in the configuration file. Defaults to the "default-context" value in the config file if set, or the first available context | Yes | Dependent |

## Configuration File

The CLI supports using a configuration file to keep track of commonly used parameters and flags.
The configuration file supports several **contexts** if you are working on several distinct Software Factory deployments
(for example, a dev instance and a production instance).

### Structure

```yaml
---
contexts:
# the name of the context
dev:
# ALL FIELDS OPTIONAL
# ---
# the path to a local copy of the Software Factory's config repository
config-repository-path: /path/to/config-repo
# the path to the manifest defining the Software Factory deployment
manifest-file: /path/to/manifest
# specify whether the deployment was applied via an operator (standalone = false) or via the `apply` subcommand of the
# CLI (standalone = true)
standalone: false
# the cluster's namespace on which to perform actions
namespace: <namespace>
# the kube context to use to perform actions, in case you have several contexts
kube-context: <kube context>
# the FQDN of the deployment if no manifest file is provided. Used mostly to interact with services' APIs.
fqdn: <fqdn>
# Developer settings
development:
# the path to a local copy of the ansible-microshift-role repository
# (https://github.com/openstack-k8s-operators/ansible-microshift-role)
ansible-microshift-role-path: /path/to/ansible-microshift-role/repository
# Microshift deployment settings (used by the Ansible deployment playbook)
microshift:
host: microshift.dev
user: cloud-user
inventory-file: /path/to/inventory
# Settings used when running the test suite locally
tests:
# Ansible extra variables to pass to the testing playbooks
extra-vars:
key1: value1
key2: value2
# SF components settings
components:
nodepool:
# path to the local copy of the `clouds.yaml` file used by the Openstack provider
clouds-file: /path/to/clouds-file
# path to the local copy of the `kube.config` file used by the K8s-based providers
kube-file: /path/to/kube-file
default-context: dev
```
## Subcommands
### Apply
The `apply` subcommand can be used to deploy a SoftwareFactory resource without installing the operator or its
associated CRDs on a cluster. This will run the operator runtime locally, deploy the resource's components
on the cluster, then exit.

```sh
go run ./main.go [GLOBAL FLAGS] apply --cr /path/to/manifest
```

Flags:

| Argument | Type | Description | Optional | Default |
|----------|------|-------|----|----|
|--cr |string | The path to the custom resource to apply | No | If a config file is used and the flag not provided, will default to the context's `manifest-file` if set |

### Operator

To start the operator controller locally, run:

```sh
go run ./main.go operator [FLAGS]
```

Flags:

| Argument | Type | Description | Optional | Default |
|----------|------|-------|----|----|
|--metrics-bind-address |string | The address the metric endpoint binds to. | Yes | :8080 |
|--health-probe-bind-address |string | The address the probe endpoint binds to. | Yes | :8081 |
|--leader-elect |boolean | Enable leader election for controller manager. | Yes | false |
Loading

0 comments on commit e5889f6

Please sign in to comment.