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 1c242673247b2cd173003202fcb74fec5c889848 Author: Christoph Deppisch <cdeppi...@redhat.com> AuthorDate: Fri Jun 24 20:28:37 2022 +0200 fix(#2177): Reduce duplicate code --- addons/threescale/3scale.go | 12 +++++------ e2e/support/test_support.go | 20 ++++++------------- pkg/apis/camel/v1/common_types_support.go | 11 ++++++++++- pkg/apis/camel/v1/integration_types_support.go | 6 +----- pkg/apis/camel/v1/integrationkit_types_support.go | 6 +----- .../camel/v1/integrationplatform_types_support.go | 6 +----- .../v1alpha1/kamelet_binding_types_support.go | 6 +----- pkg/cmd/bind.go | 13 +++--------- pkg/cmd/run.go | 12 +++-------- pkg/cmd/util.go | 23 +++++++++++++++++++--- pkg/install/operator.go | 3 --- pkg/trait/container.go | 5 +---- pkg/trait/platform.go | 3 --- pkg/trait/quarkus.go | 5 +---- pkg/util/kubernetes/replace.go | 15 +++++--------- 15 files changed, 58 insertions(+), 88 deletions(-) diff --git a/addons/threescale/3scale.go b/addons/threescale/3scale.go index 85e8c08a0..e7f246deb 100644 --- a/addons/threescale/3scale.go +++ b/addons/threescale/3scale.go @@ -22,6 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait" "github.com/apache/camel-k/pkg/trait" ) @@ -126,19 +127,16 @@ func (t *threeScaleTrait) addLabelsAndAnnotations(obj *metav1.ObjectMeta) { } obj.Labels[ThreeScaleDiscoveryLabel] = ThreeScaleDiscoveryLabelEnabled - if obj.Annotations == nil { - obj.Annotations = make(map[string]string) - } if t.Scheme != "" { - obj.Annotations[ThreeScaleSchemeAnnotation] = t.Scheme + v1.SetAnnotation(obj, ThreeScaleSchemeAnnotation, t.Scheme) } if t.Path != "" { - obj.Annotations[ThreeScalePathAnnotation] = t.Path + v1.SetAnnotation(obj, ThreeScalePathAnnotation, t.Path) } if t.Port != 0 { - obj.Annotations[ThreeScalePortAnnotation] = strconv.Itoa(t.Port) + v1.SetAnnotation(obj, ThreeScalePortAnnotation, strconv.Itoa(t.Port)) } if t.DescriptionPath != nil && *t.DescriptionPath != "" { - obj.Annotations[ThreeScaleDescriptionPathAnnotation] = *t.DescriptionPath + v1.SetAnnotation(obj, ThreeScaleDescriptionPathAnnotation, *t.DescriptionPath) } } diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index ec968fcb7..2f8cbc150 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -598,9 +598,7 @@ func AssignIntegrationToOperator(ns, name, operator string) error { if it == nil { return fmt.Errorf("cannot assign integration %q to operator: integration not found", name) } - if it.Annotations == nil { - it.Annotations = make(map[string]string) - } + it.SetOperatorID(operator) return TestClient().Update(TestContext, it) } @@ -991,10 +989,8 @@ func AssignKameletBindingToOperator(ns, name, operator string) error { if klb == nil { return fmt.Errorf("cannot assign kamelet binding %q to operator: kamelet binding not found", name) } - if klb.Annotations == nil { - klb.Annotations = make(map[string]string) - } - klb.Annotations[v1.OperatorIDAnnotation] = operator + + klb.SetOperatorID(operator) return TestClient().Update(TestContext, klb) } @@ -1363,9 +1359,7 @@ func AssignPlatformToOperator(ns, operator string) error { if pl == nil { return errors.New("cannot assign platform to operator: no platform found") } - if pl.Annotations == nil { - pl.Annotations = make(map[string]string) - } + pl.SetOperatorID(operator) return TestClient().Update(TestContext, pl) } @@ -1375,10 +1369,8 @@ func ConfigureSecondaryPlatformWith(ns string, customizer func(pl *v1.Integratio if pl == nil { return errors.New("cannot find primary platform") } - if pl.Annotations == nil { - pl.Annotations = make(map[string]string) - } - pl.Annotations[v1.SecondaryPlatformAnnotation] = "true" + + v1.SetAnnotation(&pl.ObjectMeta, v1.SecondaryPlatformAnnotation, "true") pl.ObjectMeta.ResourceVersion = "" pl.Name = "" pl.Status = v1.IntegrationPlatformStatus{} diff --git a/pkg/apis/camel/v1/common_types_support.go b/pkg/apis/camel/v1/common_types_support.go index bb93121b8..cf66c8b7f 100644 --- a/pkg/apis/camel/v1/common_types_support.go +++ b/pkg/apis/camel/v1/common_types_support.go @@ -69,7 +69,7 @@ func (m *RawMessage) UnmarshalJSON(data []byte) error { return nil } -// GetOperatorIDAnnotation to safely get the operator id annotation value +// GetOperatorIDAnnotation to safely get the operator id annotation value. func GetOperatorIDAnnotation(obj metav1.Object) string { if obj == nil || obj.GetAnnotations() == nil { return "" @@ -82,5 +82,14 @@ func GetOperatorIDAnnotation(obj metav1.Object) string { return "" } +// SetAnnotation safely sets the annotation on the given runtime object. +func SetAnnotation(obj *metav1.ObjectMeta, name string, value string) { + if obj.Annotations == nil { + obj.Annotations = make(map[string]string) + } + + obj.Annotations[name] = value +} + var _ json.Marshaler = (*RawMessage)(nil) var _ json.Unmarshaler = (*RawMessage)(nil) diff --git a/pkg/apis/camel/v1/integration_types_support.go b/pkg/apis/camel/v1/integration_types_support.go index 9d137de41..5b6fd9130 100644 --- a/pkg/apis/camel/v1/integration_types_support.go +++ b/pkg/apis/camel/v1/integration_types_support.go @@ -257,11 +257,7 @@ func (in *SourceSpec) InferLanguage() Language { // SetOperatorID sets the given operator id as an annotation func (in *Integration) SetOperatorID(operatorID string) { - if in.Annotations == nil { - in.Annotations = make(map[string]string) - } - - in.Annotations[OperatorIDAnnotation] = operatorID + SetAnnotation(&in.ObjectMeta, OperatorIDAnnotation, operatorID) } func (in *Integration) SetIntegrationPlatform(platform *IntegrationPlatform) { diff --git a/pkg/apis/camel/v1/integrationkit_types_support.go b/pkg/apis/camel/v1/integrationkit_types_support.go index 5862805f9..87d7667be 100644 --- a/pkg/apis/camel/v1/integrationkit_types_support.go +++ b/pkg/apis/camel/v1/integrationkit_types_support.go @@ -56,11 +56,7 @@ func (in *IntegrationKitSpec) Configurations() []ConfigurationSpec { // SetOperatorID sets the given operator id as an annotation func (in *IntegrationKit) SetOperatorID(operatorID string) { - if in.Annotations == nil { - in.Annotations = make(map[string]string) - } - - in.Annotations[OperatorIDAnnotation] = operatorID + SetAnnotation(&in.ObjectMeta, OperatorIDAnnotation, operatorID) } func (in *IntegrationKit) Configurations() []ConfigurationSpec { diff --git a/pkg/apis/camel/v1/integrationplatform_types_support.go b/pkg/apis/camel/v1/integrationplatform_types_support.go index faa287750..769aea622 100644 --- a/pkg/apis/camel/v1/integrationplatform_types_support.go +++ b/pkg/apis/camel/v1/integrationplatform_types_support.go @@ -70,11 +70,7 @@ func (in *IntegrationPlatformSpec) Configurations() []ConfigurationSpec { // SetOperatorID sets the given operator id as an annotation func (in *IntegrationPlatform) SetOperatorID(operatorID string) { - if in.Annotations == nil { - in.Annotations = make(map[string]string) - } - - in.Annotations[OperatorIDAnnotation] = operatorID + SetAnnotation(&in.ObjectMeta, OperatorIDAnnotation, operatorID) } // Configurations -- diff --git a/pkg/apis/camel/v1alpha1/kamelet_binding_types_support.go b/pkg/apis/camel/v1alpha1/kamelet_binding_types_support.go index 90c36c821..0e11963aa 100644 --- a/pkg/apis/camel/v1alpha1/kamelet_binding_types_support.go +++ b/pkg/apis/camel/v1alpha1/kamelet_binding_types_support.go @@ -68,11 +68,7 @@ func (c KameletBindingCondition) GetMessage() string { // SetOperatorID sets the given operator id as an annotation func (in *KameletBinding) SetOperatorID(operatorID string) { - if in.Annotations == nil { - in.Annotations = make(map[string]string) - } - - in.Annotations[v1.OperatorIDAnnotation] = operatorID + v1.SetAnnotation(&in.ObjectMeta, v1.OperatorIDAnnotation, operatorID) } // GetCondition returns the condition with the provided type. diff --git a/pkg/cmd/bind.go b/pkg/cmd/bind.go index 0632c8d87..1effdf1e8 100644 --- a/pkg/cmd/bind.go +++ b/pkg/cmd/bind.go @@ -25,7 +25,6 @@ import ( v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - platformutil "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/apache/camel-k/pkg/util/reference" @@ -238,17 +237,11 @@ func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error { } if o.OperatorID != "" { - if pl, err := platformutil.LookupForPlatformName(o.Context, client, o.OperatorID); err != nil { - if k8serrors.IsForbidden(err) { - o.PrintfVerboseOutf(cmd, "Unable to verify existence of operator id [%s] due to lack of user privileges\n", o.OperatorID) - } else { - return err - } - } else if pl == nil { + if err := verifyOperatorID(o.Context, client, o.OperatorID, cmd.OutOrStdout()); err != nil { if o.Force { - o.PrintfVerboseOutf(cmd, "Unable to find operator with given id [%s] - Kamelet binding may not be reconciled and get stuck in waiting state\n", o.OperatorID) + o.PrintfVerboseErrf(cmd, "%s, use --force option or make sure to use a proper operator id", err.Error()) } else { - return fmt.Errorf("unable to find integration platform for given operator id '%s', use --force option or make sure to use a proper operator id", o.OperatorID) + return err } } } diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 5d46a9fb0..5298680f7 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -553,17 +553,11 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C } if o.OperatorID != "" { - if pl, err := platformutil.LookupForPlatformName(o.Context, c, o.OperatorID); err != nil { - if k8serrors.IsForbidden(err) { - o.PrintfVerboseOutf(cmd, "Unable to verify existence of operator id [%s] due to lack of user privileges\n", o.OperatorID) - } else { - return nil, err - } - } else if pl == nil { + if err := verifyOperatorID(o.Context, c, o.OperatorID, cmd.OutOrStdout()); err != nil { if o.Force { - o.PrintfVerboseOutf(cmd, "Unable to find operator with given id [%s] - integration may not be reconciled and get stuck in waiting state\n", o.OperatorID) + o.PrintfVerboseErrf(cmd, "%s, use --force option or make sure to use a proper operator id", err.Error()) } else { - return nil, fmt.Errorf("unable to find integration platform for given operator id '%s', use --force option or make sure to use a proper operator id", o.OperatorID) + return nil, err } } } diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go index 09768c881..1dd67136a 100644 --- a/pkg/cmd/util.go +++ b/pkg/cmd/util.go @@ -22,23 +22,25 @@ import ( "encoding/csv" "encoding/json" "fmt" + "io" "log" "os" "reflect" "strings" - "github.com/apache/camel-k/pkg/util/gzip" - "github.com/pkg/errors" - "github.com/mitchellh/mapstructure" + "github.com/pkg/errors" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/client" + platformutil "github.com/apache/camel-k/pkg/platform" + "github.com/apache/camel-k/pkg/util/gzip" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" p "github.com/gertd/go-pluralize" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -283,3 +285,18 @@ func hasSupportedScheme(uri string) bool { return false } + +func verifyOperatorID(ctx context.Context, client client.Client, operatorID string, out io.Writer) error { + if pl, err := platformutil.LookupForPlatformName(ctx, client, operatorID); err != nil { + if k8serrors.IsForbidden(err) { + _, printErr := fmt.Fprintf(out, "Unable to verify existence of operator id [%s] due to lack of user privileges\n", operatorID) + return printErr + } + + return err + } else if pl == nil { + return fmt.Errorf("unable to find operator with given id [%s] - resource may not be reconciled and get stuck in waiting state", operatorID) + } + + return nil +} diff --git a/pkg/install/operator.go b/pkg/install/operator.go index 7ce9ad75f..0b351b180 100644 --- a/pkg/install/operator.go +++ b/pkg/install/operator.go @@ -533,9 +533,6 @@ func NewPlatform(ctx context.Context, c client.Client, clusterType string, skipR if operatorID != "" { // We must tell the operator to reconcile this IntegrationPlatform - if pl.Annotations == nil { - pl.Annotations = make(map[string]string) - } pl.SetOperatorID(operatorID) pl.Name = operatorID } diff --git a/pkg/trait/container.go b/pkg/trait/container.go index 33ab614dc..1ae267dd3 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -129,11 +129,8 @@ func (t *containerTrait) configureImageIntegrationKit(e *Environment) error { kubernetes.CamelCreatorLabelVersion: e.Integration.ResourceVersion, } - if kit.Annotations == nil { - kit.Annotations = make(map[string]string) - } if v, ok := e.Integration.Annotations[v1.PlatformSelectorAnnotation]; ok { - kit.Annotations[v1.PlatformSelectorAnnotation] = v + v1.SetAnnotation(&kit.ObjectMeta, v1.PlatformSelectorAnnotation, v) } operatorID := defaults.OperatorID() if operatorID != "" { diff --git a/pkg/trait/platform.go b/pkg/trait/platform.go index 5ee3759db..263214ef4 100644 --- a/pkg/trait/platform.go +++ b/pkg/trait/platform.go @@ -120,9 +120,6 @@ func (t *platformTrait) getOrCreatePlatform(e *Environment) (*v1.IntegrationPlat defaultPlatform.Labels["camel.apache.org/platform.generated"] = True // Cascade the operator id in charge to reconcile the Integration if v1.GetOperatorIDAnnotation(e.Integration) != "" { - if defaultPlatform.Annotations == nil { - defaultPlatform.Annotations = make(map[string]string) - } defaultPlatform.SetOperatorID(v1.GetOperatorIDAnnotation(e.Integration)) } pl = &defaultPlatform diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go index 1ad680634..a35235921 100644 --- a/pkg/trait/quarkus.go +++ b/pkg/trait/quarkus.go @@ -194,11 +194,8 @@ func (t *quarkusTrait) newIntegrationKit(e *Environment, packageType traitv1.Qua kubernetes.CamelCreatorLabelVersion: integration.ResourceVersion, } - if kit.Annotations == nil { - kit.Annotations = make(map[string]string) - } if v, ok := integration.Annotations[v1.PlatformSelectorAnnotation]; ok { - kit.Annotations[v1.PlatformSelectorAnnotation] = v + v1.SetAnnotation(&kit.ObjectMeta, v1.PlatformSelectorAnnotation, v) } operatorID := defaults.OperatorID() if operatorID != "" { diff --git a/pkg/util/kubernetes/replace.go b/pkg/util/kubernetes/replace.go index 58533b906..9554c07f4 100644 --- a/pkg/util/kubernetes/replace.go +++ b/pkg/util/kubernetes/replace.go @@ -33,6 +33,7 @@ import ( routev1 "github.com/openshift/api/route/v1" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/client" ) @@ -84,14 +85,11 @@ func mapRequiredRouteData(from runtime.Object, to runtime.Object) { func mapRequiredKnativeServiceV1Beta1Data(from runtime.Object, to runtime.Object) { if fromC, ok := from.(*serving.Service); ok { if toC, ok := to.(*serving.Service); ok { - if toC.ObjectMeta.Annotations == nil { - toC.ObjectMeta.Annotations = make(map[string]string) - } if v, present := fromC.ObjectMeta.Annotations["serving.knative.dev/creator"]; present { - toC.ObjectMeta.Annotations["serving.knative.dev/creator"] = v + v1.SetAnnotation(&toC.ObjectMeta, "serving.knative.dev/creator", v) } if v, present := fromC.ObjectMeta.Annotations["serving.knative.dev/lastModifier"]; present { - toC.ObjectMeta.Annotations["serving.knative.dev/lastModifier"] = v + v1.SetAnnotation(&toC.ObjectMeta, "serving.knative.dev/lastModifier", v) } } } @@ -100,14 +98,11 @@ func mapRequiredKnativeServiceV1Beta1Data(from runtime.Object, to runtime.Object func mapRequiredKnativeServiceV1Data(from runtime.Object, to runtime.Object) { if fromC, ok := from.(*serving.Service); ok { if toC, ok := to.(*serving.Service); ok { - if toC.ObjectMeta.Annotations == nil { - toC.ObjectMeta.Annotations = make(map[string]string) - } if v, present := fromC.ObjectMeta.Annotations["serving.knative.dev/creator"]; present { - toC.ObjectMeta.Annotations["serving.knative.dev/creator"] = v + v1.SetAnnotation(&toC.ObjectMeta, "serving.knative.dev/creator", v) } if v, present := fromC.ObjectMeta.Annotations["serving.knative.dev/lastModifier"]; present { - toC.ObjectMeta.Annotations["serving.knative.dev/lastModifier"] = v + v1.SetAnnotation(&toC.ObjectMeta, "serving.knative.dev/lastModifier", v) } } }