This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 8b130dd6e53f991beeff0455885b1155840a33f8 Author: Antonin Stefanutti <[email protected]> AuthorDate: Tue Aug 17 14:44:09 2021 +0200 feat(native): Exclude native kits from incremental base image lookup --- e2e/builder/global_test.go | 20 +++--- e2e/support/test_support.go | 8 +-- pkg/apis/camel/v1/build_types_support.go | 16 +---- pkg/apis/camel/v1/integrationkit_types.go | 13 +++- pkg/apis/camel/v1/integrationkit_types_support.go | 18 +---- pkg/builder/image.go | 18 +++-- pkg/builder/image_test.go | 4 +- pkg/cmd/describe_kit.go | 8 +-- pkg/cmd/kit_create.go | 22 +++--- pkg/cmd/kit_delete.go | 33 +++++---- pkg/cmd/kit_get.go | 2 +- pkg/controller/integration/build_kit.go | 36 ++++++---- pkg/controller/integration/build_kit_test.go | 10 +-- pkg/controller/integration/initialize.go | 2 +- .../integrationkit/integrationkit_controller.go | 2 +- pkg/trait/container.go | 6 +- pkg/trait/gc.go | 13 ++-- pkg/trait/jvm.go | 10 +-- pkg/trait/jvm_test.go | 2 +- pkg/trait/quarkus.go | 23 ++++--- pkg/trait/quarkus_test.go | 8 ++- pkg/trait/util.go | 8 +-- pkg/util/controller/util.go | 45 ------------- pkg/util/kubernetes/client.go | 78 ++++++++-------------- 24 files changed, 170 insertions(+), 235 deletions(-) diff --git a/e2e/builder/global_test.go b/e2e/builder/global_test.go index a8b676e..d2b408a 100644 --- a/e2e/builder/global_test.go +++ b/e2e/builder/global_test.go @@ -28,11 +28,11 @@ import ( . "github.com/onsi/gomega" "github.com/stretchr/testify/assert" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" . "github.com/apache/camel-k/e2e/support" - camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/util/openshift" ) @@ -57,7 +57,7 @@ func TestRunGlobalInstall(t *testing.T) { Expect(Kamel("install", "-n", ns2, "--skip-operator-setup", "--olm=false").Execute()).To(Succeed()) Expect(Kamel("run", "-n", ns2, "files/Java.java").Execute()).To(Succeed()) - Eventually(IntegrationPodPhase(ns2, "java"), TestTimeoutMedium).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns2, "java"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) Eventually(IntegrationLogs(ns2, "java"), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) Expect(Kamel("delete", "--all", "-n", ns2).Execute()).To(Succeed()) Expect(Kits(ns2)()).Should(HaveLen(1)) @@ -72,7 +72,7 @@ func TestRunGlobalInstall(t *testing.T) { Expect(Kamel("install", "-n", ns3, "--olm=false").Execute()).To(Succeed()) Expect(Kamel("run", "-n", ns3, "files/Java.java").Execute()).To(Succeed()) - Eventually(IntegrationPodPhase(ns3, "java"), TestTimeoutMedium).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns3, "java"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) Eventually(IntegrationLogs(ns3, "java"), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) Expect(Kamel("delete", "--all", "-n", ns3).Execute()).To(Succeed()) @@ -85,7 +85,7 @@ func TestRunGlobalInstall(t *testing.T) { t.Run("Global test on namespace without platform", func(t *testing.T) { WithNewTestNamespace(t, func(ns4 string) { Expect(Kamel("run", "-n", ns4, "files/Java.java").Execute()).To(Succeed()) - Eventually(IntegrationPodPhase(ns4, "java"), TestTimeoutMedium).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns4, "java"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) Eventually(IntegrationLogs(ns4, "java"), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) Expect(Kamel("delete", "--all", "-n", ns4).Execute()).To(Succeed()) Expect(Kits(ns4)()).Should(HaveLen(0)) @@ -98,7 +98,7 @@ func TestRunGlobalInstall(t *testing.T) { t.Run("Global test on namespace without platform with external kit", func(t *testing.T) { WithNewTestNamespace(t, func(ns5 string) { Expect(Kamel("run", "-n", ns5, "files/Java.java").Execute()).To(Succeed()) - Eventually(IntegrationPodPhase(ns5, "java"), TestTimeoutMedium).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns5, "java"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) Eventually(IntegrationLogs(ns5, "java"), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) Expect(Kamel("delete", "--all", "-n", ns5).Execute()).To(Succeed()) Expect(Kits(ns5)()).Should(HaveLen(0)) @@ -107,22 +107,22 @@ func TestRunGlobalInstall(t *testing.T) { if len(globalKits) == 1 { kit := globalKits[0] // external kit mirroring the global one - externalKit := camelv1.IntegrationKit{ + externalKit := v1.IntegrationKit{ ObjectMeta: metav1.ObjectMeta{ Namespace: ns5, Name: "external", Labels: map[string]string{ - "camel.apache.org/kit.type": camelv1.IntegrationKitTypeExternal, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeExternal, }, }, - Spec: camelv1.IntegrationKitSpec{ + Spec: v1.IntegrationKitSpec{ Image: kit.Status.Image, }, } Expect(TestClient().Create(TestContext, &externalKit)).Should(BeNil()) Expect(Kamel("run", "-n", ns5, "files/Java.java", "--name", "ext", "--kit", "external").Execute()).To(Succeed()) - Eventually(IntegrationPodPhase(ns5, "ext"), TestTimeoutMedium).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns5, "ext"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) Eventually(IntegrationLogs(ns5, "ext"), TestTimeoutShort).Should(ContainSubstring("Magicstring!")) Expect(IntegrationKit(ns5, "ext")()).Should(Equal("external")) Expect(Kamel("delete", "--all", "-n", ns5).Execute()).To(Succeed()) diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index 323362e..a85b0d4 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -854,17 +854,13 @@ func DeploymentCondition(ns string, name string, conditionType appsv1.Deployment func Build(ns string, name string) func() *v1.Build { return func() *v1.Build { build := v1.NewBuild(ns, name) - key := ctrl.ObjectKey{ - Namespace: ns, - Name: name, - } - if err := TestClient().Get(TestContext, key, &build); err != nil && k8serrors.IsNotFound(err) { + if err := TestClient().Get(TestContext, ctrl.ObjectKeyFromObject(build), build); err != nil && k8serrors.IsNotFound(err) { return nil } else if err != nil { log.Error(err, "Error while retrieving build "+name) return nil } - return &build + return build } } diff --git a/pkg/apis/camel/v1/build_types_support.go b/pkg/apis/camel/v1/build_types_support.go index 9b5c332..31fc668 100644 --- a/pkg/apis/camel/v1/build_types_support.go +++ b/pkg/apis/camel/v1/build_types_support.go @@ -22,9 +22,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// NewBuild -- -func NewBuild(namespace string, name string) Build { - return Build{ +func NewBuild(namespace string, name string) *Build { + return &Build{ TypeMeta: metav1.TypeMeta{ APIVersion: SchemeGroupVersion.String(), Kind: BuildKind, @@ -36,7 +35,6 @@ func NewBuild(namespace string, name string) Build { } } -// NewBuildList -- func NewBuildList() BuildList { return BuildList{ TypeMeta: metav1.TypeMeta{ @@ -50,7 +48,6 @@ func (buildPhase *BuildPhase) String() string { return string(*buildPhase) } -// SetIntegrationPlatform -- func (in *Build) SetIntegrationPlatform(platform *IntegrationPlatform) { cs := corev1.ConditionTrue @@ -79,7 +76,6 @@ func (in *BuildStatus) Failed(err error) BuildStatus { return *in } -// SetCondition -- func (in *BuildStatus) SetCondition(condType BuildConditionType, status corev1.ConditionStatus, reason string, message string) { in.SetConditions(BuildCondition{ Type: condType, @@ -91,7 +87,6 @@ func (in *BuildStatus) SetCondition(condType BuildConditionType, status corev1.C }) } -// SetErrorCondition -- func (in *BuildStatus) SetErrorCondition(condType BuildConditionType, reason string, err error) { in.SetConditions(BuildCondition{ Type: condType, @@ -145,7 +140,6 @@ func (in *BuildStatus) RemoveCondition(condType BuildConditionType) { var _ ResourceCondition = BuildCondition{} -// GetConditions -- func (in *BuildStatus) GetConditions() []ResourceCondition { res := make([]ResourceCondition, 0, len(in.Conditions)) for _, c := range in.Conditions { @@ -154,32 +148,26 @@ func (in *BuildStatus) GetConditions() []ResourceCondition { return res } -// GetType -- func (c BuildCondition) GetType() string { return string(c.Type) } -// GetStatus -- func (c BuildCondition) GetStatus() corev1.ConditionStatus { return c.Status } -// GetLastUpdateTime -- func (c BuildCondition) GetLastUpdateTime() metav1.Time { return c.LastUpdateTime } -// GetLastTransitionTime -- func (c BuildCondition) GetLastTransitionTime() metav1.Time { return c.LastTransitionTime } -// GetReason -- func (c BuildCondition) GetReason() string { return c.Reason } -// GetMessage -- func (c BuildCondition) GetMessage() string { return c.Message } diff --git a/pkg/apis/camel/v1/integrationkit_types.go b/pkg/apis/camel/v1/integrationkit_types.go index 46ebdf2..0fae093 100644 --- a/pkg/apis/camel/v1/integrationkit_types.go +++ b/pkg/apis/camel/v1/integrationkit_types.go @@ -87,15 +87,24 @@ const ( // IntegrationKitKind -- IntegrationKitKind string = "IntegrationKit" + // IntegrationKitTypeLabel labels the kit type + IntegrationKitTypeLabel = "camel.apache.org/kit.type" + // IntegrationKitTypePlatform -- IntegrationKitTypePlatform = "platform" - // IntegrationKitTypeUser -- IntegrationKitTypeUser = "user" - // IntegrationKitTypeExternal -- IntegrationKitTypeExternal = "external" + // IntegrationKitLayoutLabel labels the kit layout + IntegrationKitLayoutLabel = "camel.apache.org/kit.layout" + + // IntegrationKitLayoutFastJar labels a kit using the Quarkus fast-jar packaging + IntegrationKitLayoutFastJar = "fast-jar" + // IntegrationKitLayoutNative labels a kit using the Quarkus native packaging + IntegrationKitLayoutNative = "native" + // IntegrationKitPhaseNone -- IntegrationKitPhaseNone IntegrationKitPhase = "" // IntegrationKitPhaseInitialization -- diff --git a/pkg/apis/camel/v1/integrationkit_types_support.go b/pkg/apis/camel/v1/integrationkit_types_support.go index fb310df..a9c894e 100644 --- a/pkg/apis/camel/v1/integrationkit_types_support.go +++ b/pkg/apis/camel/v1/integrationkit_types_support.go @@ -22,9 +22,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// NewIntegrationKit -- -func NewIntegrationKit(namespace string, name string) IntegrationKit { - return IntegrationKit{ +func NewIntegrationKit(namespace string, name string) *IntegrationKit { + return &IntegrationKit{ TypeMeta: metav1.TypeMeta{ APIVersion: SchemeGroupVersion.String(), Kind: IntegrationKitKind, @@ -36,7 +35,6 @@ func NewIntegrationKit(namespace string, name string) IntegrationKit { } } -// NewIntegrationKitList -- func NewIntegrationKitList() IntegrationKitList { return IntegrationKitList{ TypeMeta: metav1.TypeMeta{ @@ -46,7 +44,6 @@ func NewIntegrationKitList() IntegrationKitList { } } -// Configurations -- func (in *IntegrationKitSpec) Configurations() []ConfigurationSpec { if in == nil { return []ConfigurationSpec{} @@ -55,7 +52,6 @@ func (in *IntegrationKitSpec) Configurations() []ConfigurationSpec { return in.Configuration } -// Configurations -- func (in *IntegrationKit) Configurations() []ConfigurationSpec { if in == nil { return []ConfigurationSpec{} @@ -64,7 +60,6 @@ func (in *IntegrationKit) Configurations() []ConfigurationSpec { return in.Spec.Configuration } -// SetIntegrationPlatform -- func (in *IntegrationKit) SetIntegrationPlatform(platform *IntegrationPlatform) { cs := corev1.ConditionTrue @@ -92,7 +87,6 @@ func (in *IntegrationKitStatus) GetCondition(condType IntegrationKitConditionTyp return nil } -// SetCondition -- func (in *IntegrationKitStatus) SetCondition(condType IntegrationKitConditionType, status corev1.ConditionStatus, reason string, message string) { in.SetConditions(IntegrationKitCondition{ Type: condType, @@ -104,7 +98,6 @@ func (in *IntegrationKitStatus) SetCondition(condType IntegrationKitConditionTyp }) } -// SetErrorCondition -- func (in *IntegrationKitStatus) SetErrorCondition(condType IntegrationKitConditionType, reason string, err error) { in.SetConditions(IntegrationKitCondition{ Type: condType, @@ -158,7 +151,6 @@ func (in *IntegrationKitStatus) RemoveCondition(condType IntegrationKitCondition var _ ResourceCondition = IntegrationKitCondition{} -// GetConditions -- func (in *IntegrationKitStatus) GetConditions() []ResourceCondition { res := make([]ResourceCondition, 0, len(in.Conditions)) for _, c := range in.Conditions { @@ -167,32 +159,26 @@ func (in *IntegrationKitStatus) GetConditions() []ResourceCondition { return res } -// GetType -- func (c IntegrationKitCondition) GetType() string { return string(c.Type) } -// GetStatus -- func (c IntegrationKitCondition) GetStatus() corev1.ConditionStatus { return c.Status } -// GetLastUpdateTime -- func (c IntegrationKitCondition) GetLastUpdateTime() metav1.Time { return c.LastUpdateTime } -// GetLastTransitionTime -- func (c IntegrationKitCondition) GetLastTransitionTime() metav1.Time { return c.LastTransitionTime } -// GetReason -- func (c IntegrationKitCondition) GetReason() string { return c.Reason } -// GetMessage -- func (c IntegrationKitCondition) GetMessage() string { return c.Message } diff --git a/pkg/builder/image.go b/pkg/builder/image.go index 8aca5a6..da702bc 100644 --- a/pkg/builder/image.go +++ b/pkg/builder/image.go @@ -22,6 +22,7 @@ import ( "os" "path" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" ctrl "sigs.k8s.io/controller-runtime/pkg/client" @@ -29,7 +30,6 @@ import ( v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/util" - "github.com/apache/camel-k/pkg/util/controller" "github.com/apache/camel-k/pkg/util/defaults" ) @@ -166,19 +166,27 @@ func imageContext(ctx *builderContext, selector artifactsSelector) error { } func listPublishedImages(context *builderContext) ([]v1.IntegrationKitStatus, error) { + excludeNativeImages, err := labels.NewRequirement(v1.IntegrationKitLayoutLabel, selection.NotEquals, []string{ + v1.IntegrationKitLayoutNative, + }) + if err != nil { + return nil, err + } + options := []ctrl.ListOption{ ctrl.InNamespace(context.Namespace), ctrl.MatchingLabels{ + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, "camel.apache.org/runtime.version": context.Catalog.Runtime.Version, "camel.apache.org/runtime.provider": string(context.Catalog.Runtime.Provider), }, - controller.NewLabelSelector("camel.apache.org/kit.type", selection.Equals, []string{ - v1.IntegrationKitTypePlatform, - }), + ctrl.MatchingLabelsSelector{ + Selector: labels.NewSelector().Add(*excludeNativeImages), + }, } list := v1.NewIntegrationKitList() - err := context.Client.List(context.C, &list, options...) + err = context.Client.List(context.C, &list, options...) if err != nil { return nil, err } diff --git a/pkg/builder/image_test.go b/pkg/builder/image_test.go index b858288..eafd6d1 100644 --- a/pkg/builder/image_test.go +++ b/pkg/builder/image_test.go @@ -44,7 +44,7 @@ func TestListPublishedImages(t *testing.T) { Namespace: "ns", Name: "my-kit-1", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, "camel.apache.org/runtime.version": catalog.Runtime.Version, "camel.apache.org/runtime.provider": string(catalog.Runtime.Provider), }, @@ -65,7 +65,7 @@ func TestListPublishedImages(t *testing.T) { Namespace: "ns", Name: "my-kit-2", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, "camel.apache.org/runtime.version": catalog.Runtime.Version, "camel.apache.org/runtime.provider": string(catalog.Runtime.Provider), }, diff --git a/pkg/cmd/describe_kit.go b/pkg/cmd/describe_kit.go index 2040352..7e3a33e 100644 --- a/pkg/cmd/describe_kit.go +++ b/pkg/cmd/describe_kit.go @@ -24,7 +24,7 @@ import ( "github.com/spf13/cobra" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/util/indentedwriter" @@ -74,12 +74,12 @@ func (command *describeKitCommandOptions) run(args []string) error { } kit := v1.NewIntegrationKit(command.Namespace, args[0]) - kitKey := k8sclient.ObjectKey{ + kitKey := ctrl.ObjectKey{ Namespace: command.Namespace, Name: args[0], } - if err := c.Get(command.Context, kitKey, &kit); err == nil { + if err := c.Get(command.Context, kitKey, kit); err == nil { if desc, err := command.describeIntegrationKit(kit); err == nil { fmt.Print(desc) } else { @@ -92,7 +92,7 @@ func (command *describeKitCommandOptions) run(args []string) error { return nil } -func (command *describeKitCommandOptions) describeIntegrationKit(kit v1.IntegrationKit) (string, error) { +func (command *describeKitCommandOptions) describeIntegrationKit(kit *v1.IntegrationKit) (string, error) { return indentedwriter.IndentedString(func(out io.Writer) error { w := indentedwriter.NewWriter(out) diff --git a/pkg/cmd/kit_create.go b/pkg/cmd/kit_create.go index 63ab0ea..ebf547f 100644 --- a/pkg/cmd/kit_create.go +++ b/pkg/cmd/kit_create.go @@ -23,8 +23,10 @@ import ( "strings" "github.com/spf13/cobra" + k8serrors "k8s.io/apimachinery/pkg/api/errors" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" + + ctrl "sigs.k8s.io/controller-runtime/pkg/client" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/trait" @@ -98,15 +100,15 @@ func (command *kitCreateCommandOptions) run(_ *cobra.Command, args []string) err } kit := v1.NewIntegrationKit(command.Namespace, args[0]) - key := k8sclient.ObjectKey{ + key := ctrl.ObjectKey{ Namespace: command.Namespace, Name: args[0], } - if err := c.Get(command.Context, key, &kit); err == nil { + if err := c.Get(command.Context, key, kit); err == nil { // the integration kit already exists, let's check that it is // not a platform one which is supposed to be "read only" - if kit.Labels["camel.apache.org/kit.type"] == v1.IntegrationKitTypePlatform { + if kit.Labels[v1.IntegrationKitTypeLabel] == v1.IntegrationKitTypePlatform { fmt.Printf("integration kit \"%s\" is not editable\n", kit.Name) return nil } @@ -114,7 +116,7 @@ func (command *kitCreateCommandOptions) run(_ *cobra.Command, args []string) err kit = v1.NewIntegrationKit(command.Namespace, kubernetes.SanitizeName(args[0])) kit.Labels = map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypeUser, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeUser, } kit.Spec = v1.IntegrationKitSpec{ Dependencies: make([]string, 0, len(command.Dependencies)), @@ -126,7 +128,7 @@ func (command *kitCreateCommandOptions) run(_ *cobra.Command, args []string) err // if the Image is set, the kit do not require any build but // is be marked as external as the information about the classpath // is missing so it cannot be used as base for other Kits - kit.Labels["camel.apache.org/kit.type"] = v1.IntegrationKitTypeExternal + kit.Labels[v1.IntegrationKitTypeLabel] = v1.IntegrationKitTypeExternal // Set the Image to be used by the kit kit.Spec.Image = command.Image @@ -162,22 +164,22 @@ func (command *kitCreateCommandOptions) run(_ *cobra.Command, args []string) err Value: item, }) } - if err := command.configureTraits(&kit, command.Traits, catalog); err != nil { + if err := command.configureTraits(kit, command.Traits, catalog); err != nil { return nil } existed := false - err = c.Create(command.Context, &kit) + err = c.Create(command.Context, kit) if err != nil && k8serrors.IsAlreadyExists(err) { existed = true existing := v1.NewIntegrationKit(kit.Namespace, kit.Name) - err = c.Get(command.Context, key, &existing) + err = c.Get(command.Context, key, existing) if err != nil { fmt.Print(err.Error()) return nil } kit.ResourceVersion = existing.ResourceVersion - err = c.Update(command.Context, &kit) + err = c.Update(command.Context, kit) } if err != nil { diff --git a/pkg/cmd/kit_delete.go b/pkg/cmd/kit_delete.go index 0a0428d..d484195 100644 --- a/pkg/cmd/kit_delete.go +++ b/pkg/cmd/kit_delete.go @@ -21,12 +21,13 @@ import ( "errors" "fmt" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" - - v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/spf13/cobra" k8errors "k8s.io/apimachinery/pkg/api/errors" + + ctrl "sigs.k8s.io/controller-runtime/pkg/client" + + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" ) func newKitDeleteCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *kitDeleteCommandOptions) { @@ -82,14 +83,14 @@ func (command *kitDeleteCommandOptions) run(args []string) error { if command.All { kitList := v1.NewIntegrationKitList() - if err := c.List(command.Context, &kitList, k8sclient.InNamespace(command.Namespace)); err != nil { + if err := c.List(command.Context, &kitList, ctrl.InNamespace(command.Namespace)); err != nil { return err } names = make([]string, 0, len(kitList.Items)) for _, item := range kitList.Items { // only include non platform Kits - if item.Labels["camel.apache.org/kit.type"] != v1.IntegrationKitTypePlatform { + if item.Labels[v1.IntegrationKitTypeLabel] != v1.IntegrationKitTypePlatform { names = append(names, item.Name) } } @@ -105,21 +106,17 @@ func (command *kitDeleteCommandOptions) run(args []string) error { } func (command *kitDeleteCommandOptions) delete(name string) error { - ctx := v1.NewIntegrationKit(command.Namespace, name) - key := k8sclient.ObjectKey{ - Namespace: command.Namespace, - Name: name, - } + kit := v1.NewIntegrationKit(command.Namespace, name) c, err := command.GetCmdClient() if err != nil { return err } - err = c.Get(command.Context, key, &ctx) + err = c.Get(command.Context, ctrl.ObjectKeyFromObject(kit), kit) // pass through if the kit is not found if err != nil && k8errors.IsNotFound(err) { - return fmt.Errorf("no integration kit found with name \"%s\"", ctx.Name) + return fmt.Errorf("no integration kit found with name \"%s\"", kit.Name) } // fail otherwise @@ -129,25 +126,25 @@ func (command *kitDeleteCommandOptions) delete(name string) error { // check that it is not a platform one which is supposed to be "read only" // thus not managed by the end user - if ctx.Labels["camel.apache.org/kit.type"] == v1.IntegrationKitTypePlatform { + if kit.Labels[v1.IntegrationKitTypeLabel] == v1.IntegrationKitTypePlatform { // skip platform Kits while deleting all Kits if command.All { return nil } - return fmt.Errorf("integration kit \"%s\" is not editable", ctx.Name) + return fmt.Errorf("integration kit \"%s\" is not editable", kit.Name) } - err = c.Delete(command.Context, &ctx) + err = c.Delete(command.Context, kit) if err != nil && !k8errors.IsNotFound(err) { - return fmt.Errorf("error deleting integration kit \"%s\", %s", ctx.Name, err) + return fmt.Errorf("error deleting integration kit \"%s\", %s", kit.Name, err) } if err != nil && k8errors.IsNotFound(err) { - return fmt.Errorf("no integration kit found with name \"%s\"", ctx.Name) + return fmt.Errorf("no integration kit found with name \"%s\"", kit.Name) } - fmt.Printf("integration kit \"%s\" has been deleted\n", ctx.Name) + fmt.Printf("integration kit \"%s\" has been deleted\n", kit.Name) return err } diff --git a/pkg/cmd/kit_get.go b/pkg/cmd/kit_get.go index 5cba832..b3063cf 100644 --- a/pkg/cmd/kit_get.go +++ b/pkg/cmd/kit_get.go @@ -81,7 +81,7 @@ func (command *kitGetCommandOptions) run(cmd *cobra.Command) error { w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 8, 1, '\t', 0) fmt.Fprintln(w, "NAME\tPHASE\tTYPE\tIMAGE") for _, ctx := range kitList.Items { - t := ctx.Labels["camel.apache.org/kit.type"] + t := ctx.Labels[v1.IntegrationKitTypeLabel] u := command.User && t == v1.IntegrationKitTypeUser e := command.External && t == v1.IntegrationKitTypeExternal p := command.Platform && t == v1.IntegrationKitTypePlatform diff --git a/pkg/controller/integration/build_kit.go b/pkg/controller/integration/build_kit.go index 005ed45..cb2919c 100644 --- a/pkg/controller/integration/build_kit.go +++ b/pkg/controller/integration/build_kit.go @@ -26,6 +26,7 @@ import ( "github.com/rs/xid" k8serrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" ctrl "sigs.k8s.io/controller-runtime/pkg/client" @@ -34,7 +35,6 @@ import ( "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util" - "github.com/apache/camel-k/pkg/util/controller" "github.com/apache/camel-k/pkg/util/kubernetes" ) @@ -63,7 +63,7 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1.Integr } if kit != nil { - if kit.Labels["camel.apache.org/kit.type"] == v1.IntegrationKitTypePlatform { + if kit.Labels[v1.IntegrationKitTypeLabel] == v1.IntegrationKitTypePlatform { // This is a platform kit and as it is auto generated it may get // out of sync if the integration that has generated it, has been // amended to add/remove dependencies @@ -116,13 +116,12 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1.Integr return nil, err } - platformKitName := fmt.Sprintf("kit-%s", xid.New()) - platformKit := v1.NewIntegrationKit(integration.GetIntegrationKitNamespace(pl), platformKitName) + kit = v1.NewIntegrationKit(integration.GetIntegrationKitNamespace(pl), fmt.Sprintf("kit-%s", xid.New())) // Add some information for post-processing, this may need to be refactored // to a proper data structure - platformKit.Labels = map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + kit.Labels = map[string]string{ + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, "camel.apache.org/runtime.version": integration.Status.RuntimeVersion, "camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider), kubernetes.CamelCreatorLabelKind: v1.IntegrationKind, @@ -132,19 +131,23 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1.Integr } // Set the kit to have the same characteristics as the integrations - platformKit.Spec = v1.IntegrationKitSpec{ + kit.Spec = v1.IntegrationKitSpec{ Dependencies: integration.Status.Dependencies, Repositories: integration.Spec.Repositories, Traits: action.filterKitTraits(ctx, integration.Spec.Traits), } - if err := action.client.Create(ctx, &platformKit); err != nil { + if _, err := trait.Apply(ctx, action.client, integration, kit); err != nil { + return nil, err + } + + if err := action.client.Create(ctx, kit); err != nil { return nil, err } // Set the kit name so the next handle loop, will fall through the // same path as integration with a user defined kit - integration.SetIntegrationKit(&platformKit) + integration.SetIntegrationKit(kit) return integration, nil } @@ -181,16 +184,23 @@ func (action *buildKitAction) lookupKitForIntegration(ctx context.Context, c ctr return nil, err } + kitTypes, err := labels.NewRequirement(v1.IntegrationKitTypeLabel, selection.In, []string{ + v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeExternal, + }) + if err != nil { + return nil, err + } + options := []ctrl.ListOption{ ctrl.InNamespace(integration.GetIntegrationKitNamespace(pl)), ctrl.MatchingLabels{ "camel.apache.org/runtime.version": integration.Status.RuntimeVersion, "camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider), }, - controller.NewLabelSelector("camel.apache.org/kit.type", selection.In, []string{ - v1.IntegrationKitTypePlatform, - v1.IntegrationKitTypeExternal, - }), + ctrl.MatchingLabelsSelector{ + Selector: labels.NewSelector().Add(*kitTypes), + }, } kits := v1.NewIntegrationKitList() diff --git a/pkg/controller/integration/build_kit_test.go b/pkg/controller/integration/build_kit_test.go index 6d1489f..fa3df64 100644 --- a/pkg/controller/integration/build_kit_test.go +++ b/pkg/controller/integration/build_kit_test.go @@ -41,7 +41,7 @@ func TestLookupKitForIntegration_DiscardKitsInError(t *testing.T) { Namespace: "ns", Name: "my-kit-1", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, }, }, Spec: v1.IntegrationKitSpec{ @@ -63,7 +63,7 @@ func TestLookupKitForIntegration_DiscardKitsInError(t *testing.T) { Namespace: "ns", Name: "my-kit-2", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, }, }, Spec: v1.IntegrationKitSpec{ @@ -120,7 +120,7 @@ func TestLookupKitForIntegration_DiscardKitsWithIncompatibleTraits(t *testing.T) Namespace: "ns", Name: "my-kit-1", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, }, }, Spec: v1.IntegrationKitSpec{ @@ -146,7 +146,7 @@ func TestLookupKitForIntegration_DiscardKitsWithIncompatibleTraits(t *testing.T) Namespace: "ns", Name: "my-kit-2", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, }, }, Spec: v1.IntegrationKitSpec{ @@ -177,7 +177,7 @@ func TestLookupKitForIntegration_DiscardKitsWithIncompatibleTraits(t *testing.T) Namespace: "ns", Name: "my-kit-3", Labels: map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypePlatform, }, }, Spec: v1.IntegrationKitSpec{ diff --git a/pkg/controller/integration/initialize.go b/pkg/controller/integration/initialize.go index 7a55d80..b55066a 100644 --- a/pkg/controller/integration/initialize.go +++ b/pkg/controller/integration/initialize.go @@ -77,7 +77,7 @@ func (action *initializeAction) Handle(ctx context.Context, integration *v1.Inte } } kit := v1.NewIntegrationKit(kitNamespace, kitName) - integration.SetIntegrationKit(&kit) + integration.SetIntegrationKit(kit) } else { integration.Status.IntegrationKit = nil } diff --git a/pkg/controller/integrationkit/integrationkit_controller.go b/pkg/controller/integrationkit/integrationkit_controller.go index 3714c52..26b969b 100644 --- a/pkg/controller/integrationkit/integrationkit_controller.go +++ b/pkg/controller/integrationkit/integrationkit_controller.go @@ -206,7 +206,7 @@ func (r *reconcileIntegrationKit) Reconcile(ctx context.Context, request reconci targetLog := rlog.ForIntegrationKit(target) if target.Status.Phase == v1.IntegrationKitPhaseNone || target.Status.Phase == v1.IntegrationKitPhaseWaitingForPlatform { - if target.Labels["camel.apache.org/kit.type"] == v1.IntegrationKitTypeExternal { + if target.Labels[v1.IntegrationKitTypeLabel] == v1.IntegrationKitTypeExternal { target.Status.Phase = v1.IntegrationKitPhaseInitialization return r.update(ctx, &instance, target) } else { diff --git a/pkg/trait/container.go b/pkg/trait/container.go index b6c5caf..3a08235 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -190,7 +190,7 @@ func (t *containerTrait) configureDependencies(e *Environment) error { // Add some information for post-processing, this may need to be refactored // to a proper data structure kit.Labels = map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypeExternal, + v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeExternal, kubernetes.CamelCreatorLabelKind: v1.IntegrationKind, kubernetes.CamelCreatorLabelName: e.Integration.Name, kubernetes.CamelCreatorLabelNamespace: e.Integration.Namespace, @@ -198,8 +198,8 @@ func (t *containerTrait) configureDependencies(e *Environment) error { } t.L.Infof("image %s", kit.Spec.Image) - e.Resources.Add(&kit) - e.Integration.SetIntegrationKit(&kit) + e.Resources.Add(kit) + e.Integration.SetIntegrationKit(kit) } if IsTrue(t.ProbesEnabled) { if capability, ok := e.CamelCatalog.Runtime.Capabilities[v1.CapabilityHealth]; ok { diff --git a/pkg/trait/gc.go b/pkg/trait/gc.go index 59210ea..aec1008 100644 --- a/pkg/trait/gc.go +++ b/pkg/trait/gc.go @@ -36,10 +36,9 @@ import ( "k8s.io/client-go/discovery/cached/disk" "k8s.io/client-go/discovery/cached/memory" - "sigs.k8s.io/controller-runtime/pkg/client" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" - util "github.com/apache/camel-k/pkg/util/controller" ) var ( @@ -99,7 +98,7 @@ func (t *garbageCollectorTrait) Apply(e *Environment) error { // are ready. This is to be added when the integration scale status is refined with ready replicas e.PostActions = append(e.PostActions, func(env *Environment) error { // The collection and deletion are performed asynchronously to avoid blocking - // the reconcile loop. + // the reconciliation loop. go t.garbageCollectResources(env) return nil }) @@ -152,9 +151,9 @@ func (t *garbageCollectorTrait) deleteEachOf(gvks map[schema.GroupVersionKind]st "kind": gvk.Kind, }, } - options := []client.ListOption{ - client.InNamespace(e.Integration.Namespace), - util.MatchingSelector{Selector: selector}, + options := []ctrl.ListOption{ + ctrl.InNamespace(e.Integration.Namespace), + ctrl.MatchingLabelsSelector{Selector: selector}, } if err := t.Client.List(context.TODO(), &resources, options...); err != nil { if !k8serrors.IsNotFound(err) && !k8serrors.IsForbidden(err) { @@ -168,7 +167,7 @@ func (t *garbageCollectorTrait) deleteEachOf(gvks map[schema.GroupVersionKind]st if !t.canBeDeleted(e, r) { continue } - err := t.Client.Delete(context.TODO(), &r, client.PropagationPolicy(metav1.DeletePropagationBackground)) + err := t.Client.Delete(context.TODO(), &r, ctrl.PropagationPolicy(metav1.DeletePropagationBackground)) if err != nil { // The resource may have already been deleted if !k8serrors.IsNotFound(err) { diff --git a/pkg/trait/jvm.go b/pkg/trait/jvm.go index 1080de4..81700dc 100644 --- a/pkg/trait/jvm.go +++ b/pkg/trait/jvm.go @@ -88,10 +88,10 @@ func (t *jvmTrait) Apply(e *Environment) error { name := e.Integration.Status.IntegrationKit.Name ns := e.Integration.GetIntegrationKitNamespace(e.Platform) k := v1.NewIntegrationKit(ns, name) - if err := t.Client.Get(t.Ctx, ctrl.ObjectKeyFromObject(&k), &k); err != nil { + if err := t.Client.Get(t.Ctx, ctrl.ObjectKeyFromObject(k), k); err != nil { return errors.Wrapf(err, "unable to find integration kit %s/%s, %s", ns, name, err) } - kit = &k + kit = k } if kit == nil { @@ -114,10 +114,10 @@ func (t *jvmTrait) Apply(e *Environment) error { classpath.Add(artifact.Target) } - if kit.Labels["camel.apache.org/kit.type"] == v1.IntegrationKitTypeExternal { + if kit.Labels[v1.IntegrationKitTypeLabel] == v1.IntegrationKitTypeExternal { // In case of an external created kit, we do not have any information about - // the classpath so we assume the all jars in /deployments/dependencies/ have - // to be taken into account + // the classpath, so we assume the all jars in /deployments/dependencies/ have + // to be taken into account. dependencies := path.Join(builder.DeploymentDir, builder.DependenciesDir) classpath.Add( dependencies+"/*", diff --git a/pkg/trait/jvm_test.go b/pkg/trait/jvm_test.go index 343b25d..4c24a18 100644 --- a/pkg/trait/jvm_test.go +++ b/pkg/trait/jvm_test.go @@ -278,7 +278,7 @@ func createNominalJvmTest(kitType string) (*jvmTrait, *Environment) { Namespace: "kit-namespace", Name: "kit-name", Labels: map[string]string{ - "camel.apache.org/kit.type": kitType, + v1.IntegrationKitTypeLabel: kitType, }, }, Status: v1.IntegrationKitStatus{ diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go index b43e045..97f05fb 100644 --- a/pkg/trait/quarkus.go +++ b/pkg/trait/quarkus.go @@ -62,13 +62,22 @@ func (t *quarkusTrait) Configure(e *Environment) (bool, error) { t.PackageType = &packageType } - return e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted) || - e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) || - e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseRunning), nil + return e.IntegrationKitInPhase(v1.IntegrationKitPhaseNone, v1.IntegrationKitPhaseBuildSubmitted) || + e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) || + e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseRunning), + nil } func (t *quarkusTrait) Apply(e *Environment) error { - if e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted) { + switch e.IntegrationKit.Status.Phase { + + case v1.IntegrationKitPhaseNone: + if e.IntegrationKit.Labels == nil { + e.IntegrationKit.Labels = make(map[string]string) + } + e.IntegrationKit.Labels[v1.IntegrationKitLayoutLabel] = string(*t.PackageType) + + case v1.IntegrationKitPhaseBuildSubmitted: build := getBuilderTask(e.BuildTasks) if build == nil { return fmt.Errorf("unable to find builder task: %s", e.Integration.Name) @@ -99,11 +108,9 @@ func (t *quarkusTrait) Apply(e *Environment) error { }) build.Steps = builder.StepIDsFor(steps...) - } - if e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) || - e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseRunning) { - if t.isNativePackageType() { + case v1.IntegrationKitPhaseReady: + if e.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning) && t.isNativePackageType() { container := e.getIntegrationContainer() if container == nil { return fmt.Errorf("unable to find integration container: %s", e.Integration.Name) diff --git a/pkg/trait/quarkus_test.go b/pkg/trait/quarkus_test.go index 7f4d970..9051401 100644 --- a/pkg/trait/quarkus_test.go +++ b/pkg/trait/quarkus_test.go @@ -55,12 +55,16 @@ func TestConfigureDisabledQuarkusTraitShouldFail(t *testing.T) { assert.Nil(t, err) } -func TestApplyQuarkusTraitDoesNothing(t *testing.T) { +func TestApplyQuarkusTraitDefaultKitLayout(t *testing.T) { quarkusTrait, environment := createNominalQuarkusTest() - err := quarkusTrait.Apply(environment) + configured, err := quarkusTrait.Configure(environment) + assert.True(t, configured) + assert.Nil(t, err) + err = quarkusTrait.Apply(environment) assert.Nil(t, err) + assert.Equal(t, environment.IntegrationKit.Labels[v1.IntegrationKitLayoutLabel], v1.IntegrationKitLayoutFastJar) } func createNominalQuarkusTest() (*quarkusTrait, *Environment) { diff --git a/pkg/trait/util.go b/pkg/trait/util.go index 630776d..dcc7b38 100644 --- a/pkg/trait/util.go +++ b/pkg/trait/util.go @@ -25,7 +25,6 @@ import ( "sort" "strings" - "github.com/apache/camel-k/pkg/util/property" user "github.com/mitchellh/go-homedir" "github.com/scylladb/go-set/strset" @@ -36,6 +35,7 @@ import ( "github.com/apache/camel-k/pkg/metadata" "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/camel" + "github.com/apache/camel-k/pkg/util/property" ) var exactVersionRegexp = regexp.MustCompile(`^(\d+)\.(\d+)\.([\w-.]+)$`) @@ -46,8 +46,8 @@ func getIntegrationKit(ctx context.Context, c client.Client, integration *v1.Int return nil, nil } kit := v1.NewIntegrationKit(integration.Status.IntegrationKit.Namespace, integration.Status.IntegrationKit.Name) - err := c.Get(ctx, ctrl.ObjectKeyFromObject(&kit), &kit) - return &kit, err + err := c.Get(ctx, ctrl.ObjectKeyFromObject(kit), kit) + return kit, err } func collectConfigurationValues(configurationType string, configurable ...v1.Configurable) []string { @@ -221,7 +221,7 @@ func AddSourceDependencies(source v1.SourceSpec, catalog *camel.RuntimeCatalog) return dependencies } -/// Bool pointer operations: +// Bool pointer operations: // BoolP returns a pointer to a bool value func BoolP(b bool) *bool { diff --git a/pkg/util/controller/util.go b/pkg/util/controller/util.go deleted file mode 100644 index b80c94e..0000000 --- a/pkg/util/controller/util.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -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 controller - -import ( - "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/selection" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -type MatchingSelector struct { - Selector labels.Selector -} - -func (s MatchingSelector) ApplyToList(opts *client.ListOptions) { - opts.LabelSelector = s.Selector -} - -func (s MatchingSelector) ApplyToDeleteAllOf(opts *client.DeleteAllOfOptions) { - opts.LabelSelector = s.Selector -} - -func NewLabelSelector(key string, op selection.Operator, values []string) MatchingSelector { - provider, _ := labels.NewRequirement(key, op, values) - selector := labels.NewSelector().Add(*provider) - - return MatchingSelector{ - Selector: selector, - } -} diff --git a/pkg/util/kubernetes/client.go b/pkg/util/kubernetes/client.go index 1f8795b..bb8514e 100644 --- a/pkg/util/kubernetes/client.go +++ b/pkg/util/kubernetes/client.go @@ -32,57 +32,34 @@ import ( ) func GetIntegrationPlatform(context context.Context, client ctrl.Reader, name string, namespace string) (*v1.IntegrationPlatform, error) { - key := ctrl.ObjectKey{ - Name: name, - Namespace: namespace, - } - - answer := v1.NewIntegrationPlatform(namespace, name) - - if err := client.Get(context, key, &answer); err != nil { + platform := v1.NewIntegrationPlatform(namespace, name) + if err := client.Get(context, ctrl.ObjectKeyFromObject(&platform), &platform); err != nil { return nil, err } - return &answer, nil + return &platform, nil } func GetIntegrationKit(context context.Context, client ctrl.Reader, name string, namespace string) (*v1.IntegrationKit, error) { - key := ctrl.ObjectKey{ - Name: name, - Namespace: namespace, - } - - answer := v1.NewIntegrationKit(namespace, name) - - if err := client.Get(context, key, &answer); err != nil { + kit := v1.NewIntegrationKit(namespace, name) + if err := client.Get(context, ctrl.ObjectKeyFromObject(kit), kit); err != nil { return nil, err } - return &answer, nil + return kit, nil } func GetBuild(context context.Context, client client.Client, name string, namespace string) (*v1.Build, error) { - key := ctrl.ObjectKey{ - Name: name, - Namespace: namespace, - } - - answer := v1.NewBuild(namespace, name) - - if err := client.Get(context, key, &answer); err != nil { + build := v1.NewBuild(namespace, name) + if err := client.Get(context, ctrl.ObjectKeyFromObject(build), build); err != nil { return nil, err } - return &answer, nil + return build, nil } func GetConfigMap(context context.Context, client ctrl.Reader, name string, namespace string) (*corev1.ConfigMap, error) { - key := ctrl.ObjectKey{ - Name: name, - Namespace: namespace, - } - - answer := corev1.ConfigMap{ + configMap := &corev1.ConfigMap{ TypeMeta: metav1.TypeMeta{ Kind: "ConfigMap", APIVersion: "v1", @@ -93,20 +70,15 @@ func GetConfigMap(context context.Context, client ctrl.Reader, name string, name }, } - if err := client.Get(context, key, &answer); err != nil { + if err := client.Get(context, ctrl.ObjectKeyFromObject(configMap), configMap); err != nil { return nil, err } - return &answer, nil + return configMap, nil } func GetSecret(context context.Context, client ctrl.Reader, name string, namespace string) (*corev1.Secret, error) { - key := ctrl.ObjectKey{ - Name: name, - Namespace: namespace, - } - - answer := corev1.Secret{ + secret := &corev1.Secret{ TypeMeta: metav1.TypeMeta{ Kind: "Secret", APIVersion: "v1", @@ -117,11 +89,11 @@ func GetSecret(context context.Context, client ctrl.Reader, name string, namespa }, } - if err := client.Get(context, key, &answer); err != nil { + if err := client.Get(context, ctrl.ObjectKeyFromObject(secret), secret); err != nil { return nil, err } - return &answer, nil + return secret, nil } // GetSecretRefValue returns the value of a secret in the supplied namespace @@ -161,7 +133,6 @@ func GetConfigMapRefValue(ctx context.Context, client ctrl.Reader, namespace str return "", fmt.Errorf("key %s not found in config map %s", selector.Key, selector.Name) } -// ResolveValueSource -- func ResolveValueSource(ctx context.Context, client ctrl.Reader, namespace string, valueSource *v1.ValueSource) (string, error) { if valueSource.ConfigMapKeyRef != nil && valueSource.SecretKeyRef != nil { return "", fmt.Errorf("value source has bot config map and secret configured") @@ -176,17 +147,20 @@ func ResolveValueSource(ctx context.Context, client ctrl.Reader, namespace strin return "", nil } -// GetDeployment -- func GetDeployment(context context.Context, client ctrl.Reader, name string, namespace string) (*appsv1.Deployment, error) { - - key := ctrl.ObjectKey{ - Name: name, - Namespace: namespace, + deployment := &appsv1.Deployment{ + TypeMeta: metav1.TypeMeta{ + Kind: "Deployment", + APIVersion: appsv1.SchemeGroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, } - deployment := appsv1.Deployment{} - if err := client.Get(context, key, &deployment); err != nil { + if err := client.Get(context, ctrl.ObjectKeyFromObject(deployment), deployment); err != nil { return nil, err } - return &deployment, nil + return deployment, nil }
