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

feat(cli): add kit prune and kit squash commands to manage IntegrationKits #4552

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .github/actions/kamel-config-cluster-kind/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ runs:
version: v0.14.0
node_image: kindest/node:v1.23.6@sha256:b1fa224cc6c7ff32455e0b1fd9cbfd3d3bc87ecaa8fcb06961ed1afb3db0f9ae
cpu: 3
registry_delete: true

- id: info
name: Info
Expand Down
30 changes: 30 additions & 0 deletions e2e/common/cli/files/FileRoute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// camel-k: property=location=files/

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

import org.apache.camel.builder.RouteBuilder;

public class FileRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
from("file:/var/camel")
.convertBodyTo(String.class)
.log("${body}");
}
}
155 changes: 155 additions & 0 deletions e2e/common/cli/prune_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//go:build integration
// +build integration

// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration"

/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 cli

import (
"context"
"fmt"
"io"
"strings"
"testing"

. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

. "github.com/apache/camel-k/v2/e2e/support"
"github.com/apache/camel-k/v2/e2e/support/util"
)

func TestPruneIntegrationKits(t *testing.T) {
RegisterTestingT(t)

t.Run("Prune IntegrationKits", func(t *testing.T) {
// make sure kits are deleted
Expect(DeleteKits(ns)).To(Succeed())
tests := []struct {
title string
kits string
toDelete string
}{
{
title: "Nothing to do",
kits: "",
toDelete: "",
},
{
title: "Single kit that is used",
kits: "a(t)",
toDelete: "",
},
{
title: "Single kit that is unused",
kits: "a(f)",
toDelete: "a",
},
{
title: "Basic tree",
kits: "a(f)b(t)",
toDelete: "a",
},
{
title: "Simple tree line",
kits: "a(f)b(f)c(f)d(f)e(t)",
toDelete: "abcd",
},
// Simple Tree
{
// Syntax for n ary tree in preorder traversal is NAME(f|t)| where:
// Name is the name of the kit,
// t : kit is used
// f : kit is unused
// | marks the end of children
title: "Simple tree",
kits: "a(f)b(f)e(f)|f(f)k(t)|||c(t)|d(f)g(t)|h(f)|i(f)|j(t)|||",
toDelete: "abefdhi",
},
}

for _, curr := range tests {
thetest := curr
t.Run(thetest.title, func(t *testing.T) {
// build kit tree
nodes := buildKits(thetest.kits, operatorID, ns, t)

// check dry run
checkPruneDryRunLogs(ns, thetest.toDelete, nodes, t)

// check real run
Expect(Kamel("kit", "prune", "-y", "-n", ns).Execute()).To(Succeed())

for _, r := range thetest.toDelete {
kitName := string(r)
Eventually(Kit(ns, kitName), TestTimeoutShort).Should(BeNil())
}
for _, node := range nodes {
// It we shouldn't delete it then it should still exist
if !strings.Contains(thetest.toDelete, node.Name) {
assert.NotNil(t, Kit(ns, node.Kit.Name)())
}
if node.Used {
// check that the integration still runs fine
for _, message := range node.ExpectedMessages {
Eventually(IntegrationLogs(ns, node.Name), TestTimeoutShort).Should(ContainSubstring(message))
}
}
}
// Clean up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
Expect(DeleteKits(ns)).To(Succeed())
})
}
})
}

func checkPruneDryRunLogs(ns string, toDelete string, nodes map[string]*TestNode, t *testing.T) {
ctx, cancel := context.WithCancel(TestContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()
kamelBuild := KamelWithContext(TestContext, "kit", "prune", "-d", "-n", ns)
kamelBuild.SetOut(pipew)
kamelBuild.SetErr(pipew)

deleteness := make([]string, 0)
if len(toDelete) > 0 {
deleteness = append(deleteness, "The following Integration Kits will be deleted:")
deleteness = append(deleteness, "The following Images will no longer be used by camel-k and can be deleted from the Image Registry:")
for _, r := range toDelete {
node := nodes[string(r)]
deleteness = append(deleteness, fmt.Sprintf("%s in namespace: %s", node.Kit.Name, ns))
deleteness = append(deleteness, node.Kit.Status.Image)
}
}
if len(toDelete) == 0 {
deleteness = append(deleteness, "Nothing to do")
}
logScanner := util.NewStrictLogScanner(ctx, piper, true, deleteness...)
go func() {
err := kamelBuild.Execute()
assert.NoError(t, err)
logScanner.Done()
cancel()
}()
Eventually(logScanner.ExactMatch(), TestTimeoutShort).Should(BeTrue())
}
Loading