-
Notifications
You must be signed in to change notification settings - Fork 974
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
ValidatingAdmissionPolicy
resource support
#2576
Draft
aayushsss1
wants to merge
9
commits into
hashicorp:main
Choose a base branch
from
aayushsss1:validating-admission-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
519a309
schema added
aayushsss1 2492f51
schema updated
aayushsss1 9af13bc
updated schema to plugin framework
aayushsss1 a5b4b90
removed redundant schema
aayushsss1 3436982
plugin framework updated
aayushsss1 cc550b7
added model and crud
aayushsss1 269b37c
updated client
aayushsss1 4a86785
removed client
aayushsss1 93654c5
added status field and updated go mod
aayushsss1 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
49 changes: 49 additions & 0 deletions
49
internal/framework/provider/appsv1/validating_admission_policy.go
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,49 @@ | ||
package appsv1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-codegen-kubernetes/autocrud" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ resource.Resource = &ValidatingAdmissionPolicy{} | ||
var _ resource.ResourceWithImportState = &ValidatingAdmissionPolicy{} | ||
|
||
func NewValidatingAdmissionPolicy() resource.Resource { | ||
return &ValidatingAdmissionPolicy{ | ||
Kind: "ValidatingAdmissionPolicy", | ||
APIVersion: "apps/v1", | ||
} | ||
} | ||
|
||
type ValidatingAdmissionPolicy struct { | ||
APIVersion string | ||
Kind string | ||
|
||
clientGetter autocrud.KubernetesClientGetter | ||
} | ||
|
||
func (r *ValidatingAdmissionPolicy) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = "kubernetes_validating_admission_policy_v1_gen" | ||
} | ||
|
||
func (r *ValidatingAdmissionPolicy) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
clientGetter, ok := req.ProviderData.(autocrud.KubernetesClientGetter) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected KubernetesClientGetter, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
r.clientGetter = clientGetter | ||
} |
151 changes: 151 additions & 0 deletions
151
internal/framework/provider/appsv1/validating_admission_policy_crud.go
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,151 @@ | ||
package appsv1 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-codegen-kubernetes/autocrud" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
func (r *ValidatingAdmissionPolicy) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var validatingAdmissionPolicyModel ValidatingAdmissionPolicyModel | ||
|
||
diag := req.Config.Get(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := validatingAdmissionPolicyModel.Timeouts.Create(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
err = autocrud.Create(ctx, r.clientGetter, r.APIVersion, r.Kind, &validatingAdmissionPolicyModel) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error creating resource", err.Error()) | ||
return | ||
} | ||
|
||
diags := resp.State.Set(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *ValidatingAdmissionPolicy) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var validatingAdmissionPolicyModel ValidatingAdmissionPolicyModel | ||
|
||
diag := req.State.Get(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := validatingAdmissionPolicyModel.Timeouts.Read(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
err = autocrud.Read(ctx, r.clientGetter, r.Kind, r.APIVersion, req, &validatingAdmissionPolicyModel) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error reading resource", err.Error()) | ||
return | ||
} | ||
|
||
diags := resp.State.Set(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *ValidatingAdmissionPolicy) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var validatingAdmissionPolicyModel ValidatingAdmissionPolicyModel | ||
|
||
diag := req.Config.Get(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := validatingAdmissionPolicyModel.Timeouts.Update(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
err = autocrud.Update(ctx, r.clientGetter, r.Kind, r.APIVersion, &validatingAdmissionPolicyModel) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error updating resource", err.Error()) | ||
return | ||
} | ||
|
||
diags := resp.State.Set(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *ValidatingAdmissionPolicy) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
waitForDeletion := false | ||
|
||
var validatingAdmissionPolicyModel ValidatingAdmissionPolicyModel | ||
|
||
diag := req.State.Get(ctx, &validatingAdmissionPolicyModel) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
|
||
defaultTimeout, err := time.ParseDuration("20m") | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error parsing timeout", err.Error()) | ||
return | ||
} | ||
timeout, diag := validatingAdmissionPolicyModel.Timeouts.Delete(ctx, defaultTimeout) | ||
resp.Diagnostics.Append(diag...) | ||
if diag.HasError() { | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
err = autocrud.Delete(ctx, r.clientGetter, r.Kind, r.APIVersion, req, waitForDeletion) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error deleting resource", err.Error()) | ||
return | ||
} | ||
|
||
} | ||
|
||
func (r *ValidatingAdmissionPolicy) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} |
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.
what's the reason for the change go.mod and go.sum?
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.
@BBBmau I've reduced the number of changes to the go.mod, the main change is I've added the
github.com/hashicorp/terraform-plugin-codegen-kubernetes/
, which in turn has updated the plugin framework and other terraform dependencies.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.
ah the inclusion of
github.com/hashicorp/terraform-plugin-codegen-kubernetes/
isn't necessary since it's a tool that you would install separately to help in generating plugin-framework resources.You can go ahead and revert changes that came from adding it.
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.
But isn't
github.com/hashicorp/terraform-plugin-codegen-kubernetes/autocrud
required for the CRUD operations?I had to also utilize it here -
terraform-provider-kubernetes/internal/framework/provider/appsv1/validating_admission_policy.go
Lines 22 to 27 in 93654c5
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.
ah yeah you're right. Apologies!
It's also worth noting that after reviewing the issue further that that once this PR is complete it unfortunately won't be merged until
v3.0.0
due to this resource being supported by default in k8s1.30
. We intend to do a major version bump in order to support this since we currently only support up tov1.28
. I've added it as part ofv3..0.0
milestone as of now.Of course you can still work on it but wanted to let you know in case you felt that this was on a deadline. We appreciate you working on this!
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.
Sure, thanks for letting me know! If there are any other issues that require immediate attention I'd be glad to help out!