Skip to content

Commit

Permalink
making namespace configurable for the operator
Browse files Browse the repository at this point in the history
  • Loading branch information
SubZer0MS committed Mar 30, 2023
1 parent b6a3304 commit 96d6fb8
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ It runs on the default Mongodb port (27017) and authentication is not required t
With your local Porter configuration file pointed to the in-cluster mongodb server, you can use Porter to query and interact with installations created by the operator.

Expose the in-cluster mongodb server on the default mongo porter: 27017.
* NOTE: Use your custom namespace if you have installed with the non-default one (porter-operator-system)
```
kubectl port-forward --namespace porter-operator-system svc/mongodb 27017:27017 >/dev/null &
```
Expand Down
3 changes: 2 additions & 1 deletion config/crd/bases/getporter.org_agentactions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,8 @@ spec:
defined in spec.resourceClaims, that are used
by this container. \n This is an alpha field
and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable."
feature gate. \n This field is immutable.
It can only be set for containers."
items:
description: ResourceClaim references one
entry in PodSpec.ResourceClaims.
Expand Down
4 changes: 1 addition & 3 deletions config/crd/bases/getporter.org_agentconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ spec:
storageClassName:
description: StorageClassName is the name of the storage class that
Porter will request when running the Porter Agent. It is used to
determine what the storage class will be for the volume requested.
The storage class must support ReadWriteOnce and ReadOnlyMany access modes
as well as allow for 'chmod' to be executed.
determine what the storage class will be for the volume requested
type: string
volumeSize:
description: VolumeSize is the size of the persistent volume that
Expand Down
10 changes: 9 additions & 1 deletion controllers/agentaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ func (r *AgentActionReconciler) createAgentJob(ctx context.Context, log logr.Log
func (r *AgentActionReconciler) resolveAgentConfig(ctx context.Context, log logr.Logger, action *porterv1.AgentAction) (porterv1.AgentConfigSpecAdapter, error) {
log.V(Log5Trace).Info("Resolving porter agent configuration")

operatorNamespace := operatorNamespaceDefault

logConfig := func(level string, config *porterv1.AgentConfig) {
if config == nil || config.Name == "" {
return
Expand All @@ -493,6 +495,8 @@ func (r *AgentActionReconciler) resolveAgentConfig(ctx context.Context, log logr
"namespace", config.Namespace,
"name", config.Name,
"plugin", config.Spec.PluginConfigFile)

operatorNamespace = config.Namespace

This comment has been minimized.

Copy link
@carolynvs

carolynvs Mar 30, 2023

Member

config.Namespace is the namespace where the AgentConfig CRD is defined, and isn't the value we want to use to determine the operator's namespace.

This controller is always going to run in the namespace where the operator was installed. So what we need to do is lookup the current namespace from inside the operator pod. I believe we can read this information directly from /var/run/secrets/kubernetes.io/serviceaccount/namespace when we start and then remember it in memory so that we don't need to look it up each time.

}

// Read agent configuration defined at the system level
Expand Down Expand Up @@ -550,6 +554,8 @@ func (r *AgentActionReconciler) resolveAgentConfig(ctx context.Context, log logr
func (r *AgentActionReconciler) resolvePorterConfig(ctx context.Context, log logr.Logger, action *porterv1.AgentAction) (porterv1.PorterConfigSpec, error) {
log.V(Log5Trace).Info("Resolving porter configuration file")

operatorNamespace := operatorNamespaceDefault

logConfig := func(level string, config *porterv1.PorterConfig) {
if config == nil || config.Name == "" {
return
Expand All @@ -558,6 +564,8 @@ func (r *AgentActionReconciler) resolvePorterConfig(ctx context.Context, log log
"level", level,
"namespace", config.Namespace,
"name", config.Name)

operatorNamespace = config.Namespace
}

// Provide a safe default config in case nothing is defined anywhere
Expand All @@ -568,7 +576,7 @@ func (r *AgentActionReconciler) resolvePorterConfig(ctx context.Context, log log
{PluginConfig: porterv1.PluginConfig{
Name: "in-cluster-mongodb",
PluginSubKey: "mongodb",
Config: runtime.RawExtension{Raw: []byte(`{"url":"mongodb://mongodb.porter-operator-system.svc.cluster.local"}`)},
Config: runtime.RawExtension{Raw: []byte(`{"url":"mongodb://mongodb.` + operatorNamespace + `.svc.cluster.local"}`)},
}},
},
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/agentaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ func TestAgentActionReconciler_NoPluginsSpecified(t *testing.T) {

func TestAgentActionReconciler_resolveAgentConfig(t *testing.T) {
systemCfg := porterv1.AgentConfig{
ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: operatorNamespace},
ObjectMeta: metav1.ObjectMeta{Name: "default", Namespace: operatorNamespaceDefault},
Status: porterv1.AgentConfigStatus{
Ready: true,
},
Expand Down
2 changes: 1 addition & 1 deletion controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
operatorNamespace = "porter-operator-system"
operatorNamespaceDefault = "porter-operator-system"
)

// InstallationReconciler calls porter to execute changes made to an Installation CRD
Expand Down
1 change: 1 addition & 0 deletions docs/content/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ porter credentials generate porterops -r ghcr.io/getporter/porter-operator:v0.8.
```

Install the operator into the porter-operator-system namespace:
* NOTE: Use your custom namespace if you want to install to a different one by adding **--param operatorNamespace=your-namespace-name**

This comment has been minimized.

Copy link
@carolynvs

carolynvs Mar 30, 2023

Member

I suggest saying "alternate namespace" instead of custom to avoid confusion with the configuration step where the user creates a namespace for using the operator (i.e. where they create the agent configuration and customize how porter should run in that namespace).

```
porter install porterops -c porterops -r ghcr.io/getporter/porter-operator:v0.8.0
```
Expand Down
3 changes: 2 additions & 1 deletion docs/content/quickstart/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The bundle includes a custom action that prepares a namespace for you:
porter invoke porterops --action configureNamespace --param namespace=quickstart -c porterops
```

The Porter Operator is now installed on your cluster in the porter-operator-system namespace, along with a Mongodb server.
The Porter Operator is now installed on your cluster in the porter-operator-system (or your custom namespace) namespace, along with a Mongodb server.

This comment has been minimized.

Copy link
@carolynvs

carolynvs Mar 30, 2023

Member

This note here is a bit confusing because the user just ran a command to create a new namespace for using the operator, but it isn't where the operator is installed. When the user follows the instructions in the quickstart, they will always end up with the default operator namespace.

I suggest we move notes about installing the operator into an alternate namespace out of this file (so that the quickstart remains simple and doesn't present a lot of choices) into the install.md. There we can add a section that explains how to use the operatorNamespace parameter to change the namespace where the operator is installed.

This database is not secured with a username/password, so do not use this default installation configuration with production secrets!
The cluster has a namespace, quickstart, where we will create resources and Porter will create jobs to run Porter.

Expand All @@ -50,6 +50,7 @@ Let's update your local porter CLI to read the data from the operator's datastor
This isn't necessary for the operator to work, but will allow us to see what's happening and understand how the operator works.

Run the following command to expose the operator's mongodb server to your localhost:
* NOTE: Use your custom namespace if you have installed with the non-default one (porter-operator-system)
```
kubectl port-forward --namespace porter-operator-system svc/mongodb 27020:27017 >/dev/null &
```
Expand Down
25 changes: 23 additions & 2 deletions installer/helpers.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail

OPNAMESPACE="porter-operator-system"

setCustomNamespaceForOperator() {
if [ -z "$1" ]; then
echo "No namespace specified, using default $OPNAMESPACE"
else
OPNAMESPACE=$1
echo "Using custom namespace $OPNAMESPACE"
fi

# Replace the namespace in the operator.yaml
echo "Setting namespace to $OPNAMESPACE"
cd manifests
kustomize edit set namespace $OPNAMESPACE
kustomize build -o operator.yaml
}

setControllerImage() {
# Replace the manager image with the image packaged with the bundle
echo "Setting manager image to $1"
Expand Down Expand Up @@ -46,11 +63,15 @@ configureNamespace() {
}

waitForDeployment() {
if [ ! -z "$1" ]; then
OPNAMESPACE=$1
fi

set +e # allow this next command to fail
kubectl rollout status deploy/porter-operator-controller-manager --namespace porter-operator-system --timeout 30s
kubectl rollout status deploy/porter-operator-controller-manager --namespace $OPNAMESPACE --timeout 30s
if [[ $? != 0 ]]; then
echo "Deployment failed, retrieving logs to troubleshoot"
kubectl logs deploy/porter-operator-controller-manager --namespace porter-operator-system -c manager
kubectl logs deploy/porter-operator-controller-manager --namespace $OPNAMESPACE -c manager
fi
}

Expand Down
22 changes: 21 additions & 1 deletion installer/vanilla.porter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ parameters:
default: "13.6.2"
applyTo:
- install
- name: operatorNamespace
description: Namespace to install the operator into
type: string
default: "porter-operator-system"
applyTo:
- install
- upgrade
- name: mongodbVals
description: Helm values file to use when installing the mongodb chart
type: file
Expand All @@ -88,6 +95,12 @@ mixins:
- kubernetes

install:
- exec:
description: "Set custom namespace for operator if present"
command: ./helpers.sh
arguments:
- setCustomNamespaceForOperator
- ${bundle.parameters.operatorNamespace}
- exec:
description: "Set manager image reference"
command: ./helpers.sh
Expand All @@ -101,7 +114,7 @@ install:
wait: true
- helm3:
description: "Install a mongo database for Porter"
namespace: porter-operator-system
namespace: ${bundle.parameters.operatorNamespace}
name: mongodb
chart: bitnami/mongodb
version: ${bundle.parameters.mongodbChartVersion}
Expand All @@ -115,8 +128,15 @@ install:
command: ./helpers.sh
arguments:
- waitForDeployment
- ${bundle.parameters.operatorNamespace}

upgrade:
- exec:
description: "Set custom namespace for operator if present"
command: ./helpers.sh
arguments:
- setCustomNamespaceForOperator
- ${bundle.parameters.operatorNamespace}
- exec:
description: "Set manager image reference"
command: ./helpers.sh
Expand Down
4 changes: 2 additions & 2 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func TestIntegration() {
// are responding to the same events.
// For now, it's up to the caller to use a fresh cluster with CRDs installed until we can fix it.

kubectl("delete", "deployment", "porter-operator-controller-manager", "-n=porter-operator-system").RunV()
kubectl("delete", "deployment", "porter-operator-controller-manager", "-n="+operatorNamespace).RunV()

if os.Getenv("PORTER_AGENT_REPOSITORY") != "" && os.Getenv("PORTER_AGENT_VERSION") != "" {
porterAgentImgRepository = os.Getenv("PORTER_AGENT_REPOSITORY")
Expand Down Expand Up @@ -334,7 +334,7 @@ func Deploy() {
buildPorterCmd("credentials", "apply", "hack/creds.yaml", "-n=operator").Must().RunV()
}
bundleRef := Env.BundlePrefix + meta.Version
installCmd := buildPorterCmd("install", "operator", "-r", bundleRef, "-c=kind", "--force", "-n=operator").Must()
installCmd := buildPorterCmd("install", "operator", "-r", bundleRef, "-c=kind", "--force", "-n=operator", "--param", "operatorNamespace="+operatorNamespace).Must()
applyHackParameters(installCmd)
installCmd.RunV()
}
Expand Down

0 comments on commit 96d6fb8

Please sign in to comment.