Skip to content

Commit

Permalink
Refactor: operator-first deployment (#164)
Browse files Browse the repository at this point in the history
* Refactor: operator-first deployment

Signed-off-by: ZachRobinson <[email protected]>
Co-authored-by: Milica Cvrkota <[email protected]>
Co-authored-by: Konrad Ohms <[email protected]>
Co-authored-by: Fredrik Gunderson <[email protected]>
  • Loading branch information
4 people authored May 29, 2024
1 parent 1da9044 commit bbfb252
Show file tree
Hide file tree
Showing 169 changed files with 18,970 additions and 3,405 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
!api/
!controllers/
!logger/
!pkg/
!version/

# Other files not to ignore
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ target/
.idea/
instana-agent.yaml

# Local development demonstration file should be ignored as it is not relevant and might be accidentally added
instana_v1_instanaagent_demo.yaml

# Eclipse https://github.com/github/gitignore/blob/master/Global/Eclipse.gitignore

*.pydevproject
Expand Down
21 changes: 9 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ linters-settings:
linters:
disable-all: true
enable:
- deadcode
- goimports
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- misspell
- goimports
- gosimple
- govet
- ineffassign
- unused
- misspell
- exhaustive
- errcheck

service:
golangci-lint-version: 1.31.x # use the fixed version to not introduce new linters unexpectedly
golangci-lint-version: 1.56.2 # use the fixed version to not introduce new linters unexpectedly

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY version/ version/
COPY pkg/ pkg/

# Build, injecting VERSION and GIT_COMMIT directly in the code
RUN export ARCH=$(case "${TARGETPLATFORM}" in 'linux/amd64') echo 'amd64' ;; 'linux/arm64') echo 'arm64' ;; 'linux/s390x') echo 's390x' ;; 'linux/ppc64le') echo 'ppc64le' ;; esac) \
Expand Down
6 changes: 3 additions & 3 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MAINTAINERS

Felix Marx - @FelixMarxIBM
Henning Treu - @htreu
Torsten Kohn - @tkohn
- Konrad Ohms - @konrad-ohms
- Milica Cvrkota - @Milica-Cvrkota-IBM
- Fredrik Gundersen
62 changes: 62 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ deploy: manifests kustomize ## Deploy controller in the configured Kubernetes cl
cd config/manager && $(KUSTOMIZE) edit set image instana/instana-agent-operator=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

scale-to-zero: ## Scales the operator to zero in the cluster to allow local testing against a cluster
kubectl -n instana-agent scale --replicas=0 deployment.apps/instana-agent-operator && sleep 5 && kubectl get all -n instana-agent

deploy-minikube: manifests kustomize ## Convenience target to push the docker image to a local running Minikube cluster and deploy the Operator there.
(eval $$(minikube docker-env) && docker rmi ${IMG} || true)
docker save ${IMG} | (eval $$(minikube docker-env) && docker load)
Expand Down Expand Up @@ -217,3 +220,62 @@ bundle-build: ## Build the bundle image for OLM.
controller-yaml: manifests kustomize ## Output the YAML for deployment, so it can be packaged with the release. Use `make --silent` to suppress other output.
cd config/manager && $(KUSTOMIZE) edit set image "instana/instana-agent-operator=$(IMG)"
$(KUSTOMIZE) build config/default

get-mockgen:
# commit 2c718f249a424ac6ce6e2afa28c3c17f95c51241 introduces --write_command_comment flag to enable us to keep the mocks without additional command comments
which mockgen >> /dev/null 2>&1 || go install go.uber.org/mock/mockgen@2c718f249a424ac6ce6e2afa28c3c17f95c51241

MOCKGEN_ARGS = --write_package_comment=false --write_source_comment=false --write_command_comment=false --copyright_file=hack/boilerplate.go.txt
gen-mocks: get-mockgen
mockgen --source ${GOPATH}/pkg/mod/sigs.k8s.io/[email protected]/pkg/client/interfaces.go --destination ./pkg/k8s/client/k8s_client_mock_test.go --package client ${MOCKGEN_ARGS}

