-
Notifications
You must be signed in to change notification settings - Fork 34
VWC generation moved to operator code #660
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
Draft
aryangorwade
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
aryangorwade:wvc-in-operator-code
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: k8s-nim-operator-webhook-service | ||
| labels: | ||
| app.kubernetes.io/name: k8s-nim-operator | ||
| app.kubernetes.io/instance: nim-operator | ||
| control-plane: controller-manager | ||
| annotations: | ||
| service.beta.openshift.io/serving-cert-secret-name: k8s-nim-operator-webhook-server-cert | ||
| spec: | ||
| selector: | ||
| app.kubernetes.io/name: k8s-nim-operator | ||
| app.kubernetes.io/instance: nim-operator | ||
| control-plane: controller-manager | ||
| ports: | ||
| - port: 443 | ||
| targetPort: 9443 | ||
| protocol: TCP |
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,10 @@ | ||
| package config | ||
|
|
||
| import "github.com/NVIDIA/k8s-nim-operator/internal/k8sutil" | ||
|
|
||
| var ( | ||
| EnableWebhooks bool | ||
| OperatorNamePrefix string | ||
| OperatorNamespace string | ||
| OrchestratorType k8sutil.OrchestratorType | ||
| ) |
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,150 @@ | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
|
|
||
| admissionv1 "k8s.io/api/admissionregistration/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/NVIDIA/k8s-nim-operator/internal/config" | ||
| "github.com/NVIDIA/k8s-nim-operator/internal/k8sutil" | ||
| ) | ||
|
|
||
| // EnsureValidatingWebhook creates or updates the ValidatingWebhookConfiguration | ||
| // that used to be templated by Helm. It is a best-effort reconciliation and | ||
| // returns an error only when we cannot make the desired state match the spec. | ||
| func EnsureValidatingWebhook( | ||
| ctx context.Context, | ||
| apiReader client.Reader, | ||
| writer client.Client, | ||
| namespace string, | ||
| fullNamePrefix string, | ||
| ) error { | ||
| // Desired validatingwebhookconfiguration spec. | ||
| desired := buildConfigurationSpec(namespace, fullNamePrefix) | ||
|
|
||
| // Check if there is already a spec. | ||
| existing := &admissionv1.ValidatingWebhookConfiguration{} | ||
| err := apiReader.Get(ctx, types.NamespacedName{Name: desired.Name}, existing) | ||
| if err != nil && !errors.IsNotFound(err) { | ||
| return err | ||
| } | ||
|
|
||
| if errors.IsNotFound(err) { | ||
| return writer.Create(ctx, desired) | ||
| } | ||
|
|
||
| // Deep-compare; update only if something differs. | ||
| cur, _ := json.Marshal(existing.Webhooks) | ||
| want, _ := json.Marshal(desired.Webhooks) | ||
|
|
||
| if string(cur) == string(want) { | ||
| return nil | ||
| } | ||
|
|
||
| existing.Webhooks = desired.Webhooks | ||
| existing.Annotations = desired.Annotations | ||
| return writer.Update(ctx, existing) | ||
| } | ||
|
|
||
| // buildDesired reproduces the spec that used to be in Helm. | ||
| func buildConfigurationSpec(namespace, namePrefix string) *admissionv1.ValidatingWebhookConfiguration { | ||
| pathCache := "/validate-apps-nvidia-com-v1alpha1-nimcache" | ||
| pathService := "/validate-apps-nvidia-com-v1alpha1-nimservice" | ||
|
|
||
| // Use appropriate annotations/labels as per deployment mode. | ||
| var annotations map[string]string | ||
| var labels map[string]string | ||
| var clientconfignimcache admissionv1.WebhookClientConfig | ||
| var clientconfignimservice admissionv1.WebhookClientConfig | ||
|
|
||
| clientconfignimcache = admissionv1.WebhookClientConfig{ | ||
| Service: &admissionv1.ServiceReference{ | ||
| Namespace: namespace, | ||
| Name: namePrefix + "-webhook-service", | ||
| Path: &pathCache, | ||
| }, | ||
| } | ||
| clientconfignimservice = admissionv1.WebhookClientConfig{ | ||
| Service: &admissionv1.ServiceReference{ | ||
| Namespace: namespace, | ||
| Name: namePrefix + "-webhook-service", | ||
| Path: &pathService, | ||
| }, | ||
| } | ||
|
|
||
| // Deployment specific values. | ||
| if config.OrchestratorType == k8sutil.K8s { | ||
| annotations = map[string]string{"cert-manager.io/inject-ca-from": namespace + "/" + namePrefix + "-serving-cert"} | ||
| labels = map[string]string{ | ||
| "app.kubernetes.io/name": "k8s-nim-operator", | ||
| "app.kubernetes.io/managed-by": "helm", | ||
| } | ||
| } else { | ||
| annotations = map[string]string{"service.beta.openshift.io/inject-cabundle": "true"} | ||
| labels = map[string]string{ | ||
| "app.kubernetes.io/name": "k8s-nim-operator", | ||
| "app.kubernetes.io/managed-by": "openshift", | ||
| } | ||
| } | ||
|
|
||
| return &admissionv1.ValidatingWebhookConfiguration{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: namePrefix + "-validating-webhook-configuration", | ||
| Annotations: annotations, | ||
| Labels: labels, | ||
| }, | ||
| Webhooks: []admissionv1.ValidatingWebhook{ | ||
| { | ||
| Name: "vnimcache-v1alpha1.kb.io", | ||
| AdmissionReviewVersions: []string{"v1"}, | ||
| ClientConfig: clientconfignimcache, | ||
| FailurePolicy: func() *admissionv1.FailurePolicyType { | ||
| fp := admissionv1.Fail | ||
| return &fp | ||
| }(), | ||
| SideEffects: func() *admissionv1.SideEffectClass { | ||
| s := admissionv1.SideEffectClassNone | ||
| return &s | ||
| }(), | ||
| Rules: []admissionv1.RuleWithOperations{{ | ||
| Operations: []admissionv1.OperationType{ | ||
| admissionv1.Create, admissionv1.Update, | ||
| }, | ||
| Rule: admissionv1.Rule{ | ||
| APIGroups: []string{"apps.nvidia.com"}, | ||
| APIVersions: []string{"v1alpha1"}, | ||
| Resources: []string{"nimcaches"}, | ||
| }, | ||
| }}, | ||
| }, | ||
| { | ||
| Name: "vnimservice-v1alpha1.kb.io", | ||
| AdmissionReviewVersions: []string{"v1"}, | ||
| ClientConfig: clientconfignimservice, | ||
| FailurePolicy: func() *admissionv1.FailurePolicyType { | ||
| fp := admissionv1.Fail | ||
| return &fp | ||
| }(), | ||
| SideEffects: func() *admissionv1.SideEffectClass { | ||
| s := admissionv1.SideEffectClassNone | ||
| return &s | ||
| }(), | ||
| Rules: []admissionv1.RuleWithOperations{{ | ||
| Operations: []admissionv1.OperationType{ | ||
| admissionv1.Create, admissionv1.Update, | ||
| }, | ||
| Rule: admissionv1.Rule{ | ||
| APIGroups: []string{"apps.nvidia.com"}, | ||
| APIVersions: []string{"v1alpha1"}, | ||
| Resources: []string{"nimservices"}, | ||
| }, | ||
| }}, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
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.
Could this be done using the
renderAndSyncResourcepattern we follow in the controllers?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.
VWC is cluster-scoped. Kubernetes doesn't allow setting a namespaced owner as controller of a cluster-scoped object. The current helper calls SetControllerReference, which would fail in that case.
To use this exact pattern for the cluster scoped vwc we'd have to either: