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

Add clusterId and checks for KubernetesOperators registration #474

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ endif
KUBE_NAMESPACE ?= ngrok-operator
HELM_RELEASE_NAME ?= ngrok-operator
KUBE_DEPLOYMENT_NAME ?= ngrok-operator-manager
KUBE_CLUSTER_NAME ?= development

.PHONY: release
release:
Expand All @@ -146,6 +147,7 @@ deploy: _deploy-check-env-vars docker-build manifests kustomize _helm_setup ## D
helm upgrade $(HELM_RELEASE_NAME) $(HELM_CHART_DIR) --install \
--namespace $(KUBE_NAMESPACE) \
--create-namespace \
--set clusterName=$(KUBE_CLUSTER_NAME) \
--set image.repository=$(IMG) \
--set image.tag="latest" \
--set podAnnotations."k8s\.ngrok\.com/test"="\{\"env\": \"local\"\}" \
Expand All @@ -162,6 +164,7 @@ deploy_gateway: _deploy-check-env-vars docker-build manifests kustomize _helm_se
helm upgrade $(HELM_RELEASE_NAME) $(HELM_CHART_DIR) --install \
--namespace $(KUBE_NAMESPACE) \
--create-namespace \
--set clusterName=$(KUBE_CLUSTER_NAME) \
--set image.repository=$(IMG) \
--set image.tag="latest" \
--set podAnnotations."k8s\.ngrok\.com/test"="\{\"env\": \"local\"\}" \
Expand All @@ -179,6 +182,7 @@ deploy_with_bindings: _deploy-check-env-vars docker-build manifests kustomize _h
helm upgrade $(HELM_RELEASE_NAME) $(HELM_CHART_DIR) --install \
--namespace $(KUBE_NAMESPACE) \
--create-namespace \
--set clusterName=$(KUBE_CLUSTER_NAME) \
--set image.repository=$(IMG) \
--set image.tag="latest" \
--set podAnnotations."k8s\.ngrok\.com/test"="\{\"env\": \"local\"\}" \
Expand Down
2 changes: 2 additions & 0 deletions api/ngrok/v1alpha1/kubernetesoperator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type ngrokAPICommon struct {
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type KubernetesOperatorDeployment struct {
// ClusterName is the name of the k8s cluster in which the operator is deployed
ClusterName string `json:"clusterName,omitempty"`
// Name is the name of the k8s deployment for the operator
Name string `json:"name,omitempty"`
// The namespace in which the operator is deployed
Expand Down
28 changes: 19 additions & 9 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -91,6 +92,7 @@ func main() {

type managerOpts struct {
// flags
clusterName string
releaseName string
metricsAddr string
electionID string
Expand Down Expand Up @@ -143,6 +145,7 @@ func cmd() *cobra.Command {
},
}

c.Flags().StringVar(&opts.clusterName, "cluster-name", "", "Cluster Name where the ngrok-operator is installed")
c.Flags().StringVar(&opts.releaseName, "release-name", "ngrok-operator", "Helm Release name for the deployed operator")
c.Flags().StringVar(&opts.metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to")
c.Flags().StringVar(&opts.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
Expand Down Expand Up @@ -170,6 +173,9 @@ func cmd() *cobra.Command {
c.Flags().StringVar(&opts.bindings.serviceLabels, "bindings-service-labels", "", "Service Labels to propagate to the target service")
c.Flags().StringVar(&opts.bindings.ingressEndpoint, "bindings-ingress-endpoint", "", "The endpoint the bindings forwarder connects to")

_ = c.MarkFlagRequired("cluster-name")
_ = c.MarkFlagRequired("release-name")

opts.zapOpts = &zap.Options{}
goFlagSet := flag.NewFlagSet("manager", flag.ContinueOnError)
opts.zapOpts.BindFlags(goFlagSet)
Expand Down Expand Up @@ -254,11 +260,9 @@ func runNormalMode(ctx context.Context, opts managerOpts, k8sClient client.Clien
return fmt.Errorf("Unable to load ngrokClientSet: %w", err)
}

if opts.enableFeatureBindings {
// register the k8sop in the ngrok API
if err := createKubernetesOperator(ctx, k8sClient, opts); err != nil {
return fmt.Errorf("unable to create KubernetesOperator: %w", err)
}
// register the k8sop in the ngrok API
if err := createKubernetesOperator(ctx, k8sClient, opts); err != nil {
return fmt.Errorf("unable to create KubernetesOperator: %w", err)
}

// k8sResourceDriver is the driver that will be used to interact with the k8s resources for all controllers
Expand Down Expand Up @@ -652,9 +656,10 @@ func createKubernetesOperator(ctx context.Context, client client.Client, opts ma
_, err := controllerutil.CreateOrUpdate(ctx, client, k8sOperator, func() error {
k8sOperator.Spec = ngrokv1alpha1.KubernetesOperatorSpec{
Deployment: &ngrokv1alpha1.KubernetesOperatorDeployment{
Name: opts.releaseName,
Namespace: opts.namespace,
Version: version.GetVersion(),
ClusterName: opts.clusterName,
Name: opts.releaseName,
Namespace: opts.namespace,
Version: version.GetVersion(),
},
Region: opts.region,
}
Expand All @@ -681,7 +686,12 @@ func createKubernetesOperator(ctx context.Context, client client.Client, opts ma
}
k8sOperator.Spec.EnabledFeatures = features

setupLog.Info("created KubernetesOperator", "name", k8sOperator.Name, "namespace", k8sOperator.Namespace, "op", fmt.Sprintf("%+v", k8sOperator.Spec.Binding))
if data, err := json.Marshal(k8sOperator); err == nil {
setupLog.Info("created KubernetesOperator", "name", k8sOperator.Name, "namespace", k8sOperator.Namespace, "op", string(data))
} else {
setupLog.Info("created KubernetesOperator", "name", k8sOperator.Name, "namespace", k8sOperator.Namespace, "op", fmt.Sprintf("%+v", k8sOperator))
}

return nil
})
return err
Expand Down
4 changes: 4 additions & 0 deletions helm/ngrok-operator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ The `ngrok-operator` helm chart now includes a `schema.json` file that can be us

- Generate and commit schema.json file by @alex-bezek in [#472](https://github.com/ngrok/ngrok-operator/pull/472)

### Added

- Required `.Values.clusterName` for the ngrok-operator to register with the ngrok API

### Changed

#### Traffic Policy
Expand Down
1 change: 1 addition & 0 deletions helm/ngrok-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ To uninstall the chart:
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------- |
| `nameOverride` | String to partially override generated resource names | `""` |
| `fullnameOverride` | String to fully override generated resource names | `""` |
| `clusterName` | String to identify the kubernetes cluster in the ngrok dashboard (must be unique per Helm release per cluster) | `""` |
| `description` | ngrok-operator description that will appear in the ngrok dashboard | `The official ngrok Kubernetes Operator.` |
| `commonLabels` | Labels to add to all deployed objects | `{}` |
| `commonAnnotations` | Annotations to add to all deployed objects | `{}` |
Expand Down
5 changes: 5 additions & 0 deletions helm/ngrok-operator/templates/controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ spec:
- /api-manager
args:
- --release-name={{ .Release.Name }}
{{- if .Values.oneClickDemoMode }}
- --cluster-name=one-click-demo-mode
{{- else }}
- --cluster-name={{ .Values.clusterName | required "Missing required .Values.clusterName!"}}
{{- end }}
{{- include "ngrok-operator.manager.cliFeatureFlags" . | nindent 8 }}
{{- if .Values.oneClickDemoMode }}
- --one-click-demo-mode
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions helm/ngrok-operator/tests/controller-deployment_test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
suite: test controller-deployment
set:
clusterName: "test-cluster"
templates:
- controller-deployment.yaml
# The following included templates are needed due to the way helm unittest works.
Expand Down Expand Up @@ -244,6 +246,22 @@ tests:
- contains:
path: spec.template.spec.containers[0].args
content: --one-click-demo-mode
- it: Should pass cluster-name
template: controller-deployment.yaml
documentIndex: 0 # Document 0 is the deployment since its the first template
asserts:
- contains:
path: spec.template.spec.containers[0].args
content: --cluster-name=test-cluster
- it: Should pass cluster-name as demo mode when enabled
template: controller-deployment.yaml
documentIndex: 0 # Document 0 is the deployment since its the first template
set:
oneClickDemoMode: true
asserts:
- contains:
path: spec.template.spec.containers[0].args
content: --cluster-name=one-click-demo-mode
- it: Should pass log format argument if set
set:
log:
Expand Down
5 changes: 5 additions & 0 deletions helm/ngrok-operator/values.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions helm/ngrok-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
##
## @param nameOverride String to partially override generated resource names
## @param fullnameOverride String to fully override generated resource names
## @param clusterName String to identify the kubernetes cluster in the ngrok dashboard (must be unique per Helm release per cluster)
## @param description ngrok-operator description that will appear in the ngrok dashboard
## @param commonLabels Labels to add to all deployed objects
## @param commonAnnotations Annotations to add to all deployed objects
## @param oneClickDemoMode If true, then the operator will startup without required fields or API registration, become Ready, but not actually be running
nameOverride: ""
fullnameOverride: ""
clusterName: ""
description: "The official ngrok Kubernetes Operator."
commonLabels: {}
commonAnnotations: {}
Expand Down
Loading