This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Ensure kube_resources always have the correct defaults, even after version change #1043
Merged
hugoShaka
merged 2 commits into
hugo/relax-version-condition
from
hugo/set-correct-kuberesource-defaults
Mar 25, 2024
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright 2024 Gravitational, Inc. | ||
|
||
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 tfschema | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apitypes "github.com/gravitational/teleport/api/types" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
tftypes "github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
const ( | ||
DefaultRoleKubeModifierErrSummary = "DefaultRoleKubeResources modifier failed" | ||
DefaultRoleKubeModifierDescription = `This modifier re-render the role.spec.allow.kubernetes_resources from the user provided config instead of using the state. | ||
The state contains server-generated defaults (in fact they are generated in the pre-apply plan). | ||
However, those defaults become outdated if the version changes. | ||
One way to deal with version change is to force-recreate, but this is too destructive. | ||
The workaround we found was to use this plan modifier.` | ||
) | ||
|
||
func DefaultRoleKubeResources() tfsdk.AttributePlanModifier { | ||
return DefaultRoleKubeResourceModifier{} | ||
} | ||
|
||
type DefaultRoleKubeResourceModifier struct { | ||
} | ||
|
||
func (d DefaultRoleKubeResourceModifier) Description(ctx context.Context) string { | ||
return DefaultRoleKubeModifierDescription | ||
} | ||
|
||
func (d DefaultRoleKubeResourceModifier) MarkdownDescription(ctx context.Context) string { | ||
return DefaultRoleKubeModifierDescription | ||
} | ||
|
||
func (d DefaultRoleKubeResourceModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) { | ||
var config tftypes.Object | ||
diags := req.Config.Get(ctx, &config) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to get config.") | ||
return | ||
} | ||
|
||
role := &apitypes.RoleV6{} | ||
diags = CopyRoleV6FromTerraform(ctx, config, role) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to create a role from the config.") | ||
return | ||
} | ||
|
||
err := role.CheckAndSetDefaults() | ||
if err != nil { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, fmt.Sprintf("Failed to set the role defaults: %s", err)) | ||
return | ||
} | ||
|
||
diags = CopyRoleV6ToTerraform(ctx, role, &config) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to convert back the role into a TF Object.") | ||
return | ||
} | ||
|
||
specRaw, ok := config.Attrs["spec"] | ||
if !ok { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to get 'spec' from TF object.") | ||
return | ||
} | ||
spec, ok := specRaw.(tftypes.Object) | ||
if !ok { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to cast 'spec' as a TF object.") | ||
return | ||
} | ||
allowRaw, ok := spec.Attrs["allow"] | ||
if !ok { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to get 'spec' from TF object.") | ||
return | ||
} | ||
allow, ok := allowRaw.(tftypes.Object) | ||
if !ok { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to cast 'allow' as a TF object.") | ||
return | ||
} | ||
kubeResources, ok := allow.Attrs["kubernetes_resources"] | ||
if !ok { | ||
resp.Diagnostics.AddError(DefaultRoleKubeModifierErrSummary, "Failed to get 'kubernetes_resources' from TF object.") | ||
return | ||
} | ||
resp.AttributePlan = kubeResources | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Can we add godoc at least for this one?