Skip to content

Commit

Permalink
Merge pull request #255 from matrus2/flux
Browse files Browse the repository at this point in the history
Add Flux support to install resources from git source:
  • Loading branch information
k8s-ci-robot committed Aug 1, 2023
2 parents c5714bb + aab80f0 commit 1af0fd6
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 1 deletion.
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")),
)

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

0 comments on commit 1af0fd6

Please sign in to comment.