diff --git a/pkg/apis/camel/v1alpha1/register.go b/pkg/apis/camel/v1alpha1/register.go index 52af408370..460dd4c012 100644 --- a/pkg/apis/camel/v1alpha1/register.go +++ b/pkg/apis/camel/v1alpha1/register.go @@ -31,14 +31,15 @@ const ( ) var ( - SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) - AddToScheme = SchemeBuilder.AddToScheme // SchemeGroupVersion is the group version used to register these objects. SchemeGroupVersion = schema.GroupVersion{Group: groupName, Version: version} ) func init() { - sdkK8sutil.AddToSDKScheme(AddToScheme) + schemeBuilder := runtime.NewSchemeBuilder(addKnownTypes) + addToScheme := schemeBuilder.AddToScheme + + sdkK8sutil.AddToSDKScheme(addToScheme) } // addKnownTypes adds the set of types defined in this package to the supplied scheme. diff --git a/pkg/build/local/local_builder.go b/pkg/build/local/local_builder.go index 0274c12add..c3412740bf 100644 --- a/pkg/build/local/local_builder.go +++ b/pkg/build/local/local_builder.go @@ -111,11 +111,11 @@ func (b *localBuilder) buildCycle(ctx context.Context) { } func (b *localBuilder) execute(source build.Request) (string, error) { - project, err := generateProjectDefinition(source) + integration, err := generateIntegration(source) if err != nil { return "", err } - tarFileName, err := maven.Build(project) + tarFileName, err := maven.Build(integration) if err != nil { return "", err } @@ -254,24 +254,24 @@ func (b *localBuilder) publish(tarFile string, source build.Request) (string, er return is.Status.DockerImageRepository + ":" + source.Identifier.Qualifier, nil } -func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, error) { - project := maven.ProjectDefinition{ +func generateIntegration(source build.Request) (maven.Integration, error) { + integration := maven.Integration{ Project: maven.Project{ XMLName: xml.Name{Local: "project"}, - XmlNs: "http://maven.apache.org/POM/4.0.0", - XmlNsXsi: "http://www.w3.org/2001/XMLSchema-instance", + XMLNs: "http://maven.apache.org/POM/4.0.0", + XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance", XsiSchemaLocation: "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd", ModelVersion: "4.0.0", - GroupId: "org.apache.camel.k.integration", - ArtifactId: "camel-k-integration", + GroupID: "org.apache.camel.k.integration", + ArtifactID: "camel-k-integration", Version: version.Version, DependencyManagement: maven.DependencyManagement{ Dependencies: maven.Dependencies{ Dependencies: []maven.Dependency{ { //TODO: camel version should be retrieved from an external source or provided as static version - GroupId: "org.apache.camel", - ArtifactId: "camel-bom", + GroupID: "org.apache.camel", + ArtifactID: "camel-bom", Version: "2.22.1", Type: "pom", Scope: "import", @@ -291,7 +291,7 @@ func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, e // set-up dependencies // - deps := &project.Project.Dependencies + deps := &integration.Project.Dependencies deps.AddGAV("org.apache.camel.k", "camel-k-runtime-jvm", version.Version) for _, d := range source.Dependencies { @@ -309,9 +309,9 @@ func generateProjectDefinition(source build.Request) (maven.ProjectDefinition, e deps.AddEncodedGAV(gav) } else { - return maven.ProjectDefinition{}, fmt.Errorf("unknown dependency type: %s", d) + return maven.Integration{}, fmt.Errorf("unknown dependency type: %s", d) } } - return project, nil + return integration, nil } diff --git a/pkg/build/local/local_builder_test.go b/pkg/build/local/local_builder_test.go index 790c124ce7..c5fd33ddd2 100644 --- a/pkg/build/local/local_builder_test.go +++ b/pkg/build/local/local_builder_test.go @@ -43,16 +43,16 @@ func TestProjectGeneration(t *testing.T) { }, } - prj, err := generateProjectDefinition(source) + prj, err := generateIntegration(source) assert.Nil(t, err) assert.NotNil(t, prj) assert.Equal(t, len(prj.Project.Dependencies.Dependencies), 5) - assert.Equal(t, prj.Project.Dependencies.Dependencies[0].ArtifactId, "camel-k-runtime-jvm") - assert.Equal(t, prj.Project.Dependencies.Dependencies[1].ArtifactId, "camel-mail") - assert.Equal(t, prj.Project.Dependencies.Dependencies[2].ArtifactId, "camel-netty4") - assert.Equal(t, prj.Project.Dependencies.Dependencies[3].ArtifactId, "camel-servicenow") + assert.Equal(t, prj.Project.Dependencies.Dependencies[0].ArtifactID, "camel-k-runtime-jvm") + assert.Equal(t, prj.Project.Dependencies.Dependencies[1].ArtifactID, "camel-mail") + assert.Equal(t, prj.Project.Dependencies.Dependencies[2].ArtifactID, "camel-netty4") + assert.Equal(t, prj.Project.Dependencies.Dependencies[3].ArtifactID, "camel-servicenow") assert.Equal(t, prj.Project.Dependencies.Dependencies[3].Version, "2.21.1") - assert.Equal(t, prj.Project.Dependencies.Dependencies[4].ArtifactId, "camel-salesforce") + assert.Equal(t, prj.Project.Dependencies.Dependencies[4].ArtifactID, "camel-salesforce") assert.Equal(t, prj.Project.Dependencies.Dependencies[4].Version, "") } @@ -75,6 +75,6 @@ func TestProjectGenerationWithFailure(t *testing.T) { }, } - _, err := generateProjectDefinition(source) + _, err := generateIntegration(source) assert.NotNil(t, err) } diff --git a/pkg/build/local/scheme.go b/pkg/build/local/scheme.go deleted file mode 100644 index 465c29040d..0000000000 --- a/pkg/build/local/scheme.go +++ /dev/null @@ -1,98 +0,0 @@ -/* -Copyright 2018 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 local - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/runtime/serializer" - "k8s.io/apimachinery/pkg/runtime/serializer/json" - "k8s.io/apimachinery/pkg/runtime/serializer/versioning" -) - -var watchScheme = runtime.NewScheme() -var basicScheme = runtime.NewScheme() -var deleteScheme = runtime.NewScheme() -var parameterScheme = runtime.NewScheme() -var deleteOptionsCodec = serializer.NewCodecFactory(deleteScheme) -var dynamicParameterCodec = runtime.NewParameterCodec(parameterScheme) - -var versionV1 = schema.GroupVersion{Version: "v1"} - -func init() { - metav1.AddToGroupVersion(watchScheme, versionV1) - metav1.AddToGroupVersion(basicScheme, versionV1) - metav1.AddToGroupVersion(parameterScheme, versionV1) - metav1.AddToGroupVersion(deleteScheme, versionV1) -} - -var watchJsonSerializerInfo = runtime.SerializerInfo{ - MediaType: "application/json", - EncodesAsText: true, - Serializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, false), - PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, true), - StreamSerializer: &runtime.StreamSerializerInfo{ - EncodesAsText: true, - Serializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, false), - Framer: json.Framer, - }, -} - -// watchNegotiatedSerializer is used to read the wrapper of the watch stream -type watchNegotiatedSerializer struct{} - -var watchNegotiatedSerializerInstance = watchNegotiatedSerializer{} - -func (s watchNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo { - return []runtime.SerializerInfo{watchJsonSerializerInfo} -} - -func (s watchNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder { - return versioning.NewDefaultingCodecForScheme(watchScheme, encoder, nil, gv, nil) -} - -func (s watchNegotiatedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder { - return versioning.NewDefaultingCodecForScheme(watchScheme, nil, decoder, nil, gv) -} - -// basicNegotiatedSerializer is used to handle discovery and error handling serialization -type basicNegotiatedSerializer struct{} - -func (s basicNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo { - return []runtime.SerializerInfo{ - { - MediaType: "application/json", - EncodesAsText: true, - Serializer: json.NewSerializer(json.DefaultMetaFactory, basicScheme, basicScheme, false), - PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, basicScheme, basicScheme, true), - StreamSerializer: &runtime.StreamSerializerInfo{ - EncodesAsText: true, - Serializer: json.NewSerializer(json.DefaultMetaFactory, basicScheme, basicScheme, false), - Framer: json.Framer, - }, - }, - } -} - -func (s basicNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder { - return versioning.NewDefaultingCodecForScheme(watchScheme, encoder, nil, gv, nil) -} - -func (s basicNegotiatedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder { - return versioning.NewDefaultingCodecForScheme(watchScheme, nil, decoder, nil, gv) -} diff --git a/pkg/client/cmd/context.go b/pkg/client/cmd/context.go index 949dab04b6..7fa66ed78d 100644 --- a/pkg/client/cmd/context.go +++ b/pkg/client/cmd/context.go @@ -21,8 +21,7 @@ import ( "github.com/spf13/cobra" ) -// NewCmdContext -- -func NewCmdContext(rootCmdOptions *RootCmdOptions) *cobra.Command { +func newCmdContext(rootCmdOptions *RootCmdOptions) *cobra.Command { cmd := cobra.Command{ Use: "context", Short: "Configure an Integration Context", diff --git a/pkg/client/cmd/get.go b/pkg/client/cmd/get.go index 7ae35ce754..5b68adcfce 100644 --- a/pkg/client/cmd/get.go +++ b/pkg/client/cmd/get.go @@ -28,12 +28,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -type GetCmdOptions struct { +type getCmdOptions struct { *RootCmdOptions } -func NewCmdGet(rootCmdOptions *RootCmdOptions) *cobra.Command { - options := GetCmdOptions{ +func newCmdGet(rootCmdOptions *RootCmdOptions) *cobra.Command { + options := getCmdOptions{ RootCmdOptions: rootCmdOptions, } cmd := cobra.Command{ @@ -46,7 +46,7 @@ func NewCmdGet(rootCmdOptions *RootCmdOptions) *cobra.Command { return &cmd } -func (o *GetCmdOptions) run(cmd *cobra.Command, args []string) error { +func (o *getCmdOptions) run(cmd *cobra.Command, args []string) error { integrationList := v1alpha1.IntegrationList{ TypeMeta: metav1.TypeMeta{ APIVersion: v1alpha1.SchemeGroupVersion.String(), diff --git a/pkg/client/cmd/install.go b/pkg/client/cmd/install.go index cd84fbdf88..2ca05ba6fb 100644 --- a/pkg/client/cmd/install.go +++ b/pkg/client/cmd/install.go @@ -23,13 +23,12 @@ import ( "os" "github.com/apache/camel-k/pkg/install" + "github.com/pkg/errors" "github.com/spf13/cobra" k8serrors "k8s.io/apimachinery/pkg/api/errors" - "github.com/pkg/errors" ) -// NewCmdInstall -- -func NewCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command { +func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command { options := installCmdOptions{ RootCmdOptions: rootCmdOptions, } diff --git a/pkg/client/cmd/root.go b/pkg/client/cmd/root.go index dfbbfd9e4c..7fbeeef73c 100644 --- a/pkg/client/cmd/root.go +++ b/pkg/client/cmd/root.go @@ -73,11 +73,11 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) { } cmd.AddCommand(newCmdCompletion(&cmd)) - cmd.AddCommand(NewCmdVersion()) - cmd.AddCommand(NewCmdRun(&options)) - cmd.AddCommand(NewCmdGet(&options)) - cmd.AddCommand(NewCmdInstall(&options)) - cmd.AddCommand(NewCmdContext(&options)) + cmd.AddCommand(newCmdVersion()) + cmd.AddCommand(newCmdRun(&options)) + cmd.AddCommand(newCmdGet(&options)) + cmd.AddCommand(newCmdInstall(&options)) + cmd.AddCommand(newCmdContext(&options)) return &cmd, nil } diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go index 47e23fe905..37d06bb4a6 100644 --- a/pkg/client/cmd/run.go +++ b/pkg/client/cmd/run.go @@ -20,13 +20,14 @@ package cmd import ( "errors" "fmt" - "github.com/apache/camel-k/pkg/util/sync" - "github.com/sirupsen/logrus" "io/ioutil" "os" "strconv" "strings" + "github.com/apache/camel-k/pkg/util/sync" + "github.com/sirupsen/logrus" + "io" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" @@ -39,8 +40,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// NewCmdRun -- -func NewCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command { +func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command { options := runCmdOptions{ RootCmdOptions: rootCmdOptions, } @@ -134,7 +134,7 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error { func (o *runCmdOptions) waitForIntegrationReady(integration *v1alpha1.Integration) error { // Block this goroutine until the integration is in a final status - changes, err := watch.WatchStateChanges(o.Context, integration) + changes, err := watch.StateChanges(o.Context, integration) if err != nil { return err } diff --git a/pkg/client/cmd/version.go b/pkg/client/cmd/version.go index 1424a376c7..2153107620 100644 --- a/pkg/client/cmd/version.go +++ b/pkg/client/cmd/version.go @@ -24,7 +24,7 @@ import ( "github.com/spf13/cobra" ) -func NewCmdVersion() *cobra.Command { +func newCmdVersion() *cobra.Command { return &cobra.Command{ Use: "version", Short: "Display client version", diff --git a/pkg/install/cluster.go b/pkg/install/cluster.go index 4fddf54f87..855d8f6de7 100644 --- a/pkg/install/cluster.go +++ b/pkg/install/cluster.go @@ -29,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/util/yaml" ) +// SetupClusterwideResources -- func SetupClusterwideResources() error { // Install CRD for Integration Context (if needed) @@ -56,6 +57,7 @@ func SetupClusterwideResources() error { return nil } +// IsCRDInstalled check if the given CRT kind is installed func IsCRDInstalled(kind string) (bool, error) { lst, err := k8sclient.GetKubeClient().Discovery().ServerResourcesForGroupVersion("camel.apache.org/v1alpha1") if err != nil && errors.IsNotFound(err) { @@ -82,7 +84,7 @@ func installCRD(kind string, resourceName string) error { } crd := []byte(deploy.Resources[resourceName]) - crdJson, err := yaml.ToJSON(crd) + crdJSON, err := yaml.ToJSON(crd) if err != nil { return err } @@ -93,7 +95,7 @@ func installCRD(kind string, resourceName string) error { // Post using dynamic client result := restClient. Post(). - Body(crdJson). + Body(crdJSON). Resource("customresourcedefinitions"). Do() // Check result @@ -104,6 +106,7 @@ func installCRD(kind string, resourceName string) error { return nil } +// IsClusterRoleInstalled check if cluster role camel-k:edit is installed func IsClusterRoleInstalled() (bool, error) { clusterRole := v1.ClusterRole{ TypeMeta: metav1.TypeMeta{ diff --git a/pkg/stub/action/context/action.go b/pkg/stub/action/context/action.go index c35b84cad4..23b0f10375 100644 --- a/pkg/stub/action/context/action.go +++ b/pkg/stub/action/context/action.go @@ -21,6 +21,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" ) +// IntegrationContextAction -- type IntegrationContextAction interface { // a user friendly name for the action diff --git a/pkg/stub/action/context/initialize.go b/pkg/stub/action/context/initialize.go index 5803b38578..64d7647103 100644 --- a/pkg/stub/action/context/initialize.go +++ b/pkg/stub/action/context/initialize.go @@ -23,22 +23,23 @@ import ( "github.com/operator-framework/operator-sdk/pkg/sdk" ) +// NewIntegrationContextInitializeAction creates a new initialization handling action for the context func NewIntegrationContextInitializeAction() IntegrationContextAction { - return &integrationContexInitializeAction{} + return &integrationContextInitializeAction{} } -type integrationContexInitializeAction struct { +type integrationContextInitializeAction struct { } -func (action *integrationContexInitializeAction) Name() string { +func (action *integrationContextInitializeAction) Name() string { return "initialize" } -func (action *integrationContexInitializeAction) CanHandle(context *v1alpha1.IntegrationContext) bool { +func (action *integrationContextInitializeAction) CanHandle(context *v1alpha1.IntegrationContext) bool { return context.Status.Phase == "" } -func (action *integrationContexInitializeAction) Handle(context *v1alpha1.IntegrationContext) error { +func (action *integrationContextInitializeAction) Handle(context *v1alpha1.IntegrationContext) error { target := context.DeepCopy() // update the status diff --git a/pkg/stub/action/context/monitor.go b/pkg/stub/action/context/monitor.go index 138900beff..e30a1ea7da 100644 --- a/pkg/stub/action/context/monitor.go +++ b/pkg/stub/action/context/monitor.go @@ -24,6 +24,7 @@ import ( "github.com/sirupsen/logrus" ) +// NewIntegrationContextMonitorAction creates a new monitoring handling action for the context func NewIntegrationContextMonitorAction() IntegrationContextAction { return &integrationContextMonitorAction{} } diff --git a/pkg/stub/action/integration/action.go b/pkg/stub/action/integration/action.go index c1c7935c17..9e4a69c071 100644 --- a/pkg/stub/action/integration/action.go +++ b/pkg/stub/action/integration/action.go @@ -21,6 +21,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" ) +// IntegrationAction -- type IntegrationAction interface { // a user friendly name for the action diff --git a/pkg/stub/action/integration/initialize.go b/pkg/stub/action/integration/initialize.go index 25322fdd67..44bac896b5 100644 --- a/pkg/stub/action/integration/initialize.go +++ b/pkg/stub/action/integration/initialize.go @@ -18,34 +18,34 @@ limitations under the License. package action import ( + "sort" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/discover" "github.com/apache/camel-k/pkg/util/digest" "github.com/operator-framework/operator-sdk/pkg/sdk" - "sort" ) -// InitializeAction initializes the integration status to trigger the deployment -type InitializeAction struct { -} - // NewInitializeAction creates a new inititialize action func NewInitializeAction() IntegrationAction { - return &InitializeAction{} + return &initializeAction{} +} + +type initializeAction struct { } // Name returns a common name of the action -func (b *InitializeAction) Name() string { +func (action *initializeAction) Name() string { return "initialize" } // CanHandle tells whether this action can handle the integration -func (b *InitializeAction) CanHandle(integration *v1alpha1.Integration) bool { +func (action *initializeAction) CanHandle(integration *v1alpha1.Integration) bool { return integration.Status.Phase == "" } // Handle handles the integratios -func (b *InitializeAction) Handle(integration *v1alpha1.Integration) error { +func (action *initializeAction) Handle(integration *v1alpha1.Integration) error { target := integration.DeepCopy() // set default values if target.Spec.Replicas == nil { @@ -62,7 +62,7 @@ func (b *InitializeAction) Handle(integration *v1alpha1.Integration) error { } if *target.Spec.DependenciesAutoDiscovery { discovered := discover.Dependencies(target.Spec.Source) - target.Spec.Dependencies = b.mergeDependencies(target.Spec.Dependencies, discovered) + target.Spec.Dependencies = action.mergeDependencies(target.Spec.Dependencies, discovered) } // sort the dependencies to get always the same list if they don't change sort.Strings(target.Spec.Dependencies) @@ -72,7 +72,7 @@ func (b *InitializeAction) Handle(integration *v1alpha1.Integration) error { return sdk.Update(target) } -func (b *InitializeAction) mergeDependencies(list1 []string, list2 []string) []string { +func (action *initializeAction) mergeDependencies(list1 []string, list2 []string) []string { set := make(map[string]bool, 0) for _, d := range list1 { set[d] = true diff --git a/pkg/stub/action/integration/monitor.go b/pkg/stub/action/integration/monitor.go index b12600a18a..483212eee8 100644 --- a/pkg/stub/action/integration/monitor.go +++ b/pkg/stub/action/integration/monitor.go @@ -24,23 +24,24 @@ import ( "github.com/sirupsen/logrus" ) -type MonitorAction struct { +// NewMonitorAction creates a new monitoring action for an integration +func NewMonitorAction() IntegrationAction { + return &monitorAction{} } -func NewMonitorAction() IntegrationAction { - return &MonitorAction{} +type monitorAction struct { } -func (b *MonitorAction) Name() string { +func (action *monitorAction) Name() string { return "monitor" } -func (a *MonitorAction) CanHandle(integration *v1alpha1.Integration) bool { +func (action *monitorAction) CanHandle(integration *v1alpha1.Integration) bool { return integration.Status.Phase == v1alpha1.IntegrationPhaseRunning || integration.Status.Phase == v1alpha1.IntegrationPhaseError } -func (a *MonitorAction) Handle(integration *v1alpha1.Integration) error { +func (action *monitorAction) Handle(integration *v1alpha1.Integration) error { hash := digest.ComputeForIntegration(integration) if hash != integration.Status.Digest { diff --git a/pkg/util/kubernetes/config.go b/pkg/util/kubernetes/config.go index 465b96c560..b5d73937d6 100644 --- a/pkg/util/kubernetes/config.go +++ b/pkg/util/kubernetes/config.go @@ -18,15 +18,17 @@ limitations under the License. package kubernetes import ( - "github.com/operator-framework/operator-sdk/pkg/k8sclient" - "k8s.io/client-go/tools/clientcmd" "os/user" "path/filepath" + + "github.com/operator-framework/operator-sdk/pkg/k8sclient" + "k8s.io/client-go/tools/clientcmd" ) +// InitKubeClient initialize the k8s client func InitKubeClient(kubeconfig string) error { if kubeconfig == "" { - kubeconfig = GetDefaultKubeConfigFile() + kubeconfig = getDefaultKubeConfigFile() } // use the current context in kubeconfig @@ -39,7 +41,7 @@ func InitKubeClient(kubeconfig string) error { return nil } -func GetDefaultKubeConfigFile() string { +func getDefaultKubeConfigFile() string { usr, err := user.Current() if err != nil { panic(err) // TODO handle error diff --git a/pkg/util/kubernetes/customclient/customclient.go b/pkg/util/kubernetes/customclient/customclient.go index 9e38b6cbbc..c0e3b64988 100644 --- a/pkg/util/kubernetes/customclient/customclient.go +++ b/pkg/util/kubernetes/customclient/customclient.go @@ -23,6 +23,7 @@ import ( "k8s.io/client-go/rest" ) +// GetClientFor returns a RESTClient for the given group and version func GetClientFor(group string, version string) (*rest.RESTClient, error) { inConfig := k8sclient.GetKubeConfig() config := rest.CopyConfig(inConfig) diff --git a/pkg/util/kubernetes/customclient/scheme.go b/pkg/util/kubernetes/customclient/scheme.go index 7edcd025fc..9f62c4279e 100644 --- a/pkg/util/kubernetes/customclient/scheme.go +++ b/pkg/util/kubernetes/customclient/scheme.go @@ -20,7 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/runtime/serializer/json" "k8s.io/apimachinery/pkg/runtime/serializer/versioning" ) @@ -29,9 +28,6 @@ var watchScheme = runtime.NewScheme() var basicScheme = runtime.NewScheme() var deleteScheme = runtime.NewScheme() var parameterScheme = runtime.NewScheme() -var deleteOptionsCodec = serializer.NewCodecFactory(deleteScheme) -var dynamicParameterCodec = runtime.NewParameterCodec(parameterScheme) - var versionV1 = schema.GroupVersion{Version: "v1"} func init() { @@ -41,35 +37,6 @@ func init() { metav1.AddToGroupVersion(deleteScheme, versionV1) } -var watchJsonSerializerInfo = runtime.SerializerInfo{ - MediaType: "application/json", - EncodesAsText: true, - Serializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, false), - PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, true), - StreamSerializer: &runtime.StreamSerializerInfo{ - EncodesAsText: true, - Serializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, false), - Framer: json.Framer, - }, -} - -// watchNegotiatedSerializer is used to read the wrapper of the watch stream -type watchNegotiatedSerializer struct{} - -var watchNegotiatedSerializerInstance = watchNegotiatedSerializer{} - -func (s watchNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInfo { - return []runtime.SerializerInfo{watchJsonSerializerInfo} -} - -func (s watchNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder { - return versioning.NewDefaultingCodecForScheme(watchScheme, encoder, nil, gv, nil) -} - -func (s watchNegotiatedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder { - return versioning.NewDefaultingCodecForScheme(watchScheme, nil, decoder, nil, gv) -} - // basicNegotiatedSerializer is used to handle discovery and error handling serialization type basicNegotiatedSerializer struct{} diff --git a/pkg/util/kubernetes/loader.go b/pkg/util/kubernetes/loader.go index 7e00bc74df..fdc1fb882b 100644 --- a/pkg/util/kubernetes/loader.go +++ b/pkg/util/kubernetes/loader.go @@ -24,14 +24,15 @@ import ( "k8s.io/apimachinery/pkg/util/yaml" ) +// LoadResourceFromYaml loads a k8s resource from a yaml definition func LoadResourceFromYaml(data string) (runtime.Object, error) { role := []byte(data) - roleJson, err := yaml.ToJSON(role) + roleJSON, err := yaml.ToJSON(role) if err != nil { return nil, err } u := unstructured.Unstructured{} - err = u.UnmarshalJSON(roleJson) + err = u.UnmarshalJSON(roleJSON) if err != nil { return nil, err } diff --git a/pkg/util/kubernetes/namespace.go b/pkg/util/kubernetes/namespace.go index 279f64bd3c..664330cc11 100644 --- a/pkg/util/kubernetes/namespace.go +++ b/pkg/util/kubernetes/namespace.go @@ -18,17 +18,19 @@ limitations under the License. package kubernetes import ( - "github.com/pkg/errors" "io/ioutil" + + "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest" ) +// GetClientCurrentNamespace -- func GetClientCurrentNamespace(kubeconfig string) (string, error) { if kubeconfig == "" { - kubeconfig = GetDefaultKubeConfigFile() + kubeconfig = getDefaultKubeConfigFile() } if kubeconfig == "" { return "default", nil diff --git a/pkg/util/kubernetes/sanitize.go b/pkg/util/kubernetes/sanitize.go index e42c554461..3021103434 100644 --- a/pkg/util/kubernetes/sanitize.go +++ b/pkg/util/kubernetes/sanitize.go @@ -32,6 +32,7 @@ func init() { disallowedChars = regexp.MustCompile("[^a-z0-9-]") } +// SanitizeName sanitizes the given name to be compatible with k8s func SanitizeName(name string) string { name = strings.Split(name, ".")[0] name = path.Base(name) diff --git a/pkg/util/kubernetes/wait.go b/pkg/util/kubernetes/wait.go index 1ce55eee4d..9ae69480a3 100644 --- a/pkg/util/kubernetes/wait.go +++ b/pkg/util/kubernetes/wait.go @@ -1,20 +1,41 @@ +/* +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 kubernetes import ( + "time" + "github.com/operator-framework/operator-sdk/pkg/sdk" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime" - "time" ) +// ResourceRetrieveFunction -- type ResourceRetrieveFunction func() (interface{}, error) +// ResourceCheckFunction -- type ResourceCheckFunction func(interface{}) (bool, error) const ( sleepTime = 400 * time.Millisecond ) +// WaitCondition -- func WaitCondition(obj runtime.Object, condition ResourceCheckFunction, maxDuration time.Duration) error { start := time.Now() diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go index 32e13173e0..c54ebd2525 100644 --- a/pkg/util/maven/maven.go +++ b/pkg/util/maven/maven.go @@ -38,15 +38,15 @@ const ( artifactDirPrefix = "maven-bin-" ) -// Takes a project description and returns a binary tar with the built artifacts -func Build(project ProjectDefinition) (string, error) { +// Build takes a project description and returns a binary tar with the built artifacts +func Build(integration Integration) (string, error) { buildDir, err := ioutil.TempDir("", buildDirPrefix) if err != nil { return "", errors.Wrap(err, "could not create temporary dir for maven source files") } defer os.RemoveAll(buildDir) - err = createMavenStructure(buildDir, project) + err = createMavenStructure(buildDir, integration) if err != nil { return "", errors.Wrap(err, "could not write maven source files") } @@ -54,7 +54,7 @@ func Build(project ProjectDefinition) (string, error) { if err != nil { return "", err } - tarfile, err := createTar(buildDir, project) + tarfile, err := createTar(buildDir, integration) if err != nil { return "", err } @@ -90,13 +90,13 @@ func mavenExtraOptions() string { return "-Dcamel.noop=true" } -func createTar(buildDir string, project ProjectDefinition) (string, error) { +func createTar(buildDir string, integration Integration) (string, error) { artifactDir, err := ioutil.TempDir("", artifactDirPrefix) if err != nil { return "", errors.Wrap(err, "could not create temporary dir for maven artifacts") } - tarFileName := path.Join(artifactDir, project.Project.ArtifactId+".tar") + tarFileName := path.Join(artifactDir, integration.Project.ArtifactID+".tar") tarFile, err := os.Create(tarFileName) if err != nil { return "", errors.Wrap(err, "cannot create tar file "+tarFileName) @@ -104,14 +104,14 @@ func createTar(buildDir string, project ProjectDefinition) (string, error) { defer tarFile.Close() writer := tar.NewWriter(tarFile) - err = appendToTar(path.Join(buildDir, "target", project.Project.ArtifactId+"-"+project.Project.Version+".jar"), "", writer) + err = appendToTar(path.Join(buildDir, "target", integration.Project.ArtifactID+"-"+integration.Project.Version+".jar"), "", writer) if err != nil { return "", err } // Environment variables - if project.Env != nil { - err = writeFile(buildDir, "run-env.sh", envFileContent(project.Env)) + if integration.Env != nil { + err = writeFile(buildDir, "run-env.sh", envFileContent(integration.Env)) if err != nil { return "", err } @@ -169,7 +169,7 @@ func appendToTar(filePath string, tarPath string, writer *tar.Writer) error { return nil } -func createMavenStructure(buildDir string, project ProjectDefinition) error { +func createMavenStructure(buildDir string, project Integration) error { pom, err := GeneratePomFileContent(project.Project) if err != nil { return err @@ -236,6 +236,7 @@ func envFileContent(env map[string]string) string { return content } +// GeneratePomFileContent generate a pom.xml file from the given project definition func GeneratePomFileContent(project Project) (string, error) { w := &bytes.Buffer{} w.WriteString(xml.Header) @@ -251,14 +252,20 @@ func GeneratePomFileContent(project Project) (string, error) { return w.String(), nil } +// ParseGAV decode a maven artifact id to a dependency definition. +// +// The artifact id is in the form of: +// +// :[:[:]]:(|'?') +// func ParseGAV(gav string) (Dependency, error) { // :[:[:]]:(|'?') dep := Dependency{} rex := regexp.MustCompile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?(:([^: ]+))?") res := rex.FindStringSubmatch(gav) - dep.GroupId = res[1] - dep.ArtifactId = res[2] + dep.GroupID = res[1] + dep.ArtifactID = res[2] dep.Type = "jar" cnt := strings.Count(gav, ":") diff --git a/pkg/util/maven/maven_test.go b/pkg/util/maven/maven_test.go index 7e59e54792..05cff2037e 100644 --- a/pkg/util/maven/maven_test.go +++ b/pkg/util/maven/maven_test.go @@ -53,19 +53,19 @@ const expectedPom = ` func TestPomGeneration(t *testing.T) { project := Project{ XMLName: xml.Name{Local: "project"}, - XmlNs: "http://maven.apache.org/POM/4.0.0", - XmlNsXsi: "http://www.w3.org/2001/XMLSchema-instance", + XMLNs: "http://maven.apache.org/POM/4.0.0", + XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance", XsiSchemaLocation: "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd", ModelVersion: "4.0.0", - GroupId: "org.apache.camel.k.integration", - ArtifactId: "camel-k-integration", + GroupID: "org.apache.camel.k.integration", + ArtifactID: "camel-k-integration", Version: "1.0.0", DependencyManagement: DependencyManagement{ Dependencies: Dependencies{ Dependencies: []Dependency{ { - GroupId: "org.apache.camel", - ArtifactId: "camel-bom", + GroupID: "org.apache.camel", + ArtifactID: "camel-bom", Version: "2.22.1", Type: "pom", Scope: "import", @@ -76,8 +76,8 @@ func TestPomGeneration(t *testing.T) { Dependencies: Dependencies{ Dependencies: []Dependency{ { - GroupId: "org.apache.camel.k", - ArtifactId: "camel-k-runtime-jvm", + GroupID: "org.apache.camel.k", + ArtifactID: "camel-k-runtime-jvm", Version: "1.0.0", }, }, @@ -96,8 +96,8 @@ func TestParseSimpleGAV(t *testing.T) { dep, err := ParseGAV("org.apache.camel:camel-core:2.21.1") assert.Nil(t, err) - assert.Equal(t, dep.GroupId, "org.apache.camel") - assert.Equal(t, dep.ArtifactId, "camel-core") + assert.Equal(t, dep.GroupID, "org.apache.camel") + assert.Equal(t, dep.ArtifactID, "camel-core") assert.Equal(t, dep.Version, "2.21.1") assert.Equal(t, dep.Type, "jar") assert.Equal(t, dep.Classifier, "") @@ -107,8 +107,8 @@ func TestParseGAVWithType(t *testing.T) { dep, err := ParseGAV("org.apache.camel:camel-core:war:2.21.1") assert.Nil(t, err) - assert.Equal(t, dep.GroupId, "org.apache.camel") - assert.Equal(t, dep.ArtifactId, "camel-core") + assert.Equal(t, dep.GroupID, "org.apache.camel") + assert.Equal(t, dep.ArtifactID, "camel-core") assert.Equal(t, dep.Version, "2.21.1") assert.Equal(t, dep.Type, "war") assert.Equal(t, dep.Classifier, "") @@ -118,8 +118,8 @@ func TestParseGAVWithClassifierAndType(t *testing.T) { dep, err := ParseGAV("org.apache.camel:camel-core:war:test:2.21.1") assert.Nil(t, err) - assert.Equal(t, dep.GroupId, "org.apache.camel") - assert.Equal(t, dep.ArtifactId, "camel-core") + assert.Equal(t, dep.GroupID, "org.apache.camel") + assert.Equal(t, dep.ArtifactID, "camel-core") assert.Equal(t, dep.Version, "2.21.1") assert.Equal(t, dep.Type, "war") assert.Equal(t, dep.Classifier, "test") diff --git a/pkg/util/maven/types.go b/pkg/util/maven/types.go index 5b1158ee31..db4b897e1b 100644 --- a/pkg/util/maven/types.go +++ b/pkg/util/maven/types.go @@ -21,42 +21,49 @@ import ( "encoding/xml" ) -type ProjectDefinition struct { +// Integration -- +type Integration struct { Project Project JavaSources map[string]string Resources map[string]string Env map[string]string // TODO: should we deprecate it ? env are set on deployment } +// Project represent a maven project type Project struct { XMLName xml.Name - XmlNs string `xml:"xmlns,attr"` - XmlNsXsi string `xml:"xmlns:xsi,attr"` + XMLNs string `xml:"xmlns,attr"` + XMLNsXsi string `xml:"xmlns:xsi,attr"` XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"` ModelVersion string `xml:"modelVersion"` - GroupId string `xml:"groupId"` - ArtifactId string `xml:"artifactId"` + GroupID string `xml:"groupId"` + ArtifactID string `xml:"artifactId"` Version string `xml:"version"` DependencyManagement DependencyManagement `xml:"dependencyManagement"` Dependencies Dependencies `xml:"dependencies"` } +// DependencyManagement represent maven's dependency management block type DependencyManagement struct { Dependencies Dependencies `xml:"dependencies"` } +// Dependencies -- type Dependencies struct { Dependencies []Dependency `xml:"dependency"` } +// Add a dependency to maven's dependencies func (deps *Dependencies) Add(dep Dependency) { deps.Dependencies = append(deps.Dependencies, dep) } -func (deps *Dependencies) AddGAV(groupId string, artifactId string, version string) { - deps.Add(NewDependency(groupId, artifactId, version)) +// AddGAV a dependency to maven's dependencies +func (deps *Dependencies) AddGAV(groupID string, artifactID string, version string) { + deps.Add(NewDependency(groupID, artifactID, version)) } +// AddEncodedGAV a dependency to maven's dependencies func (deps *Dependencies) AddEncodedGAV(gav string) { if d, err := ParseGAV(gav); err == nil { // TODO: error handling @@ -64,19 +71,21 @@ func (deps *Dependencies) AddEncodedGAV(gav string) { } } +// Dependency represent a maven's dependency type Dependency struct { - GroupId string `xml:"groupId"` - ArtifactId string `xml:"artifactId"` + GroupID string `xml:"groupId"` + ArtifactID string `xml:"artifactId"` Version string `xml:"version,omitempty"` Type string `xml:"type,omitempty"` Classifier string `xml:"classifier,omitempty"` Scope string `xml:"scope,omitempty"` } -func NewDependency(groupId string, artifactId string, version string) Dependency { +// NewDependency create an new dependency from the given gav info +func NewDependency(groupID string, artifactID string, version string) Dependency { return Dependency{ - GroupId: groupId, - ArtifactId: artifactId, + GroupID: groupID, + ArtifactID: artifactID, Version: version, Type: "jar", Classifier: "", diff --git a/pkg/util/openshift/register.go b/pkg/util/openshift/register.go index f399bab5cd..83b08aa8cd 100644 --- a/pkg/util/openshift/register.go +++ b/pkg/util/openshift/register.go @@ -15,7 +15,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Register all Openshift types that we want to manage. package openshift import ( @@ -30,14 +29,11 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// Register all Openshift types that we want to manage. func init() { - k8sutil.AddToSDKScheme(AddToScheme) + k8sutil.AddToSDKScheme(addKnownTypes) } -var ( - AddToScheme = addKnownTypes -) - type registerFunction func(*runtime.Scheme) error func addKnownTypes(scheme *runtime.Scheme) error { diff --git a/pkg/util/sync/file.go b/pkg/util/sync/file.go index 1be83b842f..d40ebe214d 100644 --- a/pkg/util/sync/file.go +++ b/pkg/util/sync/file.go @@ -20,9 +20,10 @@ package sync import ( "context" + "time" + "github.com/radovskyb/watcher" "github.com/sirupsen/logrus" - "time" ) // File returns a channel that signals each time the content of the file changes diff --git a/pkg/util/watch/watch.go b/pkg/util/watch/watch.go index 3ec0bda6f4..1ed9671910 100644 --- a/pkg/util/watch/watch.go +++ b/pkg/util/watch/watch.go @@ -18,18 +18,19 @@ limitations under the License. package watch import ( - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "context" + + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/operator-framework/operator-sdk/pkg/k8sclient" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "github.com/operator-framework/operator-sdk/pkg/util/k8sutil" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "github.com/sirupsen/logrus" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" ) -// Watches a integration resource and send it through a channel when its status changes -func WatchStateChanges(ctx context.Context, integration *v1alpha1.Integration) (<-chan *v1alpha1.Integration, error) { +// StateChanges watches a integration resource and send it through a channel when its status changes +func StateChanges(ctx context.Context, integration *v1alpha1.Integration) (<-chan *v1alpha1.Integration, error) { resourceClient, _, err := k8sclient.GetResourceClient(integration.APIVersion, integration.Kind, integration.Namespace) if err != nil { return nil, err