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 Flux support to install resources from git source: #255

Merged
merged 10 commits into from
Aug 1, 2023
Merged
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ update-deps-go: ## Update all golang dependencies for this repo
install-helm: ## Install Helm toolchain for 3rd party integration
./hack/install-helm.sh

test: install-helm ## Runs golang unit tests
install-flux:
./hack/install-flux.sh

test: install-helm install-flux ## Runs golang unit tests
./hack/test-go.sh

##@ Helpers
Expand Down
1 change: 1 addition & 0 deletions examples/third_party_integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ This section of the repository contains the example of how the third party tooli
`e2e-framework`

1. [Helm](./helm)
2. [Flux](./flux)
31 changes: 31 additions & 0 deletions examples/third_party_integration/flux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Flux Integration

This section of the document gives you an example of how to integrate the flux workflow
into the `e2e-framework` while writing your tests.

## Pre-Requisites

1. `Flux` Installed on your system where the tests are being run for details visit flux official [website](https://fluxcd.io/).

## Flux supported commands

For the time being the framework supports following functionality:
- Flux installation and uninstallation.
- Handling [Kustomization](https://fluxcd.io/flux/components/kustomize/kustomization/) objects.
- Handling [GitRepository](https://fluxcd.io/flux/components/source/gitrepositories/) objects.

## How does the example work?

1. It creates a kind cluster with `flux` prefix.
2. Creates a namespace with `flux` prefix.
3. Installs all flux resources.
4. Creates a reference to the git repository, where a simple hello world application deployment is specified. You can find it [here](https://github.com/matrus2/go-hello-world).
5. Starts reconciliation by a flux kustomization manifest to path `template` of the git repository.
6. Assesses if the deployment of simple hello world app is up and running.
7. After the test passes it removes all resources.

## How to run tests

```shell
go test -c -o flux.test . && ./flux.test --v 4
```
53 changes: 53 additions & 0 deletions examples/third_party_integration/flux/flux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package flux

import (
"context"
"testing"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

func TestFluxRepoWorkflow(t *testing.T) {
feature := features.New("Install resources by flux").
Assess("check if deployment was successful", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
deployment := &appsv1.Deployment{
ObjectMeta: v1.ObjectMeta{
Name: "hello-app",
Namespace: c.Namespace(),
},
Spec: appsv1.DeploymentSpec{},
}

err := wait.For(conditions.New(c.Client().Resources()).DeploymentConditionMatch(deployment, appsv1.DeploymentAvailable, corev1.ConditionStatus(v1.ConditionTrue)), wait.WithTimeout(time.Minute*5))
if err != nil {
t.Fatal("Error deployment not found", err)
}

return ctx
}).Feature()

testEnv.Test(t, feature)
}
58 changes: 58 additions & 0 deletions examples/third_party_integration/flux/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package flux

import (
"os"
"testing"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
"sigs.k8s.io/e2e-framework/third_party/flux"
)

var (
testEnv env.Environment
namespace string
kindClusterName string
)

func TestMain(m *testing.M) {
cfg, _ := envconf.NewFromFlags()
testEnv = env.NewWithConfig(cfg)
kindClusterName = envconf.RandomName("flux", 10)
namespace = envconf.RandomName("flux", 10)
gitRepoName := "e2e-framework"
ksName := "hello-world"
testEnv.Setup(
envfuncs.CreateKindCluster(kindClusterName),
envfuncs.CreateNamespace(namespace),
flux.InstallFlux(),
flux.CreateGitRepo(gitRepoName, "https://github.com/kubernetes-sigs/e2e-framework", flux.WithBranch("main")),
flux.CreateKustomization(ksName, "GitRepository/"+gitRepoName+".flux-system", flux.WithPath("examples/third_party_integration/flux/template"), flux.WithArgs("--target-namespace", namespace, "--prune")),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I also noticed that the repo https://github.com/matrus2/go-hello-world/blob/main/template/deployment.yaml#L18 hardcodes the image reference. Don't we need to create a ImageUpdateAutomation for it to really do the right thing ? Also I am not sure if we have any precedence for this where the image from a personal dockerhub repo is included into the rest util. May be we should move all of these repo and images to a common location and manage them like we do for rest of the test images and others in k8s ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the purpose of this test the image reference is hardcoded and this is one of the use case. For example you may have some prerequisite components to install inside the cluster like CNI, before deploy your staff. The example here is the simplest possible.
For moving the manifests see my answer above, for image reference not sure if you have any kuberentes-sigs registry.


testEnv.Finish(
flux.DeleteKustomization(ksName),
flux.DeleteGitRepo(gitRepoName),
flux.UninstallFlux(),
envfuncs.DeleteNamespace(namespace),
envfuncs.DestroyKindCluster(kindClusterName),
)
os.Exit(testEnv.Run(m))
}
24 changes: 24 additions & 0 deletions hack/install-flux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# Copyright 2023 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

FLUX_URL=https://fluxcd.io/install.sh

if ! command -v flux; then
# Running the piped command with sudo disabled to avoid any security concerns that might arise
curl -s $FLUX_URL | USE_SUDO=false bash
fi
Loading