Skip to content
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

ability to install Operator with an alternate namcespace than the default (porter-operator-system) #191

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion controllers/agentaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,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
5 changes: 3 additions & 2 deletions controllers/installation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"
"os"
"reflect"

porterv1 "get.porter.sh/operator/api/v1"
Expand All @@ -17,8 +18,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
operatorNamespace = "porter-operator-system"
var (
operatorNamespace = os.Getenv("POD_NAMESPACE")
)

// 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 alternate namespace if you want to install to a different one by adding **--param operatorNamespace=your-namespace-name**
```
porter install porterops -c porterops -r ghcr.io/getporter/porter-operator:v0.8.0
```
Expand Down
1 change: 1 addition & 0 deletions docs/content/quickstart/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The configureNamespace function is used by the configureNamespace custom action for the bundle, it sets up a namespace for use with the operator with required configuration such as a porter configuration file. By default we use the contents of installer/manifests/namespace/defaults/porter-config-spec.yaml. In that file, we reference the in-cluster mongodb service that is installed in the operator namespace and it's hard-coded to porter-operator-system.

The configureNamespace helper function should also update that file when a custom porter configuration file was not specified to use the custom operator namespace.

storage:
  - name: "in-cluster-mongodb"
    plugin: "mongodb"
    config:
      url: "mongodb://mongodb.TODO-OPERATOR-NAMESPACE.svc.cluster.local"

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but I wanted you to know about a porter feature in case that affects how you'd like to implement this.

The parameter operatorNamespace is always populated, it will have the default value if not specified by the user, and a corresponding environment variable, OPERATORNAMESPACE set. There isn't a need to define OPNAMESPACE, pass the parameter as an argument to each helper function, and initialize OPNAMESPACE in each function. You can just use OPERATORNAMESPACE.


setCustomNamespaceForOperator() {
if [ -z "$1" ]; then
echo "No namespace specified, using default $OPNAMESPACE"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's always be clear which namespace we are referring to in these messages, because we have both the "porter operator namespace" and also a namespace that is created and configured by the configureNamespace custom action in the bundle.

Suggested change
echo "No namespace specified, using default $OPNAMESPACE"
echo "No porter operator namespace specified, using default $OPNAMESPACE"

else
OPNAMESPACE=$1
echo "Using custom namespace $OPNAMESPACE"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Using custom namespace $OPNAMESPACE"
echo "Using custom porter operator namespace $OPNAMESPACE"

fi

# Replace the namespace in the operator.yaml
echo "Setting namespace to $OPNAMESPACE"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Setting namespace to $OPNAMESPACE"
echo "Setting porter operator 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
Comment on lines +75 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The configureNamespace custom action will also need the porter operator namespace parameter.

Suggested change
applyTo:
- install
- upgrade
applyTo:
- install
- upgrade
- configureNamespace

- 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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: "Set custom namespace for operator if present"
description: "Set custom namespace for operator

The parameter operatorNamespace will always be populated. So if it's not customized by the user it will just have the default value. Since this step always runs and the helper script always runs kustomize, let's make it clear that this logic always runs.

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's follow the pattern for allowing the developer to customize development parameters, like PORTER_AGENT_REPOSITORY, using local environment variables instead of requiring changes to the code.

The function applyHackParameters should be updated along with the hack/dev-build-params.yaml to set the operatorNamespace parameter from PORTER_OPERATOR_NAMESPACE on the local machine.

applyHackParameters(installCmd)
installCmd.RunV()
}
Expand Down