|
| 1 | +package framework |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/labels" |
| 11 | + "k8s.io/client-go/rest" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | +) |
| 14 | + |
| 15 | +// K8sClient wraps controller-runtime Client to add logging and custom behavior. |
| 16 | +type K8sClient struct { |
| 17 | + inner client.Client |
| 18 | +} |
| 19 | + |
| 20 | +func (c *K8sClient) InNamespace(namespace string) client.ListOption { |
| 21 | + return client.InNamespace(namespace) |
| 22 | +} |
| 23 | + |
| 24 | +// HasLabels returns a ListOption that filters for objects which have |
| 25 | +// all of the given label keys (regardless of value). |
| 26 | +func (c *K8sClient) HasLabels(keys ...string) client.ListOption { |
| 27 | + reqs := make([]metav1.LabelSelectorRequirement, 0, len(keys)) |
| 28 | + for _, k := range keys { |
| 29 | + reqs = append(reqs, metav1.LabelSelectorRequirement{ |
| 30 | + Key: k, |
| 31 | + Operator: metav1.LabelSelectorOpExists, |
| 32 | + }) |
| 33 | + } |
| 34 | + sel := &metav1.LabelSelector{MatchExpressions: reqs} |
| 35 | + ls, err := metav1.LabelSelectorAsSelector(sel) |
| 36 | + if err != nil { |
| 37 | + GinkgoWriter.Printf("error constructing label selector: %v\n", err) |
| 38 | + // fallback to a selector that matches nothing |
| 39 | + return client.MatchingLabelsSelector{Selector: labels.Nothing()} |
| 40 | + } |
| 41 | + return client.MatchingLabelsSelector{Selector: ls} |
| 42 | +} |
| 43 | + |
| 44 | +// MatchingLabels is just a passthrough to the real helper. |
| 45 | +func (c *K8sClient) MatchingLabels(m map[string]string) client.ListOption { |
| 46 | + return client.MatchingLabels(m) |
| 47 | +} |
| 48 | + |
| 49 | +func logOptions(opts ...Option) *Options { |
| 50 | + options := &Options{logEnabled: true} |
| 51 | + for _, opt := range opts { |
| 52 | + opt(options) |
| 53 | + } |
| 54 | + |
| 55 | + return options |
| 56 | +} |
| 57 | + |
| 58 | +// NewK8sClient returns a new wrapped Kubernetes client. |
| 59 | +func NewK8sClient(config *rest.Config, options client.Options) (K8sClient, error) { |
| 60 | + inner, err := client.New(config, options) |
| 61 | + if err != nil { |
| 62 | + clientErr := fmt.Errorf("error creating k8s client: %w", err) |
| 63 | + GinkgoWriter.Printf("%v\n", clientErr) |
| 64 | + |
| 65 | + return K8sClient{}, err |
| 66 | + } |
| 67 | + |
| 68 | + return K8sClient{inner: inner}, nil |
| 69 | +} |
| 70 | + |
| 71 | +// Get retrieves a resource by key, logging errors if enabled. |
| 72 | +func (c *K8sClient) Get( |
| 73 | + ctx context.Context, |
| 74 | + key client.ObjectKey, |
| 75 | + obj client.Object, |
| 76 | + opts ...Option, |
| 77 | +) error { |
| 78 | + options := logOptions(opts...) |
| 79 | + err := c.inner.Get(ctx, key, obj) |
| 80 | + if err != nil { |
| 81 | + if apierrors.IsNotFound(err) { |
| 82 | + if options.logEnabled { |
| 83 | + GinkgoWriter.Printf("Not found k8s resource %q error: %v\n", obj.GetName(), err) |
| 84 | + } |
| 85 | + return err |
| 86 | + } |
| 87 | + getErr := fmt.Errorf("error getting k8s resource %q: %w", obj.GetName(), err) |
| 88 | + if options.logEnabled { |
| 89 | + GinkgoWriter.Printf("%v\n", getErr) |
| 90 | + } |
| 91 | + |
| 92 | + return getErr |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// Create adds a new resource, returning an error on failure. |
| 99 | +func (c *K8sClient) Create( |
| 100 | + ctx context.Context, |
| 101 | + obj client.Object, |
| 102 | +) error { |
| 103 | + err := c.inner.Create(ctx, obj) |
| 104 | + if err != nil { |
| 105 | + createErr := fmt.Errorf("error creating k8s resource %q: %w", obj.GetName(), err) |
| 106 | + GinkgoWriter.Printf("%v\n", createErr) |
| 107 | + |
| 108 | + return createErr |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// Delete removes a resource, returning an error on failure. |
| 114 | +func (c *K8sClient) Delete( |
| 115 | + ctx context.Context, |
| 116 | + obj client.Object, |
| 117 | + deleteOpts []client.DeleteOption, |
| 118 | + opts ...Option, |
| 119 | +) error { |
| 120 | + options := logOptions(opts...) |
| 121 | + var dOpts []client.DeleteOption |
| 122 | + for _, do := range deleteOpts { |
| 123 | + if do != nil { |
| 124 | + dOpts = append(dOpts, do) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + err := c.inner.Delete(ctx, obj, dOpts...) |
| 129 | + if err != nil { |
| 130 | + deleteErr := fmt.Errorf("error deleting k8s resource %q: %w", obj.GetName(), err) |
| 131 | + if options.logEnabled { |
| 132 | + GinkgoWriter.Printf("%v\n", deleteErr) |
| 133 | + } |
| 134 | + |
| 135 | + return deleteErr |
| 136 | + } |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +// Update modifies a resource. |
| 141 | +func (c *K8sClient) Update( |
| 142 | + ctx context.Context, |
| 143 | + obj client.Object, |
| 144 | + updateOpts []client.UpdateOption, |
| 145 | + opts ...Option, |
| 146 | +) error { |
| 147 | + options := logOptions(opts...) |
| 148 | + var uOpts []client.UpdateOption |
| 149 | + for _, uo := range updateOpts { |
| 150 | + if uo != nil { |
| 151 | + uOpts = append(uOpts, uo) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if err := c.inner.Update(ctx, obj, uOpts...); err != nil { |
| 156 | + updateDeploymentErr := fmt.Errorf("error updating Deployment: %w", err) |
| 157 | + if options.logEnabled { |
| 158 | + GinkgoWriter.Printf( |
| 159 | + "ERROR occurred during updating Deployment in namespace %q with name %q, error: %s\n", |
| 160 | + obj.GetNamespace(), |
| 161 | + obj.GetName(), |
| 162 | + updateDeploymentErr, |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + return updateDeploymentErr |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +// List retrieves a list of resources, returning an error on failure. |
| 173 | +func (c *K8sClient) List( |
| 174 | + ctx context.Context, |
| 175 | + list client.ObjectList, |
| 176 | + listOpts ...client.ListOption, |
| 177 | +) error { |
| 178 | + var opts []client.ListOption |
| 179 | + for _, o := range listOpts { |
| 180 | + if o != nil { |
| 181 | + opts = append(opts, o) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + err := c.inner.List(ctx, list, opts...) |
| 186 | + if err != nil { |
| 187 | + listErr := fmt.Errorf("error listing k8s resources: %w", err) |
| 188 | + GinkgoWriter.Printf("%v\n", listErr) |
| 189 | + |
| 190 | + return listErr |
| 191 | + } |
| 192 | + return nil |
| 193 | +} |
0 commit comments