Skip to content

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
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ docker_build_server:
docker_build_scheduler:
docker $(BUILD) -f simulator/cmd/scheduler/Dockerfile -t simulator-scheduler simulator

.PHONY: docker_build_scenario
docker_build_scenario:
docker $(BUILD) -f scenario/Dockerfile -t scenario-controller scenario
docker $(BUILD) -f scenario/bootstrap/Dockerfile -t bootstrap scenario/bootstrap

.PHONY: docker_build_front
docker_build_front:
docker $(BUILD) -t simulator-frontend ./web/
Expand All @@ -65,6 +70,10 @@ docker_up:
docker_up_local:
docker compose -f compose.yml -f compose.local.yml up -d

.PHONY: docker_up_scenario
docker_up_scenario:
docker compose -f compose.yml -f compose.local.yml --profile scenario up -d

.PHONY: docker_build_and_up
docker_build_and_up: docker_build docker_up_local

Expand All @@ -75,3 +84,7 @@ docker_down:
.PHONY: docker_down_local
docker_down_local:
docker compose -f compose.yml -f compose.local.yml down --volumes

.PHONY: docker_down_scenario
docker_down_scenario:
docker compose -f compose.yml -f compose.local.yml --profile scenario down --volumes
41 changes: 41 additions & 0 deletions compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,50 @@ services:
- simulator-internal-network
profiles:
- externalImportEnabled

scenario-controller:
image: scenario-controller
profiles: ["scenario"]
command:
- "--kubeconfig=/config/kubeconfig.yaml"
- "--webhook-cert-path=/tmp/k8s-webhook-server/serving-certs"
volumes:
- ./simulator/cmd/scheduler/kubeconfig.yaml:/config/kubeconfig.yaml:ro
- scenario-webhook-certs:/tmp/k8s-webhook-server/serving-certs:ro
ports:
- "9443:9443"
depends_on:
bootstrap:
condition: service_healthy
networks:
- simulator-internal-network

# Bootstrap container: prepares CRDs and generates webhook certificates
bootstrap:
image: bootstrap
profiles: ["scenario"]
depends_on:
- simulator-cluster
volumes:
- ./simulator/cmd/scheduler/kubeconfig.yaml:/config/kubeconfig.yaml:ro
- ./scenario/config/webhook:/tmp/webhook:ro
- ./scenario/config/crd:/manifests/crd:ro
- scenario-webhook-certs:/manifests/webhook/certs
- scenario-webhook-work:/manifests/webhook
networks:
- simulator-internal-network
healthcheck:
test: ["CMD", "sh", "-c", "test -f /manifests/webhook/certs/tls.crt"]
interval: 5s
timeout: 3s
retries: 5
start_period: 5s

networks:
simulator-internal-network:
driver: bridge
volumes:
simulator-etcd-data:
scenario-webhook-certs:
scenario-webhook-work:
conf:
1 change: 1 addition & 0 deletions scenario/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ help: ## Display this help.
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases


.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
Expand Down
3 changes: 3 additions & 0 deletions scenario/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ resources:
kind: Scenario
path: sigs.k8s.io/kube-scheduler-simulator/scenario/api/v1alpha1
version: v1alpha1
webhooks:
validation: true
webhookVersion: v1
version: "3"
117 changes: 117 additions & 0 deletions scenario/api/v1alpha1/scenario_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package v1alpha1
Copy link
Member

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.

Copy link
Contributor Author

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!


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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
return fmt.Errorf("validateOperation find some operations")
return fmt.Errorf("exactly one operation type must be specified, but found %d", count)

}
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
}
66 changes: 66 additions & 0 deletions scenario/api/v1alpha1/scenario_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -30,8 +32,72 @@ type ScenarioSpec struct {

// Foo is an example field of Scenario. Edit scenario_types.go to remove/update
Foo string `json:"foo,omitempty"`

// Operations is a list of ScenarioOperation that define the actions to perform.
// +optional
Operations []*ScenarioOperation `json:"operations,omitempty"`
}

type ScenarioOperation struct {
// ID for this operation. Normally, the system sets this field for you.
ID string `json:"id"`
// MajorStep indicates when the operation should be done.
MajorStep int32 `json:"step"`

// One of the following four fields must be specified.
// If more than one is set or all are empty, the operation is invalid, and the scenario will fail.

// Create is the operation to create a new resource.
//
// +optional
Create *CreateOperation `json:"createOperation,omitempty"`
// Patch is the operation to patch a resource.
//
// +optional
Patch *PatchOperation `json:"patchOperation,omitempty"`
// Delete indicates the operation to delete a resource.
//
// +optional
Delete *DeleteOperation `json:"deleteOperation,omitempty"`
// Done indicates the operation to mark the scenario as Succeeded.
// When finish the step DoneOperation belongs, this Scenario changes its status to Succeeded.
//
// +optional
Done *DoneOperation `json:"doneOperation,omitempty"`
}

type CreateOperation struct {
// Object is the Object to be created.
// +kubebuilder:pruning:PreserveUnknownFields
Object *unstructured.Unstructured `json:"object"`

// +optional
CreateOptions metav1.CreateOptions `json:"createOptions,omitempty"`
}

type PatchOperation struct {
TypeMeta metav1.TypeMeta `json:"typeMeta"`
// +kubebuilder:pruning:PreserveUnknownFields
ObjectMeta metav1.ObjectMeta `json:"objectMeta"`
// Patch is the patch for target.
Patch string `json:"patch"`
// PatchType
PatchType types.PatchType `json:"patchType"`

// +optional
PatchOptions metav1.PatchOptions `json:"patchOptions,omitempty"`
}

type DeleteOperation struct {
TypeMeta metav1.TypeMeta `json:"typeMeta"`
ObjectMeta metav1.ObjectMeta `json:"objectMeta"`

// +optional
DeleteOptions metav1.DeleteOptions `json:"deleteOptions,omitempty"`
}

type DoneOperation struct{}

// ScenarioStatus defines the observed state of Scenario.
type ScenarioStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
Loading