mockgen --source ./pkg/hash/hash.go --destination ./pkg/k8s/object/builders/agent/daemonset/hash_mock_test.go --package daemonset ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/transformations/pod_selector.go --destination ./pkg/k8s/object/builders/agent/daemonset/pod_selector_mock_test.go --package daemonset ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/ports/ports.go --destination ./pkg/k8s/object/builders/agent/daemonset/ports_mock_test.go --package daemonset ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/env/env_builder.go --destination ./pkg/k8s/object/builders/agent/daemonset/env_builder_mock_test.go --package daemonset ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/volume/volume_builder.go --destination ./pkg/k8s/object/builders/agent/daemonset/volume_builder_mock_test.go --package daemonset ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/operator/status/status.go --destination ./pkg/k8s/object/builders/agent/daemonset/status_mock_test.go --package daemonset ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/common/env/helpers_mock_test.go --package env ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/common/volume/helpers_mock_test.go --package volume ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/client/client.go --destination ./pkg/k8s/operator/operator_utils/instana_agent_client_mock_test.go --package operator_utils ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/builder/builder.go --destination ./pkg/k8s/operator/operator_utils/builder_mock_test.go --package operator_utils ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/operator/lifecycle/lifecycle.go --destination ./pkg/k8s/operator/operator_utils/lifecycle_mock_test.go --package operator_utils ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/k8s-sensor/rbac/helpers_mock_test.go --package rbac ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/k8s-sensor/serviceaccount/helpers_mock_test.go --package serviceaccount ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/agent/serviceaccount/helpers_mock_test.go --package serviceaccount ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/k8s-sensor/configmap/helpers_mock_test.go --package configmap ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/ports/ports.go --destination ./pkg/k8s/object/builders/common/ports/ports_mock_test.go --package ports ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/helpers/agent_interfaces.go --destination ./pkg/k8s/object/builders/common/ports/agent_interfaces_mock_test.go --package ports ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/agent/headless-service/helpers_mock_test.go --package headless_service ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/transformations/pod_selector.go --destination ./pkg/k8s/object/builders/agent/headless-service/pod_selector_mock_test.go --package headless_service ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/ports/ports.go --destination ./pkg/k8s/object/builders/agent/headless-service/ports_mock_test.go --package headless_service ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/transformations/pod_selector.go --destination ./pkg/k8s/object/builders/agent/service/pod_selector_mock_test.go --package service ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/ports/ports.go --destination ./pkg/k8s/object/builders/agent/service/ports_mock_test.go --package service ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/helpers/agent_interfaces.go --destination ./pkg/k8s/object/builders/agent/service/agent_interfaces_mock_test.go --package service ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/transformations/transformations.go --destination ./pkg/k8s/object/builders/common/builder/transformations_mock_test.go --package builder ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/common/builder/builder.go --destination ./pkg/k8s/object/builders/common/builder/builder_mock_test.go --package builder ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/agent/secrets/tls-secret/helpers_mock_test.go --package tls_secret ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/agent/secrets/containers-instana-io-secret/helpers_mock_test.go --package containers_instana_io_secret ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/builders/agent/secrets/containers-instana-io-secret/docker_config_json.go --destination ./pkg/k8s/object/builders/agent/secrets/containers-instana-io-secret/docker_config_json_mock_test.go --package containers_instana_io_secret ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/operator/status/status.go --destination ./pkg/k8s/object/builders/agent/configmap/status_mock_test.go --package configmap ${MOCKGEN_ARGS}

mockgen --source ./pkg/k8s/object/builders/common/helpers/helpers.go --destination ./pkg/k8s/object/builders/k8s-sensor/poddisruptionbudget/helpers_mock_test.go --package poddisruptionbudget ${MOCKGEN_ARGS}
mockgen --source ./pkg/k8s/object/transformations/pod_selector.go --destination ./pkg/k8s/object/builders/k8s-sensor/poddisruptionbudget/pod_selector_mock_test.go --package poddisruptionbudget ${MOCKGEN_ARGS}




95 changes: 78 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Introduction

