Commit 3a14d7c3 authored by Vasilis Katopodis's avatar Vasilis Katopodis
Browse files

feat: support multiple namespaces per Helm release

Resolves #20
parent c9fbce81
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5,4 +5,6 @@ sonarQube
.scannerwork/
vendor/
CLAUDE.md
bin/
coverage.out

Makefile

0 → 100644
+81 −0
Original line number Diff line number Diff line
GOLANGCI_LINT ?= golangci-lint
COVERAGE_DIR ?= .
IMG ?= service-monitor:latest
DOCKERFILE ?= build/Dockerfile

# Credentials for fetching the private base.go module during the image build.
CI_SERVER_HOST ?= labs.etsi.org
NETRC          ?= $(HOME)/.netrc
GITLAB_USER    ?= $(shell awk -v h=$(CI_SERVER_HOST) '{for(i=1;i<=NF;i++){if($$i=="machine")m=$$(i+1);else if($$i=="login"&&m==h)u=$$(i+1)}}END{print u}' $(NETRC) 2>/dev/null)
GITLAB_TOKEN   ?= $(shell awk -v h=$(CI_SERVER_HOST) '{for(i=1;i<=NF;i++){if($$i=="machine")m=$$(i+1);else if($$i=="password"&&m==h)p=$$(i+1)}}END{print p}' $(NETRC) 2>/dev/null)

.PHONY: help
help:
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: fmt
fmt:
	gofmt -e -s -w .

.PHONY: vet
vet:
	go vet ./...

.PHONY: tidy
tidy:
	go mod tidy

.PHONY: lint
lint:
	$(GOLANGCI_LINT) run ./... --fast-only $(LINT_ARGS)

.PHONY: lint-fix
lint-fix:
	$(GOLANGCI_LINT) run --fix

.PHONY: test
test: fmt vet ## Run unit tests. Set COVERAGE_DIR for coverage.out location, TEST_ARGS to append flags (e.g. -run/-skip).
	go test -v -cover -coverprofile=$(COVERAGE_DIR)/coverage.out -coverpkg=./... ./... $(TEST_ARGS)

.PHONY: dev
dev:
	./scripts/air-run

##@ Local Kafka

.PHONY: compose-up
compose-up:
	docker compose -f build/docker-compose.yml up -d

.PHONY: compose-down
compose-down:
	docker compose -f build/docker-compose.yml down

##@ Build

.PHONY: build
build: fmt vet
	go build -o bin/service.monitor .

.PHONY: run
run: 
	go run .

.PHONY: docker-build
docker-build: ## Build the docker image. Creds come from ~/.netrc (override GITLAB_USER/GITLAB_TOKEN).
	@test -n "$(GITLAB_USER)" -a -n "$(GITLAB_TOKEN)" || \
		{ echo "ERROR: no credentials for $(CI_SERVER_HOST) in $(NETRC); set GITLAB_USER and GITLAB_TOKEN"; exit 1; }
	docker build -f $(DOCKERFILE) \
		--build-arg CI_SERVER_HOST=$(CI_SERVER_HOST) \
		--build-arg USERNAME=$(GITLAB_USER) \
		--build-arg PASSWORD=$(GITLAB_TOKEN) \
		-t $(IMG) .

.PHONY: docker-push
docker-push:
	docker push $(IMG)

.PHONY: all
all: build
+38 −81
Original line number Diff line number Diff line
# io.maestro.lcm.service
# service.monitor

Watches the health of a Helm release's Kubernetes workloads and streams health
reports back over Kafka. Part of the ETSI OSL Hypo core services.

## What it does

## Getting started
The service is driven entirely by Kafka messages:

To make it easy for you to get started with GitLab, here's a list of recommended next steps.
1. A **monitor request** arrives on `service-monitor-request` with a `serviceId`,
   the target Helm `releaseName`, its `serviceNamespaces`, and the registry key
   (`k8sServiceId`) for the cluster.
2. The service asks the registry for that cluster's kubeconfig over Kafka
   (`registry-retrieve-kubernetes-config`). No local state is kept — the resume
   state travels as Kafka headers, so any replica can pick up the reply.
3. When the kubeconfig comes back, a **watch session** starts: client-go
   informers track the release's Deployments, StatefulSets, DaemonSets and Jobs
   across all its namespaces.
4. On every change (debounced), the session assesses each workload's health,
   aggregates it to one release-level verdict, and emits a `Diagnosis` report on
   `monitor-result`.
5. The session runs until a `monitor-stop` message — or a new request with the
   same `serviceId` — ends it. A final report is emitted on the way out.

Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
Health assessment lives in `internal/health/`, vendored from
[argoproj/gitops-engine](https://github.com/argoproj/gitops-engine) `pkg/health`
(see `internal/health/kinds.go` for the local modifications).

## Add your files
The full message contract — topics, payloads, validation, envelope format — is in
[`docs/kafka-api.md`](docs/kafka-api.md).

- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
## Configuration

```
cd existing_repo
git remote add origin https://gitlab.ubitech.eu/maestro-v2/mdg-hypo/org.etsi.osl.hypo.core/org.etsi.osl.hypo.core.service.monitor.git
git branch -M main
git push -uf origin main
```

## Integrate with your tools

- [ ] [Set up project integrations](https://gitlab.ubitech.eu/maestro-v2/mdg-hypo/org.etsi.osl.hypo.core/org.etsi.osl.hypo.core.service.monitor/-/settings/integrations)

## Collaborate with your team

- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)

## Test and Deploy

Use the built-in continuous integration in GitLab.

- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)

***

# Editing this README

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.

## Suggestions for a good README
`service.yml` (loaded from the working directory at startup):

Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
- `kafka.brokers`, `kafka.group`, `kafka.topics.*` — broker list and topic names
- `logs.path`, `logs.console_log_level` — log file and console verbosity

## Name
Choose a self-explaining name for your project.
## Development

## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.

## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.

## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.

## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.

## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.

## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.

## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.

## Contributing
State if you are open to contributions and what your requirements are for accepting them.

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.

## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.

## License
For open source projects, say how it is licensed.
```sh
make compose-up    # local Kafka (build/docker-compose.yml)
make run           # run against service.yml
make dev           # run with live reload (air)
make test          # fmt + vet + unit tests with coverage
make build         # build ./bin/service.monitor
make docker-build  # build the image (needs GitLab creds in ~/.netrc for base.go)
```

## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
`make help` lists every target.
+10 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ RUN go clean -modcache && \
COPY ./*.yml ./
COPY ./main.go ./
COPY ./cmd ./cmd/
COPY ./internal ./internal/

# Build binary
RUN CGO_ENABLED=0 GOOS=linux go build -o ./binary
@@ -41,4 +42,12 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o ./binary
# Fix permissions
RUN chmod +x ./binary

CMD ["./binary"]
 No newline at end of file
# Second stage: create a minimal image with only the executable
FROM labs.etsi.org:5050/osl/hypo/code/org.etsi.osl.hypo.core/pkg-manager.helm/debian:latest

RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/binary /binary
COPY --from=builder /app/service.yml /service.yml

CMD ["/binary"]
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ var statusPriority = map[health.HealthStatusCode]int{
}

// AggregateHealth reduces per-resource health entries to a single release-level Diagnosis.
// The worst status across all resources wins (Degraded > Unknown > Missing > Progressing > Suspended > Healthy).
// Degraded > Unknown > Missing > Progressing > Suspended > Healthy
func AggregateHealth(resources []messages.ResourceHealth) messages.Diagnosis {
	if len(resources) == 0 {
		return messages.Diagnosis{
Loading