From 38702fee830249c69f2b7bc2e3067e7c048cb0e3 Mon Sep 17 00:00:00 2001 From: Yuvaraj Kakaraparthi Date: Wed, 8 Jun 2022 09:18:41 -0700 Subject: [PATCH] clean up --- .../cluster/cluster_controller_test.go | 2 +- .../topology/cluster/desired_state_test.go | 2 +- .../cluster/scope/hookresponsetracker.go | 1 + internal/runtime/client/test/fake_client.go | 13 +- poc/extension/handler.go | 167 ------------------ poc/extension/main.go | 156 ---------------- 6 files changed, 10 insertions(+), 331 deletions(-) delete mode 100644 poc/extension/handler.go delete mode 100644 poc/extension/main.go diff --git a/internal/controllers/cluster/cluster_controller_test.go b/internal/controllers/cluster/cluster_controller_test.go index d8cf507e235a..c1b2083596aa 100644 --- a/internal/controllers/cluster/cluster_controller_test.go +++ b/internal/controllers/cluster/cluster_controller_test.go @@ -23,12 +23,12 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + utilfeature "k8s.io/component-base/featuregate/testing" "k8s.io/utils/pointer" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - utilfeature "k8s.io/component-base/featuregate/testing" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" diff --git a/internal/controllers/topology/cluster/desired_state_test.go b/internal/controllers/topology/cluster/desired_state_test.go index a81440309be2..e2185d3a53db 100644 --- a/internal/controllers/topology/cluster/desired_state_test.go +++ b/internal/controllers/topology/cluster/desired_state_test.go @@ -28,10 +28,10 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/util/intstr" + utilfeature "k8s.io/component-base/featuregate/testing" "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - utilfeature "k8s.io/component-base/featuregate/testing" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" "sigs.k8s.io/cluster-api/feature" diff --git a/internal/controllers/topology/cluster/scope/hookresponsetracker.go b/internal/controllers/topology/cluster/scope/hookresponsetracker.go index e9a379fb08ea..6c770b0929ae 100644 --- a/internal/controllers/topology/cluster/scope/hookresponsetracker.go +++ b/internal/controllers/topology/cluster/scope/hookresponsetracker.go @@ -22,6 +22,7 @@ import ( "time" "k8s.io/apimachinery/pkg/runtime" + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" ) diff --git a/internal/runtime/client/test/fake_client.go b/internal/runtime/client/test/fake_client.go index 0d7ec3bebb00..7138e54d1012 100644 --- a/internal/runtime/client/test/fake_client.go +++ b/internal/runtime/client/test/fake_client.go @@ -22,6 +22,7 @@ import ( "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime" + runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1" runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog" @@ -86,7 +87,7 @@ type fakeRuntimeClient struct { callResponses map[string]runtimehooksv1.ResponseObject } -// CallAllExtensions implements Client +// CallAllExtensions implements Client. func (fc *fakeRuntimeClient) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook, request runtime.Object, response runtimehooksv1.ResponseObject) error { gvh, err := fc.catalog.GroupVersionHook(hook) if err != nil { @@ -107,7 +108,7 @@ func (fc *fakeRuntimeClient) CallAllExtensions(ctx context.Context, hook runtime return nil } -// CallExtension implements Client +// CallExtension implements Client. func (fc *fakeRuntimeClient) CallExtension(ctx context.Context, _ runtimecatalog.Hook, name string, request runtime.Object, response runtimehooksv1.ResponseObject) error { expectedResponse, ok := fc.callResponses[name] if !ok { @@ -121,22 +122,22 @@ func (fc *fakeRuntimeClient) CallExtension(ctx context.Context, _ runtimecatalog return nil } -// Discover implements Client +// Discover implements Client. func (fc *fakeRuntimeClient) Discover(context.Context, *runtimev1.ExtensionConfig) (*runtimev1.ExtensionConfig, error) { panic("unimplemented") } -// IsReady implements Client +// IsReady implements Client. func (fc *fakeRuntimeClient) IsReady() bool { return fc.isReady } -// Register implements Client +// Register implements Client. func (fc *fakeRuntimeClient) Register(extensionConfig *runtimev1.ExtensionConfig) error { panic("unimplemented") } -// Unregister implements Client +// Unregister implements Client. func (fc *fakeRuntimeClient) Unregister(extensionConfig *runtimev1.ExtensionConfig) error { panic("unimplemented") } diff --git a/poc/extension/handler.go b/poc/extension/handler.go deleted file mode 100644 index ed4deda0bdca..000000000000 --- a/poc/extension/handler.go +++ /dev/null @@ -1,167 +0,0 @@ -/* -Copyright 2021 The Kubernetes Authors. - -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 main - -import ( - "encoding/json" - "io/ioutil" - "net/http" - "reflect" - - "github.com/gorilla/mux" - "github.com/pkg/errors" - - "sigs.k8s.io/cluster-api/internal/runtime/catalog" -) - -type Handler interface{} - -type GroupVersionHookName struct { - catalog.GroupVersionHook - Name string -} - -type HandlerBuilder struct { - catalog *catalog.Catalog - hookToHandler map[GroupVersionHookName]Handler -} - -func NewHandlerBuilder() *HandlerBuilder { - return &HandlerBuilder{ - hookToHandler: map[GroupVersionHookName]Handler{}, - } -} - -func (bld *HandlerBuilder) WithCatalog(c *catalog.Catalog) *HandlerBuilder { - bld.catalog = c - return bld -} - -func (bld *HandlerBuilder) AddDiscovery(hook catalog.Hook, h Handler) *HandlerBuilder { - return bld.AddExtension(hook, "", h) -} - -func (bld *HandlerBuilder) AddExtension(hook catalog.Hook, name string, h Handler) *HandlerBuilder { - gvh, err := bld.catalog.GroupVersionHook(hook) - if err != nil { - panic(errors.Wrapf(err, "hook does not exist in catalog")) - } - gvhn := GroupVersionHookName{ - GroupVersionHook: gvh, - Name: name, - } - - bld.hookToHandler[gvhn] = h - return bld -} - -func (bld *HandlerBuilder) Build() (http.Handler, error) { - if bld.catalog == nil { - - } - - r := mux.NewRouter() - - for g, h := range bld.hookToHandler { - gvhn := g - handler := h - - in, err := bld.catalog.NewRequest(gvhn.GroupVersionHook) - if err != nil { - return nil, err - } - - out, err := bld.catalog.NewResponse(gvhn.GroupVersionHook) - if err != nil { - return nil, err - } - - // TODO: please use catalog.ValidateRequest/Response. - // TODO: add context - if err := validateF(handler, in, out); err != nil { - return nil, err - } - - fWrapper := func(w http.ResponseWriter, r *http.Request) { - - reqBody, err := ioutil.ReadAll(r.Body) - if err != nil { - // TODO: handle error - } - - request, err := bld.catalog.NewRequest(gvhn.GroupVersionHook) - if err != nil { - // TODO: handle error - } - - if err := json.Unmarshal(reqBody, request); err != nil { - // TODO: handle error - } - - response, err := bld.catalog.NewResponse(gvhn.GroupVersionHook) - if err != nil { - // TODO: handle error - } - - // TODO: build new context with correlation ID and pass it to the call - // TODO: context with Cancel to enforce timeout? enforce timeout on caller side? both? - - v := reflect.ValueOf(handler) - ret := v.Call([]reflect.Value{ - reflect.ValueOf(request), - reflect.ValueOf(response), - }) - - if !ret[0].IsNil() { - // TODO: handle error - } - - respBody, err := json.Marshal(response) - if err != nil { - // TODO: handle error - } - - w.WriteHeader(http.StatusOK) - w.Write(respBody) - } - - r.HandleFunc(catalog.GVHToPath(gvhn.GroupVersionHook, gvhn.Name), fWrapper).Methods("POST") - } - - return r, nil -} - -func validateF(f interface{}, params ...interface{}) error { - funcType := reflect.TypeOf(f) - - if funcType.NumIn() != len(params) { - return errors.New("InvocationCausedPanic called with a function and an incorrect number of parameter(s).") - } - - for paramIndex, paramValue := range params { - expectedType := funcType.In(paramIndex) - actualType := reflect.TypeOf(paramValue) - - if actualType != expectedType { - return errors.Errorf("InvocationCausedPanic called with a mismatched parameter type [parameter #%v: expected %v; got %v].", paramIndex, expectedType, actualType) - } - } - - // TODO: check return is error - - return nil -} diff --git a/poc/extension/main.go b/poc/extension/main.go deleted file mode 100644 index 60f60eed7be6..000000000000 --- a/poc/extension/main.go +++ /dev/null @@ -1,156 +0,0 @@ -/* -Copyright 2022 The Kubernetes Authors. - -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 main - -import ( - "flag" - "fmt" - "math/rand" - "net/http" - "time" - - "k8s.io/klog/v2" - "k8s.io/utils/pointer" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/webhook" - - runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" - "sigs.k8s.io/cluster-api/internal/runtime/catalog" -) - -// go run rte/test/rte-implementation-v1alpha2/main.go - -var c = catalog.New() -var certDir = flag.String("certDir", "/tmp/rte-implementation-secure/", "path to directory containing tls.crt and tls.key") - -func init() { - _ = runtimehooksv1.AddToCatalog(c) -} - -func main() { - ctx := ctrl.SetupSignalHandler() - - srv := webhook.Server{ - Host: "127.0.0.1", - Port: 8083, - CertDir: *certDir, - CertName: "tls.crt", - KeyName: "tls.key", - WebhookMux: http.NewServeMux(), - TLSMinVersion: "1.2", - } - - operation1Handler, err := NewHandlerBuilder(). - WithCatalog(c). - AddDiscovery(runtimehooksv1.Discovery, doDiscovery). // TODO: this is not strongly typed, but there are type checks when the service starts - AddExtension(runtimehooksv1.BeforeClusterCreate, "before-create", doBeforeCreateStuff). - AddExtension(runtimehooksv1.BeforeClusterDelete, "before-delete", doBeforeDeleteStuff). - AddExtension(runtimehooksv1.BeforeClusterUpgrade, "before-cluster-upgrade", doBeforeClusterUpgradeStuff). - // TODO: test with more services - Build() - if err != nil { - panic(err) - } - - srv.WebhookMux.Handle("/", operation1Handler) - - if err := srv.StartStandalone(ctx, nil); err != nil { - panic(err) - } -} - -// TODO: consider registering extensions with all required data and then auto-generating the discovery func based on that. -// If we want folks to write it manually, make it nicer to do. -func doDiscovery(request *runtimehooksv1.DiscoveryRequest, response *runtimehooksv1.DiscoveryResponse) error { - fmt.Println("Discovery/v1alpha1 called") - - response.Status = runtimehooksv1.ResponseStatusSuccess - response.Handlers = append(response.Handlers, runtimehooksv1.ExtensionHandler{ - Name: "before-create", - RequestHook: runtimehooksv1.GroupVersionHook{ - APIVersion: runtimehooksv1.GroupVersion.String(), - Hook: "BeforeClusterCreate", - }, - TimeoutSeconds: pointer.Int32(10), - FailurePolicy: toPtr(runtimehooksv1.FailurePolicyFail), - }) - response.Handlers = append(response.Handlers, runtimehooksv1.ExtensionHandler{ - Name: "before-delete", - RequestHook: runtimehooksv1.GroupVersionHook{ - APIVersion: runtimehooksv1.GroupVersion.String(), - Hook: "BeforeClusterDelete", - }, - TimeoutSeconds: pointer.Int32(10), - FailurePolicy: toPtr(runtimehooksv1.FailurePolicyFail), - }) - response.Handlers = append(response.Handlers, runtimehooksv1.ExtensionHandler{ - Name: "before-cluster-upgrade", - RequestHook: runtimehooksv1.GroupVersionHook{ - APIVersion: runtimehooksv1.GroupVersion.String(), - Hook: "BeforeClusterUpgrade", - }, - TimeoutSeconds: pointer.Int32(10), - FailurePolicy: toPtr(runtimehooksv1.FailurePolicyFail), - }) - - return nil -} - -func doBeforeCreateStuff(request *runtimehooksv1.BeforeClusterCreateRequest, response *runtimehooksv1.BeforeClusterCreateResponse) error { - retryAfter := randomRetryAfter() - - fmt.Println("Received BeforeClusterCreate hook call", "cluster", klog.KObj(&request.Cluster), "Sending retryAfter", retryAfter) - - response.Status = runtimehooksv1.ResponseStatusSuccess - response.RetryAfterSeconds = int32(retryAfter) - return nil -} - -func doBeforeClusterUpgradeStuff(request *runtimehooksv1.BeforeClusterUpgradeRequest, response *runtimehooksv1.BeforeClusterUpgradeResponse) error { - retryAfter := randomRetryAfter() - - fmt.Println("Received BeforeClusterUpgrade hook call", "cluster", klog.KObj(&request.Cluster), "Sending retryAfter", retryAfter) - - response.Status = runtimehooksv1.ResponseStatusSuccess - response.RetryAfterSeconds = int32(retryAfter) - return nil -} - -func doBeforeDeleteStuff(request *runtimehooksv1.BeforeClusterDeleteRequest, response *runtimehooksv1.BeforeClusterDeleteResponse) error { - retryAfter := randomRetryAfter() - - fmt.Println("Received BeforeClusterDelete hook call", "cluster", klog.KObj(&request.Cluster), "Sending retryAfter", retryAfter) - - response.Status = runtimehooksv1.ResponseStatusSuccess - response.RetryAfterSeconds = int32(retryAfter) - return nil -} - -func toPtr(f runtimehooksv1.FailurePolicy) *runtimehooksv1.FailurePolicy { - return &f -} - -func randomRetryAfter() int32 { - s := rand.NewSource(time.Now().UnixNano()) - r := rand.New(s) - - retryAfter := r.Intn(10) - // if retryAfter <= 2 { - // retryAfter = 0 - // } - return int32(retryAfter) -}