diff --git a/pkg/apis/camel/v1alpha1/types.go b/pkg/apis/camel/v1alpha1/types.go index 237dc95093..3876e829a1 100644 --- a/pkg/apis/camel/v1alpha1/types.go +++ b/pkg/apis/camel/v1alpha1/types.go @@ -18,6 +18,10 @@ limitations under the License. package v1alpha1 import ( + "strings" + + "github.com/apache/camel-k/pkg/util" + "github.com/mitchellh/mapstructure" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -69,6 +73,26 @@ func (is *IntegrationSpec) AddSources(sources ...SourceSpec) { is.Sources = append(is.Sources, sources...) } +// AddConfiguration -- +func (is *IntegrationSpec) AddConfiguration(confType string, confValue string) { + is.Configuration = append(is.Configuration, ConfigurationSpec{ + Type: confType, + Value: confValue, + }) +} + +// AddDependency -- +func (is *IntegrationSpec) AddDependency(dependency string) { + switch { + case strings.HasPrefix(dependency, "mvn:"): + util.StringSliceUniqueAdd(&is.Dependencies, dependency) + case strings.HasPrefix(dependency, "file:"): + util.StringSliceUniqueAdd(&is.Dependencies, dependency) + case strings.HasPrefix(dependency, "camel-"): + util.StringSliceUniqueAdd(&is.Dependencies, "camel:"+strings.TrimPrefix(dependency, "camel-")) + } +} + // SourceSpec -- type SourceSpec struct { Name string `json:"name,omitempty"` diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go index 8df8eeab8f..6b3bb8b590 100644 --- a/pkg/client/cmd/run.go +++ b/pkg/client/cmd/run.go @@ -20,7 +20,6 @@ package cmd import ( "bytes" "encoding/base64" - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -34,11 +33,6 @@ import ( "github.com/apache/camel-k/pkg/gzip" - "github.com/operator-framework/operator-sdk/pkg/util/k8sutil" - "gopkg.in/yaml.v2" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util" @@ -257,7 +251,6 @@ func (o *runCmdOptions) createIntegration(args []string) (*v1alpha1.Integration, return o.updateIntegrationCode(args) } -// nolint: gocyclo func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integration, error) { namespace := o.Namespace @@ -314,44 +307,24 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ }) } - for _, item := range o.Dependencies { - switch { - case strings.HasPrefix(item, "mvn:"): - integration.Spec.Dependencies = append(integration.Spec.Dependencies, item) - case strings.HasPrefix(item, "file:"): - integration.Spec.Dependencies = append(integration.Spec.Dependencies, item) - case strings.HasPrefix(item, "camel-"): - integration.Spec.Dependencies = append(integration.Spec.Dependencies, "camel:"+strings.TrimPrefix(item, "camel-")) - } - } - if o.Runtime != "" { - util.StringSliceUniqueAdd(&integration.Spec.Dependencies, "runtime:"+o.Runtime) + integration.Spec.AddDependency("runtime:" + o.Runtime) } + for _, item := range o.Dependencies { + integration.Spec.AddDependency(item) + } for _, item := range o.Properties { - integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{ - Type: "property", - Value: item, - }) + integration.Spec.AddConfiguration("property", item) } for _, item := range o.LoggingLevels { - integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{ - Type: "property", - Value: "logging.level." + item, - }) + integration.Spec.AddConfiguration("property", "logging.level."+item) } for _, item := range o.ConfigMaps { - integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{ - Type: "configmap", - Value: item, - }) + integration.Spec.AddConfiguration("configmap", item) } for _, item := range o.Secrets { - integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{ - Type: "secret", - Value: item, - }) + integration.Spec.AddConfiguration("secret", item) } for _, traitConf := range o.Traits { @@ -364,19 +337,16 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ case "": // continue.. case "yaml": - jsondata, err := toJSON(&integration) - if err != nil { - return nil, err - } - yamldata, err := jsonToYaml(jsondata) + data, err := kubernetes.ToYAML(&integration) if err != nil { return nil, err } - fmt.Print(string(yamldata)) + + fmt.Print(string(data)) return nil, nil case "json": - data, err := toJSON(&integration) + data, err := kubernetes.ToJSON(&integration) if err != nil { return nil, err } @@ -412,31 +382,6 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ return &integration, nil } -func toJSON(value runtime.Object) ([]byte, error) { - u, err := k8sutil.UnstructuredFromRuntimeObject(value) - if err != nil { - return nil, fmt.Errorf("error creating unstructured data: %v", err) - } - data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, u) - if err != nil { - return nil, fmt.Errorf("error marshalling to json: %v", err) - } - return data, nil -} - -func jsonToYaml(src []byte) ([]byte, error) { - jsondata := map[string]interface{}{} - err := json.Unmarshal(src, &jsondata) - if err != nil { - return nil, fmt.Errorf("error unmarshalling json: %v", err) - } - yamldata, err := yaml.Marshal(&jsondata) - if err != nil { - return nil, fmt.Errorf("error marshalling to yaml: %v", err) - } - return yamldata, nil -} - func (*runCmdOptions) loadCode(fileName string) (string, error) { if !strings.HasPrefix(fileName, "http://") && !strings.HasPrefix(fileName, "https://") { content, err := ioutil.ReadFile(fileName) diff --git a/pkg/util/kubernetes/util.go b/pkg/util/kubernetes/util.go new file mode 100644 index 0000000000..ae1090806b --- /dev/null +++ b/pkg/util/kubernetes/util.go @@ -0,0 +1,66 @@ +/* +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 ( + "encoding/json" + "fmt" + + "github.com/operator-framework/operator-sdk/pkg/util/k8sutil" + yaml "gopkg.in/yaml.v2" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" +) + +// ToJSON -- +func ToJSON(value runtime.Object) ([]byte, error) { + u, err := k8sutil.UnstructuredFromRuntimeObject(value) + if err != nil { + return nil, fmt.Errorf("error creating unstructured data: %v", err) + } + data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, u) + if err != nil { + return nil, fmt.Errorf("error marshalling to json: %v", err) + } + return data, nil +} + +// ToYAML -- +func ToYAML(value runtime.Object) ([]byte, error) { + data, err := ToJSON(value) + if err != nil { + return nil, err + } + + return JSONToYAML(data) +} + +// JSONToYAML -- +func JSONToYAML(src []byte) ([]byte, error) { + jsondata := map[string]interface{}{} + err := json.Unmarshal(src, &jsondata) + if err != nil { + return nil, fmt.Errorf("error unmarshalling json: %v", err) + } + yamldata, err := yaml.Marshal(&jsondata) + if err != nil { + return nil, fmt.Errorf("error marshalling to yaml: %v", err) + } + + return yamldata, nil +}