Instana is an [APM solution](https://www.instana.com/product-overview/) built for microservices that enables IT Ops to build applications faster and deliver higher quality services by automating monitoring, tracing and root cause analysis. The solution is optimized for [Kubernetes](https://www.instana.com/automatic-kubernetes-monitoring/) and [OpenShift](https://www.instana.com/blog/automatic-root-cause-analysis-for-openshift-applications/).
Instana is an [APM solution](https://www.ibm.com/products/instana) built for microservices that enables IT Ops to build applications faster and deliver higher quality services by automating monitoring, tracing and root cause analysis. The solution is optimized for [Kubernetes](https://www.ibm.com/products/instana/kubernetes-monitoring) and [OpenShift](https://www.ibm.com/products/instana/supported-technologies/openshift-monitoring).

## Instana Agent Operator

Expand All @@ -25,25 +25,87 @@ Please see the guidelines in [CONTRIBUTING.md](CONTRIBUTING.md).

## Local Development

Developing (and running) the Operator is possible in two easy ways:
- Running as Go application outside the Cluster
- Running as Deployment inside the Cluster
Prerequisites:

Both are described below.
* [Make](https://www.gnu.org/software/make/) ([Makefile](Makefile) used as a utility CMD )
* [Go](https://go.dev) (for the supported version, see the [go.mod](go.mod)-file)
* [Kubernetes](http://kubernetes.io)
* [Minikube](https://minikube.sigs.k8s.io/docs/)
* [Operator SDK](https://sdk.operatorframework.io/docs/installation/#install-from-homebrew-macos)
* Something like [Docker](https://www.docker.com/) or [Podman](https://podman.io/)
* Instana Agent key

### Running Go Operator locally against a (Minikube) cluster
Developing (and running) the Operator is easiest in two ways:

1. Create a copy of the file `config/samples/instana_v1_instanaagent.yaml`, for the below steps we're assuming `config/samples/instana_v1_instanaagent_demo.yaml`
2. In this file, put correct values for e.g. the Agent `key`, `endpointHost` and `endpointPort`.
3. Install the CRD: `make install`.
4. Run the Go application, either from your IDE, or from command-line: `make run`.
5. Deploy the custom resource earlier created using `kubectl apply -f config/samples/instana_v1_instanaagent_demo.yaml`
### **Option 1:** Running Go Operator locally against a **Minikube** cluster

1. Start minikube ([minikube docs](https://minikube.sigs.k8s.io/docs/start/))
> [!NOTE]
When minikube runs on docker (at least on `RHEL 8`), there are network issues for pods reaching the internet. This causes connection issues for the agent and will prevent auto-updates or connections to the backend. To avoid this, use kvm2 driver instead: `minikube start --driver=kvm2`. If one is using podman, don't forget to create the minikube with the podman driver: `minikube start --driver=podman`. More info and options can be found in Minikube documentation about [podman](https://minikube.sigs.k8s.io/docs/drivers/podman/)

```shell
minikube start
```

2. Install the CRD by running `make install` at the root of the repository

```shell
# Install command in root of the repository (installs custom resource to k8s)
make install
# List CRD to verify it appears in the list
kubectl get crd
```

3. Create `instana-agent` namespace on the cluster:

```shell
kubectl apply -f config/samples/instana_agent_namespace.yaml
# List namespaces to verify it appears in the list
kubectl get ns -n instana-agent
```

4. Run the `instana-agent-operator` Go application, either from your IDE, or from command-line: `make run`.

```shell
# Starts the operator using make with additional fmt vet gen functionality
make run
```

5. **Duplicate** agent [sample file](config/samples/instana_v1_instanaagent.yaml) in `config/samples/instana_v1_instanaagent.yaml`
> [!NOTE]
for this demonstration the duplicate will be named as `instana_v1_instanaagent_demo.yaml`

```shell
# Copy/Duplicate the sample file with a "demo" suffix
cp config/samples/instana_v1_instanaagent.yaml config/samples/instana_v1_instanaagent_demo.yaml
```

6. Change the placeholder values in the [**duplicated file**](config/samples/instana_v1_instanaagent_demo.yaml) to your preferred values e.g. the Agent `key`, `endpointHost` and `endpointPort`
> [!TIP]
In the configuration, there is a field `spec.zone.name`. Changing this to something more identifiable and personalised will help you find your infrastructure easier in the frontend-client.
7. Deploy the custom resource earlier created using

```shell
kubectl apply -f config/samples/instana_v1_instanaagent_demo.yaml
```

Verify that the operator reacted to the application of the yaml file by looking into the logs of the running operator
8. Depending on your local configurations, the environment should appear **IBM Instana infrastructure-page**. Standard minikube configuration should appear there as `minikube`.

To stop, take the following actions:
- `kubectl delete -f config/samples/instana_v1_instanaagent_demo.yaml`
- `make uninstall`

### Running Deployment inside the cluster
```shell
# Remove the instance from your kubernetes instance
kubectl delete -f config/samples/instana_v1_instanaagent_demo.yaml
# Final cleanup e.g `kubectl delete -k config/crd`
make uninstall
# Will stop the service
minikube start
# Will reset the whole set-up
minikube delete
```

### **Option 2:** Running Deployment inside the cluster

The Instana Agent Operator can be developed and tested easily against a local Minikube cluster or any other configured
Kubernetes cluster. Therefore, follow the below steps:
Expand All @@ -58,6 +120,5 @@ Kubernetes cluster. Therefore, follow the below steps:

Now you should have a successful running Operator.
To remove the Operator again, run:
- `kubectl delete -f config/samples/instana_v1_instanaagent_demo.yaml`
- `make undeploy`.

* `kubectl delete -f config/samples/instana_v1_instanaagent_demo.yaml`
* `make undeploy`.
Loading

0 comments on commit bbfb252

Please sign in to comment.