-
Notifications
You must be signed in to change notification settings - Fork 37
Adds support for container-overrides
#155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,23 +16,29 @@ | |||||||||||||||||||||||||||||||
| package generator | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||
| "github.com/hashicorp/go-multierror" | ||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||
| "reflect" | ||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" | ||||||||||||||||||||||||||||||||
| "github.com/devfile/api/v2/pkg/attributes" | ||||||||||||||||||||||||||||||||
| "github.com/devfile/library/v2/pkg/devfile/parser" | ||||||||||||||||||||||||||||||||
| "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common" | ||||||||||||||||||||||||||||||||
| "github.com/hashicorp/go-multierror" | ||||||||||||||||||||||||||||||||
| buildv1 "github.com/openshift/api/build/v1" | ||||||||||||||||||||||||||||||||
| routev1 "github.com/openshift/api/route/v1" | ||||||||||||||||||||||||||||||||
| jsons "github.com/qjebbs/go-jsons" | ||||||||||||||||||||||||||||||||
| appsv1 "k8s.io/api/apps/v1" | ||||||||||||||||||||||||||||||||
| corev1 "k8s.io/api/core/v1" | ||||||||||||||||||||||||||||||||
| extensionsv1 "k8s.io/api/extensions/v1beta1" | ||||||||||||||||||||||||||||||||
| networkingv1 "k8s.io/api/networking/v1" | ||||||||||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/api/resource" | ||||||||||||||||||||||||||||||||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||||||||||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/runtime" | ||||||||||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/util/intstr" | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -619,11 +625,73 @@ func getAllContainers(devfileObj parser.DevfileObj, options common.DevfileOption | |||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| containers = append(containers, *container) | ||||||||||||||||||||||||||||||||
| modifiedContainer, err := containerOverridesHandler(comp, container) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| if modifiedContainer != nil { | ||||||||||||||||||||||||||||||||
| containers = append(containers, *modifiedContainer) | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| containers = append(containers, *container) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return containers, nil | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // containerOverridesHandler modifies the container component to include any container-overrides values in the devfile. | ||||||||||||||||||||||||||||||||
| // It does so by merging the devfile JSON with the JSON string representation of container-overrides field | ||||||||||||||||||||||||||||||||
| func containerOverridesHandler(comp v1.Component, container *corev1.Container) (*corev1.Container, error) { | ||||||||||||||||||||||||||||||||
| errHandler := new(error) | ||||||||||||||||||||||||||||||||
| key := "container-overrides" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| val := comp.Attributes.Get(key, errHandler) | ||||||||||||||||||||||||||||||||
| if *errHandler != nil && !errors.Is(*errHandler, &attributes.KeyNotFoundError{Key: key}) { | ||||||||||||||||||||||||||||||||
| return nil, *errHandler | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| // no need to continue any further if container-overrides wasn't used for the container component | ||||||||||||||||||||||||||||||||
| if val == nil { | ||||||||||||||||||||||||||||||||
| return nil, nil | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+648
to
+654
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
| v, err := json.Marshal(val) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| unstructuredContainer, err := convertResourceToUnstructured(container) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| u, err := json.Marshal(unstructuredContainer) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| result, err := jsons.Merge(u, v) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| err = json.Unmarshal(result, &unstructuredContainer) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| err = convertUnstructuredToResource(unstructured.Unstructured{Object: unstructuredContainer}, container) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return container, nil | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // convertResourceToUnstructured converts a resource to map[string]interface{} which is the element of unstructured.Unstructured | ||||||||||||||||||||||||||||||||
| func convertResourceToUnstructured(obj interface{}) (map[string]interface{}, error) { | ||||||||||||||||||||||||||||||||
| return runtime.DefaultUnstructuredConverter.ToUnstructured(obj) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // convertUnstructuredToResource converts an unstructured.Unstructured object to type of object passed to it | ||||||||||||||||||||||||||||||||
| func convertUnstructuredToResource(u unstructured.Unstructured, obj interface{}) error { | ||||||||||||||||||||||||||||||||
| return runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), obj) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+691
to
+693
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // getContainerAnnotations iterates through container components and returns all annotations | ||||||||||||||||||||||||||||||||
| func getContainerAnnotations(devfileObj parser.DevfileObj, options common.DevfileOptions) (v1.Annotation, error) { | ||||||||||||||||||||||||||||||||
| options.ComponentOptions = common.ComponentOptions{ | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it a bad idea to pass a pointer and return a pointer. But I'm struggling to do otherwise because of the way
runtime.DefaultUnstructuredConverter.ToUnstructuredworks.I'm open to ideas/suggestions.