|
| 1 | +// Copyright 2026 labring. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cache |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + licensev1 "github.com/labring/sealos/controllers/license/api/v1" |
| 22 | + notificationv1 "github.com/labring/sealos/controllers/pkg/notification/api/v1" |
| 23 | + userv1 "github.com/labring/sealos/controllers/user/api/v1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | +) |
| 30 | + |
| 31 | +// Options retains only the fields used for event delivery and node validation. |
| 32 | +func Options() ctrlcache.Options { |
| 33 | + return ctrlcache.Options{ |
| 34 | + ReaderFailOnMissingInformer: true, |
| 35 | + DefaultTransform: ctrlcache.TransformStripManagedFields(), |
| 36 | + ByObject: map[client.Object]ctrlcache.ByObject{ |
| 37 | + &licensev1.License{}: {Transform: transformLicense}, |
| 38 | + &corev1.Node{}: {Transform: transformNode}, |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// UncachedObjects keeps reads that require complete or low-frequency objects on the API server. |
| 44 | +func UncachedObjects() []client.Object { |
| 45 | + return []client.Object{ |
| 46 | + &licensev1.License{}, |
| 47 | + &corev1.Namespace{}, |
| 48 | + ¬ificationv1.Notification{}, |
| 49 | + &userv1.User{}, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// SetupInformers registers the Node informer used by license validation. The License |
| 54 | +// informer is registered by the controller builder. |
| 55 | +func SetupInformers(mgr ctrl.Manager) error { |
| 56 | + if _, err := mgr.GetCache().GetInformer(context.Background(), &corev1.Node{}); err != nil { |
| 57 | + return fmt.Errorf("register node informer: %w", err) |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func transformLicense(obj any) (any, error) { |
| 63 | + license, ok := obj.(*licensev1.License) |
| 64 | + if !ok { |
| 65 | + return obj, nil |
| 66 | + } |
| 67 | + |
| 68 | + return &licensev1.License{ |
| 69 | + TypeMeta: license.TypeMeta, |
| 70 | + ObjectMeta: projectObjectMeta(license.ObjectMeta), |
| 71 | + }, nil |
| 72 | +} |
| 73 | + |
| 74 | +func transformNode(obj any) (any, error) { |
| 75 | + node, ok := obj.(*corev1.Node) |
| 76 | + if !ok { |
| 77 | + return obj, nil |
| 78 | + } |
| 79 | + |
| 80 | + return &corev1.Node{ |
| 81 | + TypeMeta: node.TypeMeta, |
| 82 | + ObjectMeta: projectObjectMeta(node.ObjectMeta), |
| 83 | + Status: corev1.NodeStatus{ |
| 84 | + Allocatable: copyNodeAllocatable(node.Status.Allocatable), |
| 85 | + }, |
| 86 | + }, nil |
| 87 | +} |
| 88 | + |
| 89 | +func copyNodeAllocatable(source corev1.ResourceList) corev1.ResourceList { |
| 90 | + var result corev1.ResourceList |
| 91 | + for _, name := range []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory} { |
| 92 | + if quantity, ok := source[name]; ok { |
| 93 | + if result == nil { |
| 94 | + result = make(corev1.ResourceList, 2) |
| 95 | + } |
| 96 | + result[name] = quantity.DeepCopy() |
| 97 | + } |
| 98 | + } |
| 99 | + return result |
| 100 | +} |
| 101 | + |
| 102 | +func projectObjectMeta(in metav1.ObjectMeta) metav1.ObjectMeta { |
| 103 | + out := metav1.ObjectMeta{ |
| 104 | + Name: in.Name, |
| 105 | + Namespace: in.Namespace, |
| 106 | + UID: in.UID, |
| 107 | + ResourceVersion: in.ResourceVersion, |
| 108 | + Generation: in.Generation, |
| 109 | + CreationTimestamp: in.CreationTimestamp, |
| 110 | + } |
| 111 | + if in.DeletionTimestamp != nil { |
| 112 | + out.DeletionTimestamp = in.DeletionTimestamp.DeepCopy() |
| 113 | + } |
| 114 | + if in.DeletionGracePeriodSeconds != nil { |
| 115 | + gracePeriod := *in.DeletionGracePeriodSeconds |
| 116 | + out.DeletionGracePeriodSeconds = &gracePeriod |
| 117 | + } |
| 118 | + return out |
| 119 | +} |
0 commit comments