|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | +package client |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/go-logr/logr" |
| 22 | + v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 23 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // DefaultPollInterval is the default interval for polling CRD status |
| 32 | + defaultPollInterval = 150 * time.Millisecond |
| 33 | + // DefaultTimeout is the default timeout for waiting for CRD status |
| 34 | + defaultTimeout = 2 * time.Minute |
| 35 | +) |
| 36 | + |
| 37 | +var _ CRDClient = &CRDWrapper{} |
| 38 | + |
| 39 | +// CRDClient represents operations for managing CustomResourceDefinitions |
| 40 | +type CRDClient interface { |
| 41 | + // EnsureCreated ensures a CRD exists and is ready |
| 42 | + Ensure(ctx context.Context, crd v1.CustomResourceDefinition) error |
| 43 | + |
| 44 | + // Delete removes a CRD if it exists |
| 45 | + Delete(ctx context.Context, name string) error |
| 46 | + |
| 47 | + // Get retrieves a CRD by name |
| 48 | + Get(ctx context.Context, name string) (*v1.CustomResourceDefinition, error) |
| 49 | +} |
| 50 | + |
| 51 | +// CRDWrapper provides a simplified interface for CRD operations |
| 52 | +type CRDWrapper struct { |
| 53 | + client apiextensionsv1.CustomResourceDefinitionInterface |
| 54 | + log logr.Logger |
| 55 | + pollInterval time.Duration |
| 56 | + timeout time.Duration |
| 57 | +} |
| 58 | + |
| 59 | +// CRDWrapperConfig contains configuration for the CRD wrapper |
| 60 | +type CRDWrapperConfig struct { |
| 61 | + Client *apiextensionsv1.ApiextensionsV1Client |
| 62 | + Log logr.Logger |
| 63 | + PollInterval time.Duration |
| 64 | + Timeout time.Duration |
| 65 | +} |
| 66 | + |
| 67 | +// DefaultConfig returns a CRDWrapperConfig with default values |
| 68 | +func DefaultCRDWrapperConfig() CRDWrapperConfig { |
| 69 | + return CRDWrapperConfig{ |
| 70 | + PollInterval: defaultPollInterval, |
| 71 | + Timeout: defaultTimeout, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// newCRDWrapper creates a new CRD wrapper |
| 76 | +func newCRDWrapper(cfg CRDWrapperConfig) *CRDWrapper { |
| 77 | + if cfg.PollInterval == 0 { |
| 78 | + cfg.PollInterval = defaultPollInterval |
| 79 | + } |
| 80 | + if cfg.Timeout == 0 { |
| 81 | + cfg.Timeout = defaultTimeout |
| 82 | + } |
| 83 | + |
| 84 | + return &CRDWrapper{ |
| 85 | + client: cfg.Client.CustomResourceDefinitions(), |
| 86 | + log: cfg.Log.WithName("crd-wrapper"), |
| 87 | + pollInterval: cfg.PollInterval, |
| 88 | + timeout: cfg.Timeout, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Ensure ensures a CRD exists, up-to-date, and is ready. This can be |
| 93 | +// a dangerous operation as it will update the CRD if it already exists. |
| 94 | +// |
| 95 | +// The caller is responsible for ensuring the CRD, isn't introducing |
| 96 | +// breaking changes. |
| 97 | +func (w *CRDWrapper) Ensure(ctx context.Context, crd v1.CustomResourceDefinition) error { |
| 98 | + _, err := w.Get(ctx, crd.Name) |
| 99 | + if err != nil { |
| 100 | + if !apierrors.IsNotFound(err) { |
| 101 | + return fmt.Errorf("failed to check for existing CRD: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + w.log.Info("Creating CRD", "name", crd.Name) |
| 105 | + if err := w.create(ctx, crd); err != nil { |
| 106 | + return fmt.Errorf("failed to create CRD: %w", err) |
| 107 | + } |
| 108 | + } else { |
| 109 | + w.log.Info("Updating existing CRD", "name", crd.Name) |
| 110 | + if err := w.patch(ctx, crd); err != nil { |
| 111 | + return fmt.Errorf("failed to patch CRD: %w", err) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return w.waitForReady(ctx, crd.Name) |
| 116 | +} |
| 117 | + |
| 118 | +// Get retrieves a CRD by name |
| 119 | +func (w *CRDWrapper) Get(ctx context.Context, name string) (*v1.CustomResourceDefinition, error) { |
| 120 | + return w.client.Get(ctx, name, metav1.GetOptions{}) |
| 121 | +} |
| 122 | + |
| 123 | +func (w *CRDWrapper) create(ctx context.Context, crd v1.CustomResourceDefinition) error { |
| 124 | + _, err := w.client.Create(ctx, &crd, metav1.CreateOptions{}) |
| 125 | + return err |
| 126 | +} |
| 127 | + |
| 128 | +func (w *CRDWrapper) patch(ctx context.Context, newCRD v1.CustomResourceDefinition) error { |
| 129 | + patchBytes, err := json.Marshal(newCRD) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to marshal CRD for patch: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + _, err = w.client.Patch( |
| 135 | + ctx, |
| 136 | + newCRD.Name, |
| 137 | + types.MergePatchType, |
| 138 | + patchBytes, |
| 139 | + metav1.PatchOptions{}, |
| 140 | + ) |
| 141 | + return err |
| 142 | +} |
| 143 | + |
| 144 | +// Delete removes a CRD if it exists |
| 145 | +func (w *CRDWrapper) Delete(ctx context.Context, name string) error { |
| 146 | + w.log.Info("Deleting CRD", "name", name) |
| 147 | + |
| 148 | + err := w.client.Delete(ctx, name, metav1.DeleteOptions{}) |
| 149 | + if err != nil && !apierrors.IsNotFound(err) { |
| 150 | + return fmt.Errorf("failed to delete CRD: %w", err) |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// waitForReady waits for a CRD to become ready |
| 156 | +func (w *CRDWrapper) waitForReady(ctx context.Context, name string) error { |
| 157 | + w.log.Info("Waiting for CRD to become ready", "name", name) |
| 158 | + |
| 159 | + return wait.PollUntilContextTimeout(ctx, w.pollInterval, w.timeout, true, |
| 160 | + func(ctx context.Context) (bool, error) { |
| 161 | + crd, err := w.Get(ctx, name) |
| 162 | + if err != nil { |
| 163 | + if apierrors.IsNotFound(err) { |
| 164 | + return false, nil |
| 165 | + } |
| 166 | + return false, err |
| 167 | + } |
| 168 | + |
| 169 | + for _, cond := range crd.Status.Conditions { |
| 170 | + if cond.Type == v1.Established && cond.Status == v1.ConditionTrue { |
| 171 | + return true, nil |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return false, nil |
| 176 | + }) |
| 177 | +} |
0 commit comments