-
Notifications
You must be signed in to change notification settings - Fork 36
Add support for "pod-overrides" attribute #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,27 +18,33 @@ package generator | |
import ( | ||
"encoding/json" | ||
"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" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
extensionsv1 "k8s.io/api/extensions/v1beta1" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/apimachinery/pkg/util/strategicpatch" | ||
) | ||
|
||
const ContainerOverridesAttribute = "container-overrides" | ||
const ( | ||
ContainerOverridesAttribute = "container-overrides" | ||
PodOverridesAttribute = "pod-overrides" | ||
) | ||
|
||
// convertEnvs converts environment variables from the devfile structure to kubernetes structure | ||
func convertEnvs(vars []v1.EnvVar) []corev1.EnvVar { | ||
|
@@ -235,7 +241,7 @@ type podTemplateSpecParams struct { | |
} | ||
|
||
// getPodTemplateSpec gets a pod template spec that can be used to create a deployment spec | ||
func getPodTemplateSpec(podTemplateSpecParams podTemplateSpecParams) *corev1.PodTemplateSpec { | ||
func getPodTemplateSpec(globalAttributes attributes.Attributes, components []v1.Component, podTemplateSpecParams podTemplateSpecParams) (*corev1.PodTemplateSpec, error) { | ||
podTemplateSpec := &corev1.PodTemplateSpec{ | ||
ObjectMeta: podTemplateSpecParams.ObjectMeta, | ||
Spec: corev1.PodSpec{ | ||
|
@@ -245,7 +251,115 @@ func getPodTemplateSpec(podTemplateSpecParams podTemplateSpecParams) *corev1.Pod | |
}, | ||
} | ||
|
||
return podTemplateSpec | ||
if needsPodOverrides(globalAttributes, components) { | ||
patchedPodTemplateSpec, err := applyPodOverrides(globalAttributes, components, podTemplateSpec) | ||
if err != nil { | ||
return nil, err | ||
} | ||
patchedPodTemplateSpec.ObjectMeta = podTemplateSpecParams.ObjectMeta | ||
podTemplateSpec = patchedPodTemplateSpec | ||
} | ||
|
||
return podTemplateSpec, nil | ||
} | ||
|
||
// needsPodOverrides returns true if PodOverridesAttribute is present at Devfile or Container level attributes | ||
func needsPodOverrides(globalAttributes attributes.Attributes, components []v1.Component) bool { | ||
if globalAttributes.Exists(PodOverridesAttribute) { | ||
return true | ||
} | ||
for _, component := range components { | ||
if component.Attributes.Exists(PodOverridesAttribute) { | ||
return true | ||
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. can you add a test case where we have only component attribute and no global attribute to cover this? |
||
} | ||
} | ||
return false | ||
} | ||
|
||
// applyPodOverrides returns a list of all the PodOverridesAttribute set at Devfile and Container level attributes | ||
func applyPodOverrides(globalAttributes attributes.Attributes, components []v1.Component, podTemplateSpec *corev1.PodTemplateSpec) (*corev1.PodTemplateSpec, error) { | ||
overrides, err := getPodOverrides(globalAttributes, components) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Workaround: the definition for corev1.PodSpec does not make containers optional, so even a nil list | ||
// will be interpreted as "delete all containers" as the serialized patch will include "containers": null. | ||
// To avoid this, save the original containers and reset them at the end. | ||
originalContainers := podTemplateSpec.Spec.Containers | ||
// Save fields we do not allow to be configured in pod-overrides | ||
originalInitContainers := podTemplateSpec.Spec.InitContainers | ||
originalVolumes := podTemplateSpec.Spec.Volumes | ||
|
||
patchedTemplateBytes, err := json.Marshal(podTemplateSpec) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal deployment to yaml: %w", err) | ||
} | ||
for _, override := range overrides { | ||
patchedTemplateBytes, err = strategicpatch.StrategicMergePatch(patchedTemplateBytes, override.Raw, &corev1.PodTemplateSpec{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error applying pod overrides: %w", err) | ||
} | ||
} | ||
patchedPodTemplateSpec := corev1.PodTemplateSpec{} | ||
if err := json.Unmarshal(patchedTemplateBytes, &patchedPodTemplateSpec); err != nil { | ||
return nil, fmt.Errorf("error applying pod overrides: %w", err) | ||
} | ||
patchedPodTemplateSpec.Spec.Containers = originalContainers | ||
patchedPodTemplateSpec.Spec.InitContainers = originalInitContainers | ||
patchedPodTemplateSpec.Spec.Volumes = originalVolumes | ||
return &patchedPodTemplateSpec, nil | ||
} | ||
|
||
// getPodOverrides returns PodTemplateSpecOverrides for every instance of the pod overrides attribute | ||
// present in the DevWorkspace. The order of elements is | ||
// 1. Pod overrides defined on Container components, in the order they appear in the DevWorkspace | ||
// 2. Pod overrides defined in the global attributes field (.spec.template.attributes) | ||
func getPodOverrides(globalAttributes attributes.Attributes, components []v1.Component) ([]apiext.JSON, error) { | ||
var allOverrides []apiext.JSON | ||
|
||
for _, component := range components { | ||
if component.Attributes.Exists(PodOverridesAttribute) { | ||
override := corev1.PodTemplateSpec{} | ||
// Check format of pod-overrides to detect errors early | ||
if err := component.Attributes.GetInto(PodOverridesAttribute, &override); err != nil { | ||
return nil, fmt.Errorf("failed to parse %s attribute on component %s: %w", PodOverridesAttribute, component.Name, err) | ||
} | ||
// Do not allow overriding containers or volumes | ||
if override.Spec.Containers != nil { | ||
return nil, fmt.Errorf("cannot use %s to override pod containers (component %s)", PodOverridesAttribute, component.Name) | ||
} | ||
if override.Spec.InitContainers != nil { | ||
return nil, fmt.Errorf("cannot use %s to override pod initContainers (component %s)", PodOverridesAttribute, component.Name) | ||
} | ||
if override.Spec.Volumes != nil { | ||
return nil, fmt.Errorf("cannot use %s to override pod volumes (component %s)", PodOverridesAttribute, component.Name) | ||
} | ||
patchData := component.Attributes[PodOverridesAttribute] | ||
allOverrides = append(allOverrides, patchData) | ||
} | ||
} | ||
|
||
if globalAttributes.Exists(PodOverridesAttribute) { | ||
override := corev1.PodTemplateSpec{} | ||
err := globalAttributes.GetInto(PodOverridesAttribute, &override) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse %s attribute for pod: %w", PodOverridesAttribute, err) | ||
} | ||
// Do not allow overriding containers or volumes | ||
if override.Spec.Containers != nil { | ||
return nil, fmt.Errorf("cannot use %s to override pod containers", PodOverridesAttribute) | ||
} | ||
if override.Spec.InitContainers != nil { | ||
return nil, fmt.Errorf("cannot use %s to override pod initContainers", PodOverridesAttribute) | ||
} | ||
if override.Spec.Volumes != nil { | ||
return nil, fmt.Errorf("cannot use %s to override pod volumes", PodOverridesAttribute) | ||
} | ||
patchData := globalAttributes[PodOverridesAttribute] | ||
allOverrides = append(allOverrides, patchData) | ||
} | ||
|
||
return allOverrides, nil | ||
} | ||
|
||
// deploymentSpecParams is a struct that contains the required data to create a deployment spec object | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
could you see if you can add one test to cover the err case for this func