-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce code and fix webhook cache latency (#361)
* add * add * add * fix test * fix comments * fix test * fix helm template * fix helm template generate --------- Co-authored-by: Jiawei Du <59427055+msftcoderdjw@users.noreply.github.com>
- Loading branch information
1 parent
9f4fb24
commit db4e4bb
Showing
23 changed files
with
390 additions
and
591 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package v1 | ||
|
||
// +kubebuilder:object:generate=true | ||
type ContainerStatus struct { | ||
Properties map[string]string `json:"properties"` | ||
} | ||
|
||
// +kubebuilder:object:generate=true | ||
type ContainerSpec struct { | ||
} |
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,97 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
"github.com/eclipse-symphony/symphony/k8s/apis/metrics/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
type GetSubResourceNums func() (n int, err error) | ||
|
||
var commoncontainerlog = logf.Log.WithName("commoncontainer-resource") | ||
var cacheClient client.Client | ||
var readerClient client.Reader | ||
var commoncontainermetrics *metrics.Metrics | ||
|
||
func InitCommonContainerWebHook(mgr ctrl.Manager) error { | ||
if commoncontainermetrics == nil { | ||
initmetrics, err := metrics.New() | ||
if err != nil { | ||
return err | ||
} | ||
commoncontainermetrics = initmetrics | ||
} | ||
cacheClient = mgr.GetClient() | ||
readerClient = mgr.GetAPIReader() | ||
return nil | ||
} | ||
|
||
func SetupWebhookWithManager(mgr ctrl.Manager, resource client.Object) error { | ||
mgr.GetFieldIndexer().IndexField(context.Background(), resource, ".metadata.name", func(rawObj client.Object) []string { | ||
return []string{rawObj.GetName()} | ||
}) | ||
|
||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(resource). | ||
Complete() | ||
} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type | ||
func DefaultImpl(r client.Object) { | ||
commoncontainerlog.Info("default", "name", r.GetName(), "kind", r.GetObjectKind()) | ||
} | ||
|
||
func ValidateCreateImpl(r client.Object) (admission.Warnings, error) { | ||
commoncontainerlog.Info("validate create", "name", r.GetName(), "kind", r.GetObjectKind()) | ||
return nil, nil | ||
} | ||
func ValidateUpdateImpl(r client.Object, old runtime.Object) (admission.Warnings, error) { | ||
commoncontainerlog.Info("validate update", "name", r.GetName(), "kind", r.GetObjectKind()) | ||
return nil, nil | ||
} | ||
|
||
func ValidateDeleteImpl(r client.Object, getSubResourceNums GetSubResourceNums) (admission.Warnings, error) { | ||
|
||
commoncontainerlog.Info("validate delete", "name", r.GetName(), "kind", r.GetObjectKind()) | ||
|
||
validateDeleteTime := time.Now() | ||
validationError := validateDeleteContainerImpl(r, getSubResourceNums) | ||
if validationError != nil { | ||
commoncontainermetrics.ControllerValidationLatency( | ||
validateDeleteTime, | ||
metrics.CreateOperationType, | ||
metrics.InvalidResource, | ||
metrics.ContainerResourceType) | ||
} else { | ||
commoncontainermetrics.ControllerValidationLatency( | ||
validateDeleteTime, | ||
metrics.CreateOperationType, | ||
metrics.ValidResource, | ||
metrics.ContainerResourceType) | ||
} | ||
|
||
return nil, validationError | ||
} | ||
|
||
func validateDeleteContainerImpl(r client.Object, getSubResourceNums GetSubResourceNums) error { | ||
itemsNum, err := getSubResourceNums() | ||
if err != nil { | ||
commoncontainerlog.Error(err, "could not list nested resources ", "name", r.GetName(), "kind", r.GetObjectKind()) | ||
return apierrors.NewBadRequest(fmt.Sprintf("%s could not list nested resources for %s.", r.GetObjectKind(), r.GetName())) | ||
} | ||
if itemsNum > 0 { | ||
commoncontainerlog.Error(err, "nested resources are not empty", "name", r.GetName(), "kind", r.GetObjectKind()) | ||
return apierrors.NewBadRequest(fmt.Sprintf("%s nested resources with root resource '%s' are not empty", r.GetObjectKind(), r.GetName())) | ||
} | ||
|
||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.