generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 167
implement ScenarioOperation CRUD logic #416
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
Open
YamasouA
wants to merge
8
commits into
kubernetes-sigs:master
Choose a base branch
from
YamasouA:ft/scenario_operation_impl
base: master
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.
+1,457
−2
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7c35df
init
YamasouA ea89472
tweak
YamasouA 43a0a9e
fix error message
YamasouA 45f0b2a
fix
YamasouA dba284a
tweak
YamasouA 9ff99b8
tweak
YamasouA 5974a71
fix error message
YamasouA 023bf21
add unit test
YamasouA 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
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,117 @@ | ||||||
package v1alpha1 | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"errors" | ||||||
"fmt" | ||||||
|
||||||
"golang.org/x/xerrors" | ||||||
runtime "k8s.io/apimachinery/pkg/runtime" | ||||||
runtimeschema "k8s.io/apimachinery/pkg/runtime/schema" | ||||||
"k8s.io/client-go/discovery" | ||||||
memory "k8s.io/client-go/discovery/cached" | ||||||
"k8s.io/client-go/dynamic" | ||||||
"k8s.io/client-go/rest" | ||||||
"k8s.io/client-go/restmapper" | ||||||
) | ||||||
|
||||||
func (s *ScenarioOperation) Run(ctx context.Context, cfg *rest.Config) (bool, error) { | ||||||
switch { | ||||||
case s.Create != nil: | ||||||
ope := s.Create | ||||||
gvk := ope.Object.GetObjectKind().GroupVersionKind() | ||||||
client, err := buildClient(gvk, cfg) | ||||||
if err != nil { | ||||||
return true, xerrors.Errorf("build client failed for id: %s error: %w", s.ID, err) | ||||||
} | ||||||
_, err = client.Create(ctx, ope.Object, ope.CreateOptions) | ||||||
if err != nil { | ||||||
return true, xerrors.Errorf("run create operation: id: %s error: %w", s.ID, err) | ||||||
} | ||||||
case s.Patch != nil: | ||||||
ope := s.Patch | ||||||
gvk := ope.TypeMeta.GroupVersionKind() | ||||||
client, err := buildClient(gvk, cfg) | ||||||
if err != nil { | ||||||
return true, xerrors.Errorf("build client failed for id: %s error: %w", s.ID, err) | ||||||
} | ||||||
_, err = client.Patch(ctx, ope.ObjectMeta.Name, ope.PatchType, []byte(ope.Patch), ope.PatchOptions) | ||||||
if err != nil { | ||||||
return true, xerrors.Errorf("run patch operation: id: %s error: %w", s.ID, err) | ||||||
} | ||||||
case s.Delete != nil: | ||||||
ope := s.Delete | ||||||
gvk := ope.TypeMeta.GroupVersionKind() | ||||||
client, err := buildClient(gvk, cfg) | ||||||
if err != nil { | ||||||
return true, xerrors.Errorf("build client failed for id: %s error: %w", s.ID, err) | ||||||
} | ||||||
err = client.Delete(ctx, ope.ObjectMeta.Name, ope.DeleteOptions) | ||||||
if err != nil { | ||||||
return true, xerrors.Errorf("run delete operation: id: %s error: %w", s.ID, err) | ||||||
} | ||||||
case s.Done != nil: | ||||||
return true, nil | ||||||
default: | ||||||
return true, ErrUnknownOperation | ||||||
} | ||||||
|
||||||
return false, nil | ||||||
} | ||||||
|
||||||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||||||
func (s *ScenarioOperation) ValidateCreate() error { | ||||||
return s.validateOperations() | ||||||
} | ||||||
|
||||||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||||||
func (s *ScenarioOperation) ValidateUpdate(old runtime.Object) error { | ||||||
return s.validateOperations() | ||||||
} | ||||||
|
||||||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||||||
func (s *ScenarioOperation) ValidateDelete() error { | ||||||
return nil | ||||||
} | ||||||
|
||||||
// validateOperations checks that exactly one operation is set. | ||||||
func (s *ScenarioOperation) validateOperations() error { | ||||||
var count int | ||||||
if s.Create != nil { | ||||||
count++ | ||||||
} | ||||||
if s.Patch != nil { | ||||||
count++ | ||||||
} | ||||||
if s.Delete != nil { | ||||||
count++ | ||||||
} | ||||||
if s.Done != nil { | ||||||
count++ | ||||||
} | ||||||
if count != 1 { | ||||||
return fmt.Errorf("validateOperation find some operations") | ||||||
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
|
||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
var ErrUnknownOperation = errors.New("unknown operation") | ||||||
|
||||||
func buildClient(gvk runtimeschema.GroupVersionKind, cfg *rest.Config) (dynamic.NamespaceableResourceInterface, error) { | ||||||
cli, err := dynamic.NewForConfig(cfg) | ||||||
if err != nil { | ||||||
return nil, xerrors.Errorf("build dynamic client: %w", err) | ||||||
} | ||||||
|
||||||
dc, err := discovery.NewDiscoveryClientForConfig(cfg) | ||||||
if err != nil { | ||||||
return nil, xerrors.Errorf("build discovery client: %w", err) | ||||||
} | ||||||
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc)) | ||||||
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version) | ||||||
if err != nil { | ||||||
return nil, xerrors.Errorf("build mapping from RESTMapper: %w", err) | ||||||
} | ||||||
|
||||||
return cli.Resource(mapping.Resource), nil | ||||||
} |
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
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.
I think we should at least add the unit tests for this file.
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.
@utam0k
Thank you for your review.
I add unit